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

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

Issue 2301713002: Remove some UI->FILE->UI thread hops in ExecuteCodeFunction (Closed)
Patch Set: 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
« no previous file with comments | « extensions/browser/api/execute_code_function.h ('k') | extensions/browser/file_reader.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..6451b24b1734cb095096a327a73b5c94af4bf65f 100644
--- a/extensions/browser/api/execute_code_function.cc
+++ b/extensions/browser/api/execute_code_function.cc
@@ -43,21 +43,33 @@ ExecuteCodeFunction::~ExecuteCodeFunction() {
}
void ExecuteCodeFunction::DidLoadFile(bool success,
+ const GURL& file_url,
std::unique_ptr<std::string> data) {
if (!success || !details_->file) {
DidLoadAndLocalizeFile(resource_.relative_path().AsUTF8Unsafe(), success,
std::move(data));
return;
}
-
- ScriptExecutor::ScriptType script_type =
- ShouldInsertCSS() ? ScriptExecutor::CSS : ScriptExecutor::JAVASCRIPT;
+ file_url_ = file_url;
std::string extension_id;
+ if (extension())
+ extension_id = extension()->id();
+
+ bool needs_css_localization =
+ ShouldInsertCSS() && !extension_id.empty() &&
+ data->find(MessageBundle::kMessageBegin) != std::string::npos;
+
+ if (!needs_css_localization) {
+ DidLoadAndLocalizeFile(resource_.relative_path().AsUTF8Unsafe(), true,
+ std::move(data));
+ return;
+ }
+
+ // The file is CSS and needs localization.
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);
@@ -65,31 +77,24 @@ void ExecuteCodeFunction::DidLoadFile(bool success,
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));
+ base::Bind(&ExecuteCodeFunction::LocalizeCSS, this,
+ base::Passed(std::move(data)), extension_id, extension_path,
+ extension_default_locale));
}
-void ExecuteCodeFunction::GetFileURLAndLocalizeCSS(
- ScriptExecutor::ScriptType script_type,
+void ExecuteCodeFunction::LocalizeCSS(
std::unique_ptr<std::string> data,
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);
- }
+ std::unique_ptr<SubstitutionMap> localization_messages(
+ file_util::LoadMessageBundleSubstitutionMap(extension_path, extension_id,
+ extension_default_locale));
- file_url_ = net::FilePathToFileURL(resource_.GetFilePath());
+ // 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);
// Call back DidLoadAndLocalizeFile on the UI thread. The success parameter
// is always true, because if loading had failed, we wouldn't have had
@@ -199,6 +204,14 @@ bool ExecuteCodeFunction::RunAsync() {
return LoadFile(*details_->file);
}
+void ExecuteCodeFunction::GetFileURL(std::unique_ptr<std::string> data) {
+ content::BrowserThread::PostTask(
+ content::BrowserThread::UI, FROM_HERE,
+ base::Bind(&ExecuteCodeFunction::DidLoadFile, this, true,
+ net::FilePathToFileURL(resource_.GetFilePath()),
+ base::Passed(std::move(data))));
+}
+
bool ExecuteCodeFunction::LoadFile(const std::string& file) {
resource_ = extension()->GetResource(file);
@@ -219,8 +232,11 @@ 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())));
+ content::BrowserThread::PostTask(
+ 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!
+ base::Bind(&ExecuteCodeFunction::GetFileURL, this,
+ base::Passed(base::WrapUnique(
+ new std::string(resource.data(), resource.size())))));
} else {
scoped_refptr<FileReader> file_reader(new FileReader(
resource_, base::Bind(&ExecuteCodeFunction::DidLoadFile, this)));
« no previous file with comments | « extensions/browser/api/execute_code_function.h ('k') | extensions/browser/file_reader.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698