| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/user_script_slave.h" | 5 #include "chrome/renderer/user_script_slave.h" |
| 6 | 6 |
| 7 #include "app/resource_bundle.h" | 7 #include "app/resource_bundle.h" |
| 8 #include "base/histogram.h" | 8 #include "base/histogram.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/perftimer.h" | 10 #include "base/perftimer.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 static const char kUserScriptHead[] = "(function (unsafeWindow) {\n"; | 25 static const char kUserScriptHead[] = "(function (unsafeWindow) {\n"; |
| 26 static const char kUserScriptTail[] = "\n})(window);"; | 26 static const char kUserScriptTail[] = "\n})(window);"; |
| 27 | 27 |
| 28 // Creates a convenient reference to a content script's parent extension. | 28 // Creates a convenient reference to a content script's parent extension. |
| 29 // TODO(mpcomplete): self.onConnect is deprecated. Remove it at 1.0. | 29 // TODO(mpcomplete): self.onConnect is deprecated. Remove it at 1.0. |
| 30 // http://code.google.com/p/chromium/issues/detail?id=16356 | 30 // http://code.google.com/p/chromium/issues/detail?id=16356 |
| 31 static const char kInitExtension[] = | 31 static const char kInitExtension[] = |
| 32 "chrome.extension = new chrome.Extension('%s');" | 32 "chrome.extension = new chrome.Extension('%s');" |
| 33 "chrome.self.onConnect = chrome.extension.onConnect;"; | 33 "chrome.self.onConnect = chrome.extension.onConnect;"; |
| 34 | 34 |
| 35 int UserScriptSlave::GetIsolatedWorldId(const std::string& extension_id) { |
| 36 typedef std::map<std::string, int> IsolatedWorldMap; |
| 37 |
| 38 static IsolatedWorldMap g_isolated_world_ids; |
| 39 static int g_next_isolated_world_id = 1; |
| 40 |
| 41 IsolatedWorldMap::iterator iter = g_isolated_world_ids.find(extension_id); |
| 42 if (iter != g_isolated_world_ids.end()) |
| 43 return iter->second; |
| 44 |
| 45 int new_id = g_next_isolated_world_id; |
| 46 ++g_next_isolated_world_id; |
| 47 |
| 48 // This map will tend to pile up over time, but realistically, you're never |
| 49 // going to have enough extensions for it to matter. |
| 50 g_isolated_world_ids[extension_id] = new_id; |
| 51 return new_id; |
| 52 } |
| 53 |
| 35 UserScriptSlave::UserScriptSlave() | 54 UserScriptSlave::UserScriptSlave() |
| 36 : shared_memory_(NULL), | 55 : shared_memory_(NULL), |
| 37 script_deleter_(&scripts_), | 56 script_deleter_(&scripts_), |
| 38 user_script_start_line_(0) { | 57 user_script_start_line_(0) { |
| 39 api_js_ = ResourceBundle::GetSharedInstance().GetRawDataResource( | 58 api_js_ = ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 40 IDR_GREASEMONKEY_API_JS); | 59 IDR_GREASEMONKEY_API_JS); |
| 41 | 60 |
| 42 // Count the number of lines that will be injected before the user script. | 61 // Count the number of lines that will be injected before the user script. |
| 43 base::StringPiece::size_type pos = 0; | 62 base::StringPiece::size_type pos = 0; |
| 44 while ((pos = api_js_.find('\n', pos)) != base::StringPiece::npos) { | 63 while ((pos = api_js_.find('\n', pos)) != base::StringPiece::npos) { |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 if (script->is_standalone()) { | 171 if (script->is_standalone()) { |
| 153 content.insert(0, kUserScriptHead); | 172 content.insert(0, kUserScriptHead); |
| 154 content += kUserScriptTail; | 173 content += kUserScriptTail; |
| 155 } | 174 } |
| 156 sources.push_back( | 175 sources.push_back( |
| 157 WebScriptSource(WebString::fromUTF8(content), file.url())); | 176 WebScriptSource(WebString::fromUTF8(content), file.url())); |
| 158 } | 177 } |
| 159 } | 178 } |
| 160 | 179 |
| 161 if (!sources.empty()) { | 180 if (!sources.empty()) { |
| 181 int isolated_world_id = 0; |
| 182 |
| 162 if (script->is_standalone()) { | 183 if (script->is_standalone()) { |
| 163 // For standalone scripts, we try to emulate the Greasemonkey API. | 184 // For standalone scripts, we try to emulate the Greasemonkey API. |
| 164 sources.insert(sources.begin(), | 185 sources.insert(sources.begin(), |
| 165 WebScriptSource(WebString::fromUTF8(api_js_.as_string()))); | 186 WebScriptSource(WebString::fromUTF8(api_js_.as_string()))); |
| 166 } else { | 187 } else { |
| 167 // Setup chrome.self to contain an Extension object with the correct | 188 // Setup chrome.self to contain an Extension object with the correct |
| 168 // ID. | 189 // ID. |
| 169 InsertInitExtensionCode(&sources, script->extension_id()); | 190 InsertInitExtensionCode(&sources, script->extension_id()); |
| 191 isolated_world_id = GetIsolatedWorldId(script->extension_id()); |
| 170 } | 192 } |
| 171 | 193 |
| 172 frame->executeScriptInNewWorld(&sources.front(), sources.size(), | 194 frame->executeScriptInIsolatedWorld( |
| 173 EXTENSION_GROUP_CONTENT_SCRIPTS); | 195 isolated_world_id, &sources.front(), sources.size(), |
| 196 EXTENSION_GROUP_CONTENT_SCRIPTS); |
| 174 } | 197 } |
| 175 } | 198 } |
| 176 | 199 |
| 177 // Log debug info. | 200 // Log debug info. |
| 178 if (location == UserScript::DOCUMENT_START) { | 201 if (location == UserScript::DOCUMENT_START) { |
| 179 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_CssCount", num_css); | 202 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_CssCount", num_css); |
| 180 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_ScriptCount", num_scripts); | 203 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_ScriptCount", num_scripts); |
| 181 UMA_HISTOGRAM_TIMES("Extensions.InjectStart_Time", timer.Elapsed()); | 204 UMA_HISTOGRAM_TIMES("Extensions.InjectStart_Time", timer.Elapsed()); |
| 182 } else { | 205 } else { |
| 183 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectEnd_ScriptCount", num_scripts); | 206 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectEnd_ScriptCount", num_scripts); |
| 184 UMA_HISTOGRAM_TIMES("Extensions.InjectEnd_Time", timer.Elapsed()); | 207 UMA_HISTOGRAM_TIMES("Extensions.InjectEnd_Time", timer.Elapsed()); |
| 185 } | 208 } |
| 186 | 209 |
| 187 LOG(INFO) << "Injected " << num_scripts << " scripts and " << num_css << | 210 LOG(INFO) << "Injected " << num_scripts << " scripts and " << num_css << |
| 188 "css files into " << frame->url().spec().data(); | 211 "css files into " << frame->url().spec().data(); |
| 189 return true; | 212 return true; |
| 190 } | 213 } |
| OLD | NEW |