Thanks, let me share a simplified example.
Here is a hypothetical class that is used to read a file.
class CacheFile {
public:
...
void Read(const base::FilePath& path, Callback callback) {
task_runner_->PostTaskAndReply(base::BindOnce(ReadInternal, path), std::move(callback));
}
private:
// Created by base::ThreadPool::CreateTaskRunner.
scoped_refptr<TaskRunner> task_runner_;
};
std::string ReadInternal(const base::FilePath& path) {
base::File file(path);
std::string result;
// Read the file contents.
...
return result;
}
I want to broker the file open operation in ReadInternal with mojo. I'm going to define a mojo interface:
interface FileOpener {
[Sync] Open(FilePath path) => (File file);
};
and give a pending_remote<FileOpener> to ReadInternal.
class CacheFile {
public:
...
void Read(const base::FilePath& path, pending_remote<FileOpener> opener, Callback callback) {
task_runner_->PostTaskAndReply(base::BindOnce(ReadInternal, path, std::move(opener)), std::move(callback));
}
private:
// Created by base::ThreadPool::CreateTaskRunner, and this instance is shared by many operations/instances.
scoped_refptr<TaskRunner> task_runner_;
};
std::string ReadInternal(const base::FilePath& path, pending_remote<FileOpener> pending_opener) {
base::File file;
mojo::Remote<FileOpener> opener(std::move(pending_opener));
opener->Open(path, &file);
std::string result;
// Read the file contents.
...
return result;
}
Now mojo::Remote<FileOpener> requires a SequencedTaskRunner, and I'm looking for a way to run ReadInternal on a SequencedTaskRunner.
Your responses sound like I can create a SequenceTaskRunner for each Read operation without much performance loss, right?
void Read(const base::FilePath& path, pending_remote<FileOpener> opener, Callback callback) {
auto task_runner = base::ThreadPool::CreateSequencedTaskRunner(...);
task_runner->PostTaskAndReply(base::BindOnce(ReadInternal, path, std::move(opener)), std::move(callback));
}