OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/renderer/extensions/user_script_scheduler.h" | 5 #include "chrome/renderer/extensions/user_script_scheduler.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "chrome/common/extensions/extension_error_utils.h" | 9 #include "chrome/common/extensions/extension_error_utils.h" |
10 #include "chrome/common/extensions/extension_manifest_constants.h" | 10 #include "chrome/common/extensions/extension_manifest_constants.h" |
11 #include "chrome/common/extensions/extension_messages.h" | 11 #include "chrome/common/extensions/extension_messages.h" |
12 #include "chrome/renderer/extensions/extension_dispatcher.h" | 12 #include "chrome/renderer/extensions/dispatcher.h" |
13 #include "chrome/renderer/extensions/extension_groups.h" | 13 #include "chrome/renderer/extensions/extension_groups.h" |
14 #include "chrome/renderer/extensions/extension_helper.h" | 14 #include "chrome/renderer/extensions/extension_helper.h" |
15 #include "chrome/renderer/extensions/user_script_slave.h" | 15 #include "chrome/renderer/extensions/user_script_slave.h" |
16 #include "content/public/renderer/render_view.h" | 16 #include "content/public/renderer/render_view.h" |
17 #include "content/public/renderer/v8_value_converter.h" | 17 #include "content/public/renderer/v8_value_converter.h" |
18 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" |
19 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebVector.h" | 19 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebVector.h" |
20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
23 #include "v8/include/v8.h" | 23 #include "v8/include/v8.h" |
24 | 24 |
25 namespace { | 25 namespace { |
26 // The length of time to wait after the DOM is complete to try and run user | 26 // The length of time to wait after the DOM is complete to try and run user |
27 // scripts. | 27 // scripts. |
28 const int kUserScriptIdleTimeoutMs = 200; | 28 const int kUserScriptIdleTimeoutMs = 200; |
29 } | 29 } |
30 | 30 |
31 using WebKit::WebDocument; | 31 using WebKit::WebDocument; |
32 using WebKit::WebFrame; | 32 using WebKit::WebFrame; |
33 using WebKit::WebString; | 33 using WebKit::WebString; |
34 using WebKit::WebVector; | 34 using WebKit::WebVector; |
35 using WebKit::WebView; | 35 using WebKit::WebView; |
36 | 36 |
37 namespace extensions { | 37 namespace extensions { |
38 | 38 |
39 UserScriptScheduler::UserScriptScheduler( | 39 UserScriptScheduler::UserScriptScheduler(WebFrame* frame, |
40 WebFrame* frame, ExtensionDispatcher* extension_dispatcher) | 40 Dispatcher* dispatcher) |
41 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), | 41 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), |
42 frame_(frame), | 42 frame_(frame), |
43 current_location_(UserScript::UNDEFINED), | 43 current_location_(UserScript::UNDEFINED), |
44 has_run_idle_(false), | 44 has_run_idle_(false), |
45 extension_dispatcher_(extension_dispatcher) { | 45 dispatcher_(dispatcher) { |
46 for (int i = UserScript::UNDEFINED; i < UserScript::RUN_LOCATION_LAST; ++i) { | 46 for (int i = UserScript::UNDEFINED; i < UserScript::RUN_LOCATION_LAST; ++i) { |
47 pending_execution_map_[static_cast<UserScript::RunLocation>(i)] = | 47 pending_execution_map_[static_cast<UserScript::RunLocation>(i)] = |
48 std::queue<linked_ptr<ExtensionMsg_ExecuteCode_Params> >(); | 48 std::queue<linked_ptr<ExtensionMsg_ExecuteCode_Params> >(); |
49 } | 49 } |
50 } | 50 } |
51 | 51 |
52 UserScriptScheduler::~UserScriptScheduler() { | 52 UserScriptScheduler::~UserScriptScheduler() { |
53 } | 53 } |
54 | 54 |
55 void UserScriptScheduler::ExecuteCode( | 55 void UserScriptScheduler::ExecuteCode( |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 current_location_ = UserScript::DOCUMENT_IDLE; | 108 current_location_ = UserScript::DOCUMENT_IDLE; |
109 MaybeRun(); | 109 MaybeRun(); |
110 } | 110 } |
111 | 111 |
112 void UserScriptScheduler::MaybeRun() { | 112 void UserScriptScheduler::MaybeRun() { |
113 if (current_location_ == UserScript::UNDEFINED) | 113 if (current_location_ == UserScript::UNDEFINED) |
114 return; | 114 return; |
115 | 115 |
116 if (!has_run_idle_ && current_location_ == UserScript::DOCUMENT_IDLE) { | 116 if (!has_run_idle_ && current_location_ == UserScript::DOCUMENT_IDLE) { |
117 has_run_idle_ = true; | 117 has_run_idle_ = true; |
118 extension_dispatcher_->user_script_slave()->InjectScripts( | 118 dispatcher_->user_script_slave()->InjectScripts( |
119 frame_, UserScript::DOCUMENT_IDLE); | 119 frame_, UserScript::DOCUMENT_IDLE); |
120 } | 120 } |
121 | 121 |
122 // Run all tasks from the current time and earlier. | 122 // Run all tasks from the current time and earlier. |
123 for (int i = UserScript::DOCUMENT_START; | 123 for (int i = UserScript::DOCUMENT_START; |
124 i <= current_location_; ++i) { | 124 i <= current_location_; ++i) { |
125 UserScript::RunLocation run_time = static_cast<UserScript::RunLocation>(i); | 125 UserScript::RunLocation run_time = static_cast<UserScript::RunLocation>(i); |
126 while (!pending_execution_map_[run_time].empty()) { | 126 while (!pending_execution_map_[run_time].empty()) { |
127 linked_ptr<ExtensionMsg_ExecuteCode_Params>& params = | 127 linked_ptr<ExtensionMsg_ExecuteCode_Params>& params = |
128 pending_execution_map_[run_time].front(); | 128 pending_execution_map_[run_time].front(); |
129 ExecuteCodeImpl(*params); | 129 ExecuteCodeImpl(*params); |
130 pending_execution_map_[run_time].pop(); | 130 pending_execution_map_[run_time].pop(); |
131 } | 131 } |
132 } | 132 } |
133 } | 133 } |
134 | 134 |
135 void UserScriptScheduler::ExecuteCodeImpl( | 135 void UserScriptScheduler::ExecuteCodeImpl( |
136 const ExtensionMsg_ExecuteCode_Params& params) { | 136 const ExtensionMsg_ExecuteCode_Params& params) { |
137 const Extension* extension = extension_dispatcher_->extensions()->GetByID( | 137 const Extension* extension = dispatcher_->extensions()->GetByID( |
138 params.extension_id); | 138 params.extension_id); |
139 content::RenderView* render_view = | 139 content::RenderView* render_view = |
140 content::RenderView::FromWebView(frame_->view()); | 140 content::RenderView::FromWebView(frame_->view()); |
141 ExtensionHelper* extension_helper = ExtensionHelper::Get(render_view); | 141 ExtensionHelper* extension_helper = ExtensionHelper::Get(render_view); |
142 base::ListValue execution_results; | 142 base::ListValue execution_results; |
143 | 143 |
144 // Since extension info is sent separately from user script info, they can | 144 // Since extension info is sent separately from user script info, they can |
145 // be out of sync. We just ignore this situation. | 145 // be out of sync. We just ignore this situation. |
146 if (!extension) { | 146 if (!extension) { |
147 render_view->Send(new ExtensionHostMsg_ExecuteCodeFinished( | 147 render_view->Send(new ExtensionHostMsg_ExecuteCodeFinished( |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 content::V8ValueConverter::create()); | 204 content::V8ValueConverter::create()); |
205 v8_converter->SetUndefinedAllowed(true); | 205 v8_converter->SetUndefinedAllowed(true); |
206 v8::Handle<v8::Value> script_value; | 206 v8::Handle<v8::Value> script_value; |
207 if (params.in_main_world) { | 207 if (params.in_main_world) { |
208 script_value = frame->executeScriptAndReturnValue(source); | 208 script_value = frame->executeScriptAndReturnValue(source); |
209 } else { | 209 } else { |
210 WebKit::WebVector<v8::Local<v8::Value> > results; | 210 WebKit::WebVector<v8::Local<v8::Value> > results; |
211 std::vector<WebScriptSource> sources; | 211 std::vector<WebScriptSource> sources; |
212 sources.push_back(source); | 212 sources.push_back(source); |
213 frame->executeScriptInIsolatedWorld( | 213 frame->executeScriptInIsolatedWorld( |
214 extension_dispatcher_->user_script_slave()-> | 214 dispatcher_->user_script_slave()-> |
215 GetIsolatedWorldIdForExtension(extension, frame), | 215 GetIsolatedWorldIdForExtension(extension, frame), |
216 &sources.front(), sources.size(), EXTENSION_GROUP_CONTENT_SCRIPTS, | 216 &sources.front(), sources.size(), EXTENSION_GROUP_CONTENT_SCRIPTS, |
217 &results); | 217 &results); |
218 // We only expect one value back since we only pushed one source | 218 // We only expect one value back since we only pushed one source |
219 if (results.size() == 1 && !results[0].IsEmpty()) | 219 if (results.size() == 1 && !results[0].IsEmpty()) |
220 script_value = results[0]; | 220 script_value = results[0]; |
221 } | 221 } |
222 if (!script_value.IsEmpty()) { | 222 if (!script_value.IsEmpty()) { |
223 base::Value* base_val = | 223 base::Value* base_val = |
224 v8_converter->FromV8Value(script_value, context); | 224 v8_converter->FromV8Value(script_value, context); |
(...skipping 25 matching lines...) Expand all Loading... |
250 | 250 |
251 for (WebFrame* child_frame = parent_frame->firstChild(); child_frame; | 251 for (WebFrame* child_frame = parent_frame->firstChild(); child_frame; |
252 child_frame = child_frame->nextSibling()) { | 252 child_frame = child_frame->nextSibling()) { |
253 frames_vector->push_back(child_frame); | 253 frames_vector->push_back(child_frame); |
254 GetAllChildFrames(child_frame, frames_vector); | 254 GetAllChildFrames(child_frame, frames_vector); |
255 } | 255 } |
256 return true; | 256 return true; |
257 } | 257 } |
258 | 258 |
259 } // namespace extensions | 259 } // namespace extensions |
OLD | NEW |