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 |
| 39 // context, but the init method deletes itself after the first time. | 44 // context, but the init method deletes itself after the first time. |
| 40 static const char kInitExtension[] = | 45 static const char kInitExtension[] = |
| 41 "if (chrome.initExtension) chrome.initExtension('%s', true, %s);"; | 46 "if (chrome.initExtension) chrome.initExtension('%s', true, %s);"; |
| 42 | 47 |
| 43 | |
| 44 int UserScriptSlave::GetIsolatedWorldId(const std::string& extension_id) { | 48 int UserScriptSlave::GetIsolatedWorldId(const std::string& extension_id) { |
| 45 typedef std::map<std::string, int> IsolatedWorldMap; | 49 typedef std::map<std::string, int> IsolatedWorldMap; |
| 46 | 50 |
| 47 static IsolatedWorldMap g_isolated_world_ids; | 51 static IsolatedWorldMap g_isolated_world_ids; |
| 48 static int g_next_isolated_world_id = 1; | 52 static int g_next_isolated_world_id = 1; |
| 49 | 53 |
| 50 IsolatedWorldMap::iterator iter = g_isolated_world_ids.find(extension_id); | 54 IsolatedWorldMap::iterator iter = g_isolated_world_ids.find(extension_id); |
| 51 if (iter != g_isolated_world_ids.end()) | 55 if (iter != g_isolated_world_ids.end()) |
| 52 return iter->second; | 56 return iter->second; |
| 53 | 57 |
| 54 int new_id = g_next_isolated_world_id; | 58 int new_id = g_next_isolated_world_id; |
| 55 ++g_next_isolated_world_id; | 59 ++g_next_isolated_world_id; |
| 56 | 60 |
| 57 // This map will tend to pile up over time, but realistically, you're never | 61 // This map will tend to pile up over time, but realistically, you're never |
| 58 // going to have enough extensions for it to matter. | 62 // going to have enough extensions for it to matter. |
| 59 g_isolated_world_ids[extension_id] = new_id; | 63 g_isolated_world_ids[extension_id] = new_id; |
| 60 return new_id; | 64 return new_id; |
| 61 } | 65 } |
| 62 | 66 |
| 67 // Associates the extension's isolated world with the extension's URL as the | |
| 68 // origin, and whitelists that origin to have access to 1) the current frame's | |
| 69 // origin 2) origins whitelisted in host permission. | |
| 70 // Unlike the whitelisting done in the extension process by | |
| 71 // ExtensionDispatcher::InitHostPermissions, this is more complex because the | |
| 72 // 1) the current frame's origin changes as the page is navigated 2) the set of | |
| 73 // extensions may change as they are loaded, unloaded and reloaded. | |
| 74 // To solve 1), we keep track of the last origin that we whitelisted for a | |
| 75 // frame, and un-whitelist it when getting a new one. To solve 2), we keep track | |
| 76 // of what host permission origins we've already whitelisted, and don't | |
| 77 // whitelist them again. This means that if an extension is reloaded with fewer | |
| 78 // host permissions we will not remove now-obsolete permissions. | |
| 79 void UserScriptSlave::InitializeIsolatedWorld( | |
|
Matt Perry
2011/05/27 19:10:54
This seems like a lot of effort to work around def
| |
| 80 WebFrame* frame, | |
| 81 int isolated_world_id, | |
| 82 const Extension* extension) { | |
| 83 frame->setIsolatedWorldSecurityOrigin( | |
| 84 isolated_world_id, | |
| 85 WebSecurityOrigin::create(extension->url())); | |
| 86 | |
| 87 // We always have access to the origin of the frame that we're injecting in. | |
| 88 FrameOriginMap::iterator iter = frame_origins_.find(frame->identifier()); | |
| 89 if (iter != frame_origins_.end()) { | |
| 90 std::pair<std::string, std::string> frame_origin = iter->second; | |
| 91 WebSecurityPolicy::removeOriginAccessWhitelistEntry( | |
| 92 extension->url(), | |
| 93 WebString::fromUTF8(frame_origin.first), | |
| 94 WebString::fromUTF8(frame_origin.second), | |
| 95 false); // do not match subdomains | |
| 96 } | |
| 97 | |
| 98 GURL frame_url = GURL(frame->url()); | |
| 99 WebSecurityPolicy::addOriginAccessWhitelistEntry( | |
| 100 extension->url(), | |
| 101 WebString::fromUTF8(frame_url.scheme()), | |
| 102 WebString::fromUTF8(frame_url.host()), | |
| 103 false); // do not match subdomains | |
| 104 frame_origins_[frame->identifier()] = | |
| 105 std::pair<std::string, std::string>(frame_url.scheme(), frame_url.host()); | |
| 106 | |
| 107 const URLPatternList& permissions = extension->host_permissions(); | |
| 108 for (size_t i = 0; i < permissions.size(); ++i) { | |
| 109 const URLPattern& permission = permissions[i]; | |
| 110 | |
| 111 bool already_whitelisted = false; | |
| 112 ExtensionOriginMap::iterator iter = | |
| 113 extension_origins_.find(extension->id()); | |
| 114 ExtensionOriginMap::iterator end = | |
| 115 extension_origins_.upper_bound(extension->id()); | |
| 116 for (; iter != end; ++iter) { | |
| 117 if (iter->second.scheme() == permission.scheme() && | |
| 118 iter->second.host() == permission.host() && | |
| 119 iter->second.match_subdomains() == permission.match_subdomains()) { | |
| 120 already_whitelisted = true; | |
| 121 break; | |
| 122 } | |
| 123 } | |
| 124 if (already_whitelisted) continue; | |
| 125 | |
| 126 extension_origins_.insert( | |
| 127 std::pair<std::string, URLPattern>(extension->id(), permission)); | |
| 128 | |
| 129 const char* schemes[] = { | |
| 130 chrome::kHttpScheme, | |
| 131 chrome::kHttpsScheme, | |
| 132 chrome::kFileScheme, | |
| 133 chrome::kChromeUIScheme, | |
| 134 }; | |
| 135 for (size_t j = 0; j < arraysize(schemes); ++j) { | |
| 136 if (permission.MatchesScheme(schemes[j])) { | |
| 137 WebSecurityPolicy::addOriginAccessWhitelistEntry( | |
| 138 extension->url(), | |
| 139 WebString::fromUTF8(schemes[j]), | |
| 140 WebString::fromUTF8(permission.host()), | |
| 141 permission.match_subdomains()); | |
| 142 } | |
| 143 } | |
| 144 } | |
| 145 } | |
| 146 | |
| 63 UserScriptSlave::UserScriptSlave(const ExtensionSet* extensions) | 147 UserScriptSlave::UserScriptSlave(const ExtensionSet* extensions) |
| 64 : shared_memory_(NULL), | 148 : shared_memory_(NULL), |
| 65 script_deleter_(&scripts_), | 149 script_deleter_(&scripts_), |
| 66 extensions_(extensions) { | 150 extensions_(extensions) { |
| 67 api_js_ = ResourceBundle::GetSharedInstance().GetRawDataResource( | 151 api_js_ = ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 68 IDR_GREASEMONKEY_API_JS); | 152 IDR_GREASEMONKEY_API_JS); |
| 69 } | 153 } |
| 70 | 154 |
| 71 UserScriptSlave::~UserScriptSlave() {} | 155 UserScriptSlave::~UserScriptSlave() {} |
| 72 | 156 |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 242 if (script->is_standalone() || script->emulate_greasemonkey()) { | 326 if (script->is_standalone() || script->emulate_greasemonkey()) { |
| 243 sources.insert(sources.begin(), | 327 sources.insert(sources.begin(), |
| 244 WebScriptSource(WebString::fromUTF8(api_js_.as_string()))); | 328 WebScriptSource(WebString::fromUTF8(api_js_.as_string()))); |
| 245 } | 329 } |
| 246 | 330 |
| 247 // Setup chrome.self to contain an Extension object with the correct | 331 // Setup chrome.self to contain an Extension object with the correct |
| 248 // ID. | 332 // ID. |
| 249 if (!script->extension_id().empty()) { | 333 if (!script->extension_id().empty()) { |
| 250 InsertInitExtensionCode(&sources, script->extension_id()); | 334 InsertInitExtensionCode(&sources, script->extension_id()); |
| 251 isolated_world_id = GetIsolatedWorldId(script->extension_id()); | 335 isolated_world_id = GetIsolatedWorldId(script->extension_id()); |
| 336 InitializeIsolatedWorld(frame, isolated_world_id, extension); | |
| 252 } | 337 } |
| 253 | 338 |
| 254 PerfTimer exec_timer; | 339 PerfTimer exec_timer; |
| 255 frame->executeScriptInIsolatedWorld( | 340 frame->executeScriptInIsolatedWorld( |
| 256 isolated_world_id, &sources.front(), sources.size(), | 341 isolated_world_id, &sources.front(), sources.size(), |
| 257 EXTENSION_GROUP_CONTENT_SCRIPTS); | 342 EXTENSION_GROUP_CONTENT_SCRIPTS); |
| 258 UMA_HISTOGRAM_TIMES("Extensions.InjectScriptTime", exec_timer.Elapsed()); | 343 UMA_HISTOGRAM_TIMES("Extensions.InjectScriptTime", exec_timer.Elapsed()); |
| 259 } | 344 } |
| 260 } | 345 } |
| 261 | 346 |
| 262 // Log debug info. | 347 // Log debug info. |
| 263 if (location == UserScript::DOCUMENT_START) { | 348 if (location == UserScript::DOCUMENT_START) { |
| 264 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_CssCount", num_css); | 349 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_CssCount", num_css); |
| 265 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_ScriptCount", num_scripts); | 350 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_ScriptCount", num_scripts); |
| 266 if (num_css || num_scripts) | 351 if (num_css || num_scripts) |
| 267 UMA_HISTOGRAM_TIMES("Extensions.InjectStart_Time", timer.Elapsed()); | 352 UMA_HISTOGRAM_TIMES("Extensions.InjectStart_Time", timer.Elapsed()); |
| 268 } else if (location == UserScript::DOCUMENT_END) { | 353 } else if (location == UserScript::DOCUMENT_END) { |
| 269 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectEnd_ScriptCount", num_scripts); | 354 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectEnd_ScriptCount", num_scripts); |
| 270 if (num_scripts) | 355 if (num_scripts) |
| 271 UMA_HISTOGRAM_TIMES("Extensions.InjectEnd_Time", timer.Elapsed()); | 356 UMA_HISTOGRAM_TIMES("Extensions.InjectEnd_Time", timer.Elapsed()); |
| 272 } else if (location == UserScript::DOCUMENT_IDLE) { | 357 } else if (location == UserScript::DOCUMENT_IDLE) { |
| 273 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectIdle_ScriptCount", num_scripts); | 358 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectIdle_ScriptCount", num_scripts); |
| 274 if (num_scripts) | 359 if (num_scripts) |
| 275 UMA_HISTOGRAM_TIMES("Extensions.InjectIdle_Time", timer.Elapsed()); | 360 UMA_HISTOGRAM_TIMES("Extensions.InjectIdle_Time", timer.Elapsed()); |
| 276 } else { | 361 } else { |
| 277 NOTREACHED(); | 362 NOTREACHED(); |
| 278 } | 363 } |
| 279 } | 364 } |
| OLD | NEW |