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" |
(...skipping 30 matching lines...) Expand all Loading... | |
41 // RenderViewObserver implementation. | 41 // RenderViewObserver implementation. |
42 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | 42 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
43 virtual void DidCreateDocumentElement(blink::WebLocalFrame* frame) OVERRIDE; | 43 virtual void DidCreateDocumentElement(blink::WebLocalFrame* frame) OVERRIDE; |
44 virtual void DidFinishDocumentLoad(blink::WebLocalFrame* frame) OVERRIDE; | 44 virtual void DidFinishDocumentLoad(blink::WebLocalFrame* frame) OVERRIDE; |
45 virtual void DidFinishLoad(blink::WebLocalFrame* frame) OVERRIDE; | 45 virtual void DidFinishLoad(blink::WebLocalFrame* frame) OVERRIDE; |
46 virtual void DidStartProvisionalLoad(blink::WebLocalFrame* frame) OVERRIDE; | 46 virtual void DidStartProvisionalLoad(blink::WebLocalFrame* frame) OVERRIDE; |
47 virtual void FrameDetached(blink::WebFrame* frame) OVERRIDE; | 47 virtual void FrameDetached(blink::WebFrame* frame) OVERRIDE; |
48 virtual void OnDestruct() OVERRIDE; | 48 virtual void OnDestruct() OVERRIDE; |
49 | 49 |
50 virtual void OnExecuteCode(const ExtensionMsg_ExecuteCode_Params& params); | 50 virtual void OnExecuteCode(const ExtensionMsg_ExecuteCode_Params& params); |
51 virtual void OnExecuteDeclarativeScript(int tab_id, | |
52 const ExtensionId& extension_id, | |
53 int64 script_id, | |
54 const GURL& url); | |
51 virtual void OnPermitScriptInjection(int64 request_id); | 55 virtual void OnPermitScriptInjection(int64 request_id); |
52 | 56 |
53 // Tells the ScriptInjectionManager to run tasks associated with | 57 // Tells the ScriptInjectionManager to run tasks associated with |
54 // document_idle. | 58 // document_idle. |
55 void RunIdle(blink::WebFrame* frame); | 59 void RunIdle(blink::WebFrame* frame); |
56 | 60 |
57 // Indicate that the given |frame| is no longer valid because it is starting | 61 // Indicate that the given |frame| is no longer valid because it is starting |
58 // a new load or closing. | 62 // a new load or closing. |
59 void InvalidateFrame(blink::WebFrame* frame); | 63 void InvalidateFrame(blink::WebFrame* frame); |
60 | 64 |
(...skipping 19 matching lines...) Expand all Loading... | |
80 ScriptInjectionManager::RVOHelper::~RVOHelper() { | 84 ScriptInjectionManager::RVOHelper::~RVOHelper() { |
81 } | 85 } |
82 | 86 |
83 bool ScriptInjectionManager::RVOHelper::OnMessageReceived( | 87 bool ScriptInjectionManager::RVOHelper::OnMessageReceived( |
84 const IPC::Message& message) { | 88 const IPC::Message& message) { |
85 bool handled = true; | 89 bool handled = true; |
86 IPC_BEGIN_MESSAGE_MAP(ScriptInjectionManager::RVOHelper, message) | 90 IPC_BEGIN_MESSAGE_MAP(ScriptInjectionManager::RVOHelper, message) |
87 IPC_MESSAGE_HANDLER(ExtensionMsg_ExecuteCode, OnExecuteCode) | 91 IPC_MESSAGE_HANDLER(ExtensionMsg_ExecuteCode, OnExecuteCode) |
88 IPC_MESSAGE_HANDLER(ExtensionMsg_PermitScriptInjection, | 92 IPC_MESSAGE_HANDLER(ExtensionMsg_PermitScriptInjection, |
89 OnPermitScriptInjection) | 93 OnPermitScriptInjection) |
94 IPC_MESSAGE_HANDLER(ExtensionMsg_ExecuteDeclarativeScript, | |
95 OnExecuteDeclarativeScript) | |
90 IPC_MESSAGE_UNHANDLED(handled = false) | 96 IPC_MESSAGE_UNHANDLED(handled = false) |
91 IPC_END_MESSAGE_MAP() | 97 IPC_END_MESSAGE_MAP() |
92 return handled; | 98 return handled; |
93 } | 99 } |
94 | 100 |
95 void ScriptInjectionManager::RVOHelper::DidCreateDocumentElement( | 101 void ScriptInjectionManager::RVOHelper::DidCreateDocumentElement( |
96 blink::WebLocalFrame* frame) { | 102 blink::WebLocalFrame* frame) { |
97 manager_->InjectScripts(frame, UserScript::DOCUMENT_START); | 103 manager_->InjectScripts(frame, UserScript::DOCUMENT_START); |
98 } | 104 } |
99 | 105 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
143 | 149 |
144 void ScriptInjectionManager::RVOHelper::OnDestruct() { | 150 void ScriptInjectionManager::RVOHelper::OnDestruct() { |
145 manager_->RemoveObserver(this); | 151 manager_->RemoveObserver(this); |
146 } | 152 } |
147 | 153 |
148 void ScriptInjectionManager::RVOHelper::OnExecuteCode( | 154 void ScriptInjectionManager::RVOHelper::OnExecuteCode( |
149 const ExtensionMsg_ExecuteCode_Params& params) { | 155 const ExtensionMsg_ExecuteCode_Params& params) { |
150 manager_->HandleExecuteCode(params, render_view()); | 156 manager_->HandleExecuteCode(params, render_view()); |
151 } | 157 } |
152 | 158 |
159 void ScriptInjectionManager::RVOHelper::OnExecuteDeclarativeScript( | |
160 int tab_id, | |
161 const ExtensionId& extension_id, | |
162 int64 script_id, | |
163 const GURL& url) { | |
164 blink::WebFrame* main_frame = render_view()->GetWebView()->mainFrame(); | |
Devlin
2014/08/26 21:59:16
Shouldn't we somewhere check that |url| hasn't cha
Mark Dittmer
2014/08/27 22:37:04
I think this is handled by script_injection_manage
| |
165 manager_->HandleExecuteDeclarativeScript(main_frame, | |
166 tab_id, | |
167 extension_id, | |
168 script_id, | |
169 url); | |
170 } | |
171 | |
153 void ScriptInjectionManager::RVOHelper::OnPermitScriptInjection( | 172 void ScriptInjectionManager::RVOHelper::OnPermitScriptInjection( |
154 int64 request_id) { | 173 int64 request_id) { |
155 manager_->HandlePermitScriptInjection(request_id); | 174 manager_->HandlePermitScriptInjection(request_id); |
156 } | 175 } |
157 | 176 |
158 void ScriptInjectionManager::RVOHelper::RunIdle(blink::WebFrame* frame) { | 177 void ScriptInjectionManager::RVOHelper::RunIdle(blink::WebFrame* frame) { |
159 // Only notify the manager if the frame hasn't either been removed or already | 178 // 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. | 179 // had idle run since the task to RunIdle() was posted. |
161 if (pending_idle_frames_.count(frame) > 0) { | 180 if (pending_idle_frames_.count(frame) > 0) { |
162 manager_->InjectScripts(frame, UserScript::DOCUMENT_IDLE); | 181 manager_->InjectScripts(frame, UserScript::DOCUMENT_IDLE); |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
316 ScriptsRunInfo scripts_run_info; | 335 ScriptsRunInfo scripts_run_info; |
317 FrameStatusMap::const_iterator iter = frame_statuses_.find(main_frame); | 336 FrameStatusMap::const_iterator iter = frame_statuses_.find(main_frame); |
318 if (!injection->TryToInject( | 337 if (!injection->TryToInject( |
319 iter == frame_statuses_.end() ? UserScript::UNDEFINED : iter->second, | 338 iter == frame_statuses_.end() ? UserScript::UNDEFINED : iter->second, |
320 extensions_->GetByID(injection->extension_id()), | 339 extensions_->GetByID(injection->extension_id()), |
321 &scripts_run_info)) { | 340 &scripts_run_info)) { |
322 pending_injections_.push_back(injection.release()); | 341 pending_injections_.push_back(injection.release()); |
323 } | 342 } |
324 } | 343 } |
325 | 344 |
345 void ScriptInjectionManager::HandleExecuteDeclarativeScript( | |
346 blink::WebFrame* web_frame, | |
347 int tab_id, | |
348 const ExtensionId& extension_id, | |
349 int64 script_id, | |
350 const GURL& url) { | |
351 const Extension* extension = extensions_->GetByID(extension_id); | |
352 scoped_ptr<ScriptInjection> injection = | |
353 user_script_set_manager_->GetInjectionForDeclarativeScript( | |
354 script_id, | |
355 web_frame, | |
356 tab_id, | |
357 url, | |
358 extension); | |
359 if (injection.get()) { | |
360 ScriptsRunInfo scripts_run_info; | |
361 // TODO(markdittmer): Use return value of TryToInject for error handling. | |
362 injection->TryToInject(UserScript::BROWSER_DRIVEN, | |
363 extension, | |
364 &scripts_run_info); | |
365 scripts_run_info.LogRun(web_frame, UserScript::BROWSER_DRIVEN); | |
366 } | |
367 } | |
368 | |
326 void ScriptInjectionManager::HandlePermitScriptInjection(int64 request_id) { | 369 void ScriptInjectionManager::HandlePermitScriptInjection(int64 request_id) { |
327 ScopedVector<ScriptInjection>::iterator iter = | 370 ScopedVector<ScriptInjection>::iterator iter = |
328 pending_injections_.begin(); | 371 pending_injections_.begin(); |
329 for (; iter != pending_injections_.end(); ++iter) { | 372 for (; iter != pending_injections_.end(); ++iter) { |
330 if ((*iter)->request_id() == request_id) | 373 if ((*iter)->request_id() == request_id) |
331 break; | 374 break; |
332 } | 375 } |
333 if (iter == pending_injections_.end()) | 376 if (iter == pending_injections_.end()) |
334 return; | 377 return; |
335 | 378 |
336 // At this point, because the request is present in pending_injections_, we | 379 // 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, | 380 // know that this is the same page that issued the request (otherwise, |
338 // RVOHelper's DidStartProvisionalLoad callback would have caused it to be | 381 // RVOHelper's DidStartProvisionalLoad callback would have caused it to be |
339 // cleared out). | 382 // cleared out). |
340 | 383 |
341 scoped_ptr<ScriptInjection> injection(*iter); | 384 scoped_ptr<ScriptInjection> injection(*iter); |
342 pending_injections_.weak_erase(iter); | 385 pending_injections_.weak_erase(iter); |
343 | 386 |
344 ScriptsRunInfo scripts_run_info; | 387 ScriptsRunInfo scripts_run_info; |
345 if (injection->OnPermissionGranted(extensions_->GetByID( | 388 if (injection->OnPermissionGranted(extensions_->GetByID( |
346 injection->extension_id()), | 389 injection->extension_id()), |
347 &scripts_run_info)) { | 390 &scripts_run_info)) { |
348 scripts_run_info.LogRun(injection->web_frame(), UserScript::RUN_DEFERRED); | 391 scripts_run_info.LogRun(injection->web_frame(), UserScript::RUN_DEFERRED); |
349 } | 392 } |
350 } | 393 } |
351 | 394 |
352 } // namespace extensions | 395 } // namespace extensions |
OLD | NEW |