| 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 "chrome/browser/browser.h" | 7 #include "chrome/browser/browser.h" |
| 8 #include "chrome/browser/extensions/extension_tabs_module.h" | 8 #include "chrome/browser/extensions/extension_tabs_module.h" |
| 9 #include "chrome/browser/extensions/extension_tabs_module_constants.h" | 9 #include "chrome/browser/extensions/extension_tabs_module_constants.h" |
| 10 #include "chrome/browser/net/file_reader.h" | 10 #include "chrome/browser/extensions/file_reader.h" |
| 11 #include "chrome/browser/tab_contents/tab_contents.h" | 11 #include "chrome/browser/tab_contents/tab_contents.h" |
| 12 #include "chrome/common/extensions/extension.h" | 12 #include "chrome/common/extensions/extension.h" |
| 13 #include "chrome/common/extensions/extension_error_utils.h" | 13 #include "chrome/common/extensions/extension_error_utils.h" |
| 14 | 14 |
| 15 namespace keys = extension_tabs_module_constants; | 15 namespace keys = extension_tabs_module_constants; |
| 16 | 16 |
| 17 const wchar_t* kCodeKey = L"code"; | 17 const wchar_t* kCodeKey = L"code"; |
| 18 const wchar_t* kFileKey = L"file"; | 18 const wchar_t* kFileKey = L"file"; |
| 19 | 19 |
| 20 bool ExecuteCodeInTabFunction::RunImpl() { | 20 bool ExecuteCodeInTabFunction::RunImpl() { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 | 73 |
| 74 if (!code_string.empty()) { | 74 if (!code_string.empty()) { |
| 75 Execute(code_string); | 75 Execute(code_string); |
| 76 return true; | 76 return true; |
| 77 } | 77 } |
| 78 | 78 |
| 79 std::string relative_path; | 79 std::string relative_path; |
| 80 if (script_info->HasKey(kFileKey)) { | 80 if (script_info->HasKey(kFileKey)) { |
| 81 if (!script_info->GetString(kFileKey, &relative_path)) | 81 if (!script_info->GetString(kFileKey, &relative_path)) |
| 82 return false; | 82 return false; |
| 83 file_path_ = GetExtension()->GetResourcePath(relative_path); | 83 resource_ = GetExtension()->GetResource(relative_path); |
| 84 } | 84 } |
| 85 if (file_path_.empty()) { | 85 if (resource_.extension_root().empty() || resource_.relative_path().empty()) { |
| 86 error_ = keys::kNoCodeOrFileToExecuteError; | 86 error_ = keys::kNoCodeOrFileToExecuteError; |
| 87 return false; | 87 return false; |
| 88 } | 88 } |
| 89 | 89 |
| 90 scoped_refptr<FileReader> file_reader(new FileReader( | 90 scoped_refptr<FileReader> file_reader(new FileReader( |
| 91 file_path_, NewCallback(this, &ExecuteCodeInTabFunction::DidLoadFile))); | 91 resource_, NewCallback(this, &ExecuteCodeInTabFunction::DidLoadFile))); |
| 92 file_reader->Start(); | 92 file_reader->Start(); |
| 93 AddRef(); // Keep us alive until DidLoadFile is called. | 93 AddRef(); // Keep us alive until DidLoadFile is called. |
| 94 | 94 |
| 95 return true; | 95 return true; |
| 96 } | 96 } |
| 97 | 97 |
| 98 void ExecuteCodeInTabFunction::DidLoadFile(bool success, | 98 void ExecuteCodeInTabFunction::DidLoadFile(bool success, |
| 99 const std::string& data) { | 99 const std::string& data) { |
| 100 if (success) { | 100 if (success) { |
| 101 Execute(data); | 101 Execute(data); |
| 102 } else { | 102 } else { |
| 103 #if defined(OS_POSIX) | 103 #if defined(OS_POSIX) |
| 104 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kLoadFileError, | 104 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kLoadFileError, |
| 105 file_path_.value()); | 105 resource_.relative_path().value()); |
| 106 #elif defined(OS_WIN) | 106 #elif defined(OS_WIN) |
| 107 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kLoadFileError, | 107 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kLoadFileError, |
| 108 WideToUTF8(file_path_.value())); | 108 WideToUTF8(resource_.relative_path().value())); |
| 109 #endif // OS_WIN | 109 #endif // OS_WIN |
| 110 SendResponse(false); | 110 SendResponse(false); |
| 111 } | 111 } |
| 112 Release(); // Balance the AddRef taken in RunImpl | 112 Release(); // Balance the AddRef taken in RunImpl |
| 113 } | 113 } |
| 114 | 114 |
| 115 void ExecuteCodeInTabFunction::Execute(const std::string& code_string) { | 115 void ExecuteCodeInTabFunction::Execute(const std::string& code_string) { |
| 116 TabContents* contents = NULL; | 116 TabContents* contents = NULL; |
| 117 Browser* browser = NULL; | 117 Browser* browser = NULL; |
| 118 if (!ExtensionTabUtil::GetTabById(execute_tab_id_, profile(), &browser, NULL, | 118 if (!ExtensionTabUtil::GetTabById(execute_tab_id_, profile(), &browser, NULL, |
| (...skipping 19 matching lines...) Expand all Loading... |
| 138 void ExecuteCodeInTabFunction::Observe(NotificationType type, | 138 void ExecuteCodeInTabFunction::Observe(NotificationType type, |
| 139 const NotificationSource& source, | 139 const NotificationSource& source, |
| 140 const NotificationDetails& details) { | 140 const NotificationDetails& details) { |
| 141 std::pair<int, bool>* result_details = | 141 std::pair<int, bool>* result_details = |
| 142 Details<std::pair<int, bool> >(details).ptr(); | 142 Details<std::pair<int, bool> >(details).ptr(); |
| 143 if (result_details->first == request_id()) { | 143 if (result_details->first == request_id()) { |
| 144 SendResponse(result_details->second); | 144 SendResponse(result_details->second); |
| 145 Release(); // balanced in Execute() | 145 Release(); // balanced in Execute() |
| 146 } | 146 } |
| 147 } | 147 } |
| OLD | NEW |