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 |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "content/public/common/url_constants.h" | 12 #include "content/public/common/url_constants.h" |
13 #include "content/public/renderer/render_frame.h" | 13 #include "content/public/renderer/render_frame.h" |
14 #include "content/public/renderer/render_thread.h" | 14 #include "content/public/renderer/render_thread.h" |
15 #include "extensions/common/extension.h" | 15 #include "extensions/common/extension.h" |
16 #include "extensions/common/extensions_client.h" | 16 #include "extensions/common/extensions_client.h" |
17 #include "extensions/common/permissions/permissions_data.h" | 17 #include "extensions/common/permissions/permissions_data.h" |
18 #include "extensions/renderer/extension_injection_host.h" | 18 #include "extensions/renderer/extension_injection_host.h" |
19 #include "extensions/renderer/extensions_renderer_client.h" | 19 #include "extensions/renderer/extensions_renderer_client.h" |
20 #include "extensions/renderer/injection_host.h" | 20 #include "extensions/renderer/injection_host.h" |
21 #include "extensions/renderer/renderer_extension_registry.h" | 21 #include "extensions/renderer/renderer_extension_registry.h" |
22 #include "extensions/renderer/script_context.h" | 22 #include "extensions/renderer/script_context.h" |
23 #include "extensions/renderer/script_injection.h" | 23 #include "extensions/renderer/script_injection.h" |
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 "grit/extensions_renderer_resources.h" | |
Devlin
2016/09/07 14:29:45
don't need these includes anymore, right?
lazyboy
2016/09/07 16:47:35
Yes.
Done.
| |
26 #include "third_party/WebKit/public/web/WebDocument.h" | 27 #include "third_party/WebKit/public/web/WebDocument.h" |
27 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 28 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
29 #include "ui/base/resource/resource_bundle.h" | |
28 #include "url/gurl.h" | 30 #include "url/gurl.h" |
29 | 31 |
30 namespace extensions { | 32 namespace extensions { |
31 | 33 |
32 namespace { | 34 namespace { |
33 | 35 |
36 // These two strings are injected before and after the Greasemonkey API and | |
37 // user script to wrap it in an anonymous scope. | |
38 const char kUserScriptHead[] = "(function (unsafeWindow) {\n"; | |
39 const char kUserScriptTail[] = "\n})(window);"; | |
40 | |
34 GURL GetDocumentUrlForFrame(blink::WebLocalFrame* frame) { | 41 GURL GetDocumentUrlForFrame(blink::WebLocalFrame* frame) { |
35 GURL data_source_url = ScriptContext::GetDataSourceURLForFrame(frame); | 42 GURL data_source_url = ScriptContext::GetDataSourceURLForFrame(frame); |
36 if (!data_source_url.is_empty() && frame->isViewSourceModeEnabled()) { | 43 if (!data_source_url.is_empty() && frame->isViewSourceModeEnabled()) { |
37 data_source_url = GURL(content::kViewSourceScheme + std::string(":") + | 44 data_source_url = GURL(content::kViewSourceScheme + std::string(":") + |
38 data_source_url.spec()); | 45 data_source_url.spec()); |
39 } | 46 } |
40 | 47 |
41 return data_source_url; | 48 return data_source_url; |
42 } | 49 } |
43 | 50 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
106 return false; | 113 return false; |
107 | 114 |
108 // Unpickle scripts. | 115 // Unpickle scripts. |
109 uint32_t num_scripts = 0; | 116 uint32_t num_scripts = 0; |
110 base::Pickle pickle(reinterpret_cast<char*>(shared_memory_->memory()), | 117 base::Pickle pickle(reinterpret_cast<char*>(shared_memory_->memory()), |
111 pickle_size); | 118 pickle_size); |
112 base::PickleIterator iter(pickle); | 119 base::PickleIterator iter(pickle); |
113 CHECK(iter.ReadUInt32(&num_scripts)); | 120 CHECK(iter.ReadUInt32(&num_scripts)); |
114 | 121 |
115 scripts_.clear(); | 122 scripts_.clear(); |
123 script_sources_.clear(); | |
116 scripts_.reserve(num_scripts); | 124 scripts_.reserve(num_scripts); |
117 for (uint32_t i = 0; i < num_scripts; ++i) { | 125 for (uint32_t i = 0; i < num_scripts; ++i) { |
118 std::unique_ptr<UserScript> script(new UserScript()); | 126 std::unique_ptr<UserScript> script(new UserScript()); |
119 script->Unpickle(pickle, &iter); | 127 script->Unpickle(pickle, &iter); |
120 | 128 |
121 // Note that this is a pointer into shared memory. We don't own it. It gets | 129 // 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 | 130 // cleared up when the last renderer or browser process drops their |
123 // reference to the shared memory. | 131 // reference to the shared memory. |
124 for (size_t j = 0; j < script->js_scripts().size(); ++j) { | 132 for (size_t j = 0; j < script->js_scripts().size(); ++j) { |
125 const char* body = NULL; | 133 const char* body = NULL; |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
219 bool inject_js = | 227 bool inject_js = |
220 !script->js_scripts().empty() && script->run_location() == run_location; | 228 !script->js_scripts().empty() && script->run_location() == run_location; |
221 if (inject_css || inject_js) { | 229 if (inject_css || inject_js) { |
222 injection.reset(new ScriptInjection(std::move(injector), render_frame, | 230 injection.reset(new ScriptInjection(std::move(injector), render_frame, |
223 std::move(injection_host), run_location, | 231 std::move(injection_host), run_location, |
224 log_activity)); | 232 log_activity)); |
225 } | 233 } |
226 return injection; | 234 return injection; |
227 } | 235 } |
228 | 236 |
237 blink::WebString UserScriptSet::GetJsSource(const UserScript::File& file, | |
238 bool emulate_greasemonkey) { | |
239 const GURL& url = file.url(); | |
240 std::map<GURL, blink::WebString>::iterator iter = script_sources_.find(url); | |
241 if (iter != script_sources_.end()) | |
242 return iter->second; | |
243 | |
244 base::StringPiece script_content = file.GetContent(); | |
245 blink::WebString source; | |
246 if (emulate_greasemonkey) { | |
247 // We add this dumb function wrapper for user scripts to emulate what | |
248 // Greasemonkey does. |script_content| becomes: | |
249 // concat(kUserScriptHead, script_content, kUserScriptTail). | |
250 std::string content; | |
251 content.reserve(strlen(kUserScriptHead) + script_content.length() + | |
252 strlen(kUserScriptTail)); | |
253 content.append(kUserScriptHead); | |
254 script_content.AppendToString(&content); | |
255 content.append(kUserScriptTail); | |
256 source = blink::WebString::fromUTF8(content); | |
257 } else { | |
258 source = blink::WebString::fromUTF8(script_content.data(), | |
259 script_content.length()); | |
260 } | |
261 script_sources_[url] = source; | |
262 return source; | |
263 } | |
264 | |
265 blink::WebString UserScriptSet::GetCssSource(const UserScript::File& file) { | |
266 const GURL& url = file.url(); | |
267 std::map<GURL, blink::WebString>::iterator iter = script_sources_.find(url); | |
268 if (iter != script_sources_.end()) | |
269 return iter->second; | |
270 | |
271 base::StringPiece script_content = file.GetContent(); | |
272 return script_sources_ | |
273 .insert(std::make_pair( | |
274 url, blink::WebString::fromUTF8(script_content.data(), | |
275 script_content.length()))) | |
276 .first->second; | |
277 } | |
278 | |
229 } // namespace extensions | 279 } // namespace extensions |
OLD | NEW |