OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/renderer/extensions/user_script_idle_scheduler.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/message_loop.h" | |
9 #include "chrome/common/extensions/extension_error_utils.h" | |
10 #include "chrome/common/extensions/extension_manifest_constants.h" | |
11 #include "chrome/common/extensions/extension_messages.h" | |
12 #include "chrome/renderer/extensions/extension_dispatcher.h" | |
13 #include "chrome/renderer/extensions/extension_groups.h" | |
14 #include "chrome/renderer/extensions/extension_helper.h" | |
15 #include "chrome/renderer/extensions/user_script_slave.h" | |
16 #include "content/public/renderer/render_view.h" | |
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | |
18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | |
19 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" | |
20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | |
21 | |
22 namespace { | |
23 // The length of time to wait after the DOM is complete to try and run user | |
24 // scripts. | |
25 const int kUserScriptIdleTimeoutMs = 200; | |
26 } | |
27 | |
28 using WebKit::WebDocument; | |
29 using WebKit::WebFrame; | |
30 using WebKit::WebString; | |
31 using WebKit::WebView; | |
32 | |
33 UserScriptIdleScheduler::UserScriptIdleScheduler( | |
34 WebFrame* frame, ExtensionDispatcher* extension_dispatcher) | |
35 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), | |
36 frame_(frame), | |
37 has_run_(false), | |
38 extension_dispatcher_(extension_dispatcher) { | |
39 } | |
40 | |
41 UserScriptIdleScheduler::~UserScriptIdleScheduler() { | |
42 } | |
43 | |
44 void UserScriptIdleScheduler::ExecuteCode( | |
45 const ExtensionMsg_ExecuteCode_Params& params) { | |
46 if (!has_run_) { | |
47 pending_code_execution_queue_.push( | |
48 linked_ptr<ExtensionMsg_ExecuteCode_Params>( | |
49 new ExtensionMsg_ExecuteCode_Params(params))); | |
50 return; | |
51 } | |
52 | |
53 ExecuteCodeImpl(params); | |
54 } | |
55 | |
56 void UserScriptIdleScheduler::DidFinishDocumentLoad() { | |
57 MessageLoop::current()->PostDelayedTask( | |
58 FROM_HERE, base::Bind(&UserScriptIdleScheduler::MaybeRun, | |
59 weak_factory_.GetWeakPtr()), | |
60 base::TimeDelta::FromMilliseconds(kUserScriptIdleTimeoutMs)); | |
61 } | |
62 | |
63 void UserScriptIdleScheduler::DidFinishLoad() { | |
64 // Ensure that running scripts does not keep any progress UI running. | |
65 MessageLoop::current()->PostTask( | |
66 FROM_HERE, base::Bind(&UserScriptIdleScheduler::MaybeRun, | |
67 weak_factory_.GetWeakPtr())); | |
68 } | |
69 | |
70 void UserScriptIdleScheduler::DidStartProvisionalLoad() { | |
71 // The frame is navigating, so reset the state since we'll want to inject | |
72 // scripts once the load finishes. | |
73 has_run_ = false; | |
74 weak_factory_.InvalidateWeakPtrs(); | |
75 while (!pending_code_execution_queue_.empty()) | |
76 pending_code_execution_queue_.pop(); | |
77 } | |
78 | |
79 void UserScriptIdleScheduler::MaybeRun() { | |
80 if (has_run_) | |
81 return; | |
82 | |
83 // Note: we must set this before calling ExecuteCodeImpl, because that may | |
84 // result in a synchronous call back into MaybeRun if there is a pending task | |
85 // currently in the queue. | |
86 // http://code.google.com/p/chromium/issues/detail?id=29644 | |
87 has_run_ = true; | |
88 | |
89 extension_dispatcher_->user_script_slave()->InjectScripts( | |
90 frame_, UserScript::DOCUMENT_IDLE); | |
91 | |
92 while (!pending_code_execution_queue_.empty()) { | |
93 linked_ptr<ExtensionMsg_ExecuteCode_Params>& params = | |
94 pending_code_execution_queue_.front(); | |
95 ExecuteCodeImpl(*params); | |
96 pending_code_execution_queue_.pop(); | |
97 } | |
98 } | |
99 | |
100 void UserScriptIdleScheduler::ExecuteCodeImpl( | |
101 const ExtensionMsg_ExecuteCode_Params& params) { | |
102 const Extension* extension = extension_dispatcher_->extensions()->GetByID( | |
103 params.extension_id); | |
104 content::RenderView* render_view = | |
105 content::RenderView::FromWebView(frame_->view()); | |
106 | |
107 // Since extension info is sent separately from user script info, they can | |
108 // be out of sync. We just ignore this situation. | |
109 if (!extension) { | |
110 render_view->Send(new ExtensionHostMsg_ExecuteCodeFinished( | |
111 render_view->GetRoutingID(), params.request_id, true, "")); | |
112 return; | |
113 } | |
114 | |
115 std::vector<WebFrame*> frame_vector; | |
116 frame_vector.push_back(frame_); | |
117 if (params.all_frames) | |
118 GetAllChildFrames(frame_, &frame_vector); | |
119 | |
120 for (std::vector<WebFrame*>::iterator frame_it = frame_vector.begin(); | |
121 frame_it != frame_vector.end(); ++frame_it) { | |
122 WebFrame* frame = *frame_it; | |
123 if (params.is_javascript) { | |
124 // We recheck access here in the renderer for extra safety against races | |
125 // with navigation. | |
126 // | |
127 // But different frames can have different URLs, and the extension might | |
128 // only have access to a subset of them. For the top frame, we can | |
129 // immediately send an error and stop because the browser process | |
130 // considers that an error too. | |
131 // | |
132 // For child frames, we just skip ones the extension doesn't have access | |
133 // to and carry on. | |
134 if (!extension->CanExecuteScriptOnPage(frame->document().url(), | |
135 NULL, NULL)) { | |
136 if (frame->parent()) { | |
137 continue; | |
138 } else { | |
139 render_view->Send(new ExtensionHostMsg_ExecuteCodeFinished( | |
140 render_view->GetRoutingID(), params.request_id, false, | |
141 ExtensionErrorUtils::FormatErrorMessage( | |
142 extension_manifest_errors::kCannotAccessPage, | |
143 frame->document().url().spec()))); | |
144 return; | |
145 } | |
146 } | |
147 | |
148 WebScriptSource source(WebString::fromUTF8(params.code)); | |
149 if (params.in_main_world) { | |
150 frame->executeScript(source); | |
151 } else { | |
152 std::vector<WebScriptSource> sources; | |
153 sources.push_back(source); | |
154 frame->executeScriptInIsolatedWorld( | |
155 extension_dispatcher_->user_script_slave()-> | |
156 GetIsolatedWorldIdForExtension(extension, frame), | |
157 &sources.front(), sources.size(), EXTENSION_GROUP_CONTENT_SCRIPTS); | |
158 } | |
159 } else { | |
160 frame->document().insertUserStyleSheet( | |
161 WebString::fromUTF8(params.code), | |
162 // Author level is consistent with WebView::addUserStyleSheet. | |
163 WebDocument::UserStyleAuthorLevel); | |
164 } | |
165 } | |
166 | |
167 render_view->Send(new ExtensionHostMsg_ExecuteCodeFinished( | |
168 render_view->GetRoutingID(), params.request_id, true, "")); | |
169 } | |
170 | |
171 bool UserScriptIdleScheduler::GetAllChildFrames( | |
172 WebFrame* parent_frame, | |
173 std::vector<WebFrame*>* frames_vector) const { | |
174 if (!parent_frame) | |
175 return false; | |
176 | |
177 for (WebFrame* child_frame = parent_frame->firstChild(); child_frame; | |
178 child_frame = child_frame->nextSibling()) { | |
179 frames_vector->push_back(child_frame); | |
180 GetAllChildFrames(child_frame, frames_vector); | |
181 } | |
182 return true; | |
183 } | |
OLD | NEW |