| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/browser/extensions/execute_code_in_tab_function.h" | 5 #include "chrome/browser/extensions/execute_code_in_tab_function.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 10 #include "chrome/browser/extensions/extension_service.h" | 10 #include "chrome/browser/extensions/extension_service.h" |
| 11 #include "chrome/browser/extensions/extension_tab_util.h" | 11 #include "chrome/browser/extensions/extension_tab_util.h" |
| 12 #include "chrome/browser/extensions/extension_tabs_module.h" | 12 #include "chrome/browser/extensions/extension_tabs_module.h" |
| 13 #include "chrome/browser/extensions/extension_tabs_module_constants.h" | 13 #include "chrome/browser/extensions/extension_tabs_module_constants.h" |
| 14 #include "chrome/browser/extensions/file_reader.h" | 14 #include "chrome/browser/extensions/file_reader.h" |
| 15 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/browser/ui/browser.h" | 16 #include "chrome/browser/ui/browser.h" |
| 17 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 17 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 18 #include "chrome/common/extensions/extension.h" | 18 #include "chrome/common/extensions/extension.h" |
| 19 #include "chrome/common/extensions/extension_constants.h" | 19 #include "chrome/common/extensions/extension_constants.h" |
| 20 #include "chrome/common/extensions/extension_error_utils.h" | 20 #include "chrome/common/extensions/extension_error_utils.h" |
| 21 #include "chrome/common/extensions/extension_file_util.h" | 21 #include "chrome/common/extensions/extension_file_util.h" |
| 22 #include "chrome/common/extensions/extension_l10n_util.h" | 22 #include "chrome/common/extensions/extension_l10n_util.h" |
| 23 #include "chrome/common/extensions/extension_manifest_constants.h" |
| 23 #include "chrome/common/extensions/extension_message_bundle.h" | 24 #include "chrome/common/extensions/extension_message_bundle.h" |
| 24 #include "chrome/common/extensions/extension_messages.h" | 25 #include "chrome/common/extensions/extension_messages.h" |
| 25 #include "content/public/browser/render_view_host.h" | 26 #include "content/public/browser/render_view_host.h" |
| 26 #include "content/public/browser/web_contents.h" | 27 #include "content/public/browser/web_contents.h" |
| 27 | 28 |
| 28 using content::BrowserThread; | 29 using content::BrowserThread; |
| 29 | 30 |
| 30 namespace keys = extension_tabs_module_constants; | 31 namespace keys = extension_tabs_module_constants; |
| 31 | 32 |
| 32 ExecuteCodeInTabFunction::ExecuteCodeInTabFunction() | 33 ExecuteCodeInTabFunction::ExecuteCodeInTabFunction() |
| 33 : execute_tab_id_(-1), | 34 : execute_tab_id_(-1), |
| 34 all_frames_(false) { | 35 all_frames_(false), |
| 36 run_at_(UserScript::DOCUMENT_IDLE) { |
| 35 } | 37 } |
| 36 | 38 |
| 37 ExecuteCodeInTabFunction::~ExecuteCodeInTabFunction() { | 39 ExecuteCodeInTabFunction::~ExecuteCodeInTabFunction() { |
| 38 } | 40 } |
| 39 | 41 |
| 40 bool ExecuteCodeInTabFunction::RunImpl() { | 42 bool ExecuteCodeInTabFunction::RunImpl() { |
| 41 DictionaryValue* script_info; | 43 DictionaryValue* script_info; |
| 42 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &script_info)); | 44 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &script_info)); |
| 43 size_t number_of_value = script_info->size(); | 45 size_t number_of_value = script_info->size(); |
| 44 if (number_of_value == 0) { | 46 if (number_of_value == 0) { |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 if (!GetExtension()->CanExecuteScriptOnPage( | 90 if (!GetExtension()->CanExecuteScriptOnPage( |
| 89 contents->web_contents()->GetURL(), NULL, &error_)) { | 91 contents->web_contents()->GetURL(), NULL, &error_)) { |
| 90 return false; | 92 return false; |
| 91 } | 93 } |
| 92 | 94 |
| 93 if (script_info->HasKey(keys::kAllFramesKey)) { | 95 if (script_info->HasKey(keys::kAllFramesKey)) { |
| 94 if (!script_info->GetBoolean(keys::kAllFramesKey, &all_frames_)) | 96 if (!script_info->GetBoolean(keys::kAllFramesKey, &all_frames_)) |
| 95 return false; | 97 return false; |
| 96 } | 98 } |
| 97 | 99 |
| 100 if (script_info->HasKey(keys::kRunAtKey)) { |
| 101 std::string run_string; |
| 102 EXTENSION_FUNCTION_VALIDATE(script_info->GetString( |
| 103 keys::kRunAtKey, &run_string)); |
| 104 |
| 105 if (run_string == extension_manifest_values::kRunAtDocumentStart) |
| 106 run_at_ = UserScript::DOCUMENT_START; |
| 107 else if (run_string == extension_manifest_values::kRunAtDocumentEnd) |
| 108 run_at_ = UserScript::DOCUMENT_END; |
| 109 else if (run_string == extension_manifest_values::kRunAtDocumentIdle) |
| 110 run_at_ = UserScript::DOCUMENT_IDLE; |
| 111 else |
| 112 EXTENSION_FUNCTION_VALIDATE(false); |
| 113 } |
| 114 |
| 98 std::string code_string; | 115 std::string code_string; |
| 99 if (script_info->HasKey(keys::kCodeKey)) { | 116 if (script_info->HasKey(keys::kCodeKey)) { |
| 100 if (!script_info->GetString(keys::kCodeKey, &code_string)) | 117 if (!script_info->GetString(keys::kCodeKey, &code_string)) |
| 101 return false; | 118 return false; |
| 102 } | 119 } |
| 103 | 120 |
| 104 if (!code_string.empty()) { | 121 if (!code_string.empty()) { |
| 105 if (!Execute(code_string)) | 122 if (!Execute(code_string)) |
| 106 return false; | 123 return false; |
| 107 return true; | 124 return true; |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 } else if (function_name != TabsExecuteScriptFunction::function_name()) { | 232 } else if (function_name != TabsExecuteScriptFunction::function_name()) { |
| 216 DCHECK(false); | 233 DCHECK(false); |
| 217 } | 234 } |
| 218 | 235 |
| 219 ExtensionMsg_ExecuteCode_Params params; | 236 ExtensionMsg_ExecuteCode_Params params; |
| 220 params.request_id = request_id(); | 237 params.request_id = request_id(); |
| 221 params.extension_id = extension->id(); | 238 params.extension_id = extension->id(); |
| 222 params.is_javascript = is_js_code; | 239 params.is_javascript = is_js_code; |
| 223 params.code = code_string; | 240 params.code = code_string; |
| 224 params.all_frames = all_frames_; | 241 params.all_frames = all_frames_; |
| 242 params.run_at = run_at_; |
| 225 params.in_main_world = false; | 243 params.in_main_world = false; |
| 226 contents->web_contents()->GetRenderViewHost()->Send( | 244 contents->web_contents()->GetRenderViewHost()->Send( |
| 227 new ExtensionMsg_ExecuteCode( | 245 new ExtensionMsg_ExecuteCode( |
| 228 contents->web_contents()->GetRenderViewHost()->GetRoutingID(), | 246 contents->web_contents()->GetRenderViewHost()->GetRoutingID(), |
| 229 params)); | 247 params)); |
| 230 | 248 |
| 231 Observe(contents->web_contents()); | 249 Observe(contents->web_contents()); |
| 232 AddRef(); // balanced in OnExecuteCodeFinished() | 250 AddRef(); // balanced in OnExecuteCodeFinished() |
| 233 return true; | 251 return true; |
| 234 } | 252 } |
| (...skipping 25 matching lines...) Expand all Loading... |
| 260 if (!error.empty()) { | 278 if (!error.empty()) { |
| 261 CHECK(!success); | 279 CHECK(!success); |
| 262 error_ = error; | 280 error_ = error; |
| 263 } | 281 } |
| 264 | 282 |
| 265 SendResponse(success); | 283 SendResponse(success); |
| 266 | 284 |
| 267 Observe(NULL); | 285 Observe(NULL); |
| 268 Release(); // balanced in Execute() | 286 Release(); // balanced in Execute() |
| 269 } | 287 } |
| OLD | NEW |