Commit f250abdd authored by edchin's avatar edchin Committed by Chromium LUCI CQ

[ios][PhishGuard] Make cross-platform GetTaskRunner()

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: default avatarVarun Khaneja <vakh@chromium.org>
Reviewed-by: default avatarAli Juma <ajuma@chromium.org>
Reviewed-by: default avatarBettina Dea <bdea@chromium.org>
Cr-Commit-Position: refs/heads/master@{#833082}
parent 8eacad37
......@@ -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"
......@@ -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
......@@ -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"
......@@ -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)));
......@@ -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) {
......@@ -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();
}
......@@ -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(
......
......@@ -27,8 +27,6 @@
#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"
......@@ -36,7 +34,6 @@
#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;
......@@ -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(
......
......@@ -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 {
......@@ -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_
......@@ -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"
......@@ -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
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