Chromium Code Reviews| Index: extensions/renderer/script_injection.cc |
| diff --git a/extensions/renderer/script_injection.cc b/extensions/renderer/script_injection.cc |
| index ea588fe90a47b20b2353411dc9ede8a463d6fc84..c737a1b90776d1d3b7e0bf9c66beef6ce7d9e2ce 100644 |
| --- a/extensions/renderer/script_injection.cc |
| +++ b/extensions/renderer/script_injection.cc |
| @@ -19,6 +19,8 @@ |
| #include "extensions/renderer/dom_activity_logger.h" |
| #include "extensions/renderer/extension_groups.h" |
| #include "extensions/renderer/extensions_renderer_client.h" |
| +#include "extensions/renderer/script_injection_callback.h" |
| +#include "extensions/renderer/script_injection_manager.h" |
| #include "third_party/WebKit/public/platform/WebString.h" |
| #include "third_party/WebKit/public/web/WebDocument.h" |
| #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| @@ -124,7 +126,10 @@ ScriptInjection::ScriptInjection( |
| run_location_(run_location), |
| tab_id_(tab_id), |
| request_id_(kInvalidRequestId), |
| - complete_(false) { |
| + complete_(false), |
| + execution_results_(new base::ListValue()), |
| + all_injection_started_(false), |
| + script_injection_manager_(nullptr) { |
| } |
| ScriptInjection::~ScriptInjection() { |
| @@ -133,8 +138,7 @@ ScriptInjection::~ScriptInjection() { |
| } |
| bool ScriptInjection::TryToInject(UserScript::RunLocation current_location, |
| - const Extension* extension, |
| - ScriptsRunInfo* scripts_run_info) { |
| + const Extension* extension) { |
| if (current_location < run_location_) |
| return false; // Wait for the right location. |
| @@ -155,7 +159,7 @@ bool ScriptInjection::TryToInject(UserScript::RunLocation current_location, |
| RequestPermission(); |
| return false; // Wait around for permission. |
| case PermissionsData::ACCESS_ALLOWED: |
| - Inject(extension, scripts_run_info); |
| + Inject(extension); |
| return true; // We're done! |
| } |
| @@ -164,14 +168,13 @@ bool ScriptInjection::TryToInject(UserScript::RunLocation current_location, |
| return false; |
| } |
| -bool ScriptInjection::OnPermissionGranted(const Extension* extension, |
| - ScriptsRunInfo* scripts_run_info) { |
| +bool ScriptInjection::OnPermissionGranted(const Extension* extension) { |
| if (!extension) { |
| NotifyWillNotInject(ScriptInjector::EXTENSION_REMOVED); |
| return false; |
| } |
| - Inject(extension, scripts_run_info); |
| + Inject(extension); |
| return true; |
| } |
| @@ -196,10 +199,8 @@ void ScriptInjection::NotifyWillNotInject( |
| injector_->OnWillNotInject(reason); |
| } |
| -void ScriptInjection::Inject(const Extension* extension, |
| - ScriptsRunInfo* scripts_run_info) { |
| +void ScriptInjection::Inject(const Extension* extension) { |
| DCHECK(extension); |
| - DCHECK(scripts_run_info); |
| DCHECK(!complete_); |
| if (ShouldNotifyBrowserOfInjections()) |
| @@ -210,16 +211,14 @@ void ScriptInjection::Inject(const Extension* extension, |
| if (injector_->ShouldExecuteInChildFrames()) |
| AppendAllChildFrames(web_frame_, &frame_vector); |
| - scoped_ptr<blink::WebScopedUserGesture> gesture; |
| - if (injector_->IsUserGesture()) |
| - gesture.reset(new blink::WebScopedUserGesture()); |
| - |
| bool inject_js = injector_->ShouldInjectJs(run_location_); |
| bool inject_css = injector_->ShouldInjectCss(run_location_); |
| DCHECK(inject_js || inject_css); |
| - scoped_ptr<base::ListValue> execution_results(new base::ListValue()); |
| + scripts_run_info_.reset(new ScriptsRunInfo()); |
| + |
| GURL top_url = web_frame_->top()->document().url(); |
| + size_t result_index = 0; |
| for (std::vector<blink::WebFrame*>::iterator iter = frame_vector.begin(); |
| iter != frame_vector.end(); |
| ++iter) { |
| @@ -240,58 +239,69 @@ void ScriptInjection::Inject(const Extension* extension, |
| DCHECK(frame->parent()); |
| continue; |
| } |
| - if (inject_js) |
| - InjectJs(extension, frame, execution_results.get()); |
| + if (inject_js) { |
| + frame_result_index_[frame] = result_index++; |
| + InjectJs(extension, frame); |
| + } |
| if (inject_css) |
| InjectCss(frame); |
| } |
| - complete_ = true; |
| - injector_->OnInjectionComplete(execution_results.Pass(), |
| - scripts_run_info, |
| - run_location_); |
| + all_injection_started_ = true; |
| + TryToFinish(); |
| } |
| void ScriptInjection::InjectJs(const Extension* extension, |
| - blink::WebLocalFrame* frame, |
| - base::ListValue* execution_results) { |
| + blink::WebLocalFrame* frame) { |
| std::vector<blink::WebScriptSource> sources = |
| injector_->GetJsSources(run_location_); |
| bool in_main_world = injector_->ShouldExecuteInMainWorld(); |
| int world_id = in_main_world |
| ? DOMActivityLogger::kMainWorldId |
| : GetIsolatedWorldIdForExtension(extension, frame); |
| - bool expects_results = injector_->ExpectsResults(); |
| + bool is_user_gesture = injector_->IsUserGesture(); |
| + |
| + scoped_ptr<blink::WebScriptExecutionCallback> callback( |
| + new ScriptInjectionCallback(this, frame)); |
| base::ElapsedTimer exec_timer; |
| DOMActivityLogger::AttachToWorld(world_id, extension->id()); |
| - v8::HandleScope scope(v8::Isolate::GetCurrent()); |
| - v8::Local<v8::Value> script_value; |
| + |
| if (in_main_world) { |
| // We only inject in the main world for javascript: urls. |
| DCHECK_EQ(1u, sources.size()); |
| - const blink::WebScriptSource& source = sources.front(); |
| - if (expects_results) |
| - script_value = frame->executeScriptAndReturnValue(source); |
| - else |
| - frame->executeScript(source); |
| + frame->requestExecuteScriptAndReturnValue(sources.front(), |
| + is_user_gesture, |
| + callback.get()); |
| } else { // in isolated world |
| - scoped_ptr<blink::WebVector<v8::Local<v8::Value> > > results; |
| - if (expects_results) |
| - results.reset(new blink::WebVector<v8::Local<v8::Value> >()); |
| - frame->executeScriptInIsolatedWorld(world_id, |
| - &sources.front(), |
| - sources.size(), |
| - EXTENSION_GROUP_CONTENT_SCRIPTS, |
| - results.get()); |
| - if (expects_results && !results->isEmpty()) |
| - script_value = (*results)[0]; |
| + frame->requestExecuteScriptInIsolatedWorld(world_id, |
| + &sources.front(), |
| + sources.size(), |
| + EXTENSION_GROUP_CONTENT_SCRIPTS, |
| + is_user_gesture, |
| + callback.get()); |
| } |
| + callbacks_.push_back(callback.release()); |
| + |
| UMA_HISTOGRAM_TIMES("Extensions.InjectScriptTime", exec_timer.Elapsed()); |
| +} |
| +void ScriptInjection::OnJSInjectionCompleted( |
| + blink::WebLocalFrame* frame, |
| + const blink::WebVector<v8::Local<v8::Value> >& results) { |
| + std::map<blink::WebLocalFrame*, size_t>::iterator it = |
| + frame_result_index_.find(frame); |
| + CHECK(it != frame_result_index_.end()); |
| + size_t idx = it->second; |
| + frame_result_index_.erase(it); |
| + |
| + bool expects_results = injector_->ExpectsResults(); |
| if (expects_results) { |
| + v8::Local<v8::Value> script_value; |
| + if (!results.isEmpty()) |
| + script_value = results[0]; |
| // Right now, we only support returning single results (per frame). |
| scoped_ptr<content::V8ValueConverter> v8_converter( |
| content::V8ValueConverter::create()); |
| @@ -304,8 +314,29 @@ void ScriptInjection::InjectJs(const Extension* extension, |
| v8_converter->FromV8Value(script_value, context)); |
| // Always append an execution result (i.e. no result == null result) |
| // so that |execution_results| lines up with the frames. |
| - execution_results->Append(result.get() ? result.release() |
| - : base::Value::CreateNullValue()); |
| + execution_results_->Set(idx, result.get() ? result.release() |
| + : base::Value::CreateNullValue()); |
| + } |
| + TryToFinish(); |
| +} |
| + |
| +void ScriptInjection::SetScriptInjectionManager( |
| + ScriptInjectionManager* manager){ |
| + script_injection_manager_ = manager; |
| +} |
| + |
| +void ScriptInjection::TryToFinish() { |
| + if (all_injection_started_ && frame_result_index_.size() == 0) { |
| + complete_ = true; |
| + injector_->OnInjectionComplete(execution_results_.Pass(), |
| + scripts_run_info_.get(), |
| + run_location_); |
| + |
| + if (script_injection_manager_) { |
|
vsevik
2015/02/06 14:33:52
This seems impossible, assert instead?
kozy
2015/02/06 17:14:46
Done.
|
| + // this object can be destroyed after next line |
| + script_injection_manager_->OnInjectionFinished(this, |
| + scripts_run_info_.get()); |
| + } |
| } |
| } |