OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/renderer/extensions/user_script_slave.h" | 5 #include "chrome/renderer/extensions/user_script_slave.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 20 matching lines...) Expand all Loading... | |
31 #include "third_party/WebKit/public/web/WebDataSource.h" | 31 #include "third_party/WebKit/public/web/WebDataSource.h" |
32 #include "third_party/WebKit/public/web/WebDocument.h" | 32 #include "third_party/WebKit/public/web/WebDocument.h" |
33 #include "third_party/WebKit/public/web/WebFrame.h" | 33 #include "third_party/WebKit/public/web/WebFrame.h" |
34 #include "third_party/WebKit/public/web/WebSecurityOrigin.h" | 34 #include "third_party/WebKit/public/web/WebSecurityOrigin.h" |
35 #include "third_party/WebKit/public/web/WebSecurityPolicy.h" | 35 #include "third_party/WebKit/public/web/WebSecurityPolicy.h" |
36 #include "third_party/WebKit/public/web/WebView.h" | 36 #include "third_party/WebKit/public/web/WebView.h" |
37 #include "ui/base/resource/resource_bundle.h" | 37 #include "ui/base/resource/resource_bundle.h" |
38 #include "url/gurl.h" | 38 #include "url/gurl.h" |
39 | 39 |
40 using blink::WebFrame; | 40 using blink::WebFrame; |
41 using blink::WebDocument; | |
41 using blink::WebSecurityOrigin; | 42 using blink::WebSecurityOrigin; |
42 using blink::WebSecurityPolicy; | 43 using blink::WebSecurityPolicy; |
43 using blink::WebString; | 44 using blink::WebString; |
44 using blink::WebVector; | 45 using blink::WebVector; |
45 using blink::WebView; | 46 using blink::WebView; |
46 using content::RenderThread; | 47 using content::RenderThread; |
47 | 48 |
48 namespace extensions { | 49 namespace extensions { |
49 | 50 |
50 // These two strings are injected before and after the Greasemonkey API and | 51 // These two strings are injected before and after the Greasemonkey API and |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
187 // the compose iframe's dataSource URL is about:blank, but the document URL | 188 // the compose iframe's dataSource URL is about:blank, but the document URL |
188 // changes to match the parent document after Gmail document.writes into | 189 // changes to match the parent document after Gmail document.writes into |
189 // it to create the editor. | 190 // it to create the editor. |
190 // http://code.google.com/p/chromium/issues/detail?id=86742 | 191 // http://code.google.com/p/chromium/issues/detail?id=86742 |
191 blink::WebDataSource* data_source = frame->provisionalDataSource() ? | 192 blink::WebDataSource* data_source = frame->provisionalDataSource() ? |
192 frame->provisionalDataSource() : frame->dataSource(); | 193 frame->provisionalDataSource() : frame->dataSource(); |
193 CHECK(data_source); | 194 CHECK(data_source); |
194 return GURL(data_source->request().url()); | 195 return GURL(data_source->request().url()); |
195 } | 196 } |
196 | 197 |
198 GURL UserScriptSlave::GetEffectiveDocumentURL(const WebFrame* frame, | |
199 const GURL& document_url, | |
200 bool match_about_blank) { | |
201 // Common scenario. If |match_about_blank| is false (as is the case in most | |
202 // extensions), or if the frame is not an about:-page, just return | |
203 // |document_url| (supposedly the URL of the frame). | |
204 if (!match_about_blank || !document_url.SchemeIs(content::kAboutScheme)) | |
205 return document_url; | |
206 | |
207 // Non-sandboxed about:blank and about:srcdoc pages inherit their security | |
208 // origin from their parent frame/window. So, traverse the frame/window | |
209 // hierarchy to find the closest non-about:-page and return its URL. | |
210 const WebFrame* parent = frame; | |
211 const WebSecurityOrigin security_origin = frame->document().securityOrigin(); | |
212 do { | |
213 parent = parent->parent() ? parent->parent() : parent->opener(); | |
214 if (parent != NULL) { | |
215 const WebDocument& parent_document = parent->document(); | |
216 if (security_origin.canAccess(parent_document.securityOrigin())) { | |
dcheng
2014/05/08 00:26:05
Hm. Sorry, I think I confused myself when I was re
robwu
2014/05/08 12:44:10
No need to apology, you were right. Consider the f
dcheng
2014/05/09 01:18:50
I don't think we actually exit early =)
But what
robwu
2014/05/09 08:52:21
Ah, of course, because the sandbox is also inherit
| |
217 GURL parent_document_url(parent_document.url()); | |
218 if (!parent_document_url.SchemeIs(content::kAboutScheme)) | |
219 return parent_document_url; | |
220 } | |
221 } | |
222 } while (parent != NULL); | |
223 | |
224 return document_url; | |
225 } | |
226 | |
197 void UserScriptSlave::InjectScripts(WebFrame* frame, | 227 void UserScriptSlave::InjectScripts(WebFrame* frame, |
198 UserScript::RunLocation location) { | 228 UserScript::RunLocation location) { |
199 GURL data_source_url = GetDataSourceURLForFrame(frame); | 229 GURL data_source_url = GetDataSourceURLForFrame(frame); |
200 if (data_source_url.is_empty()) | 230 if (data_source_url.is_empty()) |
201 return; | 231 return; |
202 | 232 |
203 if (frame->isViewSourceModeEnabled()) | 233 if (frame->isViewSourceModeEnabled()) |
204 data_source_url = GURL(content::kViewSourceScheme + std::string(":") + | 234 data_source_url = GURL(content::kViewSourceScheme + std::string(":") + |
205 data_source_url.spec()); | 235 data_source_url.spec()); |
206 | 236 |
(...skipping 10 matching lines...) Expand all Loading... | |
217 if (frame->parent() && !script->match_all_frames()) | 247 if (frame->parent() && !script->match_all_frames()) |
218 continue; // Only match subframes if the script declared it wanted to. | 248 continue; // Only match subframes if the script declared it wanted to. |
219 | 249 |
220 const Extension* extension = extensions_->GetByID(script->extension_id()); | 250 const Extension* extension = extensions_->GetByID(script->extension_id()); |
221 | 251 |
222 // Since extension info is sent separately from user script info, they can | 252 // Since extension info is sent separately from user script info, they can |
223 // be out of sync. We just ignore this situation. | 253 // be out of sync. We just ignore this situation. |
224 if (!extension) | 254 if (!extension) |
225 continue; | 255 continue; |
226 | 256 |
257 const GURL& document_url = GetEffectiveDocumentURL( | |
258 frame, data_source_url, script->match_about_blank()); | |
259 | |
227 // Content scripts are not tab-specific. | 260 // Content scripts are not tab-specific. |
228 const int kNoTabId = -1; | 261 const int kNoTabId = -1; |
229 // We don't have a process id in this context. | 262 // We don't have a process id in this context. |
230 const int kNoProcessId = -1; | 263 const int kNoProcessId = -1; |
231 if (!PermissionsData::CanExecuteScriptOnPage(extension, | 264 if (!PermissionsData::CanExecuteScriptOnPage(extension, |
232 data_source_url, | 265 document_url, |
233 frame->top()->document().url(), | 266 frame->top()->document().url(), |
234 kNoTabId, | 267 kNoTabId, |
235 script, | 268 script, |
236 kNoProcessId, | 269 kNoProcessId, |
237 NULL)) { | 270 NULL)) { |
238 continue; | 271 continue; |
239 } | 272 } |
240 | 273 |
241 if (location == UserScript::DOCUMENT_START) { | 274 if (location == UserScript::DOCUMENT_START) { |
242 num_css += script->css_scripts().size(); | 275 num_css += script->css_scripts().size(); |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
318 } else if (location == UserScript::DOCUMENT_IDLE) { | 351 } else if (location == UserScript::DOCUMENT_IDLE) { |
319 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectIdle_ScriptCount", num_scripts); | 352 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectIdle_ScriptCount", num_scripts); |
320 if (num_scripts) | 353 if (num_scripts) |
321 UMA_HISTOGRAM_TIMES("Extensions.InjectIdle_Time", timer.Elapsed()); | 354 UMA_HISTOGRAM_TIMES("Extensions.InjectIdle_Time", timer.Elapsed()); |
322 } else { | 355 } else { |
323 NOTREACHED(); | 356 NOTREACHED(); |
324 } | 357 } |
325 } | 358 } |
326 | 359 |
327 } // namespace extensions | 360 } // namespace extensions |
OLD | NEW |