OLD | NEW |
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 Loading... |
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 | |
39 GURL GetDocumentUrlForFrame(blink::WebLocalFrame* frame) { | 34 GURL GetDocumentUrlForFrame(blink::WebLocalFrame* frame) { |
40 GURL data_source_url = ScriptContext::GetDataSourceURLForFrame(frame); | 35 GURL data_source_url = ScriptContext::GetDataSourceURLForFrame(frame); |
41 if (!data_source_url.is_empty() && frame->isViewSourceModeEnabled()) { | 36 if (!data_source_url.is_empty() && frame->isViewSourceModeEnabled()) { |
42 data_source_url = GURL(content::kViewSourceScheme + std::string(":") + | 37 data_source_url = GURL(content::kViewSourceScheme + std::string(":") + |
43 data_source_url.spec()); | 38 data_source_url.spec()); |
44 } | 39 } |
45 | 40 |
46 return data_source_url; | 41 return data_source_url; |
47 } | 42 } |
48 | 43 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 return false; | 106 return false; |
112 | 107 |
113 // Unpickle scripts. | 108 // Unpickle scripts. |
114 uint32_t num_scripts = 0; | 109 uint32_t num_scripts = 0; |
115 base::Pickle pickle(reinterpret_cast<char*>(shared_memory_->memory()), | 110 base::Pickle pickle(reinterpret_cast<char*>(shared_memory_->memory()), |
116 pickle_size); | 111 pickle_size); |
117 base::PickleIterator iter(pickle); | 112 base::PickleIterator iter(pickle); |
118 CHECK(iter.ReadUInt32(&num_scripts)); | 113 CHECK(iter.ReadUInt32(&num_scripts)); |
119 | 114 |
120 scripts_.clear(); | 115 scripts_.clear(); |
121 script_sources_.clear(); | |
122 scripts_.reserve(num_scripts); | 116 scripts_.reserve(num_scripts); |
123 for (uint32_t i = 0; i < num_scripts; ++i) { | 117 for (uint32_t i = 0; i < num_scripts; ++i) { |
124 std::unique_ptr<UserScript> script(new UserScript()); | 118 std::unique_ptr<UserScript> script(new UserScript()); |
125 script->Unpickle(pickle, &iter); | 119 script->Unpickle(pickle, &iter); |
126 | 120 |
127 // Note that this is a pointer into shared memory. We don't own it. It gets | 121 // Note that this is a pointer into shared memory. We don't own it. It gets |
128 // cleared up when the last renderer or browser process drops their | 122 // cleared up when the last renderer or browser process drops their |
129 // reference to the shared memory. | 123 // reference to the shared memory. |
130 for (size_t j = 0; j < script->js_scripts().size(); ++j) { | 124 for (size_t j = 0; j < script->js_scripts().size(); ++j) { |
131 const char* body = NULL; | 125 const char* body = NULL; |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 bool inject_js = | 219 bool inject_js = |
226 !script->js_scripts().empty() && script->run_location() == run_location; | 220 !script->js_scripts().empty() && script->run_location() == run_location; |
227 if (inject_css || inject_js) { | 221 if (inject_css || inject_js) { |
228 injection.reset(new ScriptInjection(std::move(injector), render_frame, | 222 injection.reset(new ScriptInjection(std::move(injector), render_frame, |
229 std::move(injection_host), run_location, | 223 std::move(injection_host), run_location, |
230 log_activity)); | 224 log_activity)); |
231 } | 225 } |
232 return injection; | 226 return injection; |
233 } | 227 } |
234 | 228 |
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 | |
277 } // namespace extensions | 229 } // namespace extensions |
OLD | NEW |