Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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" |
| 11 #include "base/perftimer.h" | 11 #include "base/perftimer.h" |
| 12 #include "base/pickle.h" | 12 #include "base/pickle.h" |
| 13 #include "base/shared_memory.h" | 13 #include "base/shared_memory.h" |
| 14 #include "base/metrics/histogram.h" | 14 #include "base/metrics/histogram.h" |
| 15 #include "base/stringprintf.h" | 15 #include "base/stringprintf.h" |
| 16 #include "chrome/common/extensions/extension.h" | 16 #include "chrome/common/extensions/extension.h" |
| 17 #include "chrome/common/extensions/extension_set.h" | 17 #include "chrome/common/extensions/extension_set.h" |
| 18 #include "chrome/common/url_constants.h" | 18 #include "chrome/common/url_constants.h" |
| 19 #include "chrome/renderer/chrome_render_process_observer.h" | 19 #include "chrome/renderer/chrome_render_process_observer.h" |
| 20 #include "chrome/renderer/extensions/extension_dispatcher.h" | |
| 20 #include "chrome/renderer/extensions/extension_groups.h" | 21 #include "chrome/renderer/extensions/extension_groups.h" |
| 21 #include "googleurl/src/gurl.h" | 22 #include "googleurl/src/gurl.h" |
| 22 #include "grit/renderer_resources.h" | 23 #include "grit/renderer_resources.h" |
| 23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" | |
| 26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityPolicy.h" | |
| 24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebVector.h" | 27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebVector.h" |
| 25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| 26 #include "ui/base/resource/resource_bundle.h" | 29 #include "ui/base/resource/resource_bundle.h" |
| 27 | 30 |
| 28 using WebKit::WebFrame; | 31 using WebKit::WebFrame; |
| 32 using WebKit::WebSecurityOrigin; | |
| 33 using WebKit::WebSecurityPolicy; | |
| 29 using WebKit::WebString; | 34 using WebKit::WebString; |
| 30 using WebKit::WebVector; | 35 using WebKit::WebVector; |
| 31 using WebKit::WebView; | 36 using WebKit::WebView; |
| 32 | 37 |
| 33 // These two strings are injected before and after the Greasemonkey API and | 38 // These two strings are injected before and after the Greasemonkey API and |
| 34 // user script to wrap it in an anonymous scope. | 39 // user script to wrap it in an anonymous scope. |
| 35 static const char kUserScriptHead[] = "(function (unsafeWindow) {\n"; | 40 static const char kUserScriptHead[] = "(function (unsafeWindow) {\n"; |
| 36 static const char kUserScriptTail[] = "\n})(window);"; | 41 static const char kUserScriptTail[] = "\n})(window);"; |
| 37 | 42 |
| 38 // Sets up the chrome.extension module. This may be run multiple times per | 43 // Sets up the chrome.extension module. This may be run multiple times per |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 53 | 58 |
| 54 int new_id = g_next_isolated_world_id; | 59 int new_id = g_next_isolated_world_id; |
| 55 ++g_next_isolated_world_id; | 60 ++g_next_isolated_world_id; |
| 56 | 61 |
| 57 // This map will tend to pile up over time, but realistically, you're never | 62 // This map will tend to pile up over time, but realistically, you're never |
| 58 // going to have enough extensions for it to matter. | 63 // going to have enough extensions for it to matter. |
| 59 g_isolated_world_ids[extension_id] = new_id; | 64 g_isolated_world_ids[extension_id] = new_id; |
| 60 return new_id; | 65 return new_id; |
| 61 } | 66 } |
| 62 | 67 |
| 68 void UserScriptSlave::InitializeIsolatedWorld( | |
| 69 WebFrame* frame, | |
| 70 int isolated_world_id, | |
| 71 const Extension* extension) const { | |
| 72 frame->setIsolatedWorldSecurityOrigin( | |
| 73 isolated_world_id, | |
| 74 WebSecurityOrigin::create(extension->url())); | |
| 75 | |
| 76 // We always have access to the origin of the page that we're injecting in. | |
| 77 GURL frame_url = GURL(frame->url()); | |
| 78 WebSecurityPolicy::addOriginAccessWhitelistEntry( | |
|
Matt Perry
2011/05/26 22:39:48
note that we never clear the origin access whiteli
| |
| 79 extension->url(), | |
| 80 WebString::fromUTF8(frame_url.scheme()), | |
| 81 WebString::fromUTF8(frame_url.host()), | |
| 82 false); // do not match subdomains | |
| 83 | |
| 84 ExtensionDispatcher::InitHostPermissions(extension); | |
| 85 } | |
| 86 | |
| 63 UserScriptSlave::UserScriptSlave(const ExtensionSet* extensions) | 87 UserScriptSlave::UserScriptSlave(const ExtensionSet* extensions) |
| 64 : shared_memory_(NULL), | 88 : shared_memory_(NULL), |
| 65 script_deleter_(&scripts_), | 89 script_deleter_(&scripts_), |
| 66 extensions_(extensions) { | 90 extensions_(extensions) { |
| 67 api_js_ = ResourceBundle::GetSharedInstance().GetRawDataResource( | 91 api_js_ = ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 68 IDR_GREASEMONKEY_API_JS); | 92 IDR_GREASEMONKEY_API_JS); |
| 69 } | 93 } |
| 70 | 94 |
| 71 UserScriptSlave::~UserScriptSlave() {} | 95 UserScriptSlave::~UserScriptSlave() {} |
| 72 | 96 |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 242 if (script->is_standalone() || script->emulate_greasemonkey()) { | 266 if (script->is_standalone() || script->emulate_greasemonkey()) { |
| 243 sources.insert(sources.begin(), | 267 sources.insert(sources.begin(), |
| 244 WebScriptSource(WebString::fromUTF8(api_js_.as_string()))); | 268 WebScriptSource(WebString::fromUTF8(api_js_.as_string()))); |
| 245 } | 269 } |
| 246 | 270 |
| 247 // Setup chrome.self to contain an Extension object with the correct | 271 // Setup chrome.self to contain an Extension object with the correct |
| 248 // ID. | 272 // ID. |
| 249 if (!script->extension_id().empty()) { | 273 if (!script->extension_id().empty()) { |
| 250 InsertInitExtensionCode(&sources, script->extension_id()); | 274 InsertInitExtensionCode(&sources, script->extension_id()); |
| 251 isolated_world_id = GetIsolatedWorldId(script->extension_id()); | 275 isolated_world_id = GetIsolatedWorldId(script->extension_id()); |
| 276 InitializeIsolatedWorld(frame, isolated_world_id, extension); | |
| 252 } | 277 } |
| 253 | 278 |
| 254 PerfTimer exec_timer; | 279 PerfTimer exec_timer; |
| 255 frame->executeScriptInIsolatedWorld( | 280 frame->executeScriptInIsolatedWorld( |
| 256 isolated_world_id, &sources.front(), sources.size(), | 281 isolated_world_id, &sources.front(), sources.size(), |
| 257 EXTENSION_GROUP_CONTENT_SCRIPTS); | 282 EXTENSION_GROUP_CONTENT_SCRIPTS); |
| 258 UMA_HISTOGRAM_TIMES("Extensions.InjectScriptTime", exec_timer.Elapsed()); | 283 UMA_HISTOGRAM_TIMES("Extensions.InjectScriptTime", exec_timer.Elapsed()); |
| 259 } | 284 } |
| 260 } | 285 } |
| 261 | 286 |
| 262 // Log debug info. | 287 // Log debug info. |
| 263 if (location == UserScript::DOCUMENT_START) { | 288 if (location == UserScript::DOCUMENT_START) { |
| 264 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_CssCount", num_css); | 289 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_CssCount", num_css); |
| 265 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_ScriptCount", num_scripts); | 290 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_ScriptCount", num_scripts); |
| 266 if (num_css || num_scripts) | 291 if (num_css || num_scripts) |
| 267 UMA_HISTOGRAM_TIMES("Extensions.InjectStart_Time", timer.Elapsed()); | 292 UMA_HISTOGRAM_TIMES("Extensions.InjectStart_Time", timer.Elapsed()); |
| 268 } else if (location == UserScript::DOCUMENT_END) { | 293 } else if (location == UserScript::DOCUMENT_END) { |
| 269 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectEnd_ScriptCount", num_scripts); | 294 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectEnd_ScriptCount", num_scripts); |
| 270 if (num_scripts) | 295 if (num_scripts) |
| 271 UMA_HISTOGRAM_TIMES("Extensions.InjectEnd_Time", timer.Elapsed()); | 296 UMA_HISTOGRAM_TIMES("Extensions.InjectEnd_Time", timer.Elapsed()); |
| 272 } else if (location == UserScript::DOCUMENT_IDLE) { | 297 } else if (location == UserScript::DOCUMENT_IDLE) { |
| 273 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectIdle_ScriptCount", num_scripts); | 298 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectIdle_ScriptCount", num_scripts); |
| 274 if (num_scripts) | 299 if (num_scripts) |
| 275 UMA_HISTOGRAM_TIMES("Extensions.InjectIdle_Time", timer.Elapsed()); | 300 UMA_HISTOGRAM_TIMES("Extensions.InjectIdle_Time", timer.Elapsed()); |
| 276 } else { | 301 } else { |
| 277 NOTREACHED(); | 302 NOTREACHED(); |
| 278 } | 303 } |
| 279 } | 304 } |
| OLD | NEW |