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, const std::string& data) { | 45 void ExecuteCodeFunction::DidLoadFile(bool success, |
| 46 std::unique_ptr<std::string> data) { | |
| 46 if (!success || !details_->file) { | 47 if (!success || !details_->file) { |
| 47 DidLoadAndLocalizeFile( | 48 DidLoadAndLocalizeFile(resource_.relative_path().AsUTF8Unsafe(), success, |
| 48 resource_.relative_path().AsUTF8Unsafe(), success, data); | 49 std::move(data)); |
| 49 return; | 50 return; |
| 50 } | 51 } |
| 51 | 52 |
| 52 ScriptExecutor::ScriptType script_type = | 53 ScriptExecutor::ScriptType script_type = |
| 53 ShouldInsertCSS() ? ScriptExecutor::CSS : ScriptExecutor::JAVASCRIPT; | 54 ShouldInsertCSS() ? ScriptExecutor::CSS : ScriptExecutor::JAVASCRIPT; |
| 54 | 55 |
| 55 std::string extension_id; | 56 std::string extension_id; |
| 56 base::FilePath extension_path; | 57 base::FilePath extension_path; |
| 57 std::string extension_default_locale; | 58 std::string extension_default_locale; |
| 58 if (extension()) { | 59 if (extension()) { |
| 59 extension_id = extension()->id(); | 60 extension_id = extension()->id(); |
| 60 extension_path = extension()->path(); | 61 extension_path = extension()->path(); |
| 61 extension()->manifest()->GetString(manifest_keys::kDefaultLocale, | 62 extension()->manifest()->GetString(manifest_keys::kDefaultLocale, |
| 62 &extension_default_locale); | 63 &extension_default_locale); |
| 63 } | 64 } |
| 64 | 65 |
| 65 content::BrowserThread::PostTask( | 66 content::BrowserThread::PostTask( |
| 66 content::BrowserThread::FILE, | 67 content::BrowserThread::FILE, FROM_HERE, |
| 67 FROM_HERE, | 68 base::Bind(&ExecuteCodeFunction::GetFileURLAndLocalizeCSS, this, |
| 68 base::Bind(&ExecuteCodeFunction::GetFileURLAndLocalizeCSS, | 69 script_type, base::Passed(std::move(data)), extension_id, |
| 69 this, | 70 extension_path, extension_default_locale)); |
| 70 script_type, | |
| 71 data, | |
| 72 extension_id, | |
| 73 extension_path, | |
| 74 extension_default_locale)); | |
| 75 } | 71 } |
| 76 | 72 |
| 77 void ExecuteCodeFunction::GetFileURLAndLocalizeCSS( | 73 void ExecuteCodeFunction::GetFileURLAndLocalizeCSS( |
| 78 ScriptExecutor::ScriptType script_type, | 74 ScriptExecutor::ScriptType script_type, |
| 79 const std::string& data, | 75 std::unique_ptr<std::string> data, |
| 80 const std::string& extension_id, | 76 const std::string& extension_id, |
| 81 const base::FilePath& extension_path, | 77 const base::FilePath& extension_path, |
| 82 const std::string& extension_default_locale) { | 78 const std::string& extension_default_locale) { |
| 83 std::string localized_data = data; | |
| 84 // Check if the file is CSS and needs localization. | 79 // Check if the file is CSS and needs localization. |
| 85 if ((script_type == ScriptExecutor::CSS) && !extension_id.empty() && | 80 if ((script_type == ScriptExecutor::CSS) && !extension_id.empty() && |
| 86 (data.find(MessageBundle::kMessageBegin) != std::string::npos)) { | 81 (data->find(MessageBundle::kMessageBegin) != std::string::npos)) { |
| 87 std::unique_ptr<SubstitutionMap> localization_messages( | 82 std::unique_ptr<SubstitutionMap> localization_messages( |
| 88 file_util::LoadMessageBundleSubstitutionMap( | 83 file_util::LoadMessageBundleSubstitutionMap( |
| 89 extension_path, extension_id, extension_default_locale)); | 84 extension_path, extension_id, extension_default_locale)); |
| 90 | 85 |
| 91 // We need to do message replacement on the data, so it has to be mutable. | 86 // We need to do message replacement on the data, so it has to be mutable. |
| 92 std::string error; | 87 std::string error; |
| 93 MessageBundle::ReplaceMessagesWithExternalDictionary( | 88 MessageBundle::ReplaceMessagesWithExternalDictionary(*localization_messages, |
| 94 *localization_messages, &localized_data, &error); | 89 data.get(), &error); |
| 95 } | 90 } |
| 96 | 91 |
| 97 file_url_ = net::FilePathToFileURL(resource_.GetFilePath()); | 92 file_url_ = net::FilePathToFileURL(resource_.GetFilePath()); |
| 98 | 93 |
| 99 // Call back DidLoadAndLocalizeFile on the UI thread. The success parameter | 94 // Call back DidLoadAndLocalizeFile on the UI thread. The success parameter |
| 100 // is always true, because if loading had failed, we wouldn't have had | 95 // is always true, because if loading had failed, we wouldn't have had |
| 101 // anything to localize. | 96 // anything to localize. |
| 102 content::BrowserThread::PostTask( | 97 content::BrowserThread::PostTask( |
| 103 content::BrowserThread::UI, | 98 content::BrowserThread::UI, FROM_HERE, |
| 104 FROM_HERE, | 99 base::Bind(&ExecuteCodeFunction::DidLoadAndLocalizeFile, this, |
| 105 base::Bind(&ExecuteCodeFunction::DidLoadAndLocalizeFile, | 100 resource_.relative_path().AsUTF8Unsafe(), true, |
| 106 this, | 101 base::Passed(std::move(data)))); |
| 107 resource_.relative_path().AsUTF8Unsafe(), | |
| 108 true, | |
| 109 localized_data)); | |
| 110 } | 102 } |
| 111 | 103 |
| 112 void ExecuteCodeFunction::DidLoadAndLocalizeFile(const std::string& file, | 104 void ExecuteCodeFunction::DidLoadAndLocalizeFile( |
| 113 bool success, | 105 const std::string& file, |
| 114 const std::string& data) { | 106 bool success, |
| 107 std::unique_ptr<std::string> data) { | |
| 115 if (success) { | 108 if (success) { |
| 116 if (!base::IsStringUTF8(data)) { | 109 if (!base::IsStringUTF8(*data)) { |
| 117 error_ = ErrorUtils::FormatErrorMessage(kBadFileEncodingError, file); | 110 error_ = ErrorUtils::FormatErrorMessage(kBadFileEncodingError, file); |
| 118 SendResponse(false); | 111 SendResponse(false); |
| 119 } else if (!Execute(data)) | 112 } else if (!Execute(*data)) |
| 120 SendResponse(false); | 113 SendResponse(false); |
| 121 } else { | 114 } else { |
| 122 // TODO(viettrungluu): bug: there's no particular reason the path should be | 115 // TODO(viettrungluu): bug: there's no particular reason the path should be |
| 123 // UTF-8, in which case this may fail. | 116 // UTF-8, in which case this may fail. |
| 124 error_ = ErrorUtils::FormatErrorMessage(kLoadFileError, file); | 117 error_ = ErrorUtils::FormatErrorMessage(kLoadFileError, file); |
| 125 SendResponse(false); | 118 SendResponse(false); |
| 126 } | 119 } |
| 127 } | 120 } |
| 128 | 121 |
| 129 bool ExecuteCodeFunction::Execute(const std::string& code_string) { | 122 bool ExecuteCodeFunction::Execute(const std::string& code_string) { |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 218 const ComponentExtensionResourceManager* | 211 const ComponentExtensionResourceManager* |
| 219 component_extension_resource_manager = | 212 component_extension_resource_manager = |
| 220 ExtensionsBrowserClient::Get() | 213 ExtensionsBrowserClient::Get() |
| 221 ->GetComponentExtensionResourceManager(); | 214 ->GetComponentExtensionResourceManager(); |
| 222 if (component_extension_resource_manager && | 215 if (component_extension_resource_manager && |
| 223 component_extension_resource_manager->IsComponentExtensionResource( | 216 component_extension_resource_manager->IsComponentExtensionResource( |
| 224 resource_.extension_root(), | 217 resource_.extension_root(), |
| 225 resource_.relative_path(), | 218 resource_.relative_path(), |
| 226 &resource_id)) { | 219 &resource_id)) { |
| 227 const ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 220 const ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 228 DidLoadFile(true, rb.GetRawDataResource(resource_id).as_string()); | 221 DidLoadFile(true, base::WrapUnique(new std::string( |
| 222 rb.GetRawDataResource(resource_id).as_string()))); | |
|
Devlin
2016/08/12 00:35:11
Is the compiler smart enough (through rvalue refs)
lazyboy
2016/08/12 01:31:22
I would *expect*, but not sure, I'm going to expli
| |
| 229 } else { | 223 } else { |
| 230 scoped_refptr<FileReader> file_reader(new FileReader( | 224 scoped_refptr<FileReader> file_reader(new FileReader( |
| 231 resource_, base::Bind(&ExecuteCodeFunction::DidLoadFile, this))); | 225 resource_, base::Bind(&ExecuteCodeFunction::DidLoadFile, this))); |
| 232 file_reader->Start(); | 226 file_reader->Start(); |
| 233 } | 227 } |
| 234 | 228 |
| 235 return true; | 229 return true; |
| 236 } | 230 } |
| 237 | 231 |
| 238 void ExecuteCodeFunction::OnExecuteCodeFinished(const std::string& error, | 232 void ExecuteCodeFunction::OnExecuteCodeFinished(const std::string& error, |
| 239 const GURL& on_url, | 233 const GURL& on_url, |
| 240 const base::ListValue& result) { | 234 const base::ListValue& result) { |
| 241 if (!error.empty()) | 235 if (!error.empty()) |
| 242 SetError(error); | 236 SetError(error); |
| 243 | 237 |
| 244 SendResponse(error.empty()); | 238 SendResponse(error.empty()); |
| 245 } | 239 } |
| 246 | 240 |
| 247 } // namespace extensions | 241 } // namespace extensions |
| 248 | 242 |
| 249 #endif // EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_IMPL_H_ | 243 #endif // EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_IMPL_H_ |
| OLD | NEW |