Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(537)

Side by Side Diff: extensions/browser/api/execute_code_function.cc

Issue 2301713002: Remove some UI->FILE->UI thread hops in ExecuteCodeFunction (Closed)
Patch Set: fix chromeos Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 12 matching lines...) Expand all
23 23
24 // Error messages 24 // Error messages
25 const char kNoCodeOrFileToExecuteError[] = "No source code or file specified."; 25 const char kNoCodeOrFileToExecuteError[] = "No source code or file specified.";
26 const char kMoreThanOneValuesError[] = 26 const char kMoreThanOneValuesError[] =
27 "Code and file should not be specified " 27 "Code and file should not be specified "
28 "at the same time in the second argument."; 28 "at the same time in the second argument.";
29 const char kBadFileEncodingError[] = 29 const char kBadFileEncodingError[] =
30 "Could not load file '*' for content script. It isn't UTF-8 encoded."; 30 "Could not load file '*' for content script. It isn't UTF-8 encoded.";
31 const char kLoadFileError[] = "Failed to load file: \"*\". "; 31 const char kLoadFileError[] = "Failed to load file: \"*\". ";
32 32
33 bool HasLocalizationData(const std::string* data) {
Devlin 2016/09/01 04:21:25 nit: const& instead of const* if we keep this. Th
lazyboy 2016/09/01 05:52:58 Removed, originally I had multiple places that nee
34 return data->find(extensions::MessageBundle::kMessageBegin) !=
35 std::string::npos;
36 }
33 } 37 }
34 38
35 namespace extensions { 39 namespace extensions {
36 40
37 using api::extension_types::InjectDetails; 41 using api::extension_types::InjectDetails;
38 42
39 ExecuteCodeFunction::ExecuteCodeFunction() { 43 ExecuteCodeFunction::ExecuteCodeFunction() {
40 } 44 }
41 45
42 ExecuteCodeFunction::~ExecuteCodeFunction() { 46 ExecuteCodeFunction::~ExecuteCodeFunction() {
43 } 47 }
44 48
45 void ExecuteCodeFunction::DidLoadFile(bool success, 49 void ExecuteCodeFunction::GetFileURLAndMaybeLocalize(
46 std::unique_ptr<std::string> data) { 50 const std::string& extension_id,
47 if (!success || !details_->file) { 51 const base::FilePath& extension_path,
48 DidLoadAndLocalizeFile(resource_.relative_path().AsUTF8Unsafe(), success, 52 const std::string& extension_default_locale,
49 std::move(data)); 53 bool requires_localization,
54 bool success,
Devlin 2016/09/01 04:21:25 If we make the change to not call this when readin
lazyboy 2016/09/01 05:52:58 Yes, I originally intended to do that (hence the s
55 std::string* data) {
56 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE);
57
58 file_url_ = net::FilePathToFileURL(resource_.GetFilePath());
59
60 if (!success || !requires_localization || HasLocalizationData(data))
Devlin 2016/09/01 04:21:25 is this supposed to be "!HasLocalizationData"?
lazyboy 2016/09/01 05:52:58 Yes, didn't check other than compiling on last pat
50 return; 61 return;
51 }
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 // We need to do message replacement on the data, so it has to be mutable.
Devlin 2016/09/01 04:21:25 nit: I don't this comment adds anything anymore.
lazyboy 2016/09/01 05:52:58 Done.
57 base::FilePath extension_path; 68 std::string error;
58 std::string extension_default_locale; 69 MessageBundle::ReplaceMessagesWithExternalDictionary(*localization_messages,
59 if (extension()) { 70 data, &error);
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 } 71 }
72 72
73 void ExecuteCodeFunction::GetFileURLAndLocalizeCSS( 73 void ExecuteCodeFunction::GetFileURLAndLocalizeComponentResource(
74 ScriptExecutor::ScriptType script_type,
75 std::unique_ptr<std::string> data, 74 std::unique_ptr<std::string> data,
75 bool needs_localization,
76 const std::string& extension_id, 76 const std::string& extension_id,
77 const base::FilePath& extension_path, 77 const base::FilePath& extension_path,
78 const std::string& extension_default_locale) { 78 const std::string& extension_default_locale) {
79 // Check if the file is CSS and needs localization. 79 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE);
80 if ((script_type == ScriptExecutor::CSS) && !extension_id.empty() && 80 GetFileURLAndMaybeLocalize(extension_id, extension_path,
81 (data->find(MessageBundle::kMessageBegin) != std::string::npos)) { 81 extension_default_locale, needs_localization, true,
82 std::unique_ptr<SubstitutionMap> localization_messages( 82 data.get());
83 file_util::LoadMessageBundleSubstitutionMap(
84 extension_path, extension_id, extension_default_locale));
85
86 // We need to do message replacement on the data, so it has to be mutable.
87 std::string error;
88 MessageBundle::ReplaceMessagesWithExternalDictionary(*localization_messages,
89 data.get(), &error);
90 }
91
92 file_url_ = net::FilePathToFileURL(resource_.GetFilePath());
93 83
94 // Call back DidLoadAndLocalizeFile on the UI thread. The success parameter 84 // Call back DidLoadAndLocalizeFile on the UI thread. The success parameter
Devlin 2016/09/01 04:21:25 nit: The "Call back DidLoadAndLocalizeFile on the
lazyboy 2016/09/01 05:52:58 Done.
95 // is always true, because if loading had failed, we wouldn't have had 85 // is always true, because we've loaded the content from ResourceBundle.
Devlin 2016/09/01 04:21:25 nitty nit: this could be self-documenting in the c
lazyboy 2016/09/01 05:52:58 Done.
96 // anything to localize.
97 content::BrowserThread::PostTask( 86 content::BrowserThread::PostTask(
98 content::BrowserThread::UI, FROM_HERE, 87 content::BrowserThread::UI, FROM_HERE,
99 base::Bind(&ExecuteCodeFunction::DidLoadAndLocalizeFile, this, 88 base::Bind(&ExecuteCodeFunction::DidLoadAndLocalizeFile, this,
100 resource_.relative_path().AsUTF8Unsafe(), true, 89 resource_.relative_path().AsUTF8Unsafe(), true,
101 base::Passed(std::move(data)))); 90 base::Passed(std::move(data))));
102 } 91 }
103 92
104 void ExecuteCodeFunction::DidLoadAndLocalizeFile( 93 void ExecuteCodeFunction::DidLoadAndLocalizeFile(
105 const std::string& file, 94 const std::string& file,
106 bool success, 95 bool success,
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
200 } 189 }
201 190
202 bool ExecuteCodeFunction::LoadFile(const std::string& file) { 191 bool ExecuteCodeFunction::LoadFile(const std::string& file) {
203 resource_ = extension()->GetResource(file); 192 resource_ = extension()->GetResource(file);
204 193
205 if (resource_.extension_root().empty() || resource_.relative_path().empty()) { 194 if (resource_.extension_root().empty() || resource_.relative_path().empty()) {
206 error_ = kNoCodeOrFileToExecuteError; 195 error_ = kNoCodeOrFileToExecuteError;
207 return false; 196 return false;
208 } 197 }
209 198
199 std::string extension_id = extension()->id();
200 base::FilePath extension_path = extension()->path();
201 std::string extension_default_locale;
202 extension()->manifest()->GetString(manifest_keys::kDefaultLocale,
203 &extension_default_locale);
204 bool requires_localization = ShouldInsertCSS() && !extension_id.empty();
Devlin 2016/09/01 04:21:25 s/requires_localization/might_require_localization
lazyboy 2016/09/01 05:52:58 Done.
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::GetFileURLAndLocalizeComponentResource,
223 this, base::Passed(std::move(data)), requires_localization,
224 extension_id, extension_path, extension_default_locale));
224 } else { 225 } else {
226 FileReader::OptionalFileThreadTaskCallback get_file_and_l10n_callback =
227 base::Bind(&ExecuteCodeFunction::GetFileURLAndMaybeLocalize, this,
228 extension_id, extension_path, extension_default_locale,
229 requires_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_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698