Suppression for crbug/241044.
[chromium-blink-merge.git] / chrome / renderer / extensions / user_script_scheduler.cc
blob7438a370ebc1922b31b396a22deb20f4e46ecee1
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/renderer/extensions/user_script_scheduler.h"
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "base/message_loop.h"
10 #include "chrome/common/extensions/extension_manifest_constants.h"
11 #include "chrome/common/extensions/extension_messages.h"
12 #include "chrome/renderer/chrome_render_process_observer.h"
13 #include "chrome/renderer/extensions/dispatcher.h"
14 #include "chrome/renderer/extensions/dom_activity_logger.h"
15 #include "chrome/renderer/extensions/extension_groups.h"
16 #include "chrome/renderer/extensions/extension_helper.h"
17 #include "chrome/renderer/extensions/user_script_slave.h"
18 #include "content/public/renderer/render_view.h"
19 #include "content/public/renderer/v8_value_converter.h"
20 #include "extensions/common/error_utils.h"
21 #include "third_party/WebKit/Source/Platform/chromium/public/WebString.h"
22 #include "third_party/WebKit/Source/Platform/chromium/public/WebVector.h"
23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
26 #include "v8/include/v8.h"
28 namespace {
29 // The length of time to wait after the DOM is complete to try and run user
30 // scripts.
31 const int kUserScriptIdleTimeoutMs = 200;
34 using WebKit::WebDocument;
35 using WebKit::WebFrame;
36 using WebKit::WebString;
37 using WebKit::WebVector;
38 using WebKit::WebView;
40 namespace extensions {
42 UserScriptScheduler::UserScriptScheduler(WebFrame* frame,
43 Dispatcher* dispatcher)
44 : weak_factory_(this),
45 frame_(frame),
46 current_location_(UserScript::UNDEFINED),
47 has_run_idle_(false),
48 dispatcher_(dispatcher) {
49 for (int i = UserScript::UNDEFINED; i < UserScript::RUN_LOCATION_LAST; ++i) {
50 pending_execution_map_[static_cast<UserScript::RunLocation>(i)] =
51 std::queue<linked_ptr<ExtensionMsg_ExecuteCode_Params> >();
55 UserScriptScheduler::~UserScriptScheduler() {
58 void UserScriptScheduler::ExecuteCode(
59 const ExtensionMsg_ExecuteCode_Params& params) {
60 UserScript::RunLocation run_at =
61 static_cast<UserScript::RunLocation>(params.run_at);
62 if (current_location_ < run_at) {
63 pending_execution_map_[run_at].push(
64 linked_ptr<ExtensionMsg_ExecuteCode_Params>(
65 new ExtensionMsg_ExecuteCode_Params(params)));
66 return;
69 ExecuteCodeImpl(params);
72 void UserScriptScheduler::DidCreateDocumentElement() {
73 current_location_ = UserScript::DOCUMENT_START;
74 MaybeRun();
77 void UserScriptScheduler::DidFinishDocumentLoad() {
78 current_location_ = UserScript::DOCUMENT_END;
79 MaybeRun();
80 // Schedule a run for DOCUMENT_IDLE
81 base::MessageLoop::current()->PostDelayedTask(
82 FROM_HERE,
83 base::Bind(&UserScriptScheduler::IdleTimeout, weak_factory_.GetWeakPtr()),
84 base::TimeDelta::FromMilliseconds(kUserScriptIdleTimeoutMs));
87 void UserScriptScheduler::DidFinishLoad() {
88 current_location_ = UserScript::DOCUMENT_IDLE;
89 // Ensure that running scripts does not keep any progress UI running.
90 base::MessageLoop::current()->PostTask(
91 FROM_HERE,
92 base::Bind(&UserScriptScheduler::MaybeRun, weak_factory_.GetWeakPtr()));
95 void UserScriptScheduler::DidStartProvisionalLoad() {
96 // The frame is navigating, so reset the state since we'll want to inject
97 // scripts once the load finishes.
98 current_location_ = UserScript::UNDEFINED;
99 has_run_idle_ = false;
100 weak_factory_.InvalidateWeakPtrs();
101 std::map<UserScript::RunLocation, ExecutionQueue>::iterator itr =
102 pending_execution_map_.begin();
103 for (itr = pending_execution_map_.begin();
104 itr != pending_execution_map_.end(); ++itr) {
105 while (!itr->second.empty())
106 itr->second.pop();
110 void UserScriptScheduler::IdleTimeout() {
111 current_location_ = UserScript::DOCUMENT_IDLE;
112 MaybeRun();
115 void UserScriptScheduler::MaybeRun() {
116 if (current_location_ == UserScript::UNDEFINED)
117 return;
119 if (!has_run_idle_ && current_location_ == UserScript::DOCUMENT_IDLE) {
120 has_run_idle_ = true;
121 dispatcher_->user_script_slave()->InjectScripts(
122 frame_, UserScript::DOCUMENT_IDLE);
125 // Run all tasks from the current time and earlier.
126 for (int i = UserScript::DOCUMENT_START;
127 i <= current_location_; ++i) {
128 UserScript::RunLocation run_time = static_cast<UserScript::RunLocation>(i);
129 while (!pending_execution_map_[run_time].empty()) {
130 linked_ptr<ExtensionMsg_ExecuteCode_Params>& params =
131 pending_execution_map_[run_time].front();
132 ExecuteCodeImpl(*params);
133 pending_execution_map_[run_time].pop();
138 void UserScriptScheduler::ExecuteCodeImpl(
139 const ExtensionMsg_ExecuteCode_Params& params) {
140 const Extension* extension = dispatcher_->extensions()->GetByID(
141 params.extension_id);
142 content::RenderView* render_view =
143 content::RenderView::FromWebView(frame_->view());
144 ExtensionHelper* extension_helper = ExtensionHelper::Get(render_view);
145 base::ListValue execution_results;
147 // Since extension info is sent separately from user script info, they can
148 // be out of sync. We just ignore this situation.
149 if (!extension) {
150 render_view->Send(
151 new ExtensionHostMsg_ExecuteCodeFinished(render_view->GetRoutingID(),
152 params.request_id,
153 std::string(), // no error
155 GURL(std::string()),
156 execution_results));
157 return;
160 std::vector<WebFrame*> frame_vector;
161 frame_vector.push_back(frame_);
162 if (params.all_frames)
163 GetAllChildFrames(frame_, &frame_vector);
165 std::string error;
167 for (std::vector<WebFrame*>::iterator frame_it = frame_vector.begin();
168 frame_it != frame_vector.end(); ++frame_it) {
169 WebFrame* child_frame = *frame_it;
170 if (params.is_javascript) {
171 // We recheck access here in the renderer for extra safety against races
172 // with navigation.
174 // But different frames can have different URLs, and the extension might
175 // only have access to a subset of them. For the top frame, we can
176 // immediately send an error and stop because the browser process
177 // considers that an error too.
179 // For child frames, we just skip ones the extension doesn't have access
180 // to and carry on.
181 if (!params.is_web_view &&
182 !extension->CanExecuteScriptOnPage(child_frame->document().url(),
183 frame_->document().url(),
184 extension_helper->tab_id(),
185 NULL,
186 NULL)) {
187 if (child_frame->parent()) {
188 continue;
189 } else {
190 error = ErrorUtils::FormatErrorMessage(
191 extension_manifest_errors::kCannotAccessPage,
192 child_frame->document().url().spec());
193 break;
197 WebScriptSource source(WebString::fromUTF8(params.code));
198 v8::Isolate* isolate = v8::Isolate::GetCurrent();
199 v8::HandleScope scope(isolate);
201 scoped_ptr<content::V8ValueConverter> v8_converter(
202 content::V8ValueConverter::create());
203 v8::Handle<v8::Value> script_value;
205 if (params.in_main_world) {
206 DOMActivityLogger::AttachToWorld(
207 DOMActivityLogger::kMainWorldId,
208 extension->id(),
209 UserScriptSlave::GetDataSourceURLForFrame(child_frame),
210 child_frame->document().title());
211 script_value = child_frame->executeScriptAndReturnValue(source);
212 } else {
213 WebKit::WebVector<v8::Local<v8::Value> > results;
214 std::vector<WebScriptSource> sources;
215 sources.push_back(source);
216 int isolated_world_id =
217 dispatcher_->user_script_slave()->GetIsolatedWorldIdForExtension(
218 extension, child_frame);
219 DOMActivityLogger::AttachToWorld(
220 isolated_world_id,
221 extension->id(),
222 UserScriptSlave::GetDataSourceURLForFrame(child_frame),
223 child_frame->document().title());
224 child_frame->executeScriptInIsolatedWorld(
225 isolated_world_id, &sources.front(),
226 sources.size(), EXTENSION_GROUP_CONTENT_SCRIPTS, &results);
227 // We only expect one value back since we only pushed one source
228 if (results.size() == 1 && !results[0].IsEmpty())
229 script_value = results[0];
231 if (!script_value.IsEmpty()) {
232 v8::Local<v8::Context> context = v8::Context::New(isolate);
233 base::Value* base_val =
234 v8_converter->FromV8Value(script_value, context);
235 // Always append an execution result (i.e. no result == null result) so
236 // that |execution_results| lines up with the frames.
237 execution_results.Append(base_val ? base_val :
238 base::Value::CreateNullValue());
239 script_value.Clear();
241 } else {
242 child_frame->document().insertUserStyleSheet(
243 WebString::fromUTF8(params.code),
244 // Author level is consistent with WebView::addUserStyleSheet.
245 WebDocument::UserStyleAuthorLevel);
249 render_view->Send(new ExtensionHostMsg_ExecuteCodeFinished(
250 render_view->GetRoutingID(),
251 params.request_id,
252 error,
253 render_view->GetPageId(),
254 UserScriptSlave::GetDataSourceURLForFrame(frame_),
255 execution_results));
258 bool UserScriptScheduler::GetAllChildFrames(
259 WebFrame* parent_frame,
260 std::vector<WebFrame*>* frames_vector) const {
261 if (!parent_frame)
262 return false;
264 for (WebFrame* child_frame = parent_frame->firstChild(); child_frame;
265 child_frame = child_frame->nextSibling()) {
266 frames_vector->push_back(child_frame);
267 GetAllChildFrames(child_frame, frames_vector);
269 return true;
272 } // namespace extensions