Commit 8ceaa4d1 authored by Istiaque Ahmed's avatar Istiaque Ahmed Committed by Commit Bot

Extensions: Use OnceCallbacks in FileReader.

The callbacks in FileReader are used as once callbacks, but they
are declared to be RepeatingCallbacks. Fix this.

Bug: 812717
Change-Id: I89badc6bbd3583fc510d47218bb7bb69d537964a
Reviewed-on: https://chromium-review.googlesource.com/919704Reviewed-by: default avatarKaran Bhatia <karandeepb@chromium.org>
Commit-Queue: Istiaque Ahmed <lazyboy@chromium.org>
Cr-Commit-Position: refs/heads/master@{#537392}
parent 6e3a081d
...@@ -251,14 +251,15 @@ bool ExecuteCodeFunction::LoadFile(const std::string& file) { ...@@ -251,14 +251,15 @@ bool ExecuteCodeFunction::LoadFile(const std::string& file) {
true /* We assume this call always succeeds */)); true /* We assume this call always succeeds */));
} else { } else {
FileReader::OptionalFileSequenceTask get_file_and_l10n_callback = FileReader::OptionalFileSequenceTask get_file_and_l10n_callback =
base::Bind(&ExecuteCodeFunction::GetFileURLAndMaybeLocalizeInBackground, base::BindOnce(
this, extension_id, extension_path, extension_default_locale, &ExecuteCodeFunction::GetFileURLAndMaybeLocalizeInBackground, this,
might_require_localization); extension_id, extension_path, extension_default_locale,
might_require_localization);
scoped_refptr<FileReader> file_reader(new FileReader(
resource_, get_file_and_l10n_callback, auto file_reader = base::MakeRefCounted<FileReader>(
base::Bind(&ExecuteCodeFunction::DidLoadAndLocalizeFile, this, resource_, std::move(get_file_and_l10n_callback),
resource_.relative_path().AsUTF8Unsafe()))); base::BindOnce(&ExecuteCodeFunction::DidLoadAndLocalizeFile, this,
resource_.relative_path().AsUTF8Unsafe()));
file_reader->Start(); file_reader->Start();
} }
......
...@@ -10,13 +10,12 @@ ...@@ -10,13 +10,12 @@
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
#include "extensions/browser/extension_file_task_runner.h" #include "extensions/browser/extension_file_task_runner.h"
FileReader::FileReader( FileReader::FileReader(const extensions::ExtensionResource& resource,
const extensions::ExtensionResource& resource, OptionalFileSequenceTask optional_file_sequence_task,
const OptionalFileSequenceTask& optional_file_sequence_task, DoneCallback done_callback)
const DoneCallback& done_callback)
: resource_(resource), : resource_(resource),
optional_file_sequence_task_(optional_file_sequence_task), optional_file_sequence_task_(std::move(optional_file_sequence_task)),
done_callback_(done_callback), done_callback_(std::move(done_callback)),
origin_task_runner_(base::ThreadTaskRunnerHandle::Get()) {} origin_task_runner_(base::ThreadTaskRunnerHandle::Get()) {}
void FileReader::Start() { void FileReader::Start() {
...@@ -33,15 +32,14 @@ void FileReader::ReadFileOnFileSequence() { ...@@ -33,15 +32,14 @@ void FileReader::ReadFileOnFileSequence() {
std::unique_ptr<std::string> data(new std::string()); std::unique_ptr<std::string> data(new std::string());
bool success = base::ReadFileToString(resource_.GetFilePath(), data.get()); bool success = base::ReadFileToString(resource_.GetFilePath(), data.get());
if (!optional_file_sequence_task_.is_null()) { if (optional_file_sequence_task_) {
if (success) { if (success)
base::ResetAndReturn(&optional_file_sequence_task_).Run(data.get()); std::move(optional_file_sequence_task_).Run(data.get());
} else { else
optional_file_sequence_task_.Reset(); optional_file_sequence_task_.Reset();
}
} }
origin_task_runner_->PostTask( origin_task_runner_->PostTask(
FROM_HERE, base::Bind(base::ResetAndReturn(&done_callback_), success, FROM_HERE,
base::Passed(std::move(data)))); base::BindOnce(std::move(done_callback_), success, std::move(data)));
} }
...@@ -18,17 +18,17 @@ ...@@ -18,17 +18,17 @@
// back to chrome/browser/net if other subsystems want to use it. // back to chrome/browser/net if other subsystems want to use it.
class FileReader : public base::RefCountedThreadSafe<FileReader> { class FileReader : public base::RefCountedThreadSafe<FileReader> {
public: public:
// TODO(devlin): Use base::OnceCallback here.
// Reports success or failure and the data of the file upon success. // Reports success or failure and the data of the file upon success.
using DoneCallback = base::Callback<void(bool, std::unique_ptr<std::string>)>; using DoneCallback =
base::OnceCallback<void(bool, std::unique_ptr<std::string>)>;
// Lets the caller accomplish tasks on the file data, after the file content // Lets the caller accomplish tasks on the file data, after the file content
// has been read. // has been read.
// If the file reading doesn't succeed, this will be ignored. // If the file reading doesn't succeed, this will be ignored.
using OptionalFileSequenceTask = base::Callback<void(std::string*)>; using OptionalFileSequenceTask = base::OnceCallback<void(std::string*)>;
FileReader(const extensions::ExtensionResource& resource, FileReader(const extensions::ExtensionResource& resource,
const OptionalFileSequenceTask& file_sequence_task, OptionalFileSequenceTask file_sequence_task,
const DoneCallback& done_callback); DoneCallback done_callback);
// Called to start reading the file on a background sequence. Upon completion, // Called to start reading the file on a background sequence. Upon completion,
// the callback will be notified of the results. // the callback will be notified of the results.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment