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

chrome/browser/safe_browsing/download_protection: 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=nparker@chromium.org

Bug: 968047
Change-Id: I11dd0befca765ffa4b25c8df66bd7557621b2b08
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1729223
Auto-Submit: Sami Kyöstilä <skyostil@chromium.org>
Reviewed-by: default avatarNathan Parker <nparker@chromium.org>
Commit-Queue: Sami Kyöstilä <skyostil@chromium.org>
Cr-Commit-Position: refs/heads/master@{#682860}
parent 73bd0c83
...@@ -200,7 +200,7 @@ void CheckClientDownloadRequest::Start() { ...@@ -200,7 +200,7 @@ void CheckClientDownloadRequest::Start() {
// If whitelist check passes, FinishRequest() will be called to avoid // If whitelist check passes, FinishRequest() will be called to avoid
// analyzing file. Otherwise, AnalyzeFile() will be called to continue with // analyzing file. Otherwise, AnalyzeFile() will be called to continue with
// analysis. // analysis.
base::PostTaskWithTraitsAndReplyWithResult( base::PostTaskAndReplyWithResult(
FROM_HERE, {BrowserThread::IO}, FROM_HERE, {BrowserThread::IO},
base::BindOnce(&CheckUrlAgainstWhitelist, url_chain_.back(), base::BindOnce(&CheckUrlAgainstWhitelist, url_chain_.back(),
database_manager_), database_manager_),
...@@ -213,7 +213,7 @@ void CheckClientDownloadRequest::Start() { ...@@ -213,7 +213,7 @@ void CheckClientDownloadRequest::Start() {
void CheckClientDownloadRequest::StartTimeout() { void CheckClientDownloadRequest::StartTimeout() {
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
timeout_start_time_ = base::TimeTicks::Now(); timeout_start_time_ = base::TimeTicks::Now();
base::PostDelayedTaskWithTraits( base::PostDelayedTask(
FROM_HERE, {BrowserThread::UI}, FROM_HERE, {BrowserThread::UI},
base::BindOnce(&CheckClientDownloadRequest::FinishRequest, base::BindOnce(&CheckClientDownloadRequest::FinishRequest,
weakptr_factory_.GetWeakPtr(), weakptr_factory_.GetWeakPtr(),
...@@ -295,7 +295,7 @@ void CheckClientDownloadRequest::OnURLLoaderComplete( ...@@ -295,7 +295,7 @@ void CheckClientDownloadRequest::OnURLLoaderComplete(
} }
} }
base::PostTaskWithTraits( base::PostTask(
FROM_HERE, {content::BrowserThread::UI}, FROM_HERE, {content::BrowserThread::UI},
base::BindOnce( base::BindOnce(
&WebUIInfoSingleton::AddToClientDownloadResponsesReceived, &WebUIInfoSingleton::AddToClientDownloadResponsesReceived,
...@@ -458,7 +458,7 @@ void CheckClientDownloadRequest::OnFileFeatureExtractionDone( ...@@ -458,7 +458,7 @@ void CheckClientDownloadRequest::OnFileFeatureExtractionDone(
detached_code_signatures_.CopyFrom(results.detached_code_signatures); detached_code_signatures_.CopyFrom(results.detached_code_signatures);
#endif #endif
base::PostTaskWithTraitsAndReplyWithResult( base::PostTaskAndReplyWithResult(
FROM_HERE, {BrowserThread::IO}, FROM_HERE, {BrowserThread::IO},
base::BindOnce(&CheckCertificateChainAgainstWhitelist, signature_info_, base::BindOnce(&CheckCertificateChainAgainstWhitelist, signature_info_,
database_manager_), database_manager_),
...@@ -469,8 +469,7 @@ void CheckClientDownloadRequest::OnFileFeatureExtractionDone( ...@@ -469,8 +469,7 @@ void CheckClientDownloadRequest::OnFileFeatureExtractionDone(
// We wait until after the file checks finish to start the timeout, as // We wait until after the file checks finish to start the timeout, as
// windows can cause permissions errors if the timeout fired while we were // windows can cause permissions errors if the timeout fired while we were
// checking the file signature and we tried to complete the download. // checking the file signature and we tried to complete the download.
base::PostTaskWithTraits( base::PostTask(FROM_HERE, {BrowserThread::UI},
FROM_HERE, {BrowserThread::UI},
base::BindOnce(&CheckClientDownloadRequest::StartTimeout, base::BindOnce(&CheckClientDownloadRequest::StartTimeout,
weakptr_factory_.GetWeakPtr())); weakptr_factory_.GetWeakPtr()));
} }
...@@ -789,7 +788,7 @@ void CheckClientDownloadRequest::SendRequest() { ...@@ -789,7 +788,7 @@ void CheckClientDownloadRequest::SendRequest() {
// The following is to log this ClientDownloadRequest on any open // The following is to log this ClientDownloadRequest on any open
// chrome://safe-browsing pages. If no such page is open, the request is // chrome://safe-browsing pages. If no such page is open, the request is
// dropped and the |request| object deleted. // dropped and the |request| object deleted.
base::PostTaskWithTraits( base::PostTask(
FROM_HERE, {content::BrowserThread::UI}, FROM_HERE, {content::BrowserThread::UI},
base::BindOnce(&WebUIInfoSingleton::AddToClientDownloadRequestsSent, base::BindOnce(&WebUIInfoSingleton::AddToClientDownloadRequestsSent,
base::Unretained(WebUIInfoSingleton::GetInstance()), base::Unretained(WebUIInfoSingleton::GetInstance()),
......
...@@ -122,8 +122,9 @@ bool WillStorePings(DownloadCheckResult result, ...@@ -122,8 +122,9 @@ bool WillStorePings(DownloadCheckResult result,
class DownloadFeedbackServiceTest : public testing::Test { class DownloadFeedbackServiceTest : public testing::Test {
public: public:
DownloadFeedbackServiceTest() DownloadFeedbackServiceTest()
: file_task_runner_(base::CreateSequencedTaskRunnerWithTraits( : file_task_runner_(base::CreateSequencedTaskRunner(
{base::MayBlock(), base::TaskPriority::BEST_EFFORT})) {} {base::ThreadPool(), base::MayBlock(),
base::TaskPriority::BEST_EFFORT})) {}
void SetUp() override { void SetUp() override {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
......
...@@ -97,10 +97,11 @@ std::unique_ptr<TwoPhaseUploader> FakeUploaderFactory::CreateTwoPhaseUploader( ...@@ -97,10 +97,11 @@ std::unique_ptr<TwoPhaseUploader> FakeUploaderFactory::CreateTwoPhaseUploader(
class DownloadFeedbackTest : public testing::Test { class DownloadFeedbackTest : public testing::Test {
public: public:
DownloadFeedbackTest() DownloadFeedbackTest()
: file_task_runner_(base::CreateSequencedTaskRunnerWithTraits( : file_task_runner_(base::CreateSequencedTaskRunner(
{base::MayBlock(), base::TaskPriority::BEST_EFFORT})), {base::ThreadPool(), base::MayBlock(),
io_task_runner_(base::CreateSingleThreadTaskRunnerWithTraits( base::TaskPriority::BEST_EFFORT})),
{content::BrowserThread::IO})), io_task_runner_(
base::CreateSingleThreadTaskRunner({content::BrowserThread::IO})),
feedback_finish_called_(false) { feedback_finish_called_(false) {
EXPECT_NE(io_task_runner_, file_task_runner_); EXPECT_NE(io_task_runner_, file_task_runner_);
shared_url_loader_factory_ = shared_url_loader_factory_ =
......
...@@ -110,8 +110,8 @@ DownloadProtectionService::DownloadProtectionService( ...@@ -110,8 +110,8 @@ DownloadProtectionService::DownloadProtectionService(
download_request_timeout_ms_(kDownloadRequestTimeoutMs), download_request_timeout_ms_(kDownloadRequestTimeoutMs),
feedback_service_(new DownloadFeedbackService( feedback_service_(new DownloadFeedbackService(
url_loader_factory_, url_loader_factory_,
base::CreateSequencedTaskRunnerWithTraits( base::CreateSequencedTaskRunner({base::ThreadPool(), base::MayBlock(),
{base::MayBlock(), base::TaskPriority::BEST_EFFORT}) base::TaskPriority::BEST_EFFORT})
.get())), .get())),
whitelist_sample_rate_(kWhitelistDownloadSampleRate) { whitelist_sample_rate_(kWhitelistDownloadSampleRate) {
if (sb_service) { if (sb_service) {
...@@ -197,8 +197,7 @@ void DownloadProtectionService::CheckDownloadUrl( ...@@ -197,8 +197,7 @@ void DownloadProtectionService::CheckDownloadUrl(
scoped_refptr<DownloadUrlSBClient> client(new DownloadUrlSBClient( scoped_refptr<DownloadUrlSBClient> client(new DownloadUrlSBClient(
item, this, callback, ui_manager_, database_manager_)); item, this, callback, ui_manager_, database_manager_));
// The client will release itself once it is done. // The client will release itself once it is done.
base::PostTaskWithTraits( base::PostTask(FROM_HERE, {BrowserThread::IO},
FROM_HERE, {BrowserThread::IO},
base::BindOnce(&DownloadUrlSBClient::StartCheck, client)); base::BindOnce(&DownloadUrlSBClient::StartCheck, client));
} }
......
...@@ -201,7 +201,7 @@ ACTION_P(TrustSignature, contents) { ...@@ -201,7 +201,7 @@ ACTION_P(TrustSignature, contents) {
} }
ACTION_P(CheckDownloadUrlDone, threat_type) { ACTION_P(CheckDownloadUrlDone, threat_type) {
base::PostTaskWithTraits( base::PostTask(
FROM_HERE, {BrowserThread::IO}, FROM_HERE, {BrowserThread::IO},
base::BindOnce( base::BindOnce(
&SafeBrowsingDatabaseManager::Client::OnCheckDownloadUrlResult, &SafeBrowsingDatabaseManager::Client::OnCheckDownloadUrlResult,
...@@ -456,12 +456,12 @@ class DownloadProtectionServiceTest : public ChromeRenderViewHostTestHarness { ...@@ -456,12 +456,12 @@ class DownloadProtectionServiceTest : public ChromeRenderViewHostTestHarness {
// Helper functions for FlushThreadMessageLoops. // Helper functions for FlushThreadMessageLoops.
void RunAllPendingAndQuitUI(const base::Closure& quit_closure) { void RunAllPendingAndQuitUI(const base::Closure& quit_closure) {
RunLoop().RunUntilIdle(); RunLoop().RunUntilIdle();
base::PostTaskWithTraits(FROM_HERE, {BrowserThread::UI}, quit_closure); base::PostTask(FROM_HERE, {BrowserThread::UI}, quit_closure);
} }
void PostRunMessageLoopTask(BrowserThread::ID thread, void PostRunMessageLoopTask(BrowserThread::ID thread,
const base::Closure& quit_closure) { const base::Closure& quit_closure) {
base::PostTaskWithTraits( base::PostTask(
FROM_HERE, {thread}, FROM_HERE, {thread},
base::BindOnce(&DownloadProtectionServiceTest::RunAllPendingAndQuitUI, base::BindOnce(&DownloadProtectionServiceTest::RunAllPendingAndQuitUI,
base::Unretained(this), quit_closure)); base::Unretained(this), quit_closure));
...@@ -469,7 +469,7 @@ class DownloadProtectionServiceTest : public ChromeRenderViewHostTestHarness { ...@@ -469,7 +469,7 @@ class DownloadProtectionServiceTest : public ChromeRenderViewHostTestHarness {
void FlushMessageLoop(BrowserThread::ID thread) { void FlushMessageLoop(BrowserThread::ID thread) {
RunLoop run_loop; RunLoop run_loop;
base::PostTaskWithTraits( base::PostTask(
FROM_HERE, {BrowserThread::UI}, FROM_HERE, {BrowserThread::UI},
base::BindOnce(&DownloadProtectionServiceTest::PostRunMessageLoopTask, base::BindOnce(&DownloadProtectionServiceTest::PostRunMessageLoopTask,
base::Unretained(this), thread, run_loop.QuitClosure())); base::Unretained(this), thread, run_loop.QuitClosure()));
......
...@@ -91,17 +91,17 @@ void DownloadUrlSBClient::CheckDone(SBThreatType threat_type) { ...@@ -91,17 +91,17 @@ void DownloadUrlSBClient::CheckDone(SBThreatType threat_type) {
UpdateDownloadCheckStats(total_type_); UpdateDownloadCheckStats(total_type_);
if (threat_type != SB_THREAT_TYPE_SAFE) { if (threat_type != SB_THREAT_TYPE_SAFE) {
UpdateDownloadCheckStats(dangerous_type_); UpdateDownloadCheckStats(dangerous_type_);
base::PostTaskWithTraits( base::PostTask(
FROM_HERE, {BrowserThread::UI}, FROM_HERE, {BrowserThread::UI},
base::BindOnce(&DownloadUrlSBClient::ReportMalware, this, threat_type)); base::BindOnce(&DownloadUrlSBClient::ReportMalware, this, threat_type));
} else { } else {
// Identify download referrer chain, which will be used in // Identify download referrer chain, which will be used in
// ClientDownloadRequest. // ClientDownloadRequest.
base::PostTaskWithTraits( base::PostTask(
FROM_HERE, {BrowserThread::UI}, FROM_HERE, {BrowserThread::UI},
base::BindOnce(&DownloadUrlSBClient::IdentifyReferrerChain, this)); base::BindOnce(&DownloadUrlSBClient::IdentifyReferrerChain, this));
} }
base::PostTaskWithTraits(FROM_HERE, {BrowserThread::UI}, base::PostTask(FROM_HERE, {BrowserThread::UI},
base::BindOnce(callback_, result)); base::BindOnce(callback_, result));
} }
......
...@@ -107,8 +107,10 @@ void FileAnalyzer::Start(const base::FilePath& target_path, ...@@ -107,8 +107,10 @@ void FileAnalyzer::Start(const base::FilePath& target_path,
// Checks for existence of "koly" signature even if file doesn't have // Checks for existence of "koly" signature even if file doesn't have
// archive-type extension, then calls ExtractFileOrDmgFeatures() with // archive-type extension, then calls ExtractFileOrDmgFeatures() with
// result. // result.
base::PostTaskWithTraitsAndReplyWithResult( base::PostTaskAndReplyWithResult(
FROM_HERE, {base::MayBlock(), base::TaskPriority::USER_VISIBLE}, FROM_HERE,
{base::ThreadPool(), base::MayBlock(),
base::TaskPriority::USER_VISIBLE},
base::BindOnce(DiskImageTypeSnifferMac::IsAppleDiskImage, tmp_path_), base::BindOnce(DiskImageTypeSnifferMac::IsAppleDiskImage, tmp_path_),
base::BindOnce(&FileAnalyzer::ExtractFileOrDmgFeatures, base::BindOnce(&FileAnalyzer::ExtractFileOrDmgFeatures,
weakptr_factory_.GetWeakPtr())); weakptr_factory_.GetWeakPtr()));
...@@ -121,9 +123,9 @@ void FileAnalyzer::Start(const base::FilePath& target_path, ...@@ -121,9 +123,9 @@ void FileAnalyzer::Start(const base::FilePath& target_path,
void FileAnalyzer::StartExtractFileFeatures() { void FileAnalyzer::StartExtractFileFeatures() {
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
base::PostTaskWithTraitsAndReplyWithResult( base::PostTaskAndReplyWithResult(
FROM_HERE, FROM_HERE,
{base::MayBlock(), base::TaskPriority::USER_VISIBLE, {base::ThreadPool(), base::MayBlock(), base::TaskPriority::USER_VISIBLE,
base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN}, base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
base::BindOnce(&ExtractFileFeatures, binary_feature_extractor_, base::BindOnce(&ExtractFileFeatures, binary_feature_extractor_,
tmp_path_), tmp_path_),
......
...@@ -113,8 +113,7 @@ void MultipartUploadRequest::RetryOrFinish( ...@@ -113,8 +113,7 @@ void MultipartUploadRequest::RetryOrFinish(
std::move(callback_).Run(net::OK, net::HTTP_OK, *response_body.get()); std::move(callback_).Run(net::OK, net::HTTP_OK, *response_body.get());
} else { } else {
if (retry_count_ < kMaxRetryAttempts) { if (retry_count_ < kMaxRetryAttempts) {
base::PostDelayedTaskWithTraits( base::PostDelayedTask(FROM_HERE, {content::BrowserThread::UI},
FROM_HERE, {content::BrowserThread::UI},
base::BindOnce(&MultipartUploadRequest::SendRequest, base::BindOnce(&MultipartUploadRequest::SendRequest,
weak_factory_.GetWeakPtr()), weak_factory_.GetWeakPtr()),
current_backoff_); current_backoff_);
......
...@@ -107,14 +107,13 @@ void PPAPIDownloadRequest::Start() { ...@@ -107,14 +107,13 @@ void PPAPIDownloadRequest::Start() {
// verdict. The weak pointer used for the timeout will be invalidated (and // verdict. The weak pointer used for the timeout will be invalidated (and
// hence would prevent the timeout) if the check completes on time and // hence would prevent the timeout) if the check completes on time and
// execution reaches Finish(). // execution reaches Finish().
base::PostDelayedTaskWithTraits( base::PostDelayedTask(FROM_HERE, {BrowserThread::UI},
FROM_HERE, {BrowserThread::UI},
base::BindOnce(&PPAPIDownloadRequest::OnRequestTimedOut, base::BindOnce(&PPAPIDownloadRequest::OnRequestTimedOut,
weakptr_factory_.GetWeakPtr()), weakptr_factory_.GetWeakPtr()),
base::TimeDelta::FromMilliseconds( base::TimeDelta::FromMilliseconds(
service_->download_request_timeout_ms())); service_->download_request_timeout_ms()));
base::PostTaskWithTraits( base::PostTask(
FROM_HERE, {BrowserThread::IO}, FROM_HERE, {BrowserThread::IO},
base::BindOnce(&PPAPIDownloadRequest::CheckWhitelistsOnIOThread, base::BindOnce(&PPAPIDownloadRequest::CheckWhitelistsOnIOThread,
requestor_url_, database_manager_, requestor_url_, database_manager_,
...@@ -142,8 +141,7 @@ void PPAPIDownloadRequest::CheckWhitelistsOnIOThread( ...@@ -142,8 +141,7 @@ void PPAPIDownloadRequest::CheckWhitelistsOnIOThread(
bool url_was_whitelisted = bool url_was_whitelisted =
requestor_url.is_valid() && database_manager && requestor_url.is_valid() && database_manager &&
database_manager->MatchDownloadWhitelistUrl(requestor_url); database_manager->MatchDownloadWhitelistUrl(requestor_url);
base::PostTaskWithTraits( base::PostTask(FROM_HERE, {BrowserThread::UI},
FROM_HERE, {BrowserThread::UI},
base::BindOnce(&PPAPIDownloadRequest::WhitelistCheckComplete, base::BindOnce(&PPAPIDownloadRequest::WhitelistCheckComplete,
download_request, url_was_whitelisted)); download_request, url_was_whitelisted));
} }
......
...@@ -91,8 +91,8 @@ class TwoPhaseUploaderTest : public testing::Test { ...@@ -91,8 +91,8 @@ class TwoPhaseUploaderTest : public testing::Test {
protected: protected:
content::TestBrowserThreadBundle thread_bundle_; content::TestBrowserThreadBundle thread_bundle_;
const scoped_refptr<base::SequencedTaskRunner> task_runner_ = const scoped_refptr<base::SequencedTaskRunner> task_runner_ =
base::CreateSequencedTaskRunnerWithTraits( base::CreateSequencedTaskRunner({base::ThreadPool(), base::MayBlock(),
{base::MayBlock(), base::TaskPriority::BEST_EFFORT}); base::TaskPriority::BEST_EFFORT});
std::unique_ptr<network::TestNetworkServiceClient> network_service_client_; std::unique_ptr<network::TestNetworkServiceClient> network_service_client_;
scoped_refptr<network::TestSharedURLLoaderFactory> shared_url_loader_factory_; scoped_refptr<network::TestSharedURLLoaderFactory> shared_url_loader_factory_;
}; };
......
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