OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "extensions/renderer/script_injection_manager.h" | 5 #include "extensions/renderer/script_injection_manager.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/weak_ptr.h" | 8 #include "base/memory/weak_ptr.h" |
9 #include "base/values.h" | 9 #include "base/values.h" |
10 #include "content/public/renderer/render_view.h" | 10 #include "content/public/renderer/render_view.h" |
11 #include "content/public/renderer/render_view_observer.h" | 11 #include "content/public/renderer/render_view_observer.h" |
12 #include "extensions/common/extension.h" | 12 #include "extensions/common/extension.h" |
13 #include "extensions/common/extension_messages.h" | 13 #include "extensions/common/extension_messages.h" |
14 #include "extensions/common/extension_set.h" | 14 #include "extensions/common/extension_set.h" |
15 #include "extensions/renderer/extension_helper.h" | 15 #include "extensions/renderer/extension_helper.h" |
16 #include "extensions/renderer/programmatic_script_injector.h" | 16 #include "extensions/renderer/programmatic_script_injector.h" |
17 #include "extensions/renderer/script_injection.h" | 17 #include "extensions/renderer/script_injection.h" |
18 #include "extensions/renderer/scripts_run_info.h" | 18 #include "extensions/renderer/scripts_run_info.h" |
19 #include "ipc/ipc_message_macros.h" | 19 #include "ipc/ipc_message_macros.h" |
20 #include "third_party/WebKit/public/web/WebDocument.h" | |
20 #include "third_party/WebKit/public/web/WebFrame.h" | 21 #include "third_party/WebKit/public/web/WebFrame.h" |
21 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 22 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
22 #include "third_party/WebKit/public/web/WebView.h" | 23 #include "third_party/WebKit/public/web/WebView.h" |
23 #include "url/gurl.h" | 24 #include "url/gurl.h" |
24 | 25 |
25 namespace extensions { | 26 namespace extensions { |
26 | 27 |
27 namespace { | 28 namespace { |
28 | 29 |
29 // The length of time to wait after the DOM is complete to try and run user | 30 // The length of time to wait after the DOM is complete to try and run user |
(...skipping 11 matching lines...) Expand all Loading... | |
41 // RenderViewObserver implementation. | 42 // RenderViewObserver implementation. |
42 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | 43 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
43 virtual void DidCreateDocumentElement(blink::WebLocalFrame* frame) OVERRIDE; | 44 virtual void DidCreateDocumentElement(blink::WebLocalFrame* frame) OVERRIDE; |
44 virtual void DidFinishDocumentLoad(blink::WebLocalFrame* frame) OVERRIDE; | 45 virtual void DidFinishDocumentLoad(blink::WebLocalFrame* frame) OVERRIDE; |
45 virtual void DidFinishLoad(blink::WebLocalFrame* frame) OVERRIDE; | 46 virtual void DidFinishLoad(blink::WebLocalFrame* frame) OVERRIDE; |
46 virtual void DidStartProvisionalLoad(blink::WebLocalFrame* frame) OVERRIDE; | 47 virtual void DidStartProvisionalLoad(blink::WebLocalFrame* frame) OVERRIDE; |
47 virtual void FrameDetached(blink::WebFrame* frame) OVERRIDE; | 48 virtual void FrameDetached(blink::WebFrame* frame) OVERRIDE; |
48 virtual void OnDestruct() OVERRIDE; | 49 virtual void OnDestruct() OVERRIDE; |
49 | 50 |
50 virtual void OnExecuteCode(const ExtensionMsg_ExecuteCode_Params& params); | 51 virtual void OnExecuteCode(const ExtensionMsg_ExecuteCode_Params& params); |
52 virtual void OnExecuteDeclarativeScript(int tab_id, | |
53 const ExtensionId& extension_id, | |
54 int script_id, | |
55 const GURL& url); | |
51 virtual void OnPermitScriptInjection(int64 request_id); | 56 virtual void OnPermitScriptInjection(int64 request_id); |
52 | 57 |
53 // Tells the ScriptInjectionManager to run tasks associated with | 58 // Tells the ScriptInjectionManager to run tasks associated with |
54 // document_idle. | 59 // document_idle. |
55 void RunIdle(blink::WebFrame* frame); | 60 void RunIdle(blink::WebFrame* frame); |
56 | 61 |
57 // Indicate that the given |frame| is no longer valid because it is starting | 62 // Indicate that the given |frame| is no longer valid because it is starting |
58 // a new load or closing. | 63 // a new load or closing. |
59 void InvalidateFrame(blink::WebFrame* frame); | 64 void InvalidateFrame(blink::WebFrame* frame); |
60 | 65 |
(...skipping 19 matching lines...) Expand all Loading... | |
80 ScriptInjectionManager::RVOHelper::~RVOHelper() { | 85 ScriptInjectionManager::RVOHelper::~RVOHelper() { |
81 } | 86 } |
82 | 87 |
83 bool ScriptInjectionManager::RVOHelper::OnMessageReceived( | 88 bool ScriptInjectionManager::RVOHelper::OnMessageReceived( |
84 const IPC::Message& message) { | 89 const IPC::Message& message) { |
85 bool handled = true; | 90 bool handled = true; |
86 IPC_BEGIN_MESSAGE_MAP(ScriptInjectionManager::RVOHelper, message) | 91 IPC_BEGIN_MESSAGE_MAP(ScriptInjectionManager::RVOHelper, message) |
87 IPC_MESSAGE_HANDLER(ExtensionMsg_ExecuteCode, OnExecuteCode) | 92 IPC_MESSAGE_HANDLER(ExtensionMsg_ExecuteCode, OnExecuteCode) |
88 IPC_MESSAGE_HANDLER(ExtensionMsg_PermitScriptInjection, | 93 IPC_MESSAGE_HANDLER(ExtensionMsg_PermitScriptInjection, |
89 OnPermitScriptInjection) | 94 OnPermitScriptInjection) |
95 IPC_MESSAGE_HANDLER(ExtensionMsg_ExecuteDeclarativeScript, | |
96 OnExecuteDeclarativeScript) | |
90 IPC_MESSAGE_UNHANDLED(handled = false) | 97 IPC_MESSAGE_UNHANDLED(handled = false) |
91 IPC_END_MESSAGE_MAP() | 98 IPC_END_MESSAGE_MAP() |
92 return handled; | 99 return handled; |
93 } | 100 } |
94 | 101 |
95 void ScriptInjectionManager::RVOHelper::DidCreateDocumentElement( | 102 void ScriptInjectionManager::RVOHelper::DidCreateDocumentElement( |
96 blink::WebLocalFrame* frame) { | 103 blink::WebLocalFrame* frame) { |
97 manager_->InjectScripts(frame, UserScript::DOCUMENT_START); | 104 manager_->InjectScripts(frame, UserScript::DOCUMENT_START); |
98 } | 105 } |
99 | 106 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
143 | 150 |
144 void ScriptInjectionManager::RVOHelper::OnDestruct() { | 151 void ScriptInjectionManager::RVOHelper::OnDestruct() { |
145 manager_->RemoveObserver(this); | 152 manager_->RemoveObserver(this); |
146 } | 153 } |
147 | 154 |
148 void ScriptInjectionManager::RVOHelper::OnExecuteCode( | 155 void ScriptInjectionManager::RVOHelper::OnExecuteCode( |
149 const ExtensionMsg_ExecuteCode_Params& params) { | 156 const ExtensionMsg_ExecuteCode_Params& params) { |
150 manager_->HandleExecuteCode(params, render_view()); | 157 manager_->HandleExecuteCode(params, render_view()); |
151 } | 158 } |
152 | 159 |
160 void ScriptInjectionManager::RVOHelper::OnExecuteDeclarativeScript( | |
161 int tab_id, | |
162 const ExtensionId& extension_id, | |
163 int script_id, | |
164 const GURL& url) { | |
165 blink::WebFrame* main_frame = render_view()->GetWebView()->mainFrame(); | |
166 CHECK(main_frame); | |
167 | |
168 // Begin script injeciton workflow only if the current URL is identical to | |
169 // the one that matched declarative conditions in the browser. | |
170 if (main_frame->top()->document().url() == url) { | |
Devlin
2014/08/28 21:38:10
This should *probably* be done better. This doesn
Mark Dittmer
2014/08/28 23:49:47
Done.
| |
171 manager_->HandleExecuteDeclarativeScript(main_frame, | |
172 tab_id, | |
173 extension_id, | |
174 script_id, | |
175 url); | |
176 } | |
177 } | |
178 | |
153 void ScriptInjectionManager::RVOHelper::OnPermitScriptInjection( | 179 void ScriptInjectionManager::RVOHelper::OnPermitScriptInjection( |
154 int64 request_id) { | 180 int64 request_id) { |
155 manager_->HandlePermitScriptInjection(request_id); | 181 manager_->HandlePermitScriptInjection(request_id); |
156 } | 182 } |
157 | 183 |
158 void ScriptInjectionManager::RVOHelper::RunIdle(blink::WebFrame* frame) { | 184 void ScriptInjectionManager::RVOHelper::RunIdle(blink::WebFrame* frame) { |
159 // Only notify the manager if the frame hasn't either been removed or already | 185 // Only notify the manager if the frame hasn't either been removed or already |
160 // had idle run since the task to RunIdle() was posted. | 186 // had idle run since the task to RunIdle() was posted. |
161 if (pending_idle_frames_.count(frame) > 0) { | 187 if (pending_idle_frames_.count(frame) > 0) { |
162 manager_->InjectScripts(frame, UserScript::DOCUMENT_IDLE); | 188 manager_->InjectScripts(frame, UserScript::DOCUMENT_IDLE); |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
316 ScriptsRunInfo scripts_run_info; | 342 ScriptsRunInfo scripts_run_info; |
317 FrameStatusMap::const_iterator iter = frame_statuses_.find(main_frame); | 343 FrameStatusMap::const_iterator iter = frame_statuses_.find(main_frame); |
318 if (!injection->TryToInject( | 344 if (!injection->TryToInject( |
319 iter == frame_statuses_.end() ? UserScript::UNDEFINED : iter->second, | 345 iter == frame_statuses_.end() ? UserScript::UNDEFINED : iter->second, |
320 extensions_->GetByID(injection->extension_id()), | 346 extensions_->GetByID(injection->extension_id()), |
321 &scripts_run_info)) { | 347 &scripts_run_info)) { |
322 pending_injections_.push_back(injection.release()); | 348 pending_injections_.push_back(injection.release()); |
323 } | 349 } |
324 } | 350 } |
325 | 351 |
352 void ScriptInjectionManager::HandleExecuteDeclarativeScript( | |
353 blink::WebFrame* web_frame, | |
354 int tab_id, | |
355 const ExtensionId& extension_id, | |
356 int script_id, | |
357 const GURL& url) { | |
358 const Extension* extension = extensions_->GetByID(extension_id); | |
359 scoped_ptr<ScriptInjection> injection = | |
360 user_script_set_manager_->GetInjectionForDeclarativeScript( | |
361 script_id, | |
362 web_frame, | |
363 tab_id, | |
364 url, | |
365 extension); | |
366 if (injection.get()) { | |
367 ScriptsRunInfo scripts_run_info; | |
368 // TODO(markdittmer): Use return value of TryToInject for error handling. | |
369 injection->TryToInject(UserScript::BROWSER_DRIVEN, | |
370 extension, | |
371 &scripts_run_info); | |
372 scripts_run_info.LogRun(web_frame, UserScript::BROWSER_DRIVEN); | |
Devlin
2014/08/28 21:38:10
Even if we don't use the result of TryToInject yet
Mark Dittmer
2014/08/28 23:49:47
As per our conversation, ScriptInjection::TryToInj
| |
373 } | |
374 } | |
375 | |
326 void ScriptInjectionManager::HandlePermitScriptInjection(int64 request_id) { | 376 void ScriptInjectionManager::HandlePermitScriptInjection(int64 request_id) { |
327 ScopedVector<ScriptInjection>::iterator iter = | 377 ScopedVector<ScriptInjection>::iterator iter = |
328 pending_injections_.begin(); | 378 pending_injections_.begin(); |
329 for (; iter != pending_injections_.end(); ++iter) { | 379 for (; iter != pending_injections_.end(); ++iter) { |
330 if ((*iter)->request_id() == request_id) | 380 if ((*iter)->request_id() == request_id) |
331 break; | 381 break; |
332 } | 382 } |
333 if (iter == pending_injections_.end()) | 383 if (iter == pending_injections_.end()) |
334 return; | 384 return; |
335 | 385 |
336 // At this point, because the request is present in pending_injections_, we | 386 // At this point, because the request is present in pending_injections_, we |
337 // know that this is the same page that issued the request (otherwise, | 387 // know that this is the same page that issued the request (otherwise, |
338 // RVOHelper's DidStartProvisionalLoad callback would have caused it to be | 388 // RVOHelper's DidStartProvisionalLoad callback would have caused it to be |
339 // cleared out). | 389 // cleared out). |
340 | 390 |
341 scoped_ptr<ScriptInjection> injection(*iter); | 391 scoped_ptr<ScriptInjection> injection(*iter); |
342 pending_injections_.weak_erase(iter); | 392 pending_injections_.weak_erase(iter); |
343 | 393 |
344 ScriptsRunInfo scripts_run_info; | 394 ScriptsRunInfo scripts_run_info; |
345 if (injection->OnPermissionGranted(extensions_->GetByID( | 395 if (injection->OnPermissionGranted(extensions_->GetByID( |
346 injection->extension_id()), | 396 injection->extension_id()), |
347 &scripts_run_info)) { | 397 &scripts_run_info)) { |
348 scripts_run_info.LogRun(injection->web_frame(), UserScript::RUN_DEFERRED); | 398 scripts_run_info.LogRun(injection->web_frame(), UserScript::RUN_DEFERRED); |
349 } | 399 } |
350 } | 400 } |
351 | 401 |
352 } // namespace extensions | 402 } // namespace extensions |
OLD | NEW |