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

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

Issue 2978843002: [Extensions][Task Scheduler] Migrate ExecuteCodeFunction (Closed)
Patch Set: Created 3 years, 5 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 "base/task_scheduler/post_task.h"
11 #include "base/threading/thread_restrictions.h"
10 #include "extensions/browser/component_extension_resource_manager.h" 12 #include "extensions/browser/component_extension_resource_manager.h"
11 #include "extensions/browser/extension_api_frame_id_map.h" 13 #include "extensions/browser/extension_api_frame_id_map.h"
12 #include "extensions/browser/extensions_browser_client.h" 14 #include "extensions/browser/extensions_browser_client.h"
13 #include "extensions/browser/file_reader.h" 15 #include "extensions/browser/file_reader.h"
14 #include "extensions/common/error_utils.h" 16 #include "extensions/common/error_utils.h"
15 #include "extensions/common/extension_messages.h" 17 #include "extensions/common/extension_messages.h"
16 #include "extensions/common/file_util.h" 18 #include "extensions/common/file_util.h"
17 #include "extensions/common/manifest_constants.h" 19 #include "extensions/common/manifest_constants.h"
18 #include "extensions/common/message_bundle.h" 20 #include "extensions/common/message_bundle.h"
19 #include "net/base/filename_util.h" 21 #include "net/base/filename_util.h"
(...skipping 15 matching lines...) Expand all
35 namespace extensions { 37 namespace extensions {
36 38
37 using api::extension_types::InjectDetails; 39 using api::extension_types::InjectDetails;
38 40
39 ExecuteCodeFunction::ExecuteCodeFunction() { 41 ExecuteCodeFunction::ExecuteCodeFunction() {
40 } 42 }
41 43
42 ExecuteCodeFunction::~ExecuteCodeFunction() { 44 ExecuteCodeFunction::~ExecuteCodeFunction() {
43 } 45 }
44 46
45 void ExecuteCodeFunction::GetFileURLAndMaybeLocalizeOnFileThread( 47 void ExecuteCodeFunction::GetFileURLAndMaybeLocalizeOnBackgroundThread(
46 const std::string& extension_id, 48 const std::string& extension_id,
47 const base::FilePath& extension_path, 49 const base::FilePath& extension_path,
48 const std::string& extension_default_locale, 50 const std::string& extension_default_locale,
49 bool might_require_localization, 51 bool might_require_localization,
50 std::string* data) { 52 std::string* data) {
51 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE); 53 base::ThreadRestrictions::AssertIOAllowed();
52 54
53 file_url_ = net::FilePathToFileURL(resource_.GetFilePath()); 55 file_url_ = net::FilePathToFileURL(resource_.GetFilePath());
karandeepb 2017/07/13 01:47:48 It seems to me that net::FilePathToFileURL does no
Devlin 2017/07/13 18:35:09 Good find! I agree that this would be better done
54 56
55 if (!might_require_localization) 57 if (!might_require_localization)
56 return; 58 return;
57 59
58 bool needs_message_substituion = 60 bool needs_message_substituion =
59 data->find(extensions::MessageBundle::kMessageBegin) != std::string::npos; 61 data->find(extensions::MessageBundle::kMessageBegin) != std::string::npos;
60 if (!needs_message_substituion) 62 if (!needs_message_substituion)
61 return; 63 return;
62 64
63 std::unique_ptr<SubstitutionMap> localization_messages( 65 std::unique_ptr<SubstitutionMap> localization_messages(
64 file_util::LoadMessageBundleSubstitutionMap(extension_path, extension_id, 66 file_util::LoadMessageBundleSubstitutionMap(extension_path, extension_id,
65 extension_default_locale)); 67 extension_default_locale));
66 68
67 std::string error; 69 std::string error;
68 MessageBundle::ReplaceMessagesWithExternalDictionary(*localization_messages, 70 MessageBundle::ReplaceMessagesWithExternalDictionary(*localization_messages,
69 data, &error); 71 data, &error);
70 } 72 }
71 73
72 void ExecuteCodeFunction::GetFileURLAndLocalizeComponentResourceOnFileThread( 74 std::unique_ptr<std::string>
75 ExecuteCodeFunction::GetFileURLAndLocalizeComponentResourceOnBackgroundThread(
73 std::unique_ptr<std::string> data, 76 std::unique_ptr<std::string> data,
74 const std::string& extension_id, 77 const std::string& extension_id,
75 const base::FilePath& extension_path, 78 const base::FilePath& extension_path,
76 const std::string& extension_default_locale, 79 const std::string& extension_default_locale,
77 bool might_require_localization) { 80 bool might_require_localization) {
78 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE); 81 base::ThreadRestrictions::AssertIOAllowed();
79 GetFileURLAndMaybeLocalizeOnFileThread( 82 GetFileURLAndMaybeLocalizeOnBackgroundThread(
80 extension_id, extension_path, extension_default_locale, 83 extension_id, extension_path, extension_default_locale,
81 might_require_localization, data.get()); 84 might_require_localization, data.get());
82 85
83 bool success = true; 86 return data;
84 content::BrowserThread::PostTask(
85 content::BrowserThread::UI, FROM_HERE,
86 base::Bind(&ExecuteCodeFunction::DidLoadAndLocalizeFile, this,
87 resource_.relative_path().AsUTF8Unsafe(), success,
88 base::Passed(std::move(data))));
89 } 87 }
90 88
91 void ExecuteCodeFunction::DidLoadAndLocalizeFile( 89 void ExecuteCodeFunction::DidLoadAndLocalizeFile(
92 const std::string& file, 90 const std::string& file,
93 bool success, 91 bool success,
94 std::unique_ptr<std::string> data) { 92 std::unique_ptr<std::string> data) {
95 if (success) { 93 if (success) {
96 if (!base::IsStringUTF8(*data)) { 94 if (!base::IsStringUTF8(*data)) {
97 error_ = ErrorUtils::FormatErrorMessage(kBadFileEncodingError, file); 95 error_ = ErrorUtils::FormatErrorMessage(kBadFileEncodingError, file);
98 SendResponse(false); 96 SendResponse(false);
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
216 ->GetComponentExtensionResourceManager(); 214 ->GetComponentExtensionResourceManager();
217 if (component_extension_resource_manager && 215 if (component_extension_resource_manager &&
218 component_extension_resource_manager->IsComponentExtensionResource( 216 component_extension_resource_manager->IsComponentExtensionResource(
219 resource_.extension_root(), 217 resource_.extension_root(),
220 resource_.relative_path(), 218 resource_.relative_path(),
221 &resource_id)) { 219 &resource_id)) {
222 base::StringPiece resource = 220 base::StringPiece resource =
223 ResourceBundle::GetSharedInstance().GetRawDataResource(resource_id); 221 ResourceBundle::GetSharedInstance().GetRawDataResource(resource_id);
224 std::unique_ptr<std::string> data( 222 std::unique_ptr<std::string> data(
225 new std::string(resource.data(), resource.size())); 223 new std::string(resource.data(), resource.size()));
226 content::BrowserThread::PostTask( 224
227 content::BrowserThread::FILE, FROM_HERE, 225 base::PostTaskWithTraitsAndReplyWithResult(
karandeepb 2017/07/13 01:47:48 Changing to a base::PostTask instead of a sequence
Devlin 2017/07/13 18:35:09 Yep, this shouldn't be a problem. This function o
228 base::Bind(&ExecuteCodeFunction:: 226 FROM_HERE, {base::MayBlock()},
karandeepb 2017/07/13 01:47:48 It would be nice to be explicit about the shutdown
Devlin 2017/07/13 18:35:09 Done for shutdown behavior. For priority, it's di
229 GetFileURLAndLocalizeComponentResourceOnFileThread, 227 base::BindOnce(
230 this, base::Passed(std::move(data)), extension_id, 228 &ExecuteCodeFunction::
231 extension_path, extension_default_locale, 229 GetFileURLAndLocalizeComponentResourceOnBackgroundThread,
232 might_require_localization)); 230 this, base::Passed(std::move(data)), extension_id, extension_path,
231 extension_default_locale, might_require_localization),
232 base::BindOnce(&ExecuteCodeFunction::DidLoadAndLocalizeFile, this,
233 resource_.relative_path().AsUTF8Unsafe(),
234 true /* We assume this call always succeeds */));
233 } else { 235 } else {
234 FileReader::OptionalFileThreadTaskCallback get_file_and_l10n_callback = 236 FileReader::OptionalFileThreadTaskCallback get_file_and_l10n_callback =
235 base::Bind(&ExecuteCodeFunction::GetFileURLAndMaybeLocalizeOnFileThread, 237 base::Bind(
236 this, extension_id, extension_path, extension_default_locale, 238 &ExecuteCodeFunction::GetFileURLAndMaybeLocalizeOnBackgroundThread,
237 might_require_localization); 239 this, extension_id, extension_path, extension_default_locale,
240 might_require_localization);
238 241
239 scoped_refptr<FileReader> file_reader(new FileReader( 242 scoped_refptr<FileReader> file_reader(new FileReader(
240 resource_, get_file_and_l10n_callback, 243 resource_, get_file_and_l10n_callback,
241 base::Bind(&ExecuteCodeFunction::DidLoadAndLocalizeFile, this, 244 base::Bind(&ExecuteCodeFunction::DidLoadAndLocalizeFile, this,
242 resource_.relative_path().AsUTF8Unsafe()))); 245 resource_.relative_path().AsUTF8Unsafe())));
243 file_reader->Start(); 246 file_reader->Start();
244 } 247 }
245 248
246 return true; 249 return true;
247 } 250 }
248 251
249 void ExecuteCodeFunction::OnExecuteCodeFinished(const std::string& error, 252 void ExecuteCodeFunction::OnExecuteCodeFinished(const std::string& error,
250 const GURL& on_url, 253 const GURL& on_url,
251 const base::ListValue& result) { 254 const base::ListValue& result) {
252 if (!error.empty()) 255 if (!error.empty())
253 SetError(error); 256 SetError(error);
254 257
255 SendResponse(error.empty()); 258 SendResponse(error.empty());
256 } 259 }
257 260
258 } // namespace extensions 261 } // namespace extensions
259 262
260 #endif // EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_IMPL_H_ 263 #endif // EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_IMPL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698