Commit e643f563 authored by Istiaque Ahmed's avatar Istiaque Ahmed Committed by Commit Bot

[Extensions] Use OnceCallback in ScriptExecutor::ExecuteScript

ScriptFinishedCallback can/should be a OnceCallback as
it would only run (at most) once. This makes internal class Handler
take OnceCallbacks too, simplifying the code a bit.

Bug: None
Change-Id: Id74e83ba255c9507ea778a7ad9fef08f8d7b9dbd
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2133659
Commit-Queue: Istiaque Ahmed <lazyboy@chromium.org>
Reviewed-by: default avatarDevlin <rdevlin.cronin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#760318}
parent 92a97b99
...@@ -51,12 +51,16 @@ const std::string GenerateInjectionKey(const HostID& host_id, ...@@ -51,12 +51,16 @@ const std::string GenerateInjectionKey(const HostID& host_id,
// corresponding response comes from the renderer, or the renderer is destroyed. // corresponding response comes from the renderer, or the renderer is destroyed.
class Handler : public content::WebContentsObserver { class Handler : public content::WebContentsObserver {
public: public:
Handler(ScriptsExecutedNotification observer, // OnceCallback version of ScriptExecutor::ScriptsExecutedNotification:
using ScriptsExecutedOnceCallback = base::OnceCallback<
void(content::WebContents*, const ExecutingScriptsMap&, const GURL&)>;
Handler(ScriptsExecutedOnceCallback observer,
content::WebContents* web_contents, content::WebContents* web_contents,
const ExtensionMsg_ExecuteCode_Params& params, const ExtensionMsg_ExecuteCode_Params& params,
ScriptExecutor::FrameScope scope, ScriptExecutor::FrameScope scope,
int frame_id, int frame_id,
const ScriptExecutor::ScriptFinishedCallback& callback) ScriptExecutor::ScriptFinishedCallback callback)
: content::WebContentsObserver(web_contents), : content::WebContentsObserver(web_contents),
observer_(std::move(observer)), observer_(std::move(observer)),
host_id_(params.host_id), host_id_(params.host_id),
...@@ -65,7 +69,7 @@ class Handler : public content::WebContentsObserver { ...@@ -65,7 +69,7 @@ class Handler : public content::WebContentsObserver {
root_rfh_(ExtensionApiFrameIdMap::GetRenderFrameHostById(web_contents, root_rfh_(ExtensionApiFrameIdMap::GetRenderFrameHostById(web_contents,
frame_id)), frame_id)),
root_is_main_frame_(root_rfh_ ? !root_rfh_->GetParent() : false), root_is_main_frame_(root_rfh_ ? !root_rfh_->GetParent() : false),
callback_(callback) { callback_(std::move(callback)) {
if (root_rfh_) { if (root_rfh_) {
if (include_sub_frames_) { if (include_sub_frames_) {
web_contents->ForEachFrame(base::BindRepeating( web_contents->ForEachFrame(base::BindRepeating(
...@@ -180,17 +184,18 @@ class Handler : public content::WebContentsObserver { ...@@ -180,17 +184,18 @@ class Handler : public content::WebContentsObserver {
results_.Clear(); results_.Clear();
} }
if (!observer_.is_null() && root_frame_error_.empty() && if (observer_ && root_frame_error_.empty() &&
host_id_.type() == HostID::EXTENSIONS) { host_id_.type() == HostID::EXTENSIONS) {
observer_.Run(web_contents(), {{host_id_.id(), {}}}, root_frame_url_); std::move(observer_).Run(web_contents(), {{host_id_.id(), {}}},
root_frame_url_);
} }
if (!callback_.is_null()) if (callback_)
callback_.Run(root_frame_error_, root_frame_url_, results_); std::move(callback_).Run(root_frame_error_, root_frame_url_, results_);
delete this; delete this;
} }
ScriptsExecutedNotification observer_; ScriptsExecutedOnceCallback observer_;
// The id of the host (the extension or the webui) doing the injection. // The id of the host (the extension or the webui) doing the injection.
HostID host_id_; HostID host_id_;
...@@ -248,7 +253,7 @@ void ScriptExecutor::ExecuteScript(const HostID& host_id, ...@@ -248,7 +253,7 @@ void ScriptExecutor::ExecuteScript(const HostID& host_id,
bool user_gesture, bool user_gesture,
base::Optional<CSSOrigin> css_origin, base::Optional<CSSOrigin> css_origin,
ScriptExecutor::ResultType result_type, ScriptExecutor::ResultType result_type,
const ScriptFinishedCallback& callback) { ScriptFinishedCallback callback) {
if (host_id.type() == HostID::EXTENSIONS) { if (host_id.type() == HostID::EXTENSIONS) {
// Don't execute if the extension has been unloaded. // Don't execute if the extension has been unloaded.
const Extension* extension = const Extension* extension =
...@@ -281,7 +286,7 @@ void ScriptExecutor::ExecuteScript(const HostID& host_id, ...@@ -281,7 +286,7 @@ void ScriptExecutor::ExecuteScript(const HostID& host_id,
// Handler handles IPCs and deletes itself on completion. // Handler handles IPCs and deletes itself on completion.
new Handler(observer_, web_contents_, params, frame_scope, frame_id, new Handler(observer_, web_contents_, params, frame_scope, frame_id,
callback); std::move(callback));
} }
} // namespace extensions } // namespace extensions
...@@ -79,9 +79,8 @@ class ScriptExecutor { ...@@ -79,9 +79,8 @@ class ScriptExecutor {
// Callback from ExecuteScript. The arguments are (error, on_url, result). // Callback from ExecuteScript. The arguments are (error, on_url, result).
// Success is implied by an empty error. // Success is implied by an empty error.
typedef base::Callback< using ScriptFinishedCallback = base::OnceCallback<
void(const std::string&, const GURL&, const base::ListValue&)> void(const std::string&, const GURL&, const base::ListValue&)>;
ScriptFinishedCallback;
// Executes a script. The arguments match ExtensionMsg_ExecuteCode_Params in // Executes a script. The arguments match ExtensionMsg_ExecuteCode_Params in
// extension_messages.h (request_id is populated automatically). // extension_messages.h (request_id is populated automatically).
...@@ -106,7 +105,7 @@ class ScriptExecutor { ...@@ -106,7 +105,7 @@ class ScriptExecutor {
bool user_gesture, bool user_gesture,
base::Optional<CSSOrigin> css_origin, base::Optional<CSSOrigin> css_origin,
ResultType result_type, ResultType result_type,
const ScriptFinishedCallback& callback); ScriptFinishedCallback callback);
// Set the observer for ScriptsExecutedNotification callbacks. // Set the observer for ScriptsExecutedNotification callbacks.
void set_observer(ScriptsExecutedNotification observer) { void set_observer(ScriptsExecutedNotification observer) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment