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"
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"
29 // The length of time to wait after the DOM is complete to try and run user
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),
46 current_location_(UserScript::UNDEFINED
),
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
)));
69 ExecuteCodeImpl(params
);
72 void UserScriptScheduler::DidCreateDocumentElement() {
73 current_location_
= UserScript::DOCUMENT_START
;
77 void UserScriptScheduler::DidFinishDocumentLoad() {
78 current_location_
= UserScript::DOCUMENT_END
;
80 // Schedule a run for DOCUMENT_IDLE
81 base::MessageLoop::current()->PostDelayedTask(
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(
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())
110 void UserScriptScheduler::IdleTimeout() {
111 current_location_
= UserScript::DOCUMENT_IDLE
;
115 void UserScriptScheduler::MaybeRun() {
116 if (current_location_
== UserScript::UNDEFINED
)
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.
151 new ExtensionHostMsg_ExecuteCodeFinished(render_view
->GetRoutingID(),
153 std::string(), // no error
160 std::vector
<WebFrame
*> frame_vector
;
161 frame_vector
.push_back(frame_
);
162 if (params
.all_frames
)
163 GetAllChildFrames(frame_
, &frame_vector
);
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
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
181 if (!params
.is_web_view
&&
182 !extension
->CanExecuteScriptOnPage(child_frame
->document().url(),
183 frame_
->document().url(),
184 extension_helper
->tab_id(),
187 if (child_frame
->parent()) {
190 error
= ErrorUtils::FormatErrorMessage(
191 extension_manifest_errors::kCannotAccessPage
,
192 child_frame
->document().url().spec());
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
,
209 UserScriptSlave::GetDataSourceURLForFrame(child_frame
),
210 child_frame
->document().title());
211 script_value
= child_frame
->executeScriptAndReturnValue(source
);
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(
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();
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(),
253 render_view
->GetPageId(),
254 UserScriptSlave::GetDataSourceURLForFrame(frame_
),
258 bool UserScriptScheduler::GetAllChildFrames(
259 WebFrame
* parent_frame
,
260 std::vector
<WebFrame
*>* frames_vector
) const {
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
);
272 } // namespace extensions