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

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: Remove isolated worlds when extension is unloaded 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
48 // static
49 UserScriptSlave::IsolatedWorldMap UserScriptSlave::isolated_world_ids_;
43 50
44 int UserScriptSlave::GetIsolatedWorldId(const std::string& extension_id) { 51 // static
45 typedef std::map<std::string, int> IsolatedWorldMap; 52 int UserScriptSlave::GetIsolatedWorldId(
46 53 const Extension* extension, WebFrame* frame) {
47 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 = isolated_world_ids_.find(extension->id());
51 if (iter != g_isolated_world_ids.end()) 57 if (iter != isolated_world_ids_.end()) {
58 // We need to set the isolated world origin even if it's not a new world
59 // since that is stored per frame, and we might not have used this isolated
60 // world in this frame before.
61 frame->setIsolatedWorldSecurityOrigin(
62 iter->second,
63 WebSecurityOrigin::create(extension->url()));
52 return iter->second; 64 return iter->second;
65 }
53 66
54 int new_id = g_next_isolated_world_id; 67 int new_id = g_next_isolated_world_id;
55 ++g_next_isolated_world_id; 68 ++g_next_isolated_world_id;
56 69
57 // This map will tend to pile up over time, but realistically, you're never 70 // This map will tend to pile up over time, but realistically, you're never
58 // going to have enough extensions for it to matter. 71 // going to have enough extensions for it to matter.
59 g_isolated_world_ids[extension_id] = new_id; 72 isolated_world_ids_[extension->id()] = new_id;
73 InitializeIsolatedWorld(new_id, extension);
74 frame->setIsolatedWorldSecurityOrigin(
75 new_id,
76 WebSecurityOrigin::create(extension->url()));
60 return new_id; 77 return new_id;
61 } 78 }
62 79
80 // static
81 void UserScriptSlave::InitializeIsolatedWorld(
82 int isolated_world_id,
83 const Extension* extension) {
84 const URLPatternList& permissions =
85 extension->GetEffectiveHostPermissions().patterns();
Aaron Boodman 2011/05/29 04:44:09 Sorry for butting in, but it seems unexpected to m
Matt Perry 2011/05/31 18:13:32 I advocated doing this for 2 main reasons: 1. From
86 for (size_t i = 0; i < permissions.size(); ++i) {
87 const char* schemes[] = {
88 chrome::kHttpScheme,
89 chrome::kHttpsScheme,
90 chrome::kFileScheme,
91 chrome::kChromeUIScheme,
92 };
93 for (size_t j = 0; j < arraysize(schemes); ++j) {
94 if (permissions[i].MatchesScheme(schemes[j])) {
95 WebSecurityPolicy::addOriginAccessWhitelistEntry(
96 extension->url(),
97 WebString::fromUTF8(schemes[j]),
98 WebString::fromUTF8(permissions[i].host()),
99 permissions[i].match_subdomains());
100 }
101 }
102 }
103 }
104
105 // static
106 void UserScriptSlave::RemoveIsolatedWorld(const std::string& extension_id) {
107 isolated_world_ids_.erase(extension_id);
108 }
109
63 UserScriptSlave::UserScriptSlave(const ExtensionSet* extensions) 110 UserScriptSlave::UserScriptSlave(const ExtensionSet* extensions)
64 : shared_memory_(NULL), 111 : shared_memory_(NULL),
65 script_deleter_(&scripts_), 112 script_deleter_(&scripts_),
66 extensions_(extensions) { 113 extensions_(extensions) {
67 api_js_ = ResourceBundle::GetSharedInstance().GetRawDataResource( 114 api_js_ = ResourceBundle::GetSharedInstance().GetRawDataResource(
68 IDR_GREASEMONKEY_API_JS); 115 IDR_GREASEMONKEY_API_JS);
69 } 116 }
70 117
71 UserScriptSlave::~UserScriptSlave() {} 118 UserScriptSlave::~UserScriptSlave() {}
72 119
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 // and "standalone" user scripts. 288 // and "standalone" user scripts.
242 if (script->is_standalone() || script->emulate_greasemonkey()) { 289 if (script->is_standalone() || script->emulate_greasemonkey()) {
243 sources.insert(sources.begin(), 290 sources.insert(sources.begin(),
244 WebScriptSource(WebString::fromUTF8(api_js_.as_string()))); 291 WebScriptSource(WebString::fromUTF8(api_js_.as_string())));
245 } 292 }
246 293
247 // Setup chrome.self to contain an Extension object with the correct 294 // Setup chrome.self to contain an Extension object with the correct
248 // ID. 295 // ID.
249 if (!script->extension_id().empty()) { 296 if (!script->extension_id().empty()) {
250 InsertInitExtensionCode(&sources, script->extension_id()); 297 InsertInitExtensionCode(&sources, script->extension_id());
251 isolated_world_id = GetIsolatedWorldId(script->extension_id()); 298 isolated_world_id = GetIsolatedWorldId(extension, frame);
252 } 299 }
253 300
254 PerfTimer exec_timer; 301 PerfTimer exec_timer;
255 frame->executeScriptInIsolatedWorld( 302 frame->executeScriptInIsolatedWorld(
256 isolated_world_id, &sources.front(), sources.size(), 303 isolated_world_id, &sources.front(), sources.size(),
257 EXTENSION_GROUP_CONTENT_SCRIPTS); 304 EXTENSION_GROUP_CONTENT_SCRIPTS);
258 UMA_HISTOGRAM_TIMES("Extensions.InjectScriptTime", exec_timer.Elapsed()); 305 UMA_HISTOGRAM_TIMES("Extensions.InjectScriptTime", exec_timer.Elapsed());
259 } 306 }
260 } 307 }
261 308
262 // Log debug info. 309 // Log debug info.
263 if (location == UserScript::DOCUMENT_START) { 310 if (location == UserScript::DOCUMENT_START) {
264 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_CssCount", num_css); 311 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_CssCount", num_css);
265 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_ScriptCount", num_scripts); 312 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_ScriptCount", num_scripts);
266 if (num_css || num_scripts) 313 if (num_css || num_scripts)
267 UMA_HISTOGRAM_TIMES("Extensions.InjectStart_Time", timer.Elapsed()); 314 UMA_HISTOGRAM_TIMES("Extensions.InjectStart_Time", timer.Elapsed());
268 } else if (location == UserScript::DOCUMENT_END) { 315 } else if (location == UserScript::DOCUMENT_END) {
269 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectEnd_ScriptCount", num_scripts); 316 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectEnd_ScriptCount", num_scripts);
270 if (num_scripts) 317 if (num_scripts)
271 UMA_HISTOGRAM_TIMES("Extensions.InjectEnd_Time", timer.Elapsed()); 318 UMA_HISTOGRAM_TIMES("Extensions.InjectEnd_Time", timer.Elapsed());
272 } else if (location == UserScript::DOCUMENT_IDLE) { 319 } else if (location == UserScript::DOCUMENT_IDLE) {
273 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectIdle_ScriptCount", num_scripts); 320 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectIdle_ScriptCount", num_scripts);
274 if (num_scripts) 321 if (num_scripts)
275 UMA_HISTOGRAM_TIMES("Extensions.InjectIdle_Time", timer.Elapsed()); 322 UMA_HISTOGRAM_TIMES("Extensions.InjectIdle_Time", timer.Elapsed());
276 } else { 323 } else {
277 NOTREACHED(); 324 NOTREACHED();
278 } 325 }
279 } 326 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698