| 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 #ifndef EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_IMPL_H_ | 5 #ifndef EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_IMPL_H_ |
| 6 #define EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_IMPL_H_ | 6 #define EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_IMPL_H_ |
| 7 | 7 |
| 8 #include "extensions/browser/api/execute_code_function.h" | 8 #include "extensions/browser/api/execute_code_function.h" |
| 9 | 9 |
| 10 #include "extensions/browser/component_extension_resource_manager.h" | 10 #include "extensions/browser/component_extension_resource_manager.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 using core_api::extension_types::InjectDetails; | 36 using core_api::extension_types::InjectDetails; |
| 37 | 37 |
| 38 ExecuteCodeFunction::ExecuteCodeFunction() { | 38 ExecuteCodeFunction::ExecuteCodeFunction() { |
| 39 } | 39 } |
| 40 | 40 |
| 41 ExecuteCodeFunction::~ExecuteCodeFunction() { | 41 ExecuteCodeFunction::~ExecuteCodeFunction() { |
| 42 } | 42 } |
| 43 | 43 |
| 44 void ExecuteCodeFunction::DidLoadFile(bool success, const std::string& data) { | 44 void ExecuteCodeFunction::DidLoadFile(bool success, const std::string& data) { |
| 45 if (!success || !details_->file) { | 45 if (!success || !details_->file) { |
| 46 DidLoadAndLocalizeFile(success, data); | 46 DidLoadAndLocalizeFile( |
| 47 resource_.relative_path().AsUTF8Unsafe(), success, data); |
| 47 return; | 48 return; |
| 48 } | 49 } |
| 49 | 50 |
| 50 ScriptExecutor::ScriptType script_type = | 51 ScriptExecutor::ScriptType script_type = |
| 51 ShouldInsertCSS() ? ScriptExecutor::CSS : ScriptExecutor::JAVASCRIPT; | 52 ShouldInsertCSS() ? ScriptExecutor::CSS : ScriptExecutor::JAVASCRIPT; |
| 52 | 53 |
| 53 std::string extension_id; | 54 std::string extension_id; |
| 54 base::FilePath extension_path; | 55 base::FilePath extension_path; |
| 55 std::string extension_default_locale; | 56 std::string extension_default_locale; |
| 56 if (extension()) { | 57 if (extension()) { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 file_url_ = net::FilePathToFileURL(resource_.GetFilePath()); | 96 file_url_ = net::FilePathToFileURL(resource_.GetFilePath()); |
| 96 | 97 |
| 97 // Call back DidLoadAndLocalizeFile on the UI thread. The success parameter | 98 // Call back DidLoadAndLocalizeFile on the UI thread. The success parameter |
| 98 // is always true, because if loading had failed, we wouldn't have had | 99 // is always true, because if loading had failed, we wouldn't have had |
| 99 // anything to localize. | 100 // anything to localize. |
| 100 content::BrowserThread::PostTask( | 101 content::BrowserThread::PostTask( |
| 101 content::BrowserThread::UI, | 102 content::BrowserThread::UI, |
| 102 FROM_HERE, | 103 FROM_HERE, |
| 103 base::Bind(&ExecuteCodeFunction::DidLoadAndLocalizeFile, | 104 base::Bind(&ExecuteCodeFunction::DidLoadAndLocalizeFile, |
| 104 this, | 105 this, |
| 106 resource_.relative_path().AsUTF8Unsafe(), |
| 105 true, | 107 true, |
| 106 localized_data)); | 108 localized_data)); |
| 107 } | 109 } |
| 108 | 110 |
| 109 void ExecuteCodeFunction::DidLoadAndLocalizeFile(bool success, | 111 void ExecuteCodeFunction::DidLoadAndLocalizeFile(const std::string& file, |
| 112 bool success, |
| 110 const std::string& data) { | 113 const std::string& data) { |
| 111 if (success) { | 114 if (success) { |
| 112 if (!base::IsStringUTF8(data)) { | 115 if (!base::IsStringUTF8(data)) { |
| 113 error_ = ErrorUtils::FormatErrorMessage( | 116 error_ = ErrorUtils::FormatErrorMessage(kBadFileEncodingError, file); |
| 114 kBadFileEncodingError, resource_.relative_path().AsUTF8Unsafe()); | |
| 115 SendResponse(false); | 117 SendResponse(false); |
| 116 } else if (!Execute(data)) | 118 } else if (!Execute(data)) |
| 117 SendResponse(false); | 119 SendResponse(false); |
| 118 } else { | 120 } else { |
| 119 // TODO(viettrungluu): bug: there's no particular reason the path should be | 121 // TODO(viettrungluu): bug: there's no particular reason the path should be |
| 120 // UTF-8, in which case this may fail. | 122 // UTF-8, in which case this may fail. |
| 121 error_ = ErrorUtils::FormatErrorMessage( | 123 error_ = ErrorUtils::FormatErrorMessage(kLoadFileError, file); |
| 122 kLoadFileError, resource_.relative_path().AsUTF8Unsafe()); | |
| 123 SendResponse(false); | 124 SendResponse(false); |
| 124 } | 125 } |
| 125 } | 126 } |
| 126 | 127 |
| 127 bool ExecuteCodeFunction::Execute(const std::string& code_string) { | 128 bool ExecuteCodeFunction::Execute(const std::string& code_string) { |
| 128 ScriptExecutor* executor = GetScriptExecutor(); | 129 ScriptExecutor* executor = GetScriptExecutor(); |
| 129 if (!executor) | 130 if (!executor) |
| 130 return false; | 131 return false; |
| 131 | 132 |
| 132 if (!extension() && !IsWebView()) | 133 if (!extension() && !IsWebView()) |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 | 199 |
| 199 if (!CanExecuteScriptOnPage()) | 200 if (!CanExecuteScriptOnPage()) |
| 200 return false; | 201 return false; |
| 201 | 202 |
| 202 if (details_->code.get()) | 203 if (details_->code.get()) |
| 203 return Execute(*details_->code); | 204 return Execute(*details_->code); |
| 204 | 205 |
| 205 if (!details_->file.get()) | 206 if (!details_->file.get()) |
| 206 return false; | 207 return false; |
| 207 | 208 |
| 208 if (!extension()) | 209 return LoadFile(*details_->file); |
| 209 return false; | 210 } |
| 210 | 211 |
| 211 resource_ = extension()->GetResource(*details_->file); | 212 bool ExecuteCodeFunction::LoadFile(const std::string& file) { |
| 213 resource_ = extension()->GetResource(file); |
| 212 | 214 |
| 213 if (resource_.extension_root().empty() || resource_.relative_path().empty()) { | 215 if (resource_.extension_root().empty() || resource_.relative_path().empty()) { |
| 214 error_ = kNoCodeOrFileToExecuteError; | 216 error_ = kNoCodeOrFileToExecuteError; |
| 215 return false; | 217 return false; |
| 216 } | 218 } |
| 217 | 219 |
| 218 int resource_id; | 220 int resource_id; |
| 219 const ComponentExtensionResourceManager* | 221 const ComponentExtensionResourceManager* |
| 220 component_extension_resource_manager = | 222 component_extension_resource_manager = |
| 221 ExtensionsBrowserClient::Get() | 223 ExtensionsBrowserClient::Get() |
| (...skipping 19 matching lines...) Expand all Loading... |
| 241 const base::ListValue& result) { | 243 const base::ListValue& result) { |
| 242 if (!error.empty()) | 244 if (!error.empty()) |
| 243 SetError(error); | 245 SetError(error); |
| 244 | 246 |
| 245 SendResponse(error.empty()); | 247 SendResponse(error.empty()); |
| 246 } | 248 } |
| 247 | 249 |
| 248 } // namespace extensions | 250 } // namespace extensions |
| 249 | 251 |
| 250 #endif // EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_IMPL_H_ | 252 #endif // EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_IMPL_H_ |
| OLD | NEW |