Chromium Code Reviews| 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 do { | |
| 212 parent = parent->parent() ? parent->parent() : parent->opener(); | |
| 213 } while (parent != NULL && | |
| 214 GURL(parent->document().url()).SchemeIs(content::kAboutScheme)); | |
| 215 | |
| 216 if (parent) { | |
| 217 // Only return the parent URL if the frame can access it. | |
| 218 const WebDocument& parent_document = parent->document(); | |
| 219 if (frame->document().securityOrigin().canAccess( | |
| 220 parent_document.securityOrigin())) | |
|
dcheng
2014/05/02 17:23:19
I believe this check should be inside the loop. Ot
not at google - send to devlin
2014/05/02 17:43:49
oh, good point.
robwu
2014/05/02 20:17:04
I've intentionally used do-while instead of while
not at google - send to devlin
2014/05/02 20:20:10
Such an ad network would be severely limiting its
robwu
2014/05/02 20:45:53
Actually, never mind this discussion. If an iframe
robwu
2014/05/07 21:52:55
Done.
| |
| 221 return parent_document.url(); | |
| 222 } | |
| 223 return document_url; | |
| 224 } | |
| 225 | |
| 197 void UserScriptSlave::InjectScripts(WebFrame* frame, | 226 void UserScriptSlave::InjectScripts(WebFrame* frame, |
| 198 UserScript::RunLocation location) { | 227 UserScript::RunLocation location) { |
| 199 GURL data_source_url = GetDataSourceURLForFrame(frame); | 228 GURL data_source_url = GetDataSourceURLForFrame(frame); |
| 200 if (data_source_url.is_empty()) | 229 if (data_source_url.is_empty()) |
| 201 return; | 230 return; |
| 202 | 231 |
| 203 if (frame->isViewSourceModeEnabled()) | 232 if (frame->isViewSourceModeEnabled()) |
| 204 data_source_url = GURL(content::kViewSourceScheme + std::string(":") + | 233 data_source_url = GURL(content::kViewSourceScheme + std::string(":") + |
| 205 data_source_url.spec()); | 234 data_source_url.spec()); |
| 206 | 235 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 217 if (frame->parent() && !script->match_all_frames()) | 246 if (frame->parent() && !script->match_all_frames()) |
| 218 continue; // Only match subframes if the script declared it wanted to. | 247 continue; // Only match subframes if the script declared it wanted to. |
| 219 | 248 |
| 220 const Extension* extension = extensions_->GetByID(script->extension_id()); | 249 const Extension* extension = extensions_->GetByID(script->extension_id()); |
| 221 | 250 |
| 222 // Since extension info is sent separately from user script info, they can | 251 // Since extension info is sent separately from user script info, they can |
| 223 // be out of sync. We just ignore this situation. | 252 // be out of sync. We just ignore this situation. |
| 224 if (!extension) | 253 if (!extension) |
| 225 continue; | 254 continue; |
| 226 | 255 |
| 256 const GURL& document_url = GetEffectiveDocumentURL( | |
| 257 frame, data_source_url, script->match_about_blank()); | |
| 258 | |
| 227 // Content scripts are not tab-specific. | 259 // Content scripts are not tab-specific. |
| 228 const int kNoTabId = -1; | 260 const int kNoTabId = -1; |
| 229 // We don't have a process id in this context. | 261 // We don't have a process id in this context. |
| 230 const int kNoProcessId = -1; | 262 const int kNoProcessId = -1; |
| 231 if (!PermissionsData::CanExecuteScriptOnPage(extension, | 263 if (!PermissionsData::CanExecuteScriptOnPage(extension, |
| 232 data_source_url, | 264 document_url, |
| 233 frame->top()->document().url(), | 265 frame->top()->document().url(), |
| 234 kNoTabId, | 266 kNoTabId, |
| 235 script, | 267 script, |
| 236 kNoProcessId, | 268 kNoProcessId, |
| 237 NULL)) { | 269 NULL)) { |
| 238 continue; | 270 continue; |
| 239 } | 271 } |
| 240 | 272 |
| 241 if (location == UserScript::DOCUMENT_START) { | 273 if (location == UserScript::DOCUMENT_START) { |
| 242 num_css += script->css_scripts().size(); | 274 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) { | 350 } else if (location == UserScript::DOCUMENT_IDLE) { |
| 319 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectIdle_ScriptCount", num_scripts); | 351 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectIdle_ScriptCount", num_scripts); |
| 320 if (num_scripts) | 352 if (num_scripts) |
| 321 UMA_HISTOGRAM_TIMES("Extensions.InjectIdle_Time", timer.Elapsed()); | 353 UMA_HISTOGRAM_TIMES("Extensions.InjectIdle_Time", timer.Elapsed()); |
| 322 } else { | 354 } else { |
| 323 NOTREACHED(); | 355 NOTREACHED(); |
| 324 } | 356 } |
| 325 } | 357 } |
| 326 | 358 |
| 327 } // namespace extensions | 359 } // namespace extensions |
| OLD | NEW |