Index: extensions/renderer/user_script_injector.cc |
diff --git a/extensions/renderer/user_script_injector.cc b/extensions/renderer/user_script_injector.cc |
index da0c760e54425c7c329438150ed892501b18c6d5..6d3c857227a143e6a040d70617da47c3bd5507d6 100644 |
--- a/extensions/renderer/user_script_injector.cc |
+++ b/extensions/renderer/user_script_injector.cc |
@@ -18,11 +18,9 @@ |
#include "extensions/renderer/injection_host.h" |
#include "extensions/renderer/script_context.h" |
#include "extensions/renderer/scripts_run_info.h" |
-#include "grit/extensions_renderer_resources.h" |
#include "third_party/WebKit/public/web/WebDocument.h" |
#include "third_party/WebKit/public/web/WebLocalFrame.h" |
#include "third_party/WebKit/public/web/WebScriptSource.h" |
-#include "ui/base/resource/resource_bundle.h" |
#include "url/gurl.h" |
namespace extensions { |
@@ -44,11 +42,6 @@ struct RoutingInfoKey { |
using RoutingInfoMap = std::map<RoutingInfoKey, bool>; |
-// These two strings are injected before and after the Greasemonkey API and |
-// user script to wrap it in an anonymous scope. |
-const char kUserScriptHead[] = "(function (unsafeWindow) {\n"; |
-const char kUserScriptTail[] = "\n})(window);"; |
- |
// A map records whether a given |script_id| from a webview-added user script |
// is allowed to inject on the render of given |routing_id|. |
// Once a script is added, the decision of whether or not allowed to inject |
@@ -59,38 +52,13 @@ const char kUserScriptTail[] = "\n})(window);"; |
base::LazyInstance<RoutingInfoMap> g_routing_info_map = |
LAZY_INSTANCE_INITIALIZER; |
-// Greasemonkey API source that is injected with the scripts. |
-struct GreasemonkeyApiJsString { |
- GreasemonkeyApiJsString(); |
- blink::WebScriptSource GetSource() const; |
- |
- private: |
- blink::WebString source_; |
-}; |
- |
-// The below constructor, monstrous as it is, just makes a WebScriptSource from |
-// the GreasemonkeyApiJs resource. |
-GreasemonkeyApiJsString::GreasemonkeyApiJsString() { |
- base::StringPiece source_piece = |
- ResourceBundle::GetSharedInstance().GetRawDataResource( |
- IDR_GREASEMONKEY_API_JS); |
- source_ = |
- blink::WebString::fromUTF8(source_piece.data(), source_piece.length()); |
-} |
- |
-blink::WebScriptSource GreasemonkeyApiJsString::GetSource() const { |
- return blink::WebScriptSource(source_); |
-} |
- |
-base::LazyInstance<GreasemonkeyApiJsString> g_greasemonkey_api = |
- LAZY_INSTANCE_INITIALIZER; |
- |
} // namespace |
UserScriptInjector::UserScriptInjector(const UserScript* script, |
UserScriptSet* script_list, |
bool is_declarative) |
: script_(script), |
+ user_script_set_(script_list), |
script_id_(script_->id()), |
host_id_(script_->host_id()), |
is_declarative_(is_declarative), |
@@ -203,60 +171,17 @@ std::vector<blink::WebScriptSource> UserScriptInjector::GetJsSources( |
return sources; |
DCHECK_EQ(script_->run_location(), run_location); |
- |
- const UserScript::FileList& js_scripts = script_->js_scripts(); |
- sources.reserve(js_scripts.size()); |
- for (const std::unique_ptr<UserScript::File>& file : js_scripts) { |
- base::StringPiece script_content = file->GetContent(); |
- blink::WebString source; |
- if (script_->emulate_greasemonkey()) { |
- // We add this dumb function wrapper for user scripts to emulate what |
- // Greasemonkey does. |script_content| becomes: |
- // concat(kUserScriptHead, script_content, kUserScriptTail). |
- std::string content; |
- content.reserve(strlen(kUserScriptHead) + script_content.length() + |
- strlen(kUserScriptTail)); |
- content.append(kUserScriptHead); |
- script_content.AppendToString(&content); |
- content.append(kUserScriptTail); |
- // TODO(lazyboy): |content| is copied to |source|, should be avoided. |
- // Investigate if we can leverage WebString's cheap copying mechanism |
- // somehow. |
- source = blink::WebString::fromUTF8(content); |
- } else { |
- source = blink::WebString::fromUTF8(script_content.data(), |
- script_content.length()); |
- } |
- sources.push_back(blink::WebScriptSource(source, file->url())); |
- } |
- |
- // Emulate Greasemonkey API for scripts that were converted to extension |
- // user scripts. |
- if (script_->emulate_greasemonkey()) |
- sources.insert(sources.begin(), g_greasemonkey_api.Get().GetSource()); |
- |
- return sources; |
+ return user_script_set_->GetJsSources(script_->id()); |
} |
std::vector<blink::WebString> UserScriptInjector::GetCssSources( |
UserScript::RunLocation run_location) const { |
DCHECK_EQ(UserScript::DOCUMENT_START, run_location); |
- std::vector<blink::WebString> sources; |
if (!script_) |
- return sources; |
+ return std::vector<blink::WebString>(); |
- const UserScript::FileList& css_scripts = script_->css_scripts(); |
- sources.reserve(css_scripts.size()); |
- for (const std::unique_ptr<UserScript::File>& file : script_->css_scripts()) { |
- // TODO(lazyboy): |css_content| string is copied into blink::WebString for |
- // every frame in the current renderer process. Avoid the copy, possibly by |
- // only performing the copy once. |
- base::StringPiece css_content = file->GetContent(); |
- sources.push_back( |
- blink::WebString::fromUTF8(css_content.data(), css_content.length())); |
- } |
- return sources; |
+ return user_script_set_->GetCssSources(script_->id()); |
} |
void UserScriptInjector::GetRunInfo( |