Commit e0f90093 authored by Sami Kyostila's avatar Sami Kyostila Committed by Commit Bot

content/browser/background_fetch: Always specify thread affinity when posting tasks

*** Note: There is no behavior change from this patch. ***

The PostTask APIs will shortly be changed to require all tasks to explicitly
specify their thread affinity, i.e., whether the task should run on the thread
pool or a specific named thread such as a BrowserThread. This patch updates all
call sites with thread affinity annotation. We also remove the "WithTraits"
suffix to make the call sites more readable.

Before:

    // Thread pool task.
    base::PostTaskWithTraits(FROM_HERE, {...}, ...);

    // UI thread task.
    base::PostTaskWithTraits(FROM_HERE, {BrowserThread::UI, ...}, ...);

After:

    // Thread pool task.
    base::PostTask(FROM_HERE, {base::ThreadPool(), ...}, ...);

    // UI thread task.
    base::PostTask(FROM_HERE, {BrowserThread::UI, ...}, ...);

This patch was semi-automatically prepared with these steps:

    1. Patch in https://chromium-review.googlesource.com/c/chromium/src/+/1635827
       to make thread affinity a build-time requirement.
    2. Run an initial pass with a clang rewriter:
       https://chromium-review.googlesource.com/c/chromium/src/+/1635623
    3. ninja -C out/Debug | grep 'requested here' | cut -d: -f1-3 | sort | \
           uniq > errors.txt
    4. while read line; do
         f=$(echo $line | cut -d: -f 1)
         r=$(echo $line | cut -d: -f 2)
         c=$(echo $line | cut -d: -f 3)
         sed -i "${r}s/./&base::ThreadPool(),/$c" $f
       done < errors.txt
    5. GOTO 3 until build succeeds.
    6. Remove the "WithTraits" suffix from task API call sites:

       $ tools/git/mffr.py -i <(cat <<EOF
       [
         ["PostTaskWithTraits",                            "PostTask"],
         ["PostDelayedTaskWithTraits",                     "PostDelayedTask"],
         ["PostTaskWithTraitsAndReply",                    "PostTaskAndReply"],
         ["CreateTaskRunnerWithTraits",                    "CreateTaskRunner"],
         ["CreateSequencedTaskRunnerWithTraits",           "CreateSequencedTaskRunner"],
         ["CreateUpdateableSequencedTaskRunnerWithTraits", "CreateUpdateableSequencedTaskRunner"],
         ["CreateSingleThreadTaskRunnerWithTraits",        "CreateSingleThreadTaskRunner"],
         ["CreateCOMSTATaskRunnerWithTraits",              "CreateCOMSTATaskRunner"]
       ]
       EOF
       )

This CL was uploaded by git cl split.

R=rayankans@chromium.org

Bug: 968047
Change-Id: Iac4a5f35210b2baf7280bc0d9be89b68183c2ba9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1729257
Auto-Submit: Sami Kyöstilä <skyostil@chromium.org>
Reviewed-by: default avatarRayan Kanso <rayankans@chromium.org>
Commit-Queue: Rayan Kanso <rayankans@chromium.org>
Cr-Commit-Position: refs/heads/master@{#684380}
parent 05a13f1c
......@@ -174,7 +174,7 @@ void BackgroundFetchContext::DidGetPermission(
BackgroundFetchPermission permission) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
base::PostTaskWithTraits(
base::PostTask(
FROM_HERE, {BrowserThread::UI},
base::BindOnce(&background_fetch::RecordBackgroundFetchUkmEvent,
registration_id.origin(), requests.size(), options.Clone(),
......@@ -291,9 +291,8 @@ void BackgroundFetchContext::DidGetMatchingRequests(
void BackgroundFetchContext::Shutdown() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
base::PostTaskWithTraits(
FROM_HERE, {BrowserThread::IO},
base::BindOnce(&BackgroundFetchContext::ShutdownOnIO, this));
base::PostTask(FROM_HERE, {BrowserThread::IO},
base::BindOnce(&BackgroundFetchContext::ShutdownOnIO, this));
}
void BackgroundFetchContext::ShutdownOnIO() {
......
......@@ -45,8 +45,8 @@ class BackgroundFetchDelegateProxy::Core
BackgroundFetchDelegate::GetPermissionForOriginCallback callback,
BackgroundFetchPermission permission) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
base::PostTaskWithTraits(FROM_HERE, {BrowserThread::IO},
base::BindOnce(std::move(callback), permission));
base::PostTask(FROM_HERE, {BrowserThread::IO},
base::BindOnce(std::move(callback), permission));
}
void GetPermissionForOrigin(
......@@ -69,8 +69,8 @@ class BackgroundFetchDelegateProxy::Core
BackgroundFetchDelegate::GetIconDisplaySizeCallback callback,
const gfx::Size& display_size) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
base::PostTaskWithTraits(FROM_HERE, {BrowserThread::IO},
base::BindOnce(std::move(callback), display_size));
base::PostTask(FROM_HERE, {BrowserThread::IO},
base::BindOnce(std::move(callback), display_size));
}
void GetIconDisplaySize(
......@@ -82,9 +82,8 @@ class BackgroundFetchDelegateProxy::Core
base::BindOnce(&Core::ForwardGetIconDisplaySizeCallbackToIO,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
} else {
base::PostTaskWithTraits(
FROM_HERE, {BrowserThread::IO},
base::BindOnce(std::move(callback), gfx::Size(0, 0)));
base::PostTask(FROM_HERE, {BrowserThread::IO},
base::BindOnce(std::move(callback), gfx::Size(0, 0)));
}
}
......@@ -223,10 +222,9 @@ void BackgroundFetchDelegateProxy::Core::OnJobCancelled(
const std::string& job_unique_id,
blink::mojom::BackgroundFetchFailureReason reason_to_abort) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
base::PostTaskWithTraits(
FROM_HERE, {BrowserThread::IO},
base::BindOnce(&BackgroundFetchDelegateProxy::OnJobCancelled, io_parent_,
job_unique_id, reason_to_abort));
base::PostTask(FROM_HERE, {BrowserThread::IO},
base::BindOnce(&BackgroundFetchDelegateProxy::OnJobCancelled,
io_parent_, job_unique_id, reason_to_abort));
}
void BackgroundFetchDelegateProxy::Core::OnDownloadUpdated(
......@@ -235,7 +233,7 @@ void BackgroundFetchDelegateProxy::Core::OnDownloadUpdated(
uint64_t bytes_uploaded,
uint64_t bytes_downloaded) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
base::PostTaskWithTraits(
base::PostTask(
FROM_HERE, {BrowserThread::IO},
base::BindOnce(&BackgroundFetchDelegateProxy::OnDownloadUpdated,
io_parent_, job_unique_id, guid, bytes_uploaded,
......@@ -247,7 +245,7 @@ void BackgroundFetchDelegateProxy::Core::OnDownloadComplete(
const std::string& guid,
std::unique_ptr<BackgroundFetchResult> result) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
base::PostTaskWithTraits(
base::PostTask(
FROM_HERE, {BrowserThread::IO},
base::BindOnce(&BackgroundFetchDelegateProxy::OnDownloadComplete,
io_parent_, job_unique_id, guid, std::move(result)));
......@@ -259,7 +257,7 @@ void BackgroundFetchDelegateProxy::Core::OnDownloadStarted(
std::unique_ptr<content::BackgroundFetchResponse> response) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
base::PostTaskWithTraits(
base::PostTask(
FROM_HERE, {BrowserThread::IO},
base::BindOnce(&BackgroundFetchDelegateProxy::DidStartRequest, io_parent_,
job_unique_id, guid, std::move(response)));
......@@ -269,20 +267,18 @@ void BackgroundFetchDelegateProxy::Core::OnUIActivated(
const std::string& job_unique_id) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
base::PostTaskWithTraits(
FROM_HERE, {BrowserThread::IO},
base::BindOnce(&BackgroundFetchDelegateProxy::DidActivateUI, io_parent_,
job_unique_id));
base::PostTask(FROM_HERE, {BrowserThread::IO},
base::BindOnce(&BackgroundFetchDelegateProxy::DidActivateUI,
io_parent_, job_unique_id));
}
void BackgroundFetchDelegateProxy::Core::OnUIUpdated(
const std::string& job_unique_id) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
base::PostTaskWithTraits(
FROM_HERE, {BrowserThread::IO},
base::BindOnce(&BackgroundFetchDelegateProxy::DidUpdateUI, io_parent_,
job_unique_id));
base::PostTask(FROM_HERE, {BrowserThread::IO},
base::BindOnce(&BackgroundFetchDelegateProxy::DidUpdateUI,
io_parent_, job_unique_id));
}
void BackgroundFetchDelegateProxy::Core::GetUploadData(
......@@ -297,17 +293,16 @@ void BackgroundFetchDelegateProxy::Core::GetUploadData(
base::BindOnce(
[](BackgroundFetchDelegate::GetUploadDataCallback callback,
blink::mojom::SerializedBlobPtr blob) {
base::PostTaskWithTraits(
base::PostTask(
FROM_HERE, {BrowserThread::UI},
base::BindOnce(std::move(callback), std::move(blob)));
},
std::move(callback));
base::PostTaskWithTraits(
FROM_HERE, {BrowserThread::IO},
base::BindOnce(&BackgroundFetchDelegateProxy::GetUploadData, io_parent_,
job_unique_id, download_guid,
std::move(wrapped_callback)));
base::PostTask(FROM_HERE, {BrowserThread::IO},
base::BindOnce(&BackgroundFetchDelegateProxy::GetUploadData,
io_parent_, job_unique_id, download_guid,
std::move(wrapped_callback)));
}
BackgroundFetchDelegateProxy::BackgroundFetchDelegateProxy(
......@@ -336,9 +331,9 @@ void BackgroundFetchDelegateProxy::SetClickEventDispatcher(
void BackgroundFetchDelegateProxy::GetIconDisplaySize(
BackgroundFetchDelegate::GetIconDisplaySizeCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
base::PostTaskWithTraits(FROM_HERE, {BrowserThread::UI},
base::BindOnce(&Core::GetIconDisplaySize,
ui_core_ptr_, std::move(callback)));
base::PostTask(FROM_HERE, {BrowserThread::UI},
base::BindOnce(&Core::GetIconDisplaySize, ui_core_ptr_,
std::move(callback)));
}
void BackgroundFetchDelegateProxy::GetPermissionForOrigin(
......@@ -346,10 +341,9 @@ void BackgroundFetchDelegateProxy::GetPermissionForOrigin(
const WebContents::Getter& wc_getter,
BackgroundFetchDelegate::GetPermissionForOriginCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
base::PostTaskWithTraits(
FROM_HERE, {BrowserThread::UI},
base::BindOnce(&Core::GetPermissionForOrigin, ui_core_ptr_, origin,
wc_getter, std::move(callback)));
base::PostTask(FROM_HERE, {BrowserThread::UI},
base::BindOnce(&Core::GetPermissionForOrigin, ui_core_ptr_,
origin, wc_getter, std::move(callback)));
}
void BackgroundFetchDelegateProxy::CreateDownloadJob(
......@@ -360,10 +354,9 @@ void BackgroundFetchDelegateProxy::CreateDownloadJob(
DCHECK(!controller_map_.count(fetch_description->job_unique_id));
controller_map_[fetch_description->job_unique_id] = std::move(controller);
base::PostTaskWithTraits(
FROM_HERE, {BrowserThread::UI},
base::BindOnce(&Core::CreateDownloadJob, ui_core_ptr_,
std::move(fetch_description)));
base::PostTask(FROM_HERE, {BrowserThread::UI},
base::BindOnce(&Core::CreateDownloadJob, ui_core_ptr_,
std::move(fetch_description)));
}
void BackgroundFetchDelegateProxy::StartRequest(
......@@ -376,9 +369,9 @@ void BackgroundFetchDelegateProxy::StartRequest(
DCHECK(request);
DCHECK(!request->download_guid().empty());
base::PostTaskWithTraits(FROM_HERE, {BrowserThread::UI},
base::BindOnce(&Core::StartRequest, ui_core_ptr_,
job_unique_id, origin, request));
base::PostTask(FROM_HERE, {BrowserThread::UI},
base::BindOnce(&Core::StartRequest, ui_core_ptr_,
job_unique_id, origin, request));
}
void BackgroundFetchDelegateProxy::UpdateUI(
......@@ -392,22 +385,21 @@ void BackgroundFetchDelegateProxy::UpdateUI(
DCHECK(!update_ui_callback_map_.count(job_unique_id));
update_ui_callback_map_.emplace(job_unique_id, std::move(update_ui_callback));
base::PostTaskWithTraits(FROM_HERE, {BrowserThread::UI},
base::BindOnce(&Core::UpdateUI, ui_core_ptr_,
job_unique_id, title, icon));
base::PostTask(FROM_HERE, {BrowserThread::UI},
base::BindOnce(&Core::UpdateUI, ui_core_ptr_, job_unique_id,
title, icon));
}
void BackgroundFetchDelegateProxy::Abort(const std::string& job_unique_id) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
base::PostTaskWithTraits(
FROM_HERE, {BrowserThread::UI},
base::BindOnce(&Core::Abort, ui_core_ptr_, job_unique_id));
base::PostTask(FROM_HERE, {BrowserThread::UI},
base::BindOnce(&Core::Abort, ui_core_ptr_, job_unique_id));
}
void BackgroundFetchDelegateProxy::MarkJobComplete(
const std::string& job_unique_id) {
base::PostTaskWithTraits(
base::PostTask(
FROM_HERE, {BrowserThread::UI},
base::BindOnce(&Core::MarkJobComplete, ui_core_ptr_, job_unique_id));
controller_map_.erase(job_unique_id);
......
......@@ -70,7 +70,7 @@ class FakeBackgroundFetchDelegate : public BackgroundFetchDelegate {
job_id_to_client_[job_unique_id]->OnDownloadStarted(job_unique_id, guid,
std::move(response));
if (complete_downloads_) {
base::PostTaskWithTraits(
base::PostTask(
FROM_HERE, {BrowserThread::IO},
base::BindOnce(&FakeBackgroundFetchDelegate::CompleteDownload,
base::Unretained(this), job_unique_id, guid));
......
......@@ -40,7 +40,7 @@ void BackgroundFetchServiceImpl::CreateForWorker(
const url::Origin& origin) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
base::PostTaskWithTraits(
base::PostTask(
FROM_HERE, {BrowserThread::IO},
base::BindOnce(
BackgroundFetchServiceImpl::CreateOnIoThread,
......@@ -72,7 +72,7 @@ void BackgroundFetchServiceImpl::CreateForFrame(
render_frame_host->GetFrameTreeNodeId());
}
base::PostTaskWithTraits(
base::PostTask(
FROM_HERE, {BrowserThread::IO},
base::BindOnce(
BackgroundFetchServiceImpl::CreateOnIoThread,
......
......@@ -46,9 +46,10 @@ void SerializeIcon(const SkBitmap& icon, SerializeIconCallback callback) {
// Do the serialization on a seperate thread to avoid blocking on
// expensive operations (image conversions), then post back to current
// thread and continue normally.
base::PostTaskWithTraitsAndReplyWithResult(
base::PostTaskAndReplyWithResult(
FROM_HERE,
{base::MayBlock(), base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN,
{base::ThreadPool(), base::MayBlock(),
base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN,
base::TaskPriority::BEST_EFFORT},
base::BindOnce(&ConvertAndSerializeIcon, icon), std::move(callback));
}
......@@ -59,9 +60,10 @@ void DeserializeIcon(std::unique_ptr<std::string> serialized_icon,
// Do the deserialization on a seperate thread to avoid blocking on
// expensive operations (image conversions), then post back to current
// thread and continue normally.
base::PostTaskWithTraitsAndReplyWithResult(
base::PostTaskAndReplyWithResult(
FROM_HERE,
{base::MayBlock(), base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN,
{base::ThreadPool(), base::MayBlock(),
base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN,
base::TaskPriority::BEST_EFFORT},
base::BindOnce(&DeserializeAndConvertIcon, std::move(serialized_icon)),
base::BindOnce(std::move(callback)));
......
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