Use base::FunctionRef for the various ForEachRenderFrameHost helpers.
Using base::FunctionRef as the callable param for a visitor function
can significantly reduce the boilerplate required to use the visitor
function. It also avoids the heap allocation required to support
base::RepeatingCallback's strong ownership semantics.
The most common transformation in this CL is converting something
like:
rfh->ForEachRenderFrameHost(base::BindRepeating(
&MyClass::HandleRFH, base::Unretained(this)));
to simply using a lambda that captures `this`:
rfh->ForEachRenderFrameHost([this] (content::RenderFrameHost* rfh) {
HandleRFH(rfh);
});
An astute reader will note that the latter is one line longer; however,
many of these callbacks currently bind other arguments as additional
input parameters or as out parameters. Using lambda captures
significantly reduces the boilerplate, improving readability, and makes
it much easier to avoid unnecessary copies.
Bug: 1303103
Change-Id: I3aeb74a0988dbddb645faef2239e9541c9adac52
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3767487
Commit-Queue: Daniel Cheng <[email protected]>
Reviewed-by: Avi Drissman <[email protected]>
Owners-Override: Avi Drissman <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1039508}
diff --git a/content/public/browser/web_contents.h b/content/public/browser/web_contents.h
index 024cd37e..b4dda7c 100644
--- a/content/public/browser/web_contents.h
+++ b/content/public/browser/web_contents.h
@@ -14,6 +14,7 @@
#include "base/callback_forward.h"
#include "base/callback_helpers.h"
+#include "base/functional/function_ref.h"
#include "base/location.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
@@ -433,10 +434,11 @@
// For callers only interested in the primary page,
// |GetMainFrame()->ForEachRenderFrameHost()| can be used.
// See |RenderFrameHost::ForEachRenderFrameHost| for details.
+ using FrameIterationAction = RenderFrameHost::FrameIterationAction;
+ virtual void ForEachRenderFrameHostWithAction(
+ base::FunctionRef<FrameIterationAction(RenderFrameHost*)> on_frame) = 0;
virtual void ForEachRenderFrameHost(
- RenderFrameHost::FrameIterationCallback on_frame) = 0;
- virtual void ForEachRenderFrameHost(
- RenderFrameHost::FrameIterationAlwaysContinueCallback on_frame) = 0;
+ base::FunctionRef<void(RenderFrameHost*)> on_frame) = 0;
// Gets the current RenderViewHost for this tab.
virtual RenderViewHost* GetRenderViewHost() = 0;