Commit 10d8c213 authored by Colin Blundell's avatar Colin Blundell Committed by Commit Bot

[WebLayer] Enable SafeBrowsingDBManager to be created/retrieved on UI

In //weblayer, SafeBrowsingDBManager currently can can be accessed only
from the IO thread. However, the subresource filter component needs to
take in a scoped_refptr to the embedder's SafeBrowsingDBManager on the
UI thread as part of creating a navigation throttle. This instance is
then internally passed to and accessed on the IO thread.

In //chrome, the ServicesDelegate class creates the
SafeBrowsingDBManager on the UI thread and vends a reference to it
on any thread. This CL takes an analogous approach in //weblayer,
allowing the method that creates the SafeBrowsingDBManager to be
called on the UI thread, with the DBManager being *started* on the IO
thread as it is now (via a posted task if the current thread is not the
IO thread). We add an IO-thread safeguard to  ensure that the DBManager
is only started once.

The precise structure here is not the same as that in //chrome's
SafeBrowsingService, as the underlying structure is not the same:
//chrome's SafeBrowsingService has an explicit Start() method from
which it posts a task to start the SafeBrowsingDBManager on the IO
thread. //weblayer's SafeBrowsingService has no such method and
instead the SafeBrowsingDBManager is created/started on demand.

Bug: 1116095
Change-Id: If6303ee0ec36ec75109eb3917dd5cea3946d98bc
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2478950
Commit-Queue: Colin Blundell <blundell@chromium.org>
Reviewed-by: default avatarTim Volodine <timvolodine@chromium.org>
Cr-Commit-Position: refs/heads/master@{#818850}
parent 772dfcc1
......@@ -160,12 +160,35 @@ void SafeBrowsingService::CreateSafeBrowsingUIManager() {
}
void SafeBrowsingService::CreateAndStartSafeBrowsingDBManager() {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
DCHECK(!safe_browsing_db_manager_);
safe_browsing_db_manager_ =
new safe_browsing::RemoteSafeBrowsingDatabaseManager();
if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)) {
// Posting a task to start the DB here ensures that it will be started by
// the time that a consumer uses it on the IO thread, as such a consumer
// would need to make it available for usage on the IO thread via a
// PostTask() that will be ordered after this one.
content::GetIOThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(
&SafeBrowsingService::StartSafeBrowsingDBManagerOnIOThread,
base::Unretained(this)));
} else {
StartSafeBrowsingDBManagerOnIOThread();
}
}
void SafeBrowsingService::StartSafeBrowsingDBManagerOnIOThread() {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
DCHECK(safe_browsing_db_manager_);
if (started_db_manager_)
return;
started_db_manager_ = true;
// V4ProtocolConfig is not used. Just create one with empty values.
safe_browsing::V4ProtocolConfig config("", false, "", "");
safe_browsing_db_manager_->StartOnIOThread(GetURLLoaderFactoryOnIOThread(),
......@@ -225,6 +248,7 @@ void SafeBrowsingService::StopDBManagerOnIOThread() {
if (safe_browsing_db_manager_) {
safe_browsing_db_manager_->StopOnIOThread(true /*shutdown*/);
safe_browsing_db_manager_.reset();
started_db_manager_ = false;
}
}
......
......@@ -65,14 +65,19 @@ class SafeBrowsingService {
scoped_refptr<network::SharedURLLoaderFactory> GetURLLoaderFactory();
// May be called on the UI or IO thread. The instance returned should be
// *accessed* only on the IO thread.
safe_browsing::RemoteSafeBrowsingDatabaseManager* GetSafeBrowsingDBManager();
private:
SafeBrowsingUIManager* GetSafeBrowsingUIManager();
safe_browsing::RemoteSafeBrowsingDatabaseManager* GetSafeBrowsingDBManager();
// Executed on IO thread
scoped_refptr<safe_browsing::UrlCheckerDelegate>
GetSafeBrowsingUrlCheckerDelegate();
// Safe to call multiple times; invocations after the first will be no-ops.
void StartSafeBrowsingDBManagerOnIOThread();
void CreateSafeBrowsingUIManager();
void CreateAndStartSafeBrowsingDBManager();
scoped_refptr<network::SharedURLLoaderFactory>
......@@ -89,7 +94,9 @@ class SafeBrowsingService {
// is used by SimpleURLLoader for Safe Browsing requests.
std::unique_ptr<safe_browsing::SafeBrowsingNetworkContext> network_context_;
// Accessed on IO thread only.
// May be created on UI thread and have references obtained to it on that
// thread for later passing to the IO thread, but should be *accessed* only
// on the IO thread.
scoped_refptr<safe_browsing::RemoteSafeBrowsingDatabaseManager>
safe_browsing_db_manager_;
......@@ -105,6 +112,10 @@ class SafeBrowsingService {
std::string user_agent_;
// Whether |safe_browsing_db_manager_| has been started. Accessed only on the
// IO thread.
bool started_db_manager_ = false;
DISALLOW_COPY_AND_ASSIGN(SafeBrowsingService);
};
......
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