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

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

Issue 164225: Switch to WebFrame from the WebKit API.... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 11 years, 4 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
« no previous file with comments | « chrome/renderer/user_script_slave.h ('k') | chrome/renderer/webplugin_delegate_proxy.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/WebScriptSource.h" 17 #include "webkit/api/public/WebScriptSource.h"
17 #include "webkit/glue/webframe.h"
18 18
19 #include "grit/renderer_resources.h" 19 #include "grit/renderer_resources.h"
20 20
21 using WebKit::WebFrame;
21 using WebKit::WebScriptSource; 22 using WebKit::WebScriptSource;
22 using WebKit::WebString; 23 using WebKit::WebString;
23 24
24 // These two strings are injected before and after the Greasemonkey API and 25 // These two strings are injected before and after the Greasemonkey API and
25 // user script to wrap it in an anonymous scope. 26 // user script to wrap it in an anonymous scope.
26 static const char kUserScriptHead[] = "(function (unsafeWindow) {\n"; 27 static const char kUserScriptHead[] = "(function (unsafeWindow) {\n";
27 static const char kUserScriptTail[] = "\n})(window);"; 28 static const char kUserScriptTail[] = "\n})(window);";
28 29
29 // Creates a convenient reference to a content script's parent extension. 30 // Creates a convenient reference to a content script's parent extension.
30 // TODO(mpcomplete): self.onConnect is deprecated. Remove it at 1.0. 31 // TODO(mpcomplete): self.onConnect is deprecated. Remove it at 1.0.
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 StringPiece(body, body_length)); 105 StringPiece(body, body_length));
105 } 106 }
106 } 107 }
107 108
108 return true; 109 return true;
109 } 110 }
110 111
111 bool UserScriptSlave::InjectScripts(WebFrame* frame, 112 bool UserScriptSlave::InjectScripts(WebFrame* frame,
112 UserScript::RunLocation location) { 113 UserScript::RunLocation location) {
113 // Don't bother if this is not a URL we inject script into. 114 // Don't bother if this is not a URL we inject script into.
114 if (!URLPattern::IsValidScheme(frame->GetURL().scheme())) 115 if (!URLPattern::IsValidScheme(GURL(frame->url()).scheme()))
115 return true; 116 return true;
116 117
117 PerfTimer timer; 118 PerfTimer timer;
118 int num_matched = 0; 119 int num_matched = 0;
119 120
120 for (size_t i = 0; i < scripts_.size(); ++i) { 121 for (size_t i = 0; i < scripts_.size(); ++i) {
121 std::vector<WebScriptSource> sources; 122 std::vector<WebScriptSource> sources;
122 UserScript* script = scripts_[i]; 123 UserScript* script = scripts_[i];
123 if (!script->MatchesUrl(frame->GetURL())) 124 if (!script->MatchesUrl(frame->url()))
124 continue; // This frame doesn't match the script url pattern, skip it. 125 continue; // This frame doesn't match the script url pattern, skip it.
125 126
126 ++num_matched; 127 ++num_matched;
127 // CSS files are always injected on document start before js scripts. 128 // CSS files are always injected on document start before js scripts.
128 if (location == UserScript::DOCUMENT_START) { 129 if (location == UserScript::DOCUMENT_START) {
129 for (size_t j = 0; j < script->css_scripts().size(); ++j) { 130 for (size_t j = 0; j < script->css_scripts().size(); ++j) {
130 UserScript::File& file = script->css_scripts()[j]; 131 UserScript::File& file = script->css_scripts()[j];
131 frame->InsertCSSStyles(file.GetContent().as_string()); 132 frame->insertStyleText(
133 WebString::fromUTF8(file.GetContent().as_string()));
132 } 134 }
133 } 135 }
134 if (script->run_location() == location) { 136 if (script->run_location() == location) {
135 for (size_t j = 0; j < script->js_scripts().size(); ++j) { 137 for (size_t j = 0; j < script->js_scripts().size(); ++j) {
136 UserScript::File &file = script->js_scripts()[j]; 138 UserScript::File &file = script->js_scripts()[j];
137 std::string content = file.GetContent().as_string(); 139 std::string content = file.GetContent().as_string();
138 140
139 // We add this dumb function wrapper for standalone user script to 141 // We add this dumb function wrapper for standalone user script to
140 // emulate what Greasemonkey does. 142 // emulate what Greasemonkey does.
141 if (script->is_standalone()) { 143 if (script->is_standalone()) {
(...skipping 12 matching lines...) Expand all
154 sources.insert(sources.begin(), 156 sources.insert(sources.begin(),
155 WebScriptSource(WebString::fromUTF8(api_js_.as_string()))); 157 WebScriptSource(WebString::fromUTF8(api_js_.as_string())));
156 } else { 158 } else {
157 // Setup chrome.self to contain an Extension object with the correct 159 // Setup chrome.self to contain an Extension object with the correct
158 // ID. 160 // ID.
159 sources.insert(sources.begin(), 161 sources.insert(sources.begin(),
160 WebScriptSource(WebString::fromUTF8( 162 WebScriptSource(WebString::fromUTF8(
161 StringPrintf(kInitExtension, script->extension_id().c_str())))); 163 StringPrintf(kInitExtension, script->extension_id().c_str()))));
162 } 164 }
163 165
164 frame->ExecuteScriptInNewWorld(&sources.front(), sources.size(), 166 frame->executeScriptInNewWorld(&sources.front(), sources.size(),
165 EXTENSION_GROUP_CONTENT_SCRIPTS); 167 EXTENSION_GROUP_CONTENT_SCRIPTS);
166 } 168 }
167 } 169 }
168 170
169 // Log debug info. 171 // Log debug info.
170 if (location == UserScript::DOCUMENT_START) { 172 if (location == UserScript::DOCUMENT_START) {
171 HISTOGRAM_COUNTS_100("UserScripts:DocStart:Count", num_matched); 173 HISTOGRAM_COUNTS_100("UserScripts:DocStart:Count", num_matched);
172 HISTOGRAM_TIMES("UserScripts:DocStart:Time", timer.Elapsed()); 174 HISTOGRAM_TIMES("UserScripts:DocStart:Time", timer.Elapsed());
173 } else { 175 } else {
174 HISTOGRAM_COUNTS_100("UserScripts:DocEnd:Count", num_matched); 176 HISTOGRAM_COUNTS_100("UserScripts:DocEnd:Count", num_matched);
175 HISTOGRAM_TIMES("UserScripts:DocEnd:Time", timer.Elapsed()); 177 HISTOGRAM_TIMES("UserScripts:DocEnd:Time", timer.Elapsed());
176 } 178 }
177 179
178 LOG(INFO) << "Injected " << num_matched << " user scripts into " << 180 LOG(INFO) << "Injected " << num_matched << " user scripts into " <<
179 frame->GetURL().spec(); 181 frame->url().spec().data();
180 return true; 182 return true;
181 } 183 }
OLDNEW
« no previous file with comments | « chrome/renderer/user_script_slave.h ('k') | chrome/renderer/webplugin_delegate_proxy.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698