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