[cr148] PostTaskAndReplyWithResult unpacking tuples

This is a quality-of-life change but functionally the resulting code is
the same.

Chromium changes:
https://chromium.googlesource.com/chromium/src/+/b791274c81bf6b38b10bc61646432226e24dee80

commit b791274c81bf6b38b10bc61646432226e24dee80
Author: Evan Stade <evanstade@microsoft.com>
Date:   Tue Mar 24 14:22:34 2026 -0700

    Add PostTaskAndReplyWithResult support for multiple args

    PostTaskAndReplyWithResult currently only works if the result is a
    single value. The new version additionally works with a reply function
    that takes multiple arguments, where the task function must return a
    std::tuple.

    This also adds support to SequenceBound, and updates a few callers that
    were returning a tuple/taking a tuple response, since now the obligatory
    pattern is to return a tuple/take an unwrapped tuple (fn arguments).

    Before:

    void DoFoo(FooCallback callback) {
         Value value = ...;
         Info info = ...;
         Delegate delegate = ...;
         std::move(callback).Run(value, info, delegate);
       }

    task_runner->PostTask(
           FROM_HERE,
           BindOnce(&DoFoo,
                    BindPostTaskToCurrentDefault(std::move(callback))));

    Or:

    std::tuple<Value, Info, Delegate> DoFoo() {
         Value value = ...;
         Info info = ...;
         Delegate delegate = ...;
         return {value, info, delegate};
       }

    task_runner->PostTaskAndReplyWithResult(
           FROM_HERE,
           BindOnce(&DoFoo),
           /*a lambda that manually unwraps a tuple,
             wrapping a callback*/);

    After:

    std::tuple<Value, Info, Delegate> DoFoo() {
         Value value = ...;
         Info info = ...;
         Delegate delegate = ...;
         return {value, info, delegate};
       }

    task_runner->PostTaskAndReplyWithResult(
           FROM_HERE,
           BindOnce(&DoFoo),
           std::move(callback));

    Change-Id: I85ec5a6879d62266a7cbd6e20d647f8f89ee4b55
    Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7664766
    Reviewed-by: Daniel Cheng <dcheng@chromium.org>
    Owners-Override: Daniel Cheng <dcheng@chromium.org>
    Commit-Queue: Evan Stade <evanstade@microsoft.com>
    Cr-Commit-Position: refs/heads/main@{#1604388}
This commit is contained in:
Claudio DeSouza
2026-04-27 12:00:52 +01:00
parent 2995c0cbbc
commit 2c58df05d4
3 changed files with 20 additions and 25 deletions
+14 -19
View File
@@ -250,11 +250,8 @@ void UploadFileHelper::MultiFilesSelected(
base::OnceCallback<void(
std::tuple<std::optional<std::vector<uint8_t>>, base::FilePath,
std::optional<mojom::UploadedFileType>>)> callback,
std::tuple<std::optional<std::vector<uint8_t>>, base::FilePath>
result) {
auto file_data = std::get<0>(result);
auto filepath = std::get<1>(result);
std::optional<std::vector<uint8_t>> file_data,
base::FilePath filepath) {
if (!file_data) {
std::move(callback).Run(std::make_tuple(
std::nullopt, std::move(filepath), std::nullopt));
@@ -311,43 +308,41 @@ void UploadFileHelper::FileSelectionCanceled() {
}
}
void UploadFileHelper::OnFileRead(
std::tuple<std::optional<std::vector<uint8_t>>, base::FilePath> result) {
void UploadFileHelper::OnFileRead(std::optional<std::vector<uint8_t>> data,
base::FilePath path) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
auto file_data = std::get<0>(result);
if (!file_data) {
if (!data) {
std::move(upload_file_callback_).Run(std::nullopt);
return;
}
// Determine file type based on extension and validate PDF content
auto file_type_opt = DetermineFileType(std::get<1>(result), *file_data);
auto file_type_opt = DetermineFileType(path, *data);
if (file_type_opt && (*file_type_opt == mojom::UploadedFileType::kPdf ||
*file_type_opt == mojom::UploadedFileType::kText)) {
// Return raw data; text extraction happens via ProcessPdfFile or
// ProcessTextFile mojo endpoint.
std::vector<mojom::UploadedFilePtr> files;
files.push_back(mojom::UploadedFile::New(std::get<1>(result).AsUTF8Unsafe(),
file_data->size(), *file_data,
*file_type_opt, std::nullopt));
files.push_back(mojom::UploadedFile::New(path.AsUTF8Unsafe(), data->size(),
*data, *file_type_opt,
std::nullopt));
std::move(upload_file_callback_).Run(std::make_optional(std::move(files)));
} else if (file_type_opt &&
*file_type_opt == mojom::UploadedFileType::kImage) {
// For images, process them as before
UploadFileHelper::ProcessImageData(
&data_decoder_, *file_data,
&data_decoder_, *data,
base::BindOnce(&UploadFileHelper::OnImageEncoded,
weak_ptr_factory_.GetWeakPtr(),
std::get<1>(result).AsUTF8Unsafe()));
weak_ptr_factory_.GetWeakPtr(), path.AsUTF8Unsafe()));
} else {
// Include as empty stub so the frontend can detect the unsupported
// file and show an error (as opposed to nullopt which means the user
// cancelled the file picker).
std::vector<mojom::UploadedFilePtr> files;
files.push_back(mojom::UploadedFile::New(
std::get<1>(result).AsUTF8Unsafe(), 0, std::vector<uint8_t>(),
mojom::UploadedFileType::kText, std::nullopt));
files.push_back(
mojom::UploadedFile::New(path.AsUTF8Unsafe(), 0, std::vector<uint8_t>(),
mojom::UploadedFileType::kText, std::nullopt));
std::move(upload_file_callback_).Run(std::make_optional(std::move(files)));
}
}
+2 -2
View File
@@ -68,8 +68,8 @@ class UploadFileHelper : public ui::SelectFileDialog::Listener {
const std::vector<ui::SelectedFileInfo>& files) override;
void FileSelectionCanceled() override;
void OnFileRead(
std::tuple<std::optional<std::vector<uint8_t>>, base::FilePath> result);
void OnFileRead(std::optional<std::vector<uint8_t>> data,
base::FilePath path);
void OnImageEncoded(std::string filename,
std::optional<std::vector<uint8_t>> output);
@@ -225,16 +225,16 @@ void FeedFetcher::OnFetchFeedFetchedFeed(
void FeedFetcher::OnFetchFeedFetchedAll(FetchFeedCallback callback,
std::vector<FeedSourceResult> results) {
base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE, base::BindOnce(&CombineFeedSourceResults, std::move(results)),
FROM_HERE, {},
base::BindOnce(&CombineFeedSourceResults, std::move(results)),
base::BindOnce(
[](base::WeakPtr<FeedFetcher> fetcher, FetchFeedCallback callback,
std::tuple<FeedItems, ETags> result) {
FeedItems items, ETags tags) {
// If we've been destroyed, don't run the callback.
if (!fetcher) {
return;
}
std::move(callback).Run(std::move(std::get<0>(result)),
std::move(std::get<1>(result)));
std::move(callback).Run(std::move(items), std::move(tags));
},
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}