OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/extensions/execute_code_in_tab_function.h" |
| 6 |
| 7 #include "base/thread.h" |
| 8 #include "base/file_util.h" |
| 9 #include "chrome/browser/browser.h" |
| 10 #include "chrome/browser/browser_process.h" |
| 11 #include "chrome/browser/extensions/extension_tabs_module.h" |
| 12 #include "chrome/browser/extensions/extension_tabs_module_constants.h" |
| 13 #include "chrome/browser/tab_contents/tab_contents.h" |
| 14 #include "chrome/common/extensions/extension.h" |
| 15 #include "chrome/common/extensions/extension_error_utils.h" |
| 16 |
| 17 namespace keys = extension_tabs_module_constants; |
| 18 |
| 19 const wchar_t* kCodeKey = L"code"; |
| 20 const wchar_t* kFileKey = L"file"; |
| 21 |
| 22 bool ExecuteCodeInTabFunction::RunImpl() { |
| 23 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_LIST)); |
| 24 const ListValue* args = static_cast<const ListValue*>(args_); |
| 25 |
| 26 DictionaryValue* script_info; |
| 27 EXTENSION_FUNCTION_VALIDATE(args->GetDictionary(1, &script_info)); |
| 28 size_t number_of_value = script_info->GetSize(); |
| 29 if (number_of_value == 0) { |
| 30 error_ = keys::kNoCodeOrFileToExecuteError; |
| 31 return false; |
| 32 } else if (number_of_value > 1) { |
| 33 error_ = keys::kMoreThanOneValuesError; |
| 34 return false; |
| 35 } |
| 36 |
| 37 execute_tab_id_ = -1; |
| 38 Browser* browser = NULL; |
| 39 TabContents* contents = NULL; |
| 40 |
| 41 // If |tab_id| is specified, look for it. Otherwise default to selected tab |
| 42 // in the current window. |
| 43 Value* tab_value = NULL; |
| 44 EXTENSION_FUNCTION_VALIDATE(args->Get(0, &tab_value)); |
| 45 if (tab_value->IsType(Value::TYPE_NULL)) { |
| 46 browser = dispatcher()->GetBrowser(); |
| 47 if (!browser) { |
| 48 error_ = keys::kNoCurrentWindowError; |
| 49 return false; |
| 50 } |
| 51 if (!ExtensionTabUtil::GetDefaultTab(browser, &contents, &execute_tab_id_)) |
| 52 return false; |
| 53 } else { |
| 54 EXTENSION_FUNCTION_VALIDATE(tab_value->GetAsInteger(&execute_tab_id_)); |
| 55 if (!ExtensionTabUtil::GetTabById(execute_tab_id_, profile(), &browser, |
| 56 NULL, &contents, NULL)) { |
| 57 return false; |
| 58 } |
| 59 } |
| 60 |
| 61 DCHECK(browser); |
| 62 DCHECK(contents); |
| 63 |
| 64 if (!GetExtension()->CanAccessHost(contents->GetURL())) { |
| 65 error_ = ExtensionErrorUtils::FormatErrorMessage( |
| 66 keys::kCannotAccessPageError, contents->GetURL().spec()); |
| 67 return false; |
| 68 } |
| 69 |
| 70 if (script_info->HasKey(kCodeKey)) { |
| 71 if (!script_info->GetString(kCodeKey, &code_string_)) |
| 72 return false; |
| 73 } |
| 74 |
| 75 if (!code_string_.empty()) { |
| 76 Execute(); |
| 77 return true; |
| 78 } |
| 79 |
| 80 std::string relative_path; |
| 81 if (script_info->HasKey(kFileKey)) { |
| 82 if (!script_info->GetString(kFileKey, &relative_path)) |
| 83 return false; |
| 84 file_path_ = GetExtension()->GetResourcePath(relative_path); |
| 85 } |
| 86 if (file_path_.empty()) { |
| 87 error_ = keys::kNoCodeOrFileToExecuteError; |
| 88 return false; |
| 89 } |
| 90 |
| 91 ui_loop_ = MessageLoop::current(); |
| 92 MessageLoop* work_loop = g_browser_process->file_thread()->message_loop(); |
| 93 work_loop->PostTask(FROM_HERE, |
| 94 NewRunnableMethod(this, &ExecuteCodeInTabFunction::LoadFile)); |
| 95 |
| 96 return true; |
| 97 } |
| 98 |
| 99 void ExecuteCodeInTabFunction::LoadFile() { |
| 100 DCHECK(ui_loop_); |
| 101 std::string content; |
| 102 if (!file_util::ReadFileToString(file_path_, &content)) { |
| 103 |
| 104 #if defined(OS_POSIX) |
| 105 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kLoadFileError, |
| 106 file_path_.value()); |
| 107 #elif defined(OS_WIN) |
| 108 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kLoadFileError, |
| 109 WideToUTF8(file_path_.value())); |
| 110 #endif // OS_WIN |
| 111 |
| 112 ui_loop_->PostTask(FROM_HERE, |
| 113 NewRunnableMethod(this, &ExecuteCodeInTabFunction::SendResponse, |
| 114 false)); |
| 115 } else { |
| 116 code_string_ = content; |
| 117 ui_loop_->PostTask(FROM_HERE, |
| 118 NewRunnableMethod(this, &ExecuteCodeInTabFunction::Execute)); |
| 119 } |
| 120 |
| 121 return; |
| 122 } |
| 123 |
| 124 void ExecuteCodeInTabFunction::Execute() { |
| 125 TabContents* contents = NULL; |
| 126 Browser* browser = NULL; |
| 127 if (!ExtensionTabUtil::GetTabById(execute_tab_id_, profile(), &browser, NULL, |
| 128 &contents, NULL) && contents && browser) { |
| 129 SendResponse(false); |
| 130 return; |
| 131 } |
| 132 |
| 133 bool is_js_code = true; |
| 134 std::string function_name = name(); |
| 135 if (function_name == keys::kInsertCSSFunction) { |
| 136 is_js_code = false; |
| 137 } else if (function_name != keys::kExecuteScriptFunction) { |
| 138 DCHECK(false); |
| 139 } |
| 140 registrar_.Add(this, NotificationType::TAB_CODE_EXECUTED, |
| 141 NotificationService::AllSources()); |
| 142 AddRef(); // balanced in Observe() |
| 143 contents->ExecuteCode(request_id(), extension_id(), is_js_code, |
| 144 code_string_); |
| 145 } |
| 146 |
| 147 void ExecuteCodeInTabFunction::Observe(NotificationType type, |
| 148 const NotificationSource& source, |
| 149 const NotificationDetails& details) { |
| 150 std::pair<int, bool>* result_details = |
| 151 Details<std::pair<int, bool> >(details).ptr(); |
| 152 if (result_details->first == request_id()) { |
| 153 SendResponse(result_details->second); |
| 154 Release(); // balanced in Execute() |
| 155 } |
| 156 } |
OLD | NEW |