Chromium Code Reviews| 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/lazy_instance.h" | |
| 11 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 12 #include "content/public/common/url_constants.h" | 13 #include "content/public/common/url_constants.h" |
| 13 #include "content/public/renderer/render_frame.h" | 14 #include "content/public/renderer/render_frame.h" |
| 14 #include "content/public/renderer/render_thread.h" | 15 #include "content/public/renderer/render_thread.h" |
| 15 #include "extensions/common/extension.h" | 16 #include "extensions/common/extension.h" |
| 16 #include "extensions/common/extensions_client.h" | 17 #include "extensions/common/extensions_client.h" |
| 17 #include "extensions/common/permissions/permissions_data.h" | 18 #include "extensions/common/permissions/permissions_data.h" |
| 18 #include "extensions/renderer/extension_injection_host.h" | 19 #include "extensions/renderer/extension_injection_host.h" |
| 19 #include "extensions/renderer/extensions_renderer_client.h" | 20 #include "extensions/renderer/extensions_renderer_client.h" |
| 20 #include "extensions/renderer/injection_host.h" | 21 #include "extensions/renderer/injection_host.h" |
| 21 #include "extensions/renderer/renderer_extension_registry.h" | 22 #include "extensions/renderer/renderer_extension_registry.h" |
| 22 #include "extensions/renderer/script_context.h" | 23 #include "extensions/renderer/script_context.h" |
| 23 #include "extensions/renderer/script_injection.h" | 24 #include "extensions/renderer/script_injection.h" |
| 24 #include "extensions/renderer/user_script_injector.h" | 25 #include "extensions/renderer/user_script_injector.h" |
| 25 #include "extensions/renderer/web_ui_injection_host.h" | 26 #include "extensions/renderer/web_ui_injection_host.h" |
| 27 #include "grit/extensions_renderer_resources.h" | |
| 26 #include "third_party/WebKit/public/web/WebDocument.h" | 28 #include "third_party/WebKit/public/web/WebDocument.h" |
| 27 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 29 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 30 #include "ui/base/resource/resource_bundle.h" | |
| 28 #include "url/gurl.h" | 31 #include "url/gurl.h" |
| 29 | 32 |
| 30 namespace extensions { | 33 namespace extensions { |
| 31 | 34 |
| 32 namespace { | 35 namespace { |
| 33 | 36 |
| 37 // These two strings are injected before and after the Greasemonkey API and | |
| 38 // user script to wrap it in an anonymous scope. | |
| 39 const char kUserScriptHead[] = "(function (unsafeWindow) {\n"; | |
| 40 const char kUserScriptTail[] = "\n})(window);"; | |
| 41 | |
| 34 GURL GetDocumentUrlForFrame(blink::WebLocalFrame* frame) { | 42 GURL GetDocumentUrlForFrame(blink::WebLocalFrame* frame) { |
| 35 GURL data_source_url = ScriptContext::GetDataSourceURLForFrame(frame); | 43 GURL data_source_url = ScriptContext::GetDataSourceURLForFrame(frame); |
| 36 if (!data_source_url.is_empty() && frame->isViewSourceModeEnabled()) { | 44 if (!data_source_url.is_empty() && frame->isViewSourceModeEnabled()) { |
| 37 data_source_url = GURL(content::kViewSourceScheme + std::string(":") + | 45 data_source_url = GURL(content::kViewSourceScheme + std::string(":") + |
| 38 data_source_url.spec()); | 46 data_source_url.spec()); |
| 39 } | 47 } |
| 40 | 48 |
| 41 return data_source_url; | 49 return data_source_url; |
| 42 } | 50 } |
| 43 | 51 |
| 52 // Greasemonkey API source that is injected with the scripts. | |
| 53 struct GreasemonkeyApiJsString { | |
| 54 GreasemonkeyApiJsString(); | |
| 55 blink::WebScriptSource GetSource() const; | |
| 56 | |
| 57 private: | |
| 58 blink::WebString source_; | |
| 59 }; | |
| 60 | |
| 61 // The below constructor, monstrous as it is, just makes a WebScriptSource from | |
| 62 // the GreasemonkeyApiJs resource. | |
| 63 GreasemonkeyApiJsString::GreasemonkeyApiJsString() { | |
| 64 base::StringPiece source_piece = | |
| 65 ResourceBundle::GetSharedInstance().GetRawDataResource( | |
| 66 IDR_GREASEMONKEY_API_JS); | |
| 67 source_ = | |
| 68 blink::WebString::fromUTF8(source_piece.data(), source_piece.length()); | |
| 69 } | |
| 70 | |
| 71 blink::WebScriptSource GreasemonkeyApiJsString::GetSource() const { | |
| 72 return blink::WebScriptSource(source_); | |
| 73 } | |
| 74 | |
| 75 base::LazyInstance<GreasemonkeyApiJsString> g_greasemonkey_api = | |
| 76 LAZY_INSTANCE_INITIALIZER; | |
| 77 | |
| 44 } // namespace | 78 } // namespace |
| 45 | 79 |
| 46 UserScriptSet::UserScriptSet() {} | 80 UserScriptSet::UserScriptSet() {} |
| 47 | 81 |
| 48 UserScriptSet::~UserScriptSet() { | 82 UserScriptSet::~UserScriptSet() { |
| 49 } | 83 } |
| 50 | 84 |
| 51 void UserScriptSet::AddObserver(Observer* observer) { | 85 void UserScriptSet::AddObserver(Observer* observer) { |
| 52 observers_.AddObserver(observer); | 86 observers_.AddObserver(observer); |
| 53 } | 87 } |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 106 return false; | 140 return false; |
| 107 | 141 |
| 108 // Unpickle scripts. | 142 // Unpickle scripts. |
| 109 uint32_t num_scripts = 0; | 143 uint32_t num_scripts = 0; |
| 110 base::Pickle pickle(reinterpret_cast<char*>(shared_memory_->memory()), | 144 base::Pickle pickle(reinterpret_cast<char*>(shared_memory_->memory()), |
| 111 pickle_size); | 145 pickle_size); |
| 112 base::PickleIterator iter(pickle); | 146 base::PickleIterator iter(pickle); |
| 113 CHECK(iter.ReadUInt32(&num_scripts)); | 147 CHECK(iter.ReadUInt32(&num_scripts)); |
| 114 | 148 |
| 115 scripts_.clear(); | 149 scripts_.clear(); |
| 150 js_script_sources_.clear(); | |
| 151 css_script_sources_.clear(); | |
| 116 scripts_.reserve(num_scripts); | 152 scripts_.reserve(num_scripts); |
| 117 for (uint32_t i = 0; i < num_scripts; ++i) { | 153 for (uint32_t i = 0; i < num_scripts; ++i) { |
| 118 std::unique_ptr<UserScript> script(new UserScript()); | 154 std::unique_ptr<UserScript> script(new UserScript()); |
| 119 script->Unpickle(pickle, &iter); | 155 script->Unpickle(pickle, &iter); |
| 120 | 156 |
| 121 // Note that this is a pointer into shared memory. We don't own it. It gets | 157 // 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 | 158 // cleared up when the last renderer or browser process drops their |
| 123 // reference to the shared memory. | 159 // reference to the shared memory. |
| 124 for (size_t j = 0; j < script->js_scripts().size(); ++j) { | 160 for (size_t j = 0; j < script->js_scripts().size(); ++j) { |
| 125 const char* body = NULL; | 161 const char* body = NULL; |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 219 bool inject_js = | 255 bool inject_js = |
| 220 !script->js_scripts().empty() && script->run_location() == run_location; | 256 !script->js_scripts().empty() && script->run_location() == run_location; |
| 221 if (inject_css || inject_js) { | 257 if (inject_css || inject_js) { |
| 222 injection.reset(new ScriptInjection(std::move(injector), render_frame, | 258 injection.reset(new ScriptInjection(std::move(injector), render_frame, |
| 223 std::move(injection_host), run_location, | 259 std::move(injection_host), run_location, |
| 224 log_activity)); | 260 log_activity)); |
| 225 } | 261 } |
| 226 return injection; | 262 return injection; |
| 227 } | 263 } |
| 228 | 264 |
| 265 std::vector<blink::WebScriptSource> UserScriptSet::GetJsSources(int script_id) { | |
|
Devlin
2016/08/25 17:07:35
Semi-random placement: This is going to conflict w
lazyboy
2016/09/06 19:37:32
I've changed the storage to map GURL-> WebString.
| |
| 266 UserScriptList::const_iterator script_iter = | |
| 267 std::find_if(scripts_.begin(), scripts_.end(), | |
|
Devlin
2016/08/25 17:07:35
In the past, I've found that I've been too gung-ho
lazyboy
2016/09/06 19:37:31
Acknowledged, this is no longer relevant.
| |
| 268 [&script_id](const std::unique_ptr<UserScript>& script) { | |
| 269 return script->id() == script_id; | |
| 270 }); | |
| 271 if (script_iter == scripts_.end()) | |
| 272 return std::vector<blink::WebScriptSource>(); | |
| 273 | |
| 274 const UserScript& script = *script_iter->get(); | |
| 275 std::map<int, std::vector<blink::WebScriptSource>>::iterator iter = | |
| 276 js_script_sources_.find(script_id); | |
| 277 if (iter != js_script_sources_.end()) { | |
| 278 // Win, we already have the script contents in WebString-s. | |
| 279 return iter->second; | |
| 280 } | |
| 281 | |
| 282 iter = js_script_sources_.insert(iter, std::make_pair( | |
| 283 script_id, std::vector<blink::WebScriptSource>())); | |
| 284 std::vector<blink::WebScriptSource>& sources_vector = iter->second; | |
| 285 | |
| 286 for (const std::unique_ptr<UserScript::File>& js_file : | |
| 287 script_iter->get()->js_scripts()) { | |
| 288 base::StringPiece script_content = js_file->GetContent(); | |
| 289 blink::WebString source; | |
| 290 if (script.emulate_greasemonkey()) { | |
| 291 // We add this dumb function wrapper for user scripts to emulate what | |
| 292 // Greasemonkey does. |script_content| becomes: | |
| 293 // concat(kUserScriptHead, script_content, kUserScriptTail). | |
| 294 std::string content; | |
| 295 content.reserve(strlen(kUserScriptHead) + script_content.length() + | |
| 296 strlen(kUserScriptTail)); | |
| 297 content.append(kUserScriptHead); | |
| 298 script_content.AppendToString(&content); | |
| 299 content.append(kUserScriptTail); | |
| 300 source = blink::WebString::fromUTF8(content); | |
| 301 } else { | |
| 302 source = blink::WebString::fromUTF8(script_content.data(), | |
| 303 script_content.length()); | |
| 304 } | |
| 305 sources_vector.push_back(blink::WebScriptSource(source)); | |
| 306 } | |
| 307 | |
| 308 // Emulate Greasemonkey API for scripts that were converted to extension | |
| 309 // user scripts. | |
| 310 if (script.emulate_greasemonkey()) { | |
| 311 sources_vector.insert(sources_vector.begin(), | |
| 312 g_greasemonkey_api.Get().GetSource()); | |
| 313 } | |
| 314 | |
| 315 return sources_vector; | |
| 316 } | |
| 317 | |
| 318 std::vector<blink::WebString> UserScriptSet::GetCssSources(int script_id) { | |
| 319 const auto& script_iter = | |
| 320 std::find_if(scripts_.begin(), scripts_.end(), | |
| 321 [&script_id](const std::unique_ptr<UserScript>& script) { | |
| 322 return script->id() == script_id; | |
| 323 }); | |
| 324 if (script_iter == scripts_.end()) | |
| 325 return std::vector<blink::WebString>(); | |
| 326 const UserScript& script = *script_iter->get(); | |
| 327 | |
| 328 auto iter = css_script_sources_.find(script_id); | |
| 329 if (iter != css_script_sources_.end()) { | |
| 330 // Win, we already have the script contents in WebString-s. | |
| 331 return iter->second; | |
| 332 } | |
| 333 | |
| 334 iter = css_script_sources_.insert(iter, std::make_pair( | |
| 335 script_id, std::vector<blink::WebString>())); | |
| 336 | |
| 337 std::vector<blink::WebString>& sources_vector = iter->second; | |
| 338 for (const std::unique_ptr<UserScript::File>& css_file : | |
| 339 script.css_scripts()) { | |
| 340 base::StringPiece script_content = css_file->GetContent(); | |
| 341 sources_vector.push_back(blink::WebString::fromUTF8( | |
| 342 script_content.data(), script_content.length())); | |
| 343 } | |
| 344 | |
| 345 return sources_vector; | |
| 346 } | |
| 347 | |
| 229 } // namespace extensions | 348 } // namespace extensions |
| OLD | NEW |