| 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 24 matching lines...) Expand all Loading... |
| 35 namespace extensions { | 35 namespace extensions { |
| 36 | 36 |
| 37 using api::extension_types::InjectDetails; | 37 using api::extension_types::InjectDetails; |
| 38 | 38 |
| 39 ExecuteCodeFunction::ExecuteCodeFunction() { | 39 ExecuteCodeFunction::ExecuteCodeFunction() { |
| 40 } | 40 } |
| 41 | 41 |
| 42 ExecuteCodeFunction::~ExecuteCodeFunction() { | 42 ExecuteCodeFunction::~ExecuteCodeFunction() { |
| 43 } | 43 } |
| 44 | 44 |
| 45 void ExecuteCodeFunction::DidLoadFile(bool success, | 45 void ExecuteCodeFunction::GetFileURLAndMaybeLocalizeOnFileThread( |
| 46 std::unique_ptr<std::string> data) { | 46 const std::string& extension_id, |
| 47 if (!success || !details_->file) { | 47 const base::FilePath& extension_path, |
| 48 DidLoadAndLocalizeFile(resource_.relative_path().AsUTF8Unsafe(), success, | 48 const std::string& extension_default_locale, |
| 49 std::move(data)); | 49 bool might_require_localization, |
| 50 std::string* data) { |
| 51 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE); |
| 52 |
| 53 file_url_ = net::FilePathToFileURL(resource_.GetFilePath()); |
| 54 |
| 55 if (!might_require_localization) |
| 50 return; | 56 return; |
| 51 } | |
| 52 | 57 |
| 53 ScriptExecutor::ScriptType script_type = | 58 bool needs_message_substituion = |
| 54 ShouldInsertCSS() ? ScriptExecutor::CSS : ScriptExecutor::JAVASCRIPT; | 59 data->find(extensions::MessageBundle::kMessageBegin) != std::string::npos; |
| 60 if (!needs_message_substituion) |
| 61 return; |
| 55 | 62 |
| 56 std::string extension_id; | 63 std::unique_ptr<SubstitutionMap> localization_messages( |
| 57 base::FilePath extension_path; | 64 file_util::LoadMessageBundleSubstitutionMap(extension_path, extension_id, |
| 58 std::string extension_default_locale; | 65 extension_default_locale)); |
| 59 if (extension()) { | |
| 60 extension_id = extension()->id(); | |
| 61 extension_path = extension()->path(); | |
| 62 extension()->manifest()->GetString(manifest_keys::kDefaultLocale, | |
| 63 &extension_default_locale); | |
| 64 } | |
| 65 | 66 |
| 66 content::BrowserThread::PostTask( | 67 std::string error; |
| 67 content::BrowserThread::FILE, FROM_HERE, | 68 MessageBundle::ReplaceMessagesWithExternalDictionary(*localization_messages, |
| 68 base::Bind(&ExecuteCodeFunction::GetFileURLAndLocalizeCSS, this, | 69 data, &error); |
| 69 script_type, base::Passed(std::move(data)), extension_id, | |
| 70 extension_path, extension_default_locale)); | |
| 71 } | 70 } |
| 72 | 71 |
| 73 void ExecuteCodeFunction::GetFileURLAndLocalizeCSS( | 72 void ExecuteCodeFunction::GetFileURLAndLocalizeComponentResourceOnFileThread( |
| 74 ScriptExecutor::ScriptType script_type, | |
| 75 std::unique_ptr<std::string> data, | 73 std::unique_ptr<std::string> data, |
| 76 const std::string& extension_id, | 74 const std::string& extension_id, |
| 77 const base::FilePath& extension_path, | 75 const base::FilePath& extension_path, |
| 78 const std::string& extension_default_locale) { | 76 const std::string& extension_default_locale, |
| 79 // Check if the file is CSS and needs localization. | 77 bool might_require_localization) { |
| 80 if ((script_type == ScriptExecutor::CSS) && !extension_id.empty() && | 78 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE); |
| 81 (data->find(MessageBundle::kMessageBegin) != std::string::npos)) { | 79 GetFileURLAndMaybeLocalizeOnFileThread( |
| 82 std::unique_ptr<SubstitutionMap> localization_messages( | 80 extension_id, extension_path, extension_default_locale, |
| 83 file_util::LoadMessageBundleSubstitutionMap( | 81 might_require_localization, data.get()); |
| 84 extension_path, extension_id, extension_default_locale)); | |
| 85 | 82 |
| 86 // We need to do message replacement on the data, so it has to be mutable. | 83 bool success = true; |
| 87 std::string error; | |
| 88 MessageBundle::ReplaceMessagesWithExternalDictionary(*localization_messages, | |
| 89 data.get(), &error); | |
| 90 } | |
| 91 | |
| 92 file_url_ = net::FilePathToFileURL(resource_.GetFilePath()); | |
| 93 | |
| 94 // Call back DidLoadAndLocalizeFile on the UI thread. The success parameter | |
| 95 // is always true, because if loading had failed, we wouldn't have had | |
| 96 // anything to localize. | |
| 97 content::BrowserThread::PostTask( | 84 content::BrowserThread::PostTask( |
| 98 content::BrowserThread::UI, FROM_HERE, | 85 content::BrowserThread::UI, FROM_HERE, |
| 99 base::Bind(&ExecuteCodeFunction::DidLoadAndLocalizeFile, this, | 86 base::Bind(&ExecuteCodeFunction::DidLoadAndLocalizeFile, this, |
| 100 resource_.relative_path().AsUTF8Unsafe(), true, | 87 resource_.relative_path().AsUTF8Unsafe(), success, |
| 101 base::Passed(std::move(data)))); | 88 base::Passed(std::move(data)))); |
| 102 } | 89 } |
| 103 | 90 |
| 104 void ExecuteCodeFunction::DidLoadAndLocalizeFile( | 91 void ExecuteCodeFunction::DidLoadAndLocalizeFile( |
| 105 const std::string& file, | 92 const std::string& file, |
| 106 bool success, | 93 bool success, |
| 107 std::unique_ptr<std::string> data) { | 94 std::unique_ptr<std::string> data) { |
| 108 if (success) { | 95 if (success) { |
| 109 if (!base::IsStringUTF8(*data)) { | 96 if (!base::IsStringUTF8(*data)) { |
| 110 error_ = ErrorUtils::FormatErrorMessage(kBadFileEncodingError, file); | 97 error_ = ErrorUtils::FormatErrorMessage(kBadFileEncodingError, file); |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 } | 187 } |
| 201 | 188 |
| 202 bool ExecuteCodeFunction::LoadFile(const std::string& file) { | 189 bool ExecuteCodeFunction::LoadFile(const std::string& file) { |
| 203 resource_ = extension()->GetResource(file); | 190 resource_ = extension()->GetResource(file); |
| 204 | 191 |
| 205 if (resource_.extension_root().empty() || resource_.relative_path().empty()) { | 192 if (resource_.extension_root().empty() || resource_.relative_path().empty()) { |
| 206 error_ = kNoCodeOrFileToExecuteError; | 193 error_ = kNoCodeOrFileToExecuteError; |
| 207 return false; | 194 return false; |
| 208 } | 195 } |
| 209 | 196 |
| 197 const std::string& extension_id = extension()->id(); |
| 198 base::FilePath extension_path = extension()->path(); |
| 199 std::string extension_default_locale; |
| 200 extension()->manifest()->GetString(manifest_keys::kDefaultLocale, |
| 201 &extension_default_locale); |
| 202 // TODO(lazyboy): |extension_id| should not be empty(), turn this into a |
| 203 // DCHECK. |
| 204 bool might_require_localization = ShouldInsertCSS() && !extension_id.empty(); |
| 205 |
| 210 int resource_id; | 206 int resource_id; |
| 211 const ComponentExtensionResourceManager* | 207 const ComponentExtensionResourceManager* |
| 212 component_extension_resource_manager = | 208 component_extension_resource_manager = |
| 213 ExtensionsBrowserClient::Get() | 209 ExtensionsBrowserClient::Get() |
| 214 ->GetComponentExtensionResourceManager(); | 210 ->GetComponentExtensionResourceManager(); |
| 215 if (component_extension_resource_manager && | 211 if (component_extension_resource_manager && |
| 216 component_extension_resource_manager->IsComponentExtensionResource( | 212 component_extension_resource_manager->IsComponentExtensionResource( |
| 217 resource_.extension_root(), | 213 resource_.extension_root(), |
| 218 resource_.relative_path(), | 214 resource_.relative_path(), |
| 219 &resource_id)) { | 215 &resource_id)) { |
| 220 base::StringPiece resource = | 216 base::StringPiece resource = |
| 221 ResourceBundle::GetSharedInstance().GetRawDataResource(resource_id); | 217 ResourceBundle::GetSharedInstance().GetRawDataResource(resource_id); |
| 222 DidLoadFile(true, base::WrapUnique( | 218 std::unique_ptr<std::string> data( |
| 223 new std::string(resource.data(), resource.size()))); | 219 new std::string(resource.data(), resource.size())); |
| 220 content::BrowserThread::PostTask( |
| 221 content::BrowserThread::FILE, FROM_HERE, |
| 222 base::Bind(&ExecuteCodeFunction:: |
| 223 GetFileURLAndLocalizeComponentResourceOnFileThread, |
| 224 this, base::Passed(std::move(data)), extension_id, |
| 225 extension_path, extension_default_locale, |
| 226 might_require_localization)); |
| 224 } else { | 227 } else { |
| 228 FileReader::OptionalFileThreadTaskCallback get_file_and_l10n_callback = |
| 229 base::Bind(&ExecuteCodeFunction::GetFileURLAndMaybeLocalizeOnFileThread, |
| 230 this, extension_id, extension_path, extension_default_locale, |
| 231 might_require_localization); |
| 232 |
| 225 scoped_refptr<FileReader> file_reader(new FileReader( | 233 scoped_refptr<FileReader> file_reader(new FileReader( |
| 226 resource_, base::Bind(&ExecuteCodeFunction::DidLoadFile, this))); | 234 resource_, get_file_and_l10n_callback, |
| 235 base::Bind(&ExecuteCodeFunction::DidLoadAndLocalizeFile, this, |
| 236 resource_.relative_path().AsUTF8Unsafe()))); |
| 227 file_reader->Start(); | 237 file_reader->Start(); |
| 228 } | 238 } |
| 229 | 239 |
| 230 return true; | 240 return true; |
| 231 } | 241 } |
| 232 | 242 |
| 233 void ExecuteCodeFunction::OnExecuteCodeFinished(const std::string& error, | 243 void ExecuteCodeFunction::OnExecuteCodeFinished(const std::string& error, |
| 234 const GURL& on_url, | 244 const GURL& on_url, |
| 235 const base::ListValue& result) { | 245 const base::ListValue& result) { |
| 236 if (!error.empty()) | 246 if (!error.empty()) |
| 237 SetError(error); | 247 SetError(error); |
| 238 | 248 |
| 239 SendResponse(error.empty()); | 249 SendResponse(error.empty()); |
| 240 } | 250 } |
| 241 | 251 |
| 242 } // namespace extensions | 252 } // namespace extensions |
| 243 | 253 |
| 244 #endif // EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_IMPL_H_ | 254 #endif // EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_IMPL_H_ |
| OLD | NEW |