| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "chrome/renderer/user_script_slave.h" | 5 #include "chrome/renderer/user_script_slave.h" |
| 6 | 6 |
| 7 #include "base/histogram.h" | 7 #include "base/histogram.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/perftimer.h" | 9 #include "base/perftimer.h" |
| 10 #include "base/pickle.h" | 10 #include "base/pickle.h" |
| 11 #include "base/shared_memory.h" | 11 #include "base/shared_memory.h" |
| 12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
| 13 #include "chrome/common/resource_bundle.h" | 13 #include "chrome/common/resource_bundle.h" |
| 14 #include "googleurl/src/gurl.h" | 14 #include "googleurl/src/gurl.h" |
| 15 #include "third_party/WebKit/WebKit/chromium/public/WebScriptSource.h" | 15 #include "third_party/WebKit/WebKit/chromium/public/WebScriptSource.h" |
| 16 #include "webkit/glue/webframe.h" | 16 #include "webkit/glue/webframe.h" |
| 17 | 17 |
| 18 #include "grit/renderer_resources.h" | 18 #include "grit/renderer_resources.h" |
| 19 | 19 |
| 20 using WebKit::WebScriptSource; | 20 using WebKit::WebScriptSource; |
| 21 using WebKit::WebString; | 21 using WebKit::WebString; |
| 22 | 22 |
| 23 // These two strings are injected before and after the Greasemonkey API and | 23 // These two strings are injected before and after the Greasemonkey API and |
| 24 // user script to wrap it in an anonymous scope. | 24 // user script to wrap it in an anonymous scope. |
| 25 static const char kUserScriptHead[] = "(function (unsafeWindow) {\n"; | 25 static const char kUserScriptHead[] = "(function (unsafeWindow) {\n"; |
| 26 static const char kUserScriptTail[] = "\n})(window);"; | 26 static const char kUserScriptTail[] = "\n})(window);"; |
| 27 | 27 |
| 28 static const char kInitSelf[] = "chromium.self = new chromium.Extension('%s')"; |
| 29 |
| 28 UserScriptSlave::UserScriptSlave() | 30 UserScriptSlave::UserScriptSlave() |
| 29 : shared_memory_(NULL), | 31 : shared_memory_(NULL), |
| 30 script_deleter_(&scripts_), | 32 script_deleter_(&scripts_), |
| 31 user_script_start_line_(0) { | 33 user_script_start_line_(0) { |
| 32 // TODO: Only windows supports resources and only windows supports user | 34 // TODO: Only windows supports resources and only windows supports user |
| 33 // scrips, so only load the Greasemonkey API on windows. Fix this when | 35 // scrips, so only load the Greasemonkey API on windows. Fix this when |
| 34 // better cross platofrm support is available. | 36 // better cross platofrm support is available. |
| 35 #if defined(OS_WIN) | 37 #if defined(OS_WIN) |
| 36 api_js_ = ResourceBundle::GetSharedInstance().GetRawDataResource( | 38 api_js_ = ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 37 IDR_GREASEMONKEY_API_JS); | 39 IDR_GREASEMONKEY_API_JS); |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 } | 105 } |
| 104 | 106 |
| 105 return true; | 107 return true; |
| 106 } | 108 } |
| 107 | 109 |
| 108 bool UserScriptSlave::InjectScripts(WebFrame* frame, | 110 bool UserScriptSlave::InjectScripts(WebFrame* frame, |
| 109 UserScript::RunLocation location) { | 111 UserScript::RunLocation location) { |
| 110 PerfTimer timer; | 112 PerfTimer timer; |
| 111 int num_matched = 0; | 113 int num_matched = 0; |
| 112 | 114 |
| 113 std::vector<WebScriptSource> sources; | |
| 114 for (size_t i = 0; i < scripts_.size(); ++i) { | 115 for (size_t i = 0; i < scripts_.size(); ++i) { |
| 116 std::vector<WebScriptSource> sources; |
| 115 UserScript* script = scripts_[i]; | 117 UserScript* script = scripts_[i]; |
| 116 if (!script->MatchesUrl(frame->GetURL())) | 118 if (!script->MatchesUrl(frame->GetURL())) |
| 117 continue; // This frame doesn't match the script url pattern, skip it. | 119 continue; // This frame doesn't match the script url pattern, skip it. |
| 118 | 120 |
| 119 ++num_matched; | 121 ++num_matched; |
| 120 // CSS files are always injected on document start before js scripts. | 122 // CSS files are always injected on document start before js scripts. |
| 121 if (location == UserScript::DOCUMENT_START) { | 123 if (location == UserScript::DOCUMENT_START) { |
| 122 for (size_t j = 0; j < script->css_scripts().size(); ++j) { | 124 for (size_t j = 0; j < script->css_scripts().size(); ++j) { |
| 123 UserScript::File& file = script->css_scripts()[j]; | 125 UserScript::File& file = script->css_scripts()[j]; |
| 124 frame->InsertCSSStyles(file.GetContent().as_string()); | 126 frame->InsertCSSStyles(file.GetContent().as_string()); |
| 125 } | 127 } |
| 126 } | 128 } |
| 127 if (script->run_location() == location) { | 129 if (script->run_location() == location) { |
| 128 for (size_t j = 0; j < script->js_scripts().size(); ++j) { | 130 for (size_t j = 0; j < script->js_scripts().size(); ++j) { |
| 129 UserScript::File &file = script->js_scripts()[j]; | 131 UserScript::File &file = script->js_scripts()[j]; |
| 132 std::string content = file.GetContent().as_string(); |
| 133 |
| 134 // We add this dumb function wrapper for standalone user script to |
| 135 // emulate what Greasemonkey does. |
| 136 if (script->is_standalone()) { |
| 137 content.insert(0, kUserScriptHead); |
| 138 content += kUserScriptTail; |
| 139 } |
| 130 sources.push_back(WebScriptSource( | 140 sources.push_back(WebScriptSource( |
| 131 WebString::fromUTF8(file.GetContent()), file.url())); | 141 WebString::fromUTF8(content.c_str(), content.length()), |
| 142 file.url())); |
| 132 } | 143 } |
| 133 } | 144 } |
| 145 |
| 146 if (!sources.empty()) { |
| 147 if (script->is_standalone()) { |
| 148 // For standalone scripts, we try to emulate the Greasemonkey API. |
| 149 sources.insert(sources.begin(), |
| 150 WebScriptSource(WebString::fromUTF8(api_js_.as_string()))); |
| 151 } else { |
| 152 // Setup chromium.self to contain an Extension object with the correct |
| 153 // ID. |
| 154 sources.insert(sources.begin(), |
| 155 WebScriptSource(WebString::fromUTF8( |
| 156 StringPrintf(kInitSelf, script->extension_id().c_str())))); |
| 157 } |
| 158 |
| 159 frame->ExecuteScriptInNewContext(&sources.front(), sources.size()); |
| 160 } |
| 134 } | 161 } |
| 135 | 162 |
| 136 if (!sources.empty()) { | |
| 137 sources.insert( | |
| 138 sources.begin(), WebScriptSource(WebString::fromUTF8(api_js_))); | |
| 139 frame->ExecuteScriptInNewContext(&sources.front(), sources.size()); | |
| 140 } | |
| 141 | |
| 142 // Log debug info. | 163 // Log debug info. |
| 143 if (location == UserScript::DOCUMENT_START) { | 164 if (location == UserScript::DOCUMENT_START) { |
| 144 HISTOGRAM_COUNTS_100("UserScripts:DocStart:Count", num_matched); | 165 HISTOGRAM_COUNTS_100("UserScripts:DocStart:Count", num_matched); |
| 145 HISTOGRAM_TIMES("UserScripts:DocStart:Time", timer.Elapsed()); | 166 HISTOGRAM_TIMES("UserScripts:DocStart:Time", timer.Elapsed()); |
| 146 } else { | 167 } else { |
| 147 HISTOGRAM_COUNTS_100("UserScripts:DocEnd:Count", num_matched); | 168 HISTOGRAM_COUNTS_100("UserScripts:DocEnd:Count", num_matched); |
| 148 HISTOGRAM_TIMES("UserScripts:DocEnd:Time", timer.Elapsed()); | 169 HISTOGRAM_TIMES("UserScripts:DocEnd:Time", timer.Elapsed()); |
| 149 } | 170 } |
| 150 | 171 |
| 151 LOG(INFO) << "Injected " << num_matched << " user scripts into " << | 172 LOG(INFO) << "Injected " << num_matched << " user scripts into " << |
| 152 frame->GetURL().spec(); | 173 frame->GetURL().spec(); |
| 153 return true; | 174 return true; |
| 154 } | 175 } |
| OLD | NEW |