Chromium Code Reviews| 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) | |
| 56 return; | |
| 57 | |
| 58 if (data->find(extensions::MessageBundle::kMessageBegin) == | |
|
Devlin
2016/09/01 06:15:21
nit: either a comment explaining this or a self-do
lazyboy
2016/09/01 18:10:18
Done.
| |
| 59 std::string::npos) { | |
| 50 return; | 60 return; |
| 51 } | 61 } |
| 52 | 62 |
| 53 ScriptExecutor::ScriptType script_type = | 63 std::unique_ptr<SubstitutionMap> localization_messages( |
| 54 ShouldInsertCSS() ? ScriptExecutor::CSS : ScriptExecutor::JAVASCRIPT; | 64 file_util::LoadMessageBundleSubstitutionMap(extension_path, extension_id, |
| 65 extension_default_locale)); | |
| 55 | 66 |
| 56 std::string extension_id; | 67 std::string error; |
| 57 base::FilePath extension_path; | 68 MessageBundle::ReplaceMessagesWithExternalDictionary(*localization_messages, |
| 58 std::string extension_default_locale; | 69 data, &error); |
| 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 content::BrowserThread::PostTask( | |
| 67 content::BrowserThread::FILE, FROM_HERE, | |
| 68 base::Bind(&ExecuteCodeFunction::GetFileURLAndLocalizeCSS, this, | |
| 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, |
| 74 bool might_require_localization, | |
| 76 const std::string& extension_id, | 75 const std::string& extension_id, |
| 77 const base::FilePath& extension_path, | 76 const base::FilePath& extension_path, |
| 78 const std::string& extension_default_locale) { | 77 const std::string& extension_default_locale) { |
| 79 // Check if the file is CSS and needs localization. | 78 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE); |
| 80 if ((script_type == ScriptExecutor::CSS) && !extension_id.empty() && | 79 GetFileURLAndMaybeLocalizeOnFileThread( |
| 81 (data->find(MessageBundle::kMessageBegin) != std::string::npos)) { | 80 extension_id, extension_path, extension_default_locale, |
| 82 std::unique_ptr<SubstitutionMap> localization_messages( | 81 might_require_localization, data.get()); |
| 83 file_util::LoadMessageBundleSubstitutionMap( | |
| 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 std::string extension_id = extension()->id(); | |
|
Devlin
2016/09/01 06:15:21
const& should be sufficient here.
lazyboy
2016/09/01 18:10:18
Done.
| |
| 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 bool might_require_localization = ShouldInsertCSS() && !extension_id.empty(); | |
|
Devlin
2016/09/01 06:15:21
Hmm... can extension_id actually be empty? Before
lazyboy
2016/09/01 18:10:18
That's probably because we checked extension() in
Devlin
2016/09/01 18:15:42
extension_id should definitely always be defined f
| |
| 203 | |
| 210 int resource_id; | 204 int resource_id; |
| 211 const ComponentExtensionResourceManager* | 205 const ComponentExtensionResourceManager* |
| 212 component_extension_resource_manager = | 206 component_extension_resource_manager = |
| 213 ExtensionsBrowserClient::Get() | 207 ExtensionsBrowserClient::Get() |
| 214 ->GetComponentExtensionResourceManager(); | 208 ->GetComponentExtensionResourceManager(); |
| 215 if (component_extension_resource_manager && | 209 if (component_extension_resource_manager && |
| 216 component_extension_resource_manager->IsComponentExtensionResource( | 210 component_extension_resource_manager->IsComponentExtensionResource( |
| 217 resource_.extension_root(), | 211 resource_.extension_root(), |
| 218 resource_.relative_path(), | 212 resource_.relative_path(), |
| 219 &resource_id)) { | 213 &resource_id)) { |
| 220 base::StringPiece resource = | 214 base::StringPiece resource = |
| 221 ResourceBundle::GetSharedInstance().GetRawDataResource(resource_id); | 215 ResourceBundle::GetSharedInstance().GetRawDataResource(resource_id); |
| 222 DidLoadFile(true, base::WrapUnique( | 216 std::unique_ptr<std::string> data( |
| 223 new std::string(resource.data(), resource.size()))); | 217 new std::string(resource.data(), resource.size())); |
| 218 content::BrowserThread::PostTask( | |
| 219 content::BrowserThread::FILE, FROM_HERE, | |
| 220 base::Bind(&ExecuteCodeFunction:: | |
| 221 GetFileURLAndLocalizeComponentResourceOnFileThread, | |
| 222 this, base::Passed(std::move(data)), | |
| 223 might_require_localization, extension_id, extension_path, | |
| 224 extension_default_locale)); | |
| 224 } else { | 225 } else { |
| 226 FileReader::OptionalFileThreadTaskCallback get_file_and_l10n_callback = | |
| 227 base::Bind(&ExecuteCodeFunction::GetFileURLAndMaybeLocalizeOnFileThread, | |
| 228 this, extension_id, extension_path, extension_default_locale, | |
| 229 might_require_localization); | |
| 230 | |
| 225 scoped_refptr<FileReader> file_reader(new FileReader( | 231 scoped_refptr<FileReader> file_reader(new FileReader( |
| 226 resource_, base::Bind(&ExecuteCodeFunction::DidLoadFile, this))); | 232 resource_, get_file_and_l10n_callback, |
| 233 base::Bind(&ExecuteCodeFunction::DidLoadAndLocalizeFile, this, | |
| 234 resource_.relative_path().AsUTF8Unsafe()))); | |
| 227 file_reader->Start(); | 235 file_reader->Start(); |
| 228 } | 236 } |
| 229 | 237 |
| 230 return true; | 238 return true; |
| 231 } | 239 } |
| 232 | 240 |
| 233 void ExecuteCodeFunction::OnExecuteCodeFinished(const std::string& error, | 241 void ExecuteCodeFunction::OnExecuteCodeFinished(const std::string& error, |
| 234 const GURL& on_url, | 242 const GURL& on_url, |
| 235 const base::ListValue& result) { | 243 const base::ListValue& result) { |
| 236 if (!error.empty()) | 244 if (!error.empty()) |
| 237 SetError(error); | 245 SetError(error); |
| 238 | 246 |
| 239 SendResponse(error.empty()); | 247 SendResponse(error.empty()); |
| 240 } | 248 } |
| 241 | 249 |
| 242 } // namespace extensions | 250 } // namespace extensions |
| 243 | 251 |
| 244 #endif // EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_IMPL_H_ | 252 #endif // EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_IMPL_H_ |
| OLD | NEW |