Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / extensions / renderer / user_script_scheduler.cc
blob9c82fb7aaa411659c77c9857e0aaccb3369c75ea
1 // Copyright 2014 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 "extensions/renderer/user_script_scheduler.h"
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h"
10 #include "content/public/renderer/render_view.h"
11 #include "content/public/renderer/v8_value_converter.h"
12 #include "extensions/common/error_utils.h"
13 #include "extensions/common/extension_messages.h"
14 #include "extensions/common/manifest_constants.h"
15 #include "extensions/common/permissions/permissions_data.h"
16 #include "extensions/renderer/dispatcher.h"
17 #include "extensions/renderer/dom_activity_logger.h"
18 #include "extensions/renderer/extension_groups.h"
19 #include "extensions/renderer/extension_helper.h"
20 #include "extensions/renderer/script_context.h"
21 #include "extensions/renderer/user_script_slave.h"
22 #include "third_party/WebKit/public/platform/WebString.h"
23 #include "third_party/WebKit/public/platform/WebVector.h"
24 #include "third_party/WebKit/public/web/WebDocument.h"
25 #include "third_party/WebKit/public/web/WebFrame.h"
26 #include "third_party/WebKit/public/web/WebScopedUserGesture.h"
27 #include "third_party/WebKit/public/web/WebView.h"
28 #include "v8/include/v8.h"
30 namespace {
31 // The length of time to wait after the DOM is complete to try and run user
32 // scripts.
33 const int kUserScriptIdleTimeoutMs = 200;
36 using blink::WebDocument;
37 using blink::WebFrame;
38 using blink::WebString;
39 using blink::WebVector;
40 using blink::WebView;
42 namespace extensions {
44 UserScriptScheduler::UserScriptScheduler(WebFrame* frame,
45 Dispatcher* dispatcher)
46 : weak_factory_(this),
47 frame_(frame),
48 current_location_(UserScript::UNDEFINED),
49 has_run_idle_(false),
50 dispatcher_(dispatcher) {
51 for (int i = UserScript::UNDEFINED; i < UserScript::RUN_LOCATION_LAST; ++i) {
52 pending_execution_map_[static_cast<UserScript::RunLocation>(i)] =
53 std::queue<linked_ptr<ExtensionMsg_ExecuteCode_Params> >();
57 UserScriptScheduler::~UserScriptScheduler() {
60 void UserScriptScheduler::ExecuteCode(
61 const ExtensionMsg_ExecuteCode_Params& params) {
62 UserScript::RunLocation run_at =
63 static_cast<UserScript::RunLocation>(params.run_at);
64 if (current_location_ < run_at) {
65 pending_execution_map_[run_at].push(
66 linked_ptr<ExtensionMsg_ExecuteCode_Params>(
67 new ExtensionMsg_ExecuteCode_Params(params)));
68 return;
71 ExecuteCodeImpl(params);
74 void UserScriptScheduler::DidCreateDocumentElement() {
75 current_location_ = UserScript::DOCUMENT_START;
76 MaybeRun();
79 void UserScriptScheduler::DidFinishDocumentLoad() {
80 current_location_ = UserScript::DOCUMENT_END;
81 MaybeRun();
82 // Schedule a run for DOCUMENT_IDLE
83 base::MessageLoop::current()->PostDelayedTask(
84 FROM_HERE,
85 base::Bind(&UserScriptScheduler::IdleTimeout, weak_factory_.GetWeakPtr()),
86 base::TimeDelta::FromMilliseconds(kUserScriptIdleTimeoutMs));
89 void UserScriptScheduler::DidFinishLoad() {
90 current_location_ = UserScript::DOCUMENT_IDLE;
91 // Ensure that running scripts does not keep any progress UI running.
92 base::MessageLoop::current()->PostTask(
93 FROM_HERE,
94 base::Bind(&UserScriptScheduler::MaybeRun, weak_factory_.GetWeakPtr()));
97 void UserScriptScheduler::DidStartProvisionalLoad() {
98 // The frame is navigating, so reset the state since we'll want to inject
99 // scripts once the load finishes.
100 current_location_ = UserScript::UNDEFINED;
101 has_run_idle_ = false;
102 weak_factory_.InvalidateWeakPtrs();
103 std::map<UserScript::RunLocation, ExecutionQueue>::iterator itr =
104 pending_execution_map_.begin();
105 for (itr = pending_execution_map_.begin();
106 itr != pending_execution_map_.end(); ++itr) {
107 while (!itr->second.empty())
108 itr->second.pop();
112 void UserScriptScheduler::IdleTimeout() {
113 current_location_ = UserScript::DOCUMENT_IDLE;
114 MaybeRun();
117 void UserScriptScheduler::MaybeRun() {
118 if (current_location_ == UserScript::UNDEFINED)
119 return;
121 if (!has_run_idle_ && current_location_ == UserScript::DOCUMENT_IDLE) {
122 has_run_idle_ = true;
123 dispatcher_->user_script_slave()->InjectScripts(
124 frame_, UserScript::DOCUMENT_IDLE);
127 // Run all tasks from the current time and earlier.
128 for (int i = UserScript::DOCUMENT_START;
129 i <= current_location_; ++i) {
130 UserScript::RunLocation run_time = static_cast<UserScript::RunLocation>(i);
131 while (!pending_execution_map_[run_time].empty()) {
132 linked_ptr<ExtensionMsg_ExecuteCode_Params>& params =
133 pending_execution_map_[run_time].front();
134 ExecuteCodeImpl(*params);
135 pending_execution_map_[run_time].pop();
140 void UserScriptScheduler::ExecuteCodeImpl(
141 const ExtensionMsg_ExecuteCode_Params& params) {
142 const Extension* extension = dispatcher_->extensions()->GetByID(
143 params.extension_id);
144 content::RenderView* render_view =
145 content::RenderView::FromWebView(frame_->view());
146 ExtensionHelper* extension_helper = ExtensionHelper::Get(render_view);
147 base::ListValue execution_results;
149 // Since extension info is sent separately from user script info, they can
150 // be out of sync. We just ignore this situation.
151 if (!extension) {
152 render_view->Send(
153 new ExtensionHostMsg_ExecuteCodeFinished(render_view->GetRoutingID(),
154 params.request_id,
155 std::string(), // no error
157 GURL(std::string()),
158 execution_results));
159 return;
162 std::vector<WebFrame*> frame_vector;
163 frame_vector.push_back(frame_);
164 if (params.all_frames)
165 GetAllChildFrames(frame_, &frame_vector);
167 std::string error;
169 scoped_ptr<blink::WebScopedUserGesture> gesture;
170 if (params.user_gesture)
171 gesture.reset(new blink::WebScopedUserGesture);
173 GURL top_url = frame_->document().url();
175 for (std::vector<WebFrame*>::iterator frame_it = frame_vector.begin();
176 frame_it != frame_vector.end(); ++frame_it) {
177 WebFrame* child_frame = *frame_it;
178 CHECK(child_frame) << top_url;
180 // We recheck access here in the renderer for extra safety against races
181 // with navigation.
183 // But different frames can have different URLs, and the extension might
184 // only have access to a subset of them. For the top frame, we can
185 // immediately send an error and stop because the browser process
186 // considers that an error too.
188 // For child frames, we just skip ones the extension doesn't have access
189 // to and carry on.
191 bool can_execute_script =
192 PermissionsData::CanExecuteScriptOnPage(extension,
193 child_frame->document().url(),
194 top_url,
195 extension_helper->tab_id(),
196 NULL,
198 NULL);
199 if ((!params.is_web_view && !can_execute_script) ||
200 (params.is_web_view &&
201 child_frame->document().url() != params.webview_src)) {
202 if (child_frame->parent()) {
203 continue;
204 } else {
205 error = ErrorUtils::FormatErrorMessage(
206 manifest_errors::kCannotAccessPage,
207 child_frame->document().url().spec());
208 break;
212 if (params.is_javascript) {
213 WebScriptSource source(WebString::fromUTF8(params.code), params.file_url);
214 v8::HandleScope scope(v8::Isolate::GetCurrent());
216 scoped_ptr<content::V8ValueConverter> v8_converter(
217 content::V8ValueConverter::create());
218 v8::Local<v8::Value> script_value;
220 if (params.in_main_world) {
221 DOMActivityLogger::AttachToWorld(DOMActivityLogger::kMainWorldId,
222 extension->id());
223 script_value = child_frame->executeScriptAndReturnValue(source);
224 } else {
225 blink::WebVector<v8::Local<v8::Value> > results;
226 std::vector<WebScriptSource> sources;
227 sources.push_back(source);
228 int isolated_world_id =
229 dispatcher_->user_script_slave()->GetIsolatedWorldIdForExtension(
230 extension, child_frame);
231 DOMActivityLogger::AttachToWorld(isolated_world_id, extension->id());
232 child_frame->executeScriptInIsolatedWorld(
233 isolated_world_id, &sources.front(),
234 sources.size(), EXTENSION_GROUP_CONTENT_SCRIPTS, &results);
235 // We only expect one value back since we only pushed one source
236 if (results.size() == 1 && !results[0].IsEmpty())
237 script_value = results[0];
240 if (params.wants_result && !script_value.IsEmpty()) {
241 // It's safe to always use the main world context when converting here.
242 // V8ValueConverterImpl shouldn't actually care about the context scope,
243 // and it switches to v8::Object's creation context when encountered.
244 v8::Local<v8::Context> context = child_frame->mainWorldScriptContext();
245 base::Value* result = v8_converter->FromV8Value(script_value, context);
246 // Always append an execution result (i.e. no result == null result) so
247 // that |execution_results| lines up with the frames.
248 execution_results.Append(
249 result ? result : base::Value::CreateNullValue());
251 } else {
252 child_frame->document().insertStyleSheet(
253 WebString::fromUTF8(params.code));
257 render_view->Send(new ExtensionHostMsg_ExecuteCodeFinished(
258 render_view->GetRoutingID(),
259 params.request_id,
260 error,
261 render_view->GetPageId(),
262 ScriptContext::GetDataSourceURLForFrame(frame_),
263 execution_results));
266 bool UserScriptScheduler::GetAllChildFrames(
267 WebFrame* parent_frame,
268 std::vector<WebFrame*>* frames_vector) const {
269 if (!parent_frame)
270 return false;
272 for (WebFrame* child_frame = parent_frame->firstChild(); child_frame;
273 child_frame = child_frame->nextSibling()) {
274 frames_vector->push_back(child_frame);
275 GetAllChildFrames(child_frame, frames_vector);
277 return true;
280 } // namespace extensions