Skip to content

Commit

Permalink
[ios][PhishGuard] Make cross-platform GetTaskRunner()
Browse files Browse the repository at this point in the history
This CL adds utility functions that provide a cross-platform interface
for obtaining the task runner for the IO and UI threads.

Note that base::CreateSingleThreadTaskRunner() is an older approach
to obtaining the task runners for the IO and UI threads. But it is
still used in iOS.

Bug: 1147967
Change-Id: I2d16613864f5a5b927157b5a26377d7513032905
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2559793
Commit-Queue: Varun Khaneja <vakh@chromium.org>
Reviewed-by: Varun Khaneja <vakh@chromium.org>
Reviewed-by: Ali Juma <ajuma@chromium.org>
Reviewed-by: Bettina Dea <bdea@chromium.org>
Cr-Commit-Position: refs/heads/master@{#833082}
  • Loading branch information
edx246 authored and Chromium LUCI CQ committed Dec 3, 2020
1 parent 8eacad3 commit f250abd
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 24 deletions.
13 changes: 13 additions & 0 deletions components/safe_browsing/content/common/thread_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "components/safe_browsing/core/common/thread_utils.h"

#include "base/notreached.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"

Expand Down Expand Up @@ -35,4 +36,16 @@ base::TaskTraits CreateTaskTraits(ThreadID thread_id) {
return {BrowserThreadID(thread_id)};
}

scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner(ThreadID thread_id) {
switch (thread_id) {
case ThreadID::UI:
return content::GetUIThreadTaskRunner({});
case ThreadID::IO:
return content::GetIOThreadTaskRunner({});
default:
NOTREACHED();
return content::GetUIThreadTaskRunner({});
}
}

} // namespace safe_browsing
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include "components/safe_browsing/core/db/database_manager.h"
#include "components/safe_browsing/core/features.h"
#include "components/url_formatter/url_formatter.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/navigation_throttle.h"
#include "net/base/escape.h"
#include "net/base/load_flags.h"
Expand Down Expand Up @@ -171,7 +170,7 @@ void PasswordProtectionRequest::CheckWhitelist() {
auto result_callback =
base::BindOnce(&OnWhitelistCheckDoneOnIO, GetWeakPtr());
tracker_.PostTask(
content::GetIOThreadTaskRunner({}).get(), FROM_HERE,
GetTaskRunner(ThreadID::IO).get(), FROM_HERE,
base::BindOnce(&AllowlistCheckerClient::StartCheckCsdWhitelist,
password_protection_service_->database_manager(),
main_frame_url_, std::move(result_callback)));
Expand All @@ -182,10 +181,11 @@ void PasswordProtectionRequest::OnWhitelistCheckDoneOnIO(
base::WeakPtr<PasswordProtectionRequest> weak_request,
bool match_whitelist) {
// Don't access weak_request on IO thread. Move it back to UI thread first.
content::GetUIThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(&PasswordProtectionRequest::OnWhitelistCheckDone,
weak_request, match_whitelist));
GetTaskRunner(ThreadID::UI)
->PostTask(
FROM_HERE,
base::BindOnce(&PasswordProtectionRequest::OnWhitelistCheckDone,
weak_request, match_whitelist));
}

void PasswordProtectionRequest::OnWhitelistCheckDone(bool match_whitelist) {
Expand Down Expand Up @@ -350,11 +350,12 @@ void PasswordProtectionRequest::GetDomFeatures() {
main_frame_url_,
base::BindRepeating(&PasswordProtectionRequest::OnGetDomFeatures,
GetWeakPtr()));
content::GetUIThreadTaskRunner({})->PostDelayedTask(
FROM_HERE,
base::BindOnce(&PasswordProtectionRequest::OnGetDomFeatureTimeout,
GetWeakPtr()),
base::TimeDelta::FromMilliseconds(kDomFeatureTimeoutMs));
GetTaskRunner(ThreadID::UI)
->PostDelayedTask(
FROM_HERE,
base::BindOnce(&PasswordProtectionRequest::OnGetDomFeatureTimeout,
GetWeakPtr()),
base::TimeDelta::FromMilliseconds(kDomFeatureTimeoutMs));
dom_feature_start_time_ = base::TimeTicks::Now();
}

Expand Down Expand Up @@ -540,10 +541,12 @@ void PasswordProtectionRequest::StartTimeout() {
// The weak pointer used for the timeout will be invalidated (and
// hence would prevent the timeout) if the check completes on time and
// execution reaches Finish().
content::GetUIThreadTaskRunner({})->PostDelayedTask(
FROM_HERE,
base::BindOnce(&PasswordProtectionRequest::Cancel, GetWeakPtr(), true),
base::TimeDelta::FromMilliseconds(request_timeout_in_ms_));
GetTaskRunner(ThreadID::UI)
->PostDelayedTask(
FROM_HERE,
base::BindOnce(&PasswordProtectionRequest::Cancel, GetWeakPtr(),
true),
base::TimeDelta::FromMilliseconds(request_timeout_in_ms_));
}

void PasswordProtectionRequest::OnURLLoaderComplete(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,13 @@
#include "components/safe_browsing/core/db/database_manager.h"
#include "components/safe_browsing/core/features.h"
#include "components/zoom/zoom_controller.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/web_contents.h"
#include "google_apis/google_api_keys.h"
#include "net/base/escape.h"
#include "net/base/url_util.h"
#include "third_party/blink/public/common/page/page_zoom.h"

using content::BrowserThread;
using content::WebContents;
using history::HistoryService;
using password_manager::metrics_util::PasswordType;
Expand Down Expand Up @@ -386,12 +383,13 @@ void PasswordProtectionService::FillUserPopulation(
void PasswordProtectionService::OnURLsDeleted(
history::HistoryService* history_service,
const history::DeletionInfo& deletion_info) {
content::GetUIThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindRepeating(&PasswordProtectionService::
RemoveUnhandledSyncPasswordReuseOnURLsDeleted,
GetWeakPtr(), deletion_info.IsAllHistory(),
deletion_info.deleted_rows()));
GetTaskRunner(ThreadID::UI)
->PostTask(
FROM_HERE,
base::BindRepeating(&PasswordProtectionService::
RemoveUnhandledSyncPasswordReuseOnURLsDeleted,
GetWeakPtr(), deletion_info.IsAllHistory(),
deletion_info.deleted_rows()));
}

void PasswordProtectionService::HistoryServiceBeingDeleted(
Expand Down
6 changes: 6 additions & 0 deletions components/safe_browsing/core/common/thread_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#ifndef COMPONENTS_SAFE_BROWSING_CORE_COMMON_THREAD_UTILS_H_
#define COMPONENTS_SAFE_BROWSING_CORE_COMMON_THREAD_UTILS_H_

#include "base/memory/scoped_refptr.h"
#include "base/single_thread_task_runner.h"
#include "base/task/task_traits.h"

namespace safe_browsing {
Expand All @@ -31,6 +33,10 @@ bool CurrentlyOnThread(ThreadID thread_id) WARN_UNUSED_RESULT;
// thread.
base::TaskTraits CreateTaskTraits(ThreadID thread_id);

// Callable on any thread. Returns the task runner associated with the given
// identifier.
scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner(ThreadID thread_id);

} // namespace safe_browsing

#endif // COMPONENTS_SAFE_BROWSING_CORE_COMMON_THREAD_UTILS_H_
6 changes: 6 additions & 0 deletions components/safe_browsing/ios/thread_utils_ios.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include "components/safe_browsing/core/common/thread_utils.h"

#include "base/notreached.h"
#include "base/task/post_task.h"
#include "ios/web/public/thread/web_task_traits.h"
#include "ios/web/public/thread/web_thread.h"

Expand Down Expand Up @@ -31,4 +33,8 @@ base::TaskTraits CreateTaskTraits(ThreadID thread_id) {
return {WebThreadID(thread_id)};
}

scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner(ThreadID thread_id) {
return base::CreateSingleThreadTaskRunner(CreateTaskTraits(thread_id));
}

} // namespace safe_browsing

0 comments on commit f250abd

Please sign in to comment.