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

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

Issue 340057: Add first-class support for user scripts (Closed)
Patch Set: newness Created 11 years, 1 month 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
OLDNEW
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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 ++g_next_isolated_world_id; 44 ++g_next_isolated_world_id;
45 45
46 // This map will tend to pile up over time, but realistically, you're never 46 // This map will tend to pile up over time, but realistically, you're never
47 // going to have enough extensions for it to matter. 47 // going to have enough extensions for it to matter.
48 g_isolated_world_ids[extension_id] = new_id; 48 g_isolated_world_ids[extension_id] = new_id;
49 return new_id; 49 return new_id;
50 } 50 }
51 51
52 UserScriptSlave::UserScriptSlave() 52 UserScriptSlave::UserScriptSlave()
53 : shared_memory_(NULL), 53 : shared_memory_(NULL),
54 script_deleter_(&scripts_), 54 script_deleter_(&scripts_) {
55 user_script_start_line_(0) {
56 api_js_ = ResourceBundle::GetSharedInstance().GetRawDataResource( 55 api_js_ = ResourceBundle::GetSharedInstance().GetRawDataResource(
57 IDR_GREASEMONKEY_API_JS); 56 IDR_GREASEMONKEY_API_JS);
58
59 // Count the number of lines that will be injected before the user script.
60 base::StringPiece::size_type pos = 0;
61 while ((pos = api_js_.find('\n', pos)) != base::StringPiece::npos) {
62 user_script_start_line_++;
63 pos++;
64 }
65
66 // NOTE: There is actually one extra line in the injected script because the
67 // function header includes a newline as well. But WebKit expects the
68 // numbering to be one-based, not zero-based, so actually *not* accounting for
69 // this extra line ends us up with the right offset.
70 } 57 }
71 58
72 bool UserScriptSlave::UpdateScripts(base::SharedMemoryHandle shared_memory) { 59 bool UserScriptSlave::UpdateScripts(base::SharedMemoryHandle shared_memory) {
73 scripts_.clear(); 60 scripts_.clear();
74 61
75 // Create the shared memory object (read only). 62 // Create the shared memory object (read only).
76 shared_memory_.reset(new base::SharedMemory(shared_memory, true)); 63 shared_memory_.reset(new base::SharedMemory(shared_memory, true));
77 if (!shared_memory_.get()) 64 if (!shared_memory_.get())
78 return false; 65 return false;
79 66
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
171 content += kUserScriptTail; 158 content += kUserScriptTail;
172 } 159 }
173 sources.push_back( 160 sources.push_back(
174 WebScriptSource(WebString::fromUTF8(content), file.url())); 161 WebScriptSource(WebString::fromUTF8(content), file.url()));
175 } 162 }
176 } 163 }
177 164
178 if (!sources.empty()) { 165 if (!sources.empty()) {
179 int isolated_world_id = 0; 166 int isolated_world_id = 0;
180 167
181 if (script->is_standalone()) { 168 // Emulate Greasemonkey API for scripts that were converted to extensions
182 // For standalone scripts, we try to emulate the Greasemonkey API. 169 // and "standalone" user scripts.
170 if (script->is_standalone() || script->emulate_greasemonkey()) {
183 sources.insert(sources.begin(), 171 sources.insert(sources.begin(),
184 WebScriptSource(WebString::fromUTF8(api_js_.as_string()))); 172 WebScriptSource(WebString::fromUTF8(api_js_.as_string())));
185 } else { 173 }
186 // Setup chrome.self to contain an Extension object with the correct 174
187 // ID. 175 // Setup chrome.self to contain an Extension object with the correct
176 // ID.
177 if (!script->extension_id().empty()) {
188 InsertInitExtensionCode(&sources, script->extension_id()); 178 InsertInitExtensionCode(&sources, script->extension_id());
189 isolated_world_id = GetIsolatedWorldId(script->extension_id()); 179 isolated_world_id = GetIsolatedWorldId(script->extension_id());
190 } 180 }
191 181
192 frame->executeScriptInIsolatedWorld( 182 frame->executeScriptInIsolatedWorld(
193 isolated_world_id, &sources.front(), sources.size(), 183 isolated_world_id, &sources.front(), sources.size(),
194 EXTENSION_GROUP_CONTENT_SCRIPTS); 184 EXTENSION_GROUP_CONTENT_SCRIPTS);
195 } 185 }
196 } 186 }
197 187
198 // Log debug info. 188 // Log debug info.
199 if (location == UserScript::DOCUMENT_START) { 189 if (location == UserScript::DOCUMENT_START) {
200 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_CssCount", num_css); 190 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_CssCount", num_css);
201 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_ScriptCount", num_scripts); 191 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_ScriptCount", num_scripts);
202 UMA_HISTOGRAM_TIMES("Extensions.InjectStart_Time", timer.Elapsed()); 192 UMA_HISTOGRAM_TIMES("Extensions.InjectStart_Time", timer.Elapsed());
203 } else { 193 } else {
204 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectEnd_ScriptCount", num_scripts); 194 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectEnd_ScriptCount", num_scripts);
205 UMA_HISTOGRAM_TIMES("Extensions.InjectEnd_Time", timer.Elapsed()); 195 UMA_HISTOGRAM_TIMES("Extensions.InjectEnd_Time", timer.Elapsed());
206 } 196 }
207 197
208 LOG(INFO) << "Injected " << num_scripts << " scripts and " << num_css << 198 LOG(INFO) << "Injected " << num_scripts << " scripts and " << num_css <<
209 "css files into " << frame->url().spec().data(); 199 "css files into " << frame->url().spec().data();
210 return true; 200 return true;
211 } 201 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698