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 // TODO(markdittmer): This would be cleaner if we compared page_ids instead. |
| 169 // Begin script injeciton workflow only if the current URL is identical to |
| 170 // the one that matched declarative conditions in the browser. |
| 171 if (main_frame->top()->document().url() == url) { |
| 172 manager_->HandleExecuteDeclarativeScript(main_frame, |
| 173 tab_id, |
| 174 extension_id, |
| 175 script_id, |
| 176 url); |
| 177 } |
| 178 } |
| 179 |
153 void ScriptInjectionManager::RVOHelper::OnPermitScriptInjection( | 180 void ScriptInjectionManager::RVOHelper::OnPermitScriptInjection( |
154 int64 request_id) { | 181 int64 request_id) { |
155 manager_->HandlePermitScriptInjection(request_id); | 182 manager_->HandlePermitScriptInjection(request_id); |
156 } | 183 } |
157 | 184 |
158 void ScriptInjectionManager::RVOHelper::RunIdle(blink::WebFrame* frame) { | 185 void ScriptInjectionManager::RVOHelper::RunIdle(blink::WebFrame* frame) { |
159 // Only notify the manager if the frame hasn't either been removed or already | 186 // 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. | 187 // had idle run since the task to RunIdle() was posted. |
161 if (pending_idle_frames_.count(frame) > 0) { | 188 if (pending_idle_frames_.count(frame) > 0) { |
162 manager_->InjectScripts(frame, UserScript::DOCUMENT_IDLE); | 189 manager_->InjectScripts(frame, UserScript::DOCUMENT_IDLE); |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 ScriptsRunInfo scripts_run_info; | 343 ScriptsRunInfo scripts_run_info; |
317 FrameStatusMap::const_iterator iter = frame_statuses_.find(main_frame); | 344 FrameStatusMap::const_iterator iter = frame_statuses_.find(main_frame); |
318 if (!injection->TryToInject( | 345 if (!injection->TryToInject( |
319 iter == frame_statuses_.end() ? UserScript::UNDEFINED : iter->second, | 346 iter == frame_statuses_.end() ? UserScript::UNDEFINED : iter->second, |
320 extensions_->GetByID(injection->extension_id()), | 347 extensions_->GetByID(injection->extension_id()), |
321 &scripts_run_info)) { | 348 &scripts_run_info)) { |
322 pending_injections_.push_back(injection.release()); | 349 pending_injections_.push_back(injection.release()); |
323 } | 350 } |
324 } | 351 } |
325 | 352 |
| 353 void ScriptInjectionManager::HandleExecuteDeclarativeScript( |
| 354 blink::WebFrame* web_frame, |
| 355 int tab_id, |
| 356 const ExtensionId& extension_id, |
| 357 int script_id, |
| 358 const GURL& url) { |
| 359 const Extension* extension = extensions_->GetByID(extension_id); |
| 360 scoped_ptr<ScriptInjection> injection = |
| 361 user_script_set_manager_->GetInjectionForDeclarativeScript( |
| 362 script_id, |
| 363 web_frame, |
| 364 tab_id, |
| 365 url, |
| 366 extension); |
| 367 if (injection.get()) { |
| 368 ScriptsRunInfo scripts_run_info; |
| 369 // TODO(markdittmer): Use return value of TryToInject for error handling. |
| 370 injection->TryToInject(UserScript::BROWSER_DRIVEN, |
| 371 extension, |
| 372 &scripts_run_info); |
| 373 scripts_run_info.LogRun(web_frame, UserScript::BROWSER_DRIVEN); |
| 374 } |
| 375 } |
| 376 |
326 void ScriptInjectionManager::HandlePermitScriptInjection(int64 request_id) { | 377 void ScriptInjectionManager::HandlePermitScriptInjection(int64 request_id) { |
327 ScopedVector<ScriptInjection>::iterator iter = | 378 ScopedVector<ScriptInjection>::iterator iter = |
328 pending_injections_.begin(); | 379 pending_injections_.begin(); |
329 for (; iter != pending_injections_.end(); ++iter) { | 380 for (; iter != pending_injections_.end(); ++iter) { |
330 if ((*iter)->request_id() == request_id) | 381 if ((*iter)->request_id() == request_id) |
331 break; | 382 break; |
332 } | 383 } |
333 if (iter == pending_injections_.end()) | 384 if (iter == pending_injections_.end()) |
334 return; | 385 return; |
335 | 386 |
336 // At this point, because the request is present in pending_injections_, we | 387 // 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, | 388 // know that this is the same page that issued the request (otherwise, |
338 // RVOHelper's DidStartProvisionalLoad callback would have caused it to be | 389 // RVOHelper's DidStartProvisionalLoad callback would have caused it to be |
339 // cleared out). | 390 // cleared out). |
340 | 391 |
341 scoped_ptr<ScriptInjection> injection(*iter); | 392 scoped_ptr<ScriptInjection> injection(*iter); |
342 pending_injections_.weak_erase(iter); | 393 pending_injections_.weak_erase(iter); |
343 | 394 |
344 ScriptsRunInfo scripts_run_info; | 395 ScriptsRunInfo scripts_run_info; |
345 if (injection->OnPermissionGranted(extensions_->GetByID( | 396 if (injection->OnPermissionGranted(extensions_->GetByID( |
346 injection->extension_id()), | 397 injection->extension_id()), |
347 &scripts_run_info)) { | 398 &scripts_run_info)) { |
348 scripts_run_info.LogRun(injection->web_frame(), UserScript::RUN_DEFERRED); | 399 scripts_run_info.LogRun(injection->web_frame(), UserScript::RUN_DEFERRED); |
349 } | 400 } |
350 } | 401 } |
351 | 402 |
352 } // namespace extensions | 403 } // namespace extensions |
OLD | NEW |