| 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_injector.h" | 5 #include "extensions/renderer/user_script_injector.h" |
| 6 | 6 |
| 7 #include <tuple> | 7 #include <tuple> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 | 196 |
| 197 std::vector<blink::WebScriptSource> UserScriptInjector::GetJsSources( | 197 std::vector<blink::WebScriptSource> UserScriptInjector::GetJsSources( |
| 198 UserScript::RunLocation run_location) const { | 198 UserScript::RunLocation run_location) const { |
| 199 std::vector<blink::WebScriptSource> sources; | 199 std::vector<blink::WebScriptSource> sources; |
| 200 if (!script_) | 200 if (!script_) |
| 201 return sources; | 201 return sources; |
| 202 | 202 |
| 203 DCHECK_EQ(script_->run_location(), run_location); | 203 DCHECK_EQ(script_->run_location(), run_location); |
| 204 | 204 |
| 205 const UserScript::FileList& js_scripts = script_->js_scripts(); | 205 const UserScript::FileList& js_scripts = script_->js_scripts(); |
| 206 | 206 sources.reserve(js_scripts.size()); |
| 207 for (UserScript::FileList::const_iterator iter = js_scripts.begin(); | 207 for (UserScript::FileList::const_iterator iter = js_scripts.begin(); |
| 208 iter != js_scripts.end(); | 208 iter != js_scripts.end(); |
| 209 ++iter) { | 209 ++iter) { |
| 210 std::string content = iter->GetContent().as_string(); | 210 base::StringPiece script_content = iter->GetContent(); |
| 211 | 211 blink::WebString source; |
| 212 // We add this dumb function wrapper for user scripts to emulate what | |
| 213 // Greasemonkey does. | |
| 214 if (script_->emulate_greasemonkey()) { | 212 if (script_->emulate_greasemonkey()) { |
| 215 content.insert(0, kUserScriptHead); | 213 // We add this dumb function wrapper for user scripts to emulate what |
| 216 content += kUserScriptTail; | 214 // Greasemonkey does. |script_content| becomes: |
| 215 // concat(kUserScriptHead, script_content, kUserScriptTail). |
| 216 std::string content; |
| 217 content.reserve(strlen(kUserScriptHead) + script_content.length() + |
| 218 strlen(kUserScriptTail)); |
| 219 content.append(kUserScriptHead); |
| 220 script_content.AppendToString(&content); |
| 221 content.append(kUserScriptTail); |
| 222 // TODO(lazyboy): |content| is copied to |source|, should be avoided. |
| 223 // Investigate if we can leverage WebString's cheap copying mechanism |
| 224 // somehow. |
| 225 source = blink::WebString::fromUTF8(content); |
| 226 } else { |
| 227 source = blink::WebString::fromUTF8(script_content.data(), |
| 228 script_content.length()); |
| 217 } | 229 } |
| 218 sources.push_back(blink::WebScriptSource( | 230 sources.push_back(blink::WebScriptSource(source, iter->url())); |
| 219 blink::WebString::fromUTF8(content), iter->url())); | |
| 220 } | 231 } |
| 221 | 232 |
| 222 // Emulate Greasemonkey API for scripts that were converted to extension | 233 // Emulate Greasemonkey API for scripts that were converted to extension |
| 223 // user scripts. | 234 // user scripts. |
| 224 if (script_->emulate_greasemonkey()) | 235 if (script_->emulate_greasemonkey()) |
| 225 sources.insert(sources.begin(), g_greasemonkey_api.Get().GetSource()); | 236 sources.insert(sources.begin(), g_greasemonkey_api.Get().GetSource()); |
| 226 | 237 |
| 227 return sources; | 238 return sources; |
| 228 } | 239 } |
| 229 | 240 |
| 230 std::vector<std::string> UserScriptInjector::GetCssSources( | 241 std::vector<blink::WebString> UserScriptInjector::GetCssSources( |
| 231 UserScript::RunLocation run_location) const { | 242 UserScript::RunLocation run_location) const { |
| 232 DCHECK_EQ(UserScript::DOCUMENT_START, run_location); | 243 DCHECK_EQ(UserScript::DOCUMENT_START, run_location); |
| 233 | 244 |
| 234 std::vector<std::string> sources; | 245 std::vector<blink::WebString> sources; |
| 235 if (!script_) | 246 if (!script_) |
| 236 return sources; | 247 return sources; |
| 237 | 248 |
| 238 const UserScript::FileList& css_scripts = script_->css_scripts(); | 249 const UserScript::FileList& css_scripts = script_->css_scripts(); |
| 250 sources.reserve(css_scripts.size()); |
| 239 for (UserScript::FileList::const_iterator iter = css_scripts.begin(); | 251 for (UserScript::FileList::const_iterator iter = css_scripts.begin(); |
| 240 iter != css_scripts.end(); | 252 iter != css_scripts.end(); |
| 241 ++iter) { | 253 ++iter) { |
| 242 sources.push_back(iter->GetContent().as_string()); | 254 base::StringPiece css_content = iter->GetContent(); |
| 255 sources.push_back( |
| 256 blink::WebString::fromUTF8(css_content.data(), css_content.length())); |
| 243 } | 257 } |
| 244 return sources; | 258 return sources; |
| 245 } | 259 } |
| 246 | 260 |
| 247 void UserScriptInjector::GetRunInfo( | 261 void UserScriptInjector::GetRunInfo( |
| 248 ScriptsRunInfo* scripts_run_info, | 262 ScriptsRunInfo* scripts_run_info, |
| 249 UserScript::RunLocation run_location) const { | 263 UserScript::RunLocation run_location) const { |
| 250 if (!script_) | 264 if (!script_) |
| 251 return; | 265 return; |
| 252 | 266 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 268 void UserScriptInjector::OnInjectionComplete( | 282 void UserScriptInjector::OnInjectionComplete( |
| 269 std::unique_ptr<base::Value> execution_result, | 283 std::unique_ptr<base::Value> execution_result, |
| 270 UserScript::RunLocation run_location, | 284 UserScript::RunLocation run_location, |
| 271 content::RenderFrame* render_frame) {} | 285 content::RenderFrame* render_frame) {} |
| 272 | 286 |
| 273 void UserScriptInjector::OnWillNotInject(InjectFailureReason reason, | 287 void UserScriptInjector::OnWillNotInject(InjectFailureReason reason, |
| 274 content::RenderFrame* render_frame) { | 288 content::RenderFrame* render_frame) { |
| 275 } | 289 } |
| 276 | 290 |
| 277 } // namespace extensions | 291 } // namespace extensions |
| OLD | NEW |