Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(199)

Side by Side Diff: chrome/renderer/extensions/user_script_slave.cc

Issue 7071025: Use WebFrame::setIsolatedWorldSecurityOrigin to allow cross-origin XHRs in content scripts. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Switch to whitelisting effective permissions. Created 9 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 48 // static
44 int UserScriptSlave::GetIsolatedWorldId(const std::string& extension_id) { 49 int UserScriptSlave::GetIsolatedWorldId(
50 const Extension* extension, WebFrame* frame) {
45 typedef std::map<std::string, int> IsolatedWorldMap; 51 typedef std::map<std::string, int> IsolatedWorldMap;
46 52
47 static IsolatedWorldMap g_isolated_world_ids; 53 static IsolatedWorldMap g_isolated_world_ids;
48 static int g_next_isolated_world_id = 1; 54 static int g_next_isolated_world_id = 1;
49 55
50 IsolatedWorldMap::iterator iter = g_isolated_world_ids.find(extension_id); 56 IsolatedWorldMap::iterator iter = g_isolated_world_ids.find(extension->id());
51 if (iter != g_isolated_world_ids.end()) 57 if (iter != g_isolated_world_ids.end()) {
58 frame->setIsolatedWorldSecurityOrigin(
Matt Perry 2011/05/27 20:55:28 Is this necessary? If it's already in the map, thi
59 iter->second,
60 WebSecurityOrigin::create(extension->url()));
52 return iter->second; 61 return iter->second;
62 }
53 63
54 int new_id = g_next_isolated_world_id; 64 int new_id = g_next_isolated_world_id;
55 ++g_next_isolated_world_id; 65 ++g_next_isolated_world_id;
56 66
57 // This map will tend to pile up over time, but realistically, you're never 67 // This map will tend to pile up over time, but realistically, you're never
58 // going to have enough extensions for it to matter. 68 // going to have enough extensions for it to matter.
59 g_isolated_world_ids[extension_id] = new_id; 69 g_isolated_world_ids[extension->id()] = new_id;
70 InitializeIsolatedWorld(new_id, extension);
71 frame->setIsolatedWorldSecurityOrigin(
72 new_id,
73 WebSecurityOrigin::create(extension->url()));
60 return new_id; 74 return new_id;
61 } 75 }
62 76
77 // static
78 void UserScriptSlave::InitializeIsolatedWorld(
79 int isolated_world_id,
80 const Extension* extension) {
81 const URLPatternList& permissions =
82 extension->GetEffectiveHostPermissions().patterns();
83 for (size_t i = 0; i < permissions.size(); ++i) {
84 const char* schemes[] = {
85 chrome::kHttpScheme,
86 chrome::kHttpsScheme,
87 chrome::kFileScheme,
88 chrome::kChromeUIScheme,
89 };
90 for (size_t j = 0; j < arraysize(schemes); ++j) {
91 if (permissions[i].MatchesScheme(schemes[j])) {
92 WebSecurityPolicy::addOriginAccessWhitelistEntry(
93 extension->url(),
94 WebString::fromUTF8(schemes[j]),
95 WebString::fromUTF8(permissions[i].host()),
96 permissions[i].match_subdomains());
97 }
98 }
99 }
100 }
101
63 UserScriptSlave::UserScriptSlave(const ExtensionSet* extensions) 102 UserScriptSlave::UserScriptSlave(const ExtensionSet* extensions)
64 : shared_memory_(NULL), 103 : shared_memory_(NULL),
65 script_deleter_(&scripts_), 104 script_deleter_(&scripts_),
66 extensions_(extensions) { 105 extensions_(extensions) {
67 api_js_ = ResourceBundle::GetSharedInstance().GetRawDataResource( 106 api_js_ = ResourceBundle::GetSharedInstance().GetRawDataResource(
68 IDR_GREASEMONKEY_API_JS); 107 IDR_GREASEMONKEY_API_JS);
69 } 108 }
70 109
71 UserScriptSlave::~UserScriptSlave() {} 110 UserScriptSlave::~UserScriptSlave() {}
72 111
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 // and "standalone" user scripts. 280 // and "standalone" user scripts.
242 if (script->is_standalone() || script->emulate_greasemonkey()) { 281 if (script->is_standalone() || script->emulate_greasemonkey()) {
243 sources.insert(sources.begin(), 282 sources.insert(sources.begin(),
244 WebScriptSource(WebString::fromUTF8(api_js_.as_string()))); 283 WebScriptSource(WebString::fromUTF8(api_js_.as_string())));
245 } 284 }
246 285
247 // Setup chrome.self to contain an Extension object with the correct 286 // Setup chrome.self to contain an Extension object with the correct
248 // ID. 287 // ID.
249 if (!script->extension_id().empty()) { 288 if (!script->extension_id().empty()) {
250 InsertInitExtensionCode(&sources, script->extension_id()); 289 InsertInitExtensionCode(&sources, script->extension_id());
251 isolated_world_id = GetIsolatedWorldId(script->extension_id()); 290 isolated_world_id = GetIsolatedWorldId(extension, frame);
252 } 291 }
253 292
254 PerfTimer exec_timer; 293 PerfTimer exec_timer;
255 frame->executeScriptInIsolatedWorld( 294 frame->executeScriptInIsolatedWorld(
256 isolated_world_id, &sources.front(), sources.size(), 295 isolated_world_id, &sources.front(), sources.size(),
257 EXTENSION_GROUP_CONTENT_SCRIPTS); 296 EXTENSION_GROUP_CONTENT_SCRIPTS);
258 UMA_HISTOGRAM_TIMES("Extensions.InjectScriptTime", exec_timer.Elapsed()); 297 UMA_HISTOGRAM_TIMES("Extensions.InjectScriptTime", exec_timer.Elapsed());
259 } 298 }
260 } 299 }
261 300
262 // Log debug info. 301 // Log debug info.
263 if (location == UserScript::DOCUMENT_START) { 302 if (location == UserScript::DOCUMENT_START) {
264 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_CssCount", num_css); 303 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_CssCount", num_css);
265 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_ScriptCount", num_scripts); 304 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_ScriptCount", num_scripts);
266 if (num_css || num_scripts) 305 if (num_css || num_scripts)
267 UMA_HISTOGRAM_TIMES("Extensions.InjectStart_Time", timer.Elapsed()); 306 UMA_HISTOGRAM_TIMES("Extensions.InjectStart_Time", timer.Elapsed());
268 } else if (location == UserScript::DOCUMENT_END) { 307 } else if (location == UserScript::DOCUMENT_END) {
269 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectEnd_ScriptCount", num_scripts); 308 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectEnd_ScriptCount", num_scripts);
270 if (num_scripts) 309 if (num_scripts)
271 UMA_HISTOGRAM_TIMES("Extensions.InjectEnd_Time", timer.Elapsed()); 310 UMA_HISTOGRAM_TIMES("Extensions.InjectEnd_Time", timer.Elapsed());
272 } else if (location == UserScript::DOCUMENT_IDLE) { 311 } else if (location == UserScript::DOCUMENT_IDLE) {
273 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectIdle_ScriptCount", num_scripts); 312 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectIdle_ScriptCount", num_scripts);
274 if (num_scripts) 313 if (num_scripts)
275 UMA_HISTOGRAM_TIMES("Extensions.InjectIdle_Time", timer.Elapsed()); 314 UMA_HISTOGRAM_TIMES("Extensions.InjectIdle_Time", timer.Elapsed());
276 } else { 315 } else {
277 NOTREACHED(); 316 NOTREACHED();
278 } 317 }
279 } 318 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698