| 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 "base/task_scheduler/post_task.h" |
| 11 #include "base/threading/thread_restrictions.h" |
| 10 #include "extensions/browser/component_extension_resource_manager.h" | 12 #include "extensions/browser/component_extension_resource_manager.h" |
| 11 #include "extensions/browser/extension_api_frame_id_map.h" | 13 #include "extensions/browser/extension_api_frame_id_map.h" |
| 12 #include "extensions/browser/extensions_browser_client.h" | 14 #include "extensions/browser/extensions_browser_client.h" |
| 13 #include "extensions/browser/file_reader.h" | 15 #include "extensions/browser/file_reader.h" |
| 14 #include "extensions/common/error_utils.h" | 16 #include "extensions/common/error_utils.h" |
| 15 #include "extensions/common/extension_messages.h" | 17 #include "extensions/common/extension_messages.h" |
| 16 #include "extensions/common/file_util.h" | 18 #include "extensions/common/file_util.h" |
| 17 #include "extensions/common/manifest_constants.h" | 19 #include "extensions/common/manifest_constants.h" |
| 18 #include "extensions/common/message_bundle.h" | 20 #include "extensions/common/message_bundle.h" |
| 19 #include "net/base/filename_util.h" | 21 #include "net/base/filename_util.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 35 namespace extensions { | 37 namespace extensions { |
| 36 | 38 |
| 37 using api::extension_types::InjectDetails; | 39 using api::extension_types::InjectDetails; |
| 38 | 40 |
| 39 ExecuteCodeFunction::ExecuteCodeFunction() { | 41 ExecuteCodeFunction::ExecuteCodeFunction() { |
| 40 } | 42 } |
| 41 | 43 |
| 42 ExecuteCodeFunction::~ExecuteCodeFunction() { | 44 ExecuteCodeFunction::~ExecuteCodeFunction() { |
| 43 } | 45 } |
| 44 | 46 |
| 45 void ExecuteCodeFunction::GetFileURLAndMaybeLocalizeOnFileThread( | 47 void ExecuteCodeFunction::GetFileURLAndMaybeLocalizeInBackground( |
| 46 const std::string& extension_id, | 48 const std::string& extension_id, |
| 47 const base::FilePath& extension_path, | 49 const base::FilePath& extension_path, |
| 48 const std::string& extension_default_locale, | 50 const std::string& extension_default_locale, |
| 49 bool might_require_localization, | 51 bool might_require_localization, |
| 50 std::string* data) { | 52 std::string* data) { |
| 51 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE); | 53 base::ThreadRestrictions::AssertIOAllowed(); |
| 52 | 54 |
| 55 // TODO(devlin): FilePathToFileURL() doesn't need to be done on a blocking |
| 56 // task runner, so we could do that on the UI thread and then avoid the hop |
| 57 // if we don't need localization. |
| 53 file_url_ = net::FilePathToFileURL(resource_.GetFilePath()); | 58 file_url_ = net::FilePathToFileURL(resource_.GetFilePath()); |
| 54 | 59 |
| 55 if (!might_require_localization) | 60 if (!might_require_localization) |
| 56 return; | 61 return; |
| 57 | 62 |
| 58 bool needs_message_substituion = | 63 bool needs_message_substituion = |
| 59 data->find(extensions::MessageBundle::kMessageBegin) != std::string::npos; | 64 data->find(extensions::MessageBundle::kMessageBegin) != std::string::npos; |
| 60 if (!needs_message_substituion) | 65 if (!needs_message_substituion) |
| 61 return; | 66 return; |
| 62 | 67 |
| 63 std::unique_ptr<SubstitutionMap> localization_messages( | 68 std::unique_ptr<SubstitutionMap> localization_messages( |
| 64 file_util::LoadMessageBundleSubstitutionMap(extension_path, extension_id, | 69 file_util::LoadMessageBundleSubstitutionMap(extension_path, extension_id, |
| 65 extension_default_locale)); | 70 extension_default_locale)); |
| 66 | 71 |
| 67 std::string error; | 72 std::string error; |
| 68 MessageBundle::ReplaceMessagesWithExternalDictionary(*localization_messages, | 73 MessageBundle::ReplaceMessagesWithExternalDictionary(*localization_messages, |
| 69 data, &error); | 74 data, &error); |
| 70 } | 75 } |
| 71 | 76 |
| 72 void ExecuteCodeFunction::GetFileURLAndLocalizeComponentResourceOnFileThread( | 77 std::unique_ptr<std::string> |
| 78 ExecuteCodeFunction::GetFileURLAndLocalizeComponentResourceInBackground( |
| 73 std::unique_ptr<std::string> data, | 79 std::unique_ptr<std::string> data, |
| 74 const std::string& extension_id, | 80 const std::string& extension_id, |
| 75 const base::FilePath& extension_path, | 81 const base::FilePath& extension_path, |
| 76 const std::string& extension_default_locale, | 82 const std::string& extension_default_locale, |
| 77 bool might_require_localization) { | 83 bool might_require_localization) { |
| 78 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE); | 84 base::ThreadRestrictions::AssertIOAllowed(); |
| 79 GetFileURLAndMaybeLocalizeOnFileThread( | 85 GetFileURLAndMaybeLocalizeInBackground( |
| 80 extension_id, extension_path, extension_default_locale, | 86 extension_id, extension_path, extension_default_locale, |
| 81 might_require_localization, data.get()); | 87 might_require_localization, data.get()); |
| 82 | 88 |
| 83 bool success = true; | 89 return data; |
| 84 content::BrowserThread::PostTask( | |
| 85 content::BrowserThread::UI, FROM_HERE, | |
| 86 base::Bind(&ExecuteCodeFunction::DidLoadAndLocalizeFile, this, | |
| 87 resource_.relative_path().AsUTF8Unsafe(), success, | |
| 88 base::Passed(std::move(data)))); | |
| 89 } | 90 } |
| 90 | 91 |
| 91 void ExecuteCodeFunction::DidLoadAndLocalizeFile( | 92 void ExecuteCodeFunction::DidLoadAndLocalizeFile( |
| 92 const std::string& file, | 93 const std::string& file, |
| 93 bool success, | 94 bool success, |
| 94 std::unique_ptr<std::string> data) { | 95 std::unique_ptr<std::string> data) { |
| 95 if (success) { | 96 if (success) { |
| 96 if (!base::IsStringUTF8(*data)) { | 97 if (!base::IsStringUTF8(*data)) { |
| 97 error_ = ErrorUtils::FormatErrorMessage(kBadFileEncodingError, file); | 98 error_ = ErrorUtils::FormatErrorMessage(kBadFileEncodingError, file); |
| 98 SendResponse(false); | 99 SendResponse(false); |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 ->GetComponentExtensionResourceManager(); | 217 ->GetComponentExtensionResourceManager(); |
| 217 if (component_extension_resource_manager && | 218 if (component_extension_resource_manager && |
| 218 component_extension_resource_manager->IsComponentExtensionResource( | 219 component_extension_resource_manager->IsComponentExtensionResource( |
| 219 resource_.extension_root(), | 220 resource_.extension_root(), |
| 220 resource_.relative_path(), | 221 resource_.relative_path(), |
| 221 &resource_id)) { | 222 &resource_id)) { |
| 222 base::StringPiece resource = | 223 base::StringPiece resource = |
| 223 ResourceBundle::GetSharedInstance().GetRawDataResource(resource_id); | 224 ResourceBundle::GetSharedInstance().GetRawDataResource(resource_id); |
| 224 std::unique_ptr<std::string> data( | 225 std::unique_ptr<std::string> data( |
| 225 new std::string(resource.data(), resource.size())); | 226 new std::string(resource.data(), resource.size())); |
| 226 content::BrowserThread::PostTask( | 227 |
| 227 content::BrowserThread::FILE, FROM_HERE, | 228 base::PostTaskWithTraitsAndReplyWithResult( |
| 228 base::Bind(&ExecuteCodeFunction:: | 229 FROM_HERE, |
| 229 GetFileURLAndLocalizeComponentResourceOnFileThread, | 230 {base::MayBlock(), base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN}, |
| 230 this, base::Passed(std::move(data)), extension_id, | 231 base::BindOnce(&ExecuteCodeFunction:: |
| 231 extension_path, extension_default_locale, | 232 GetFileURLAndLocalizeComponentResourceInBackground, |
| 232 might_require_localization)); | 233 this, base::Passed(std::move(data)), extension_id, |
| 234 extension_path, extension_default_locale, |
| 235 might_require_localization), |
| 236 base::BindOnce(&ExecuteCodeFunction::DidLoadAndLocalizeFile, this, |
| 237 resource_.relative_path().AsUTF8Unsafe(), |
| 238 true /* We assume this call always succeeds */)); |
| 233 } else { | 239 } else { |
| 234 FileReader::OptionalFileThreadTaskCallback get_file_and_l10n_callback = | 240 FileReader::OptionalFileThreadTaskCallback get_file_and_l10n_callback = |
| 235 base::Bind(&ExecuteCodeFunction::GetFileURLAndMaybeLocalizeOnFileThread, | 241 base::Bind(&ExecuteCodeFunction::GetFileURLAndMaybeLocalizeInBackground, |
| 236 this, extension_id, extension_path, extension_default_locale, | 242 this, extension_id, extension_path, extension_default_locale, |
| 237 might_require_localization); | 243 might_require_localization); |
| 238 | 244 |
| 239 scoped_refptr<FileReader> file_reader(new FileReader( | 245 scoped_refptr<FileReader> file_reader(new FileReader( |
| 240 resource_, get_file_and_l10n_callback, | 246 resource_, get_file_and_l10n_callback, |
| 241 base::Bind(&ExecuteCodeFunction::DidLoadAndLocalizeFile, this, | 247 base::Bind(&ExecuteCodeFunction::DidLoadAndLocalizeFile, this, |
| 242 resource_.relative_path().AsUTF8Unsafe()))); | 248 resource_.relative_path().AsUTF8Unsafe()))); |
| 243 file_reader->Start(); | 249 file_reader->Start(); |
| 244 } | 250 } |
| 245 | 251 |
| 246 return true; | 252 return true; |
| 247 } | 253 } |
| 248 | 254 |
| 249 void ExecuteCodeFunction::OnExecuteCodeFinished(const std::string& error, | 255 void ExecuteCodeFunction::OnExecuteCodeFinished(const std::string& error, |
| 250 const GURL& on_url, | 256 const GURL& on_url, |
| 251 const base::ListValue& result) { | 257 const base::ListValue& result) { |
| 252 if (!error.empty()) | 258 if (!error.empty()) |
| 253 SetError(error); | 259 SetError(error); |
| 254 | 260 |
| 255 SendResponse(error.empty()); | 261 SendResponse(error.empty()); |
| 256 } | 262 } |
| 257 | 263 |
| 258 } // namespace extensions | 264 } // namespace extensions |
| 259 | 265 |
| 260 #endif // EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_IMPL_H_ | 266 #endif // EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_IMPL_H_ |
| OLD | NEW |