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

Unified Diff: extensions/browser/api/execute_code_function.cc

Issue 2301713002: Remove some UI->FILE->UI thread hops in ExecuteCodeFunction (Closed)
Patch Set: git cl format Created 4 years, 4 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 side-by-side diff with in-line comments
Download patch
Index: extensions/browser/api/execute_code_function.cc
diff --git a/extensions/browser/api/execute_code_function.cc b/extensions/browser/api/execute_code_function.cc
index f1a80cfd873fe4f5d3c6f8b14a69e58dfa69da6c..454902857142d5e65c2a597718c28e07e550f80e 100644
--- a/extensions/browser/api/execute_code_function.cc
+++ b/extensions/browser/api/execute_code_function.cc
@@ -42,62 +42,49 @@ ExecuteCodeFunction::ExecuteCodeFunction() {
ExecuteCodeFunction::~ExecuteCodeFunction() {
}
-void ExecuteCodeFunction::DidLoadFile(bool success,
- std::unique_ptr<std::string> data) {
- if (!success || !details_->file) {
- DidLoadAndLocalizeFile(resource_.relative_path().AsUTF8Unsafe(), success,
- std::move(data));
- return;
- }
+void ExecuteCodeFunction::GetFileURLAndMaybeLocalizeOnFileThread(
+ const std::string& extension_id,
+ const base::FilePath& extension_path,
+ const std::string& extension_default_locale,
+ bool might_require_localization,
+ std::string* data) {
+ DCHECK_CURRENTLY_ON(content::BrowserThread::FILE);
- ScriptExecutor::ScriptType script_type =
- ShouldInsertCSS() ? ScriptExecutor::CSS : ScriptExecutor::JAVASCRIPT;
+ file_url_ = net::FilePathToFileURL(resource_.GetFilePath());
- std::string extension_id;
- base::FilePath extension_path;
- std::string extension_default_locale;
- if (extension()) {
- extension_id = extension()->id();
- extension_path = extension()->path();
- extension()->manifest()->GetString(manifest_keys::kDefaultLocale,
- &extension_default_locale);
+ if (!might_require_localization)
+ return;
+
+ 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.
+ std::string::npos) {
+ return;
}
- content::BrowserThread::PostTask(
- content::BrowserThread::FILE, FROM_HERE,
- base::Bind(&ExecuteCodeFunction::GetFileURLAndLocalizeCSS, this,
- script_type, base::Passed(std::move(data)), extension_id,
- extension_path, extension_default_locale));
+ std::unique_ptr<SubstitutionMap> localization_messages(
+ file_util::LoadMessageBundleSubstitutionMap(extension_path, extension_id,
+ extension_default_locale));
+
+ std::string error;
+ MessageBundle::ReplaceMessagesWithExternalDictionary(*localization_messages,
+ data, &error);
}
-void ExecuteCodeFunction::GetFileURLAndLocalizeCSS(
- ScriptExecutor::ScriptType script_type,
+void ExecuteCodeFunction::GetFileURLAndLocalizeComponentResourceOnFileThread(
std::unique_ptr<std::string> data,
+ bool might_require_localization,
const std::string& extension_id,
const base::FilePath& extension_path,
const std::string& extension_default_locale) {
- // Check if the file is CSS and needs localization.
- if ((script_type == ScriptExecutor::CSS) && !extension_id.empty() &&
- (data->find(MessageBundle::kMessageBegin) != std::string::npos)) {
- std::unique_ptr<SubstitutionMap> localization_messages(
- file_util::LoadMessageBundleSubstitutionMap(
- extension_path, extension_id, extension_default_locale));
-
- // We need to do message replacement on the data, so it has to be mutable.
- std::string error;
- MessageBundle::ReplaceMessagesWithExternalDictionary(*localization_messages,
- data.get(), &error);
- }
-
- file_url_ = net::FilePathToFileURL(resource_.GetFilePath());
+ DCHECK_CURRENTLY_ON(content::BrowserThread::FILE);
+ GetFileURLAndMaybeLocalizeOnFileThread(
+ extension_id, extension_path, extension_default_locale,
+ might_require_localization, data.get());
- // Call back DidLoadAndLocalizeFile on the UI thread. The success parameter
- // is always true, because if loading had failed, we wouldn't have had
- // anything to localize.
+ bool success = true;
content::BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE,
base::Bind(&ExecuteCodeFunction::DidLoadAndLocalizeFile, this,
- resource_.relative_path().AsUTF8Unsafe(), true,
+ resource_.relative_path().AsUTF8Unsafe(), success,
base::Passed(std::move(data))));
}
@@ -207,6 +194,13 @@ bool ExecuteCodeFunction::LoadFile(const std::string& file) {
return false;
}
+ 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.
+ base::FilePath extension_path = extension()->path();
+ std::string extension_default_locale;
+ extension()->manifest()->GetString(manifest_keys::kDefaultLocale,
+ &extension_default_locale);
+ 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
+
int resource_id;
const ComponentExtensionResourceManager*
component_extension_resource_manager =
@@ -219,11 +213,25 @@ bool ExecuteCodeFunction::LoadFile(const std::string& file) {
&resource_id)) {
base::StringPiece resource =
ResourceBundle::GetSharedInstance().GetRawDataResource(resource_id);
- DidLoadFile(true, base::WrapUnique(
- new std::string(resource.data(), resource.size())));
+ std::unique_ptr<std::string> data(
+ new std::string(resource.data(), resource.size()));
+ content::BrowserThread::PostTask(
+ content::BrowserThread::FILE, FROM_HERE,
+ base::Bind(&ExecuteCodeFunction::
+ GetFileURLAndLocalizeComponentResourceOnFileThread,
+ this, base::Passed(std::move(data)),
+ might_require_localization, extension_id, extension_path,
+ extension_default_locale));
} else {
+ FileReader::OptionalFileThreadTaskCallback get_file_and_l10n_callback =
+ base::Bind(&ExecuteCodeFunction::GetFileURLAndMaybeLocalizeOnFileThread,
+ this, extension_id, extension_path, extension_default_locale,
+ might_require_localization);
+
scoped_refptr<FileReader> file_reader(new FileReader(
- resource_, base::Bind(&ExecuteCodeFunction::DidLoadFile, this)));
+ resource_, get_file_and_l10n_callback,
+ base::Bind(&ExecuteCodeFunction::DidLoadAndLocalizeFile, this,
+ resource_.relative_path().AsUTF8Unsafe())));
file_reader->Start();
}

Powered by Google App Engine
This is Rietveld 408576698