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

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

Issue 173556: Implement script API:executeScript (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: '' Created 11 years, 3 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) 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"
11 #include "base/pickle.h" 11 #include "base/pickle.h"
12 #include "base/shared_memory.h" 12 #include "base/shared_memory.h"
13 #include "base/string_util.h" 13 #include "base/string_util.h"
14 #include "chrome/renderer/extension_groups.h" 14 #include "chrome/renderer/extension_groups.h"
15 #include "googleurl/src/gurl.h" 15 #include "googleurl/src/gurl.h"
16 #include "webkit/api/public/WebFrame.h" 16 #include "webkit/api/public/WebFrame.h"
17 #include "webkit/api/public/WebScriptSource.h"
18 17
19 #include "grit/renderer_resources.h" 18 #include "grit/renderer_resources.h"
20 19
21 using WebKit::WebFrame; 20 using WebKit::WebFrame;
22 using WebKit::WebScriptSource;
23 using WebKit::WebString; 21 using WebKit::WebString;
24 22
25 // These two strings are injected before and after the Greasemonkey API and 23 // These two strings are injected before and after the Greasemonkey API and
26 // user script to wrap it in an anonymous scope. 24 // user script to wrap it in an anonymous scope.
27 static const char kUserScriptHead[] = "(function (unsafeWindow) {\n"; 25 static const char kUserScriptHead[] = "(function (unsafeWindow) {\n";
28 static const char kUserScriptTail[] = "\n})(window);"; 26 static const char kUserScriptTail[] = "\n})(window);";
29 27
30 // Creates a convenient reference to a content script's parent extension. 28 // Creates a convenient reference to a content script's parent extension.
31 // TODO(mpcomplete): self.onConnect is deprecated. Remove it at 1.0. 29 // TODO(mpcomplete): self.onConnect is deprecated. Remove it at 1.0.
32 // http://code.google.com/p/chromium/issues/detail?id=16356 30 // http://code.google.com/p/chromium/issues/detail?id=16356
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 int body_length = 0; 100 int body_length = 0;
103 CHECK(pickle.ReadData(&iter, &body, &body_length)); 101 CHECK(pickle.ReadData(&iter, &body, &body_length));
104 script->css_scripts()[j].set_external_content( 102 script->css_scripts()[j].set_external_content(
105 base::StringPiece(body, body_length)); 103 base::StringPiece(body, body_length));
106 } 104 }
107 } 105 }
108 106
109 return true; 107 return true;
110 } 108 }
111 109
110 // static
111 void UserScriptSlave::InsertInitExtensionCode(
112 std::vector<WebScriptSource>* sources, const std::string& extension_id) {
113 DCHECK(sources);
114 sources->insert(sources->begin(),
115 WebScriptSource(WebString::fromUTF8(
116 StringPrintf(kInitExtension, extension_id.c_str()))));
117 }
118
112 bool UserScriptSlave::InjectScripts(WebFrame* frame, 119 bool UserScriptSlave::InjectScripts(WebFrame* frame,
113 UserScript::RunLocation location) { 120 UserScript::RunLocation location) {
114 // Don't bother if this is not a URL we inject script into. 121 // Don't bother if this is not a URL we inject script into.
115 if (!URLPattern::IsValidScheme(GURL(frame->url()).scheme())) 122 if (!URLPattern::IsValidScheme(GURL(frame->url()).scheme()))
116 return true; 123 return true;
117 124
118 PerfTimer timer; 125 PerfTimer timer;
119 int num_matched = 0; 126 int num_matched = 0;
120 127
121 for (size_t i = 0; i < scripts_.size(); ++i) { 128 for (size_t i = 0; i < scripts_.size(); ++i) {
(...skipping 28 matching lines...) Expand all
150 } 157 }
151 158
152 if (!sources.empty()) { 159 if (!sources.empty()) {
153 if (script->is_standalone()) { 160 if (script->is_standalone()) {
154 // For standalone scripts, we try to emulate the Greasemonkey API. 161 // For standalone scripts, we try to emulate the Greasemonkey API.
155 sources.insert(sources.begin(), 162 sources.insert(sources.begin(),
156 WebScriptSource(WebString::fromUTF8(api_js_.as_string()))); 163 WebScriptSource(WebString::fromUTF8(api_js_.as_string())));
157 } else { 164 } else {
158 // Setup chrome.self to contain an Extension object with the correct 165 // Setup chrome.self to contain an Extension object with the correct
159 // ID. 166 // ID.
160 sources.insert(sources.begin(), 167 InsertInitExtensionCode(&sources, script->extension_id());
161 WebScriptSource(WebString::fromUTF8(
162 StringPrintf(kInitExtension, script->extension_id().c_str()))));
163 } 168 }
164 169
165 frame->executeScriptInNewWorld(&sources.front(), sources.size(), 170 frame->executeScriptInNewWorld(&sources.front(), sources.size(),
166 EXTENSION_GROUP_CONTENT_SCRIPTS); 171 EXTENSION_GROUP_CONTENT_SCRIPTS);
167 } 172 }
168 } 173 }
169 174
170 // Log debug info. 175 // Log debug info.
171 if (location == UserScript::DOCUMENT_START) { 176 if (location == UserScript::DOCUMENT_START) {
172 HISTOGRAM_COUNTS_100("UserScripts:DocStart:Count", num_matched); 177 HISTOGRAM_COUNTS_100("UserScripts:DocStart:Count", num_matched);
173 HISTOGRAM_TIMES("UserScripts:DocStart:Time", timer.Elapsed()); 178 HISTOGRAM_TIMES("UserScripts:DocStart:Time", timer.Elapsed());
174 } else { 179 } else {
175 HISTOGRAM_COUNTS_100("UserScripts:DocEnd:Count", num_matched); 180 HISTOGRAM_COUNTS_100("UserScripts:DocEnd:Count", num_matched);
176 HISTOGRAM_TIMES("UserScripts:DocEnd:Time", timer.Elapsed()); 181 HISTOGRAM_TIMES("UserScripts:DocEnd:Time", timer.Elapsed());
177 } 182 }
178 183
179 LOG(INFO) << "Injected " << num_matched << " user scripts into " << 184 LOG(INFO) << "Injected " << num_matched << " user scripts into " <<
180 frame->url().spec().data(); 185 frame->url().spec().data();
181 return true; 186 return true;
182 } 187 }
OLDNEW
« no previous file with comments | « chrome/renderer/user_script_slave.h ('k') | chrome/test/data/extensions/api_test/executescript/1.css » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698