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.h" | 5 #include "extensions/renderer/script_injection.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
11 #include "content/public/common/url_constants.h" | 11 #include "content/public/common/url_constants.h" |
12 #include "content/public/renderer/render_view.h" | |
12 #include "extensions/common/extension.h" | 13 #include "extensions/common/extension.h" |
13 #include "extensions/common/extension_messages.h" | 14 #include "extensions/common/extension_messages.h" |
14 #include "extensions/common/permissions/permissions_data.h" | 15 #include "extensions/common/permissions/permissions_data.h" |
15 #include "extensions/renderer/dom_activity_logger.h" | 16 #include "extensions/renderer/dom_activity_logger.h" |
16 #include "extensions/renderer/extension_groups.h" | 17 #include "extensions/renderer/extension_groups.h" |
18 #include "extensions/renderer/extension_helper.h" | |
17 #include "extensions/renderer/script_context.h" | 19 #include "extensions/renderer/script_context.h" |
18 #include "extensions/renderer/user_script_slave.h" | 20 #include "extensions/renderer/user_script_slave.h" |
19 #include "grit/renderer_resources.h" | 21 #include "grit/renderer_resources.h" |
20 #include "third_party/WebKit/public/web/WebDocument.h" | 22 #include "third_party/WebKit/public/web/WebDocument.h" |
21 #include "third_party/WebKit/public/web/WebFrame.h" | 23 #include "third_party/WebKit/public/web/WebFrame.h" |
22 #include "third_party/WebKit/public/web/WebScriptSource.h" | 24 #include "third_party/WebKit/public/web/WebScriptSource.h" |
25 #include "third_party/WebKit/public/web/WebView.h" | |
23 #include "ui/base/resource/resource_bundle.h" | 26 #include "ui/base/resource/resource_bundle.h" |
24 #include "url/gurl.h" | 27 #include "url/gurl.h" |
25 | 28 |
26 namespace extensions { | 29 namespace extensions { |
27 | 30 |
28 namespace { | 31 namespace { |
29 | 32 |
33 // The id of the next pending injection. | |
34 int64 g_next_pending_id = 0; | |
35 | |
30 // These two strings are injected before and after the Greasemonkey API and | 36 // These two strings are injected before and after the Greasemonkey API and |
31 // user script to wrap it in an anonymous scope. | 37 // user script to wrap it in an anonymous scope. |
32 const char kUserScriptHead[] = "(function (unsafeWindow) {\n"; | 38 const char kUserScriptHead[] = "(function (unsafeWindow) {\n"; |
33 const char kUserScriptTail[] = "\n})(window);"; | 39 const char kUserScriptTail[] = "\n})(window);"; |
34 | 40 |
35 // Greasemonkey API source that is injected with the scripts. | 41 // Greasemonkey API source that is injected with the scripts. |
36 struct GreasemonkeyApiJsString { | 42 struct GreasemonkeyApiJsString { |
37 GreasemonkeyApiJsString(); | 43 GreasemonkeyApiJsString(); |
38 blink::WebScriptSource source; | 44 blink::WebScriptSource source; |
39 }; | 45 }; |
(...skipping 10 matching lines...) Expand all Loading... | |
50 LAZY_INSTANCE_INITIALIZER; | 56 LAZY_INSTANCE_INITIALIZER; |
51 | 57 |
52 } // namespace | 58 } // namespace |
53 | 59 |
54 ScriptInjection::ScriptsRunInfo::ScriptsRunInfo() : num_css(0u), num_js(0u) { | 60 ScriptInjection::ScriptsRunInfo::ScriptsRunInfo() : num_css(0u), num_js(0u) { |
55 } | 61 } |
56 | 62 |
57 ScriptInjection::ScriptsRunInfo::~ScriptsRunInfo() { | 63 ScriptInjection::ScriptsRunInfo::~ScriptsRunInfo() { |
58 } | 64 } |
59 | 65 |
66 struct ScriptInjection::PendingInjection { | |
67 PendingInjection(blink::WebFrame* web_frame, | |
68 UserScript::RunLocation run_location, | |
69 int page_id); | |
70 ~PendingInjection(); | |
71 | |
72 // The globally-unique id of this request. | |
73 int64 id; | |
74 | |
75 // The pointer to the web frame into which the script should be injected. | |
76 // This is weak, but safe because we remove pending requests when a frame is | |
77 // terminated. | |
78 blink::WebFrame* web_frame; | |
79 | |
80 // The run location to inject at. | |
81 // Note: This could be a lie - we might inject well after this run location | |
82 // has come and gone. But we need to know it to know which scripts to inject. | |
83 UserScript::RunLocation run_location; | |
84 | |
85 // The corresponding page id, to protect against races. | |
86 int page_id; | |
87 }; | |
88 | |
89 ScriptInjection::PendingInjection::PendingInjection( | |
90 blink::WebFrame* web_frame, | |
91 UserScript::RunLocation run_location, | |
92 int page_id) | |
93 : id(g_next_pending_id++), | |
94 web_frame(web_frame), | |
95 run_location(run_location), | |
96 page_id(page_id) { | |
97 } | |
98 | |
99 ScriptInjection::PendingInjection::~PendingInjection() { | |
100 } | |
101 | |
60 // static | 102 // static |
61 GURL ScriptInjection::GetDocumentUrlForFrame(blink::WebFrame* frame) { | 103 GURL ScriptInjection::GetDocumentUrlForFrame(blink::WebFrame* frame) { |
62 GURL data_source_url = ScriptContext::GetDataSourceURLForFrame(frame); | 104 GURL data_source_url = ScriptContext::GetDataSourceURLForFrame(frame); |
63 if (!data_source_url.is_empty() && frame->isViewSourceModeEnabled()) { | 105 if (!data_source_url.is_empty() && frame->isViewSourceModeEnabled()) { |
64 data_source_url = GURL(content::kViewSourceScheme + std::string(":") + | 106 data_source_url = GURL(content::kViewSourceScheme + std::string(":") + |
65 data_source_url.spec()); | 107 data_source_url.spec()); |
66 } | 108 } |
67 | 109 |
68 return data_source_url; | 110 return data_source_url; |
69 } | 111 } |
70 | 112 |
71 ScriptInjection::ScriptInjection( | 113 ScriptInjection::ScriptInjection( |
72 scoped_ptr<UserScript> script, | 114 scoped_ptr<UserScript> script, |
73 UserScriptSlave* user_script_slave) | 115 UserScriptSlave* user_script_slave) |
74 : script_(script.Pass()), | 116 : script_(script.Pass()), |
75 extension_id_(script_->extension_id()), | 117 extension_id_(script_->extension_id()), |
76 user_script_slave_(user_script_slave), | 118 user_script_slave_(user_script_slave), |
77 is_standalone_or_emulate_greasemonkey_( | 119 is_standalone_or_emulate_greasemonkey_( |
78 script_->is_standalone() || script_->emulate_greasemonkey()) { | 120 script_->is_standalone() || script_->emulate_greasemonkey()) { |
79 } | 121 } |
80 | 122 |
81 ScriptInjection::~ScriptInjection() { | 123 ScriptInjection::~ScriptInjection() { |
82 } | 124 } |
83 | 125 |
126 void ScriptInjection::InjectIfAllowed(blink::WebFrame* frame, | |
127 UserScript::RunLocation run_location, | |
128 const GURL& document_url, | |
129 ScriptsRunInfo* scripts_run_info) { | |
130 if (!WantsToRun(frame, run_location, document_url)) | |
131 return; | |
132 | |
133 const Extension* extension = user_script_slave_->GetExtension(extension_id_); | |
134 DCHECK(extension); // WantsToRun() should be false if there's no extension. | |
135 | |
136 content::RenderView* top_render_view = | |
137 content::RenderView::FromWebView(frame->top()->view()); | |
138 // If there's no tab id, we would have no surface to ask for permissions. | |
139 // Until we resolve this, just allow it. | |
140 if (PermissionsData::RequiresActionForScriptExecution(extension) && | |
not at google - send to devlin
2014/05/23 18:05:43
as noted over chat, we also need a check that the
not at google - send to devlin
2014/05/23 18:06:56
and it's very worrying that this didn't break any
Devlin
2014/05/23 19:19:47
Done.
| |
141 ExtensionHelper::Get(top_render_view)->tab_id() != -1) { | |
142 // We use the top render view here (instead of the render view for the | |
143 // frame), because script injection on any frame requires permission for | |
144 // the top frame. Additionally, if we have to show any UI for permissions, | |
145 // it should only be done on the top frame. | |
146 int page_id = top_render_view->GetPageId(); | |
147 ScopedVector<PendingInjection>::iterator pending_injection = | |
148 pending_injections_.insert( | |
149 pending_injections_.end(), | |
150 new PendingInjection(frame, run_location, page_id)); | |
151 | |
152 top_render_view->Send( | |
153 new ExtensionHostMsg_RequestContentScriptPermission( | |
154 top_render_view->GetRoutingID(), | |
155 extension->id(), | |
156 page_id, | |
157 (*pending_injection)->id)); | |
158 } else { | |
159 Inject(frame, run_location, scripts_run_info); | |
160 } | |
161 } | |
162 | |
163 bool ScriptInjection::NotifyScriptPermitted( | |
164 int64 request_id, | |
165 content::RenderView* render_view, | |
166 ScriptsRunInfo* scripts_run_info, | |
167 blink::WebFrame** frame_out) { | |
168 ScopedVector<PendingInjection>::iterator iter = pending_injections_.begin(); | |
169 while (iter != pending_injections_.end() && (*iter)->id != request_id) | |
170 ++iter; | |
171 | |
172 // No matching request. | |
173 if (iter == pending_injections_.end()) | |
174 return false; | |
175 | |
176 // We found the request, so pull it out of the pending list. | |
177 scoped_ptr<PendingInjection> pending_injection(*iter); | |
178 pending_injections_.weak_erase(iter); | |
179 | |
180 // Ensure the Page ID and Extension are still valid. Otherwise, don't inject. | |
181 if (render_view->GetPageId() != pending_injection->page_id) | |
182 return false; | |
183 | |
184 const Extension* extension = user_script_slave_->GetExtension(extension_id_); | |
185 if (!extension) | |
186 return false; | |
187 | |
188 // Everything matches! Inject the script. | |
189 if (frame_out) | |
190 *frame_out = pending_injection->web_frame; | |
191 Inject(pending_injection->web_frame, | |
192 pending_injection->run_location, | |
193 scripts_run_info); | |
194 return true; | |
195 } | |
196 | |
197 void ScriptInjection::FrameDetached(blink::WebFrame* frame) { | |
198 // Any pending injections associated with the given frame will never run. | |
199 // Remove them. | |
200 for (ScopedVector<PendingInjection>::iterator iter = | |
201 pending_injections_.begin(); | |
202 iter != pending_injections_.end();) { | |
203 if ((*iter)->web_frame == frame) | |
204 pending_injections_.erase(iter); | |
205 else | |
206 ++iter; | |
207 } | |
208 } | |
209 | |
84 bool ScriptInjection::WantsToRun(blink::WebFrame* frame, | 210 bool ScriptInjection::WantsToRun(blink::WebFrame* frame, |
85 UserScript::RunLocation run_location, | 211 UserScript::RunLocation run_location, |
86 const GURL& document_url) const { | 212 const GURL& document_url) const { |
87 if (frame->parent() && !script_->match_all_frames()) | 213 if (frame->parent() && !script_->match_all_frames()) |
88 return false; // Only match subframes if the script declared it wanted to. | 214 return false; // Only match subframes if the script declared it wanted to. |
89 | 215 |
90 const Extension* extension = user_script_slave_->GetExtension(extension_id_); | 216 const Extension* extension = user_script_slave_->GetExtension(extension_id_); |
91 // Since extension info is sent separately from user script info, they can | 217 // Since extension info is sent separately from user script info, they can |
92 // be out of sync. We just ignore this situation. | 218 // be out of sync. We just ignore this situation. |
93 if (!extension) | 219 if (!extension) |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
193 scripts_run_info->num_css += css_scripts.size(); | 319 scripts_run_info->num_css += css_scripts.size(); |
194 for (UserScript::FileList::const_iterator iter = css_scripts.begin(); | 320 for (UserScript::FileList::const_iterator iter = css_scripts.begin(); |
195 iter != css_scripts.end(); | 321 iter != css_scripts.end(); |
196 ++iter) { | 322 ++iter) { |
197 frame->document().insertStyleSheet( | 323 frame->document().insertStyleSheet( |
198 blink::WebString::fromUTF8(iter->GetContent().as_string())); | 324 blink::WebString::fromUTF8(iter->GetContent().as_string())); |
199 } | 325 } |
200 } | 326 } |
201 | 327 |
202 } // namespace extensions | 328 } // namespace extensions |
OLD | NEW |