| 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/browser/extensions/execute_code_in_tab_function.h" | 5 #include "chrome/browser/extensions/execute_code_in_tab_function.h" |
| 6 | 6 |
| 7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
| 8 #include "chrome/browser/browser.h" | 8 #include "chrome/browser/browser.h" |
| 9 #include "chrome/browser/extensions/extension_tabs_module.h" | 9 #include "chrome/browser/extensions/extension_tabs_module.h" |
| 10 #include "chrome/browser/extensions/extension_tabs_module_constants.h" | 10 #include "chrome/browser/extensions/extension_tabs_module_constants.h" |
| 11 #include "chrome/browser/extensions/file_reader.h" | 11 #include "chrome/browser/extensions/file_reader.h" |
| 12 #include "chrome/browser/tab_contents/tab_contents.h" | 12 #include "chrome/browser/tab_contents/tab_contents.h" |
| 13 #include "chrome/common/extensions/extension.h" | 13 #include "chrome/common/extensions/extension.h" |
| 14 #include "chrome/common/extensions/extension_error_utils.h" | 14 #include "chrome/common/extensions/extension_error_utils.h" |
| 15 | 15 |
| 16 namespace keys = extension_tabs_module_constants; | 16 namespace keys = extension_tabs_module_constants; |
| 17 | 17 |
| 18 const wchar_t* kCodeKey = L"code"; | 18 const wchar_t* kCodeKey = L"code"; |
| 19 const wchar_t* kFileKey = L"file"; | 19 const wchar_t* kFileKey = L"file"; |
| 20 const wchar_t* kAllFramesKey = L"allFrames"; |
| 20 | 21 |
| 21 bool ExecuteCodeInTabFunction::RunImpl() { | 22 bool ExecuteCodeInTabFunction::RunImpl() { |
| 22 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_LIST)); | 23 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_LIST)); |
| 23 const ListValue* args = args_as_list(); | 24 const ListValue* args = args_as_list(); |
| 24 | 25 |
| 25 DictionaryValue* script_info; | 26 DictionaryValue* script_info; |
| 26 EXTENSION_FUNCTION_VALIDATE(args->GetDictionary(1, &script_info)); | 27 EXTENSION_FUNCTION_VALIDATE(args->GetDictionary(1, &script_info)); |
| 27 size_t number_of_value = script_info->GetSize(); | 28 size_t number_of_value = script_info->GetSize(); |
| 28 if (number_of_value == 0) { | 29 if (number_of_value == 0) { |
| 29 error_ = keys::kNoCodeOrFileToExecuteError; | 30 error_ = keys::kNoCodeOrFileToExecuteError; |
| 30 return false; | 31 return false; |
| 31 } else if (number_of_value > 1) { | 32 } else { |
| 32 error_ = keys::kMoreThanOneValuesError; | 33 bool has_code = script_info->HasKey(kCodeKey); |
| 33 return false; | 34 bool has_file = script_info->HasKey(kFileKey); |
| 35 if (has_code && has_file) { |
| 36 error_ = keys::kMoreThanOneValuesError; |
| 37 return false; |
| 38 } else if (!has_code && !has_file) { |
| 39 error_ = keys::kNoCodeOrFileToExecuteError; |
| 40 return false; |
| 41 } |
| 34 } | 42 } |
| 35 | 43 |
| 36 execute_tab_id_ = -1; | 44 execute_tab_id_ = -1; |
| 37 Browser* browser = NULL; | 45 Browser* browser = NULL; |
| 38 TabContents* contents = NULL; | 46 TabContents* contents = NULL; |
| 39 | 47 |
| 40 // If |tab_id| is specified, look for it. Otherwise default to selected tab | 48 // If |tab_id| is specified, look for it. Otherwise default to selected tab |
| 41 // in the current window. | 49 // in the current window. |
| 42 Value* tab_value = NULL; | 50 Value* tab_value = NULL; |
| 43 EXTENSION_FUNCTION_VALIDATE(args->Get(0, &tab_value)); | 51 EXTENSION_FUNCTION_VALIDATE(args->Get(0, &tab_value)); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 59 | 67 |
| 60 DCHECK(browser); | 68 DCHECK(browser); |
| 61 DCHECK(contents); | 69 DCHECK(contents); |
| 62 | 70 |
| 63 if (!GetExtension()->CanAccessHost(contents->GetURL())) { | 71 if (!GetExtension()->CanAccessHost(contents->GetURL())) { |
| 64 error_ = ExtensionErrorUtils::FormatErrorMessage( | 72 error_ = ExtensionErrorUtils::FormatErrorMessage( |
| 65 keys::kCannotAccessPageError, contents->GetURL().spec()); | 73 keys::kCannotAccessPageError, contents->GetURL().spec()); |
| 66 return false; | 74 return false; |
| 67 } | 75 } |
| 68 | 76 |
| 77 if (script_info->HasKey(kAllFramesKey)) { |
| 78 if (!script_info->GetBoolean(kAllFramesKey, &all_frames_)) |
| 79 return false; |
| 80 } |
| 81 |
| 69 std::string code_string; | 82 std::string code_string; |
| 70 if (script_info->HasKey(kCodeKey)) { | 83 if (script_info->HasKey(kCodeKey)) { |
| 71 if (!script_info->GetString(kCodeKey, &code_string)) | 84 if (!script_info->GetString(kCodeKey, &code_string)) |
| 72 return false; | 85 return false; |
| 73 } | 86 } |
| 74 | 87 |
| 75 if (!code_string.empty()) { | 88 if (!code_string.empty()) { |
| 76 Execute(code_string); | 89 Execute(code_string); |
| 77 return true; | 90 return true; |
| 78 } | 91 } |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 std::string function_name = name(); | 139 std::string function_name = name(); |
| 127 if (function_name == TabsInsertCSSFunction::function_name()) { | 140 if (function_name == TabsInsertCSSFunction::function_name()) { |
| 128 is_js_code = false; | 141 is_js_code = false; |
| 129 } else if (function_name != TabsExecuteScriptFunction::function_name()) { | 142 } else if (function_name != TabsExecuteScriptFunction::function_name()) { |
| 130 DCHECK(false); | 143 DCHECK(false); |
| 131 } | 144 } |
| 132 registrar_.Add(this, NotificationType::TAB_CODE_EXECUTED, | 145 registrar_.Add(this, NotificationType::TAB_CODE_EXECUTED, |
| 133 NotificationService::AllSources()); | 146 NotificationService::AllSources()); |
| 134 AddRef(); // balanced in Observe() | 147 AddRef(); // balanced in Observe() |
| 135 contents->ExecuteCode(request_id(), extension_id(), is_js_code, | 148 contents->ExecuteCode(request_id(), extension_id(), is_js_code, |
| 136 code_string); | 149 code_string, all_frames_); |
| 137 } | 150 } |
| 138 | 151 |
| 139 void ExecuteCodeInTabFunction::Observe(NotificationType type, | 152 void ExecuteCodeInTabFunction::Observe(NotificationType type, |
| 140 const NotificationSource& source, | 153 const NotificationSource& source, |
| 141 const NotificationDetails& details) { | 154 const NotificationDetails& details) { |
| 142 std::pair<int, bool>* result_details = | 155 std::pair<int, bool>* result_details = |
| 143 Details<std::pair<int, bool> >(details).ptr(); | 156 Details<std::pair<int, bool> >(details).ptr(); |
| 144 if (result_details->first == request_id()) { | 157 if (result_details->first == request_id()) { |
| 145 SendResponse(result_details->second); | 158 SendResponse(result_details->second); |
| 146 Release(); // balanced in Execute() | 159 Release(); // balanced in Execute() |
| 147 } | 160 } |
| 148 } | 161 } |
| OLD | NEW |