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

Side by Side Diff: extensions/renderer/user_script_set.cc

Issue 2278003002: Stop copying script contents for each RenderFrames. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address comments from Devlin Created 4 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
« no previous file with comments | « extensions/renderer/user_script_set.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "extensions/renderer/user_script_set.h" 5 #include "extensions/renderer/user_script_set.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <utility> 9 #include <utility>
10 10
(...skipping 13 matching lines...) Expand all
24 #include "extensions/renderer/user_script_injector.h" 24 #include "extensions/renderer/user_script_injector.h"
25 #include "extensions/renderer/web_ui_injection_host.h" 25 #include "extensions/renderer/web_ui_injection_host.h"
26 #include "third_party/WebKit/public/web/WebDocument.h" 26 #include "third_party/WebKit/public/web/WebDocument.h"
27 #include "third_party/WebKit/public/web/WebLocalFrame.h" 27 #include "third_party/WebKit/public/web/WebLocalFrame.h"
28 #include "url/gurl.h" 28 #include "url/gurl.h"
29 29
30 namespace extensions { 30 namespace extensions {
31 31
32 namespace { 32 namespace {
33 33
34 // These two strings are injected before and after the Greasemonkey API and
35 // user script to wrap it in an anonymous scope.
36 const char kUserScriptHead[] = "(function (unsafeWindow) {\n";
37 const char kUserScriptTail[] = "\n})(window);";
38
34 GURL GetDocumentUrlForFrame(blink::WebLocalFrame* frame) { 39 GURL GetDocumentUrlForFrame(blink::WebLocalFrame* frame) {
35 GURL data_source_url = ScriptContext::GetDataSourceURLForFrame(frame); 40 GURL data_source_url = ScriptContext::GetDataSourceURLForFrame(frame);
36 if (!data_source_url.is_empty() && frame->isViewSourceModeEnabled()) { 41 if (!data_source_url.is_empty() && frame->isViewSourceModeEnabled()) {
37 data_source_url = GURL(content::kViewSourceScheme + std::string(":") + 42 data_source_url = GURL(content::kViewSourceScheme + std::string(":") +
38 data_source_url.spec()); 43 data_source_url.spec());
39 } 44 }
40 45
41 return data_source_url; 46 return data_source_url;
42 } 47 }
43 48
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 return false; 111 return false;
107 112
108 // Unpickle scripts. 113 // Unpickle scripts.
109 uint32_t num_scripts = 0; 114 uint32_t num_scripts = 0;
110 base::Pickle pickle(reinterpret_cast<char*>(shared_memory_->memory()), 115 base::Pickle pickle(reinterpret_cast<char*>(shared_memory_->memory()),
111 pickle_size); 116 pickle_size);
112 base::PickleIterator iter(pickle); 117 base::PickleIterator iter(pickle);
113 CHECK(iter.ReadUInt32(&num_scripts)); 118 CHECK(iter.ReadUInt32(&num_scripts));
114 119
115 scripts_.clear(); 120 scripts_.clear();
121 script_sources_.clear();
116 scripts_.reserve(num_scripts); 122 scripts_.reserve(num_scripts);
117 for (uint32_t i = 0; i < num_scripts; ++i) { 123 for (uint32_t i = 0; i < num_scripts; ++i) {
118 std::unique_ptr<UserScript> script(new UserScript()); 124 std::unique_ptr<UserScript> script(new UserScript());
119 script->Unpickle(pickle, &iter); 125 script->Unpickle(pickle, &iter);
120 126
121 // Note that this is a pointer into shared memory. We don't own it. It gets 127 // Note that this is a pointer into shared memory. We don't own it. It gets
122 // cleared up when the last renderer or browser process drops their 128 // cleared up when the last renderer or browser process drops their
123 // reference to the shared memory. 129 // reference to the shared memory.
124 for (size_t j = 0; j < script->js_scripts().size(); ++j) { 130 for (size_t j = 0; j < script->js_scripts().size(); ++j) {
125 const char* body = NULL; 131 const char* body = NULL;
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 bool inject_js = 225 bool inject_js =
220 !script->js_scripts().empty() && script->run_location() == run_location; 226 !script->js_scripts().empty() && script->run_location() == run_location;
221 if (inject_css || inject_js) { 227 if (inject_css || inject_js) {
222 injection.reset(new ScriptInjection(std::move(injector), render_frame, 228 injection.reset(new ScriptInjection(std::move(injector), render_frame,
223 std::move(injection_host), run_location, 229 std::move(injection_host), run_location,
224 log_activity)); 230 log_activity));
225 } 231 }
226 return injection; 232 return injection;
227 } 233 }
228 234
235 blink::WebString UserScriptSet::GetJsSource(const UserScript::File& file,
236 bool emulate_greasemonkey) {
237 const GURL& url = file.url();
238 std::map<GURL, blink::WebString>::iterator iter = script_sources_.find(url);
239 if (iter != script_sources_.end())
240 return iter->second;
241
242 base::StringPiece script_content = file.GetContent();
243 blink::WebString source;
244 if (emulate_greasemonkey) {
245 // We add this dumb function wrapper for user scripts to emulate what
246 // Greasemonkey does. |script_content| becomes:
247 // concat(kUserScriptHead, script_content, kUserScriptTail).
248 std::string content;
249 content.reserve(strlen(kUserScriptHead) + script_content.length() +
250 strlen(kUserScriptTail));
251 content.append(kUserScriptHead);
252 script_content.AppendToString(&content);
253 content.append(kUserScriptTail);
254 source = blink::WebString::fromUTF8(content);
255 } else {
256 source = blink::WebString::fromUTF8(script_content.data(),
257 script_content.length());
258 }
259 script_sources_[url] = source;
260 return source;
261 }
262
263 blink::WebString UserScriptSet::GetCssSource(const UserScript::File& file) {
264 const GURL& url = file.url();
265 std::map<GURL, blink::WebString>::iterator iter = script_sources_.find(url);
266 if (iter != script_sources_.end())
267 return iter->second;
268
269 base::StringPiece script_content = file.GetContent();
270 return script_sources_
271 .insert(std::make_pair(
272 url, blink::WebString::fromUTF8(script_content.data(),
273 script_content.length())))
274 .first->second;
275 }
276
229 } // namespace extensions 277 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/renderer/user_script_set.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698