Commit 1718b889 authored by Tim Volodine's avatar Tim Volodine Committed by Commit Bot

[WebLayer] Add SafeBrowsingService implementation, objects ownership and initialization.

In this patch:
 - Add SafeBrowsingService and its implementation for managing
   initialization and ownership of safebrowsing related objects,
   such as database manager, ui manager and safebrowsing context.
   This also includes proper threading and startup of the database
   manager.
 - Move some functionality and ownership from ContentBrowserClient
   to SafeBrowsingService for better decoupling and less code
   complexity.
 - Add user agent and user data path for safebrowsing network
   context.
 - Make safebrowsing android only for now.
 - Update build files.

BUG=1015809

Change-Id: I8f83c62538bafe81479cb9fe40f065022e1699d0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1888033Reviewed-by: default avatarJohn Abd-El-Malek <jam@chromium.org>
Commit-Queue: Tim Volodine <timvolodine@chromium.org>
Cr-Commit-Position: refs/heads/master@{#711288}
parent cc7ce0ef
...@@ -53,10 +53,6 @@ jumbo_static_library("weblayer_lib") { ...@@ -53,10 +53,6 @@ jumbo_static_library("weblayer_lib") {
"browser/navigation_impl.h", "browser/navigation_impl.h",
"browser/profile_impl.cc", "browser/profile_impl.cc",
"browser/profile_impl.h", "browser/profile_impl.h",
"browser/safe_browsing/safe_browsing_ui_manager.cc",
"browser/safe_browsing/safe_browsing_ui_manager.h",
"browser/safe_browsing/url_checker_delegate_impl.cc",
"browser/safe_browsing/url_checker_delegate_impl.h",
"browser/weblayer_content_browser_overlay_manifest.cc", "browser/weblayer_content_browser_overlay_manifest.cc",
"browser/weblayer_content_browser_overlay_manifest.h", "browser/weblayer_content_browser_overlay_manifest.h",
"browser/webui/web_ui_controller_factory.cc", "browser/webui/web_ui_controller_factory.cc",
...@@ -82,6 +78,18 @@ jumbo_static_library("weblayer_lib") { ...@@ -82,6 +78,18 @@ jumbo_static_library("weblayer_lib") {
"utility/content_utility_client_impl.h", "utility/content_utility_client_impl.h",
] ]
if (is_android) {
# TODO(timvolodine): move this to safe_browsing/
sources += [
"browser/safe_browsing/safe_browsing_service.cc",
"browser/safe_browsing/safe_browsing_service.h",
"browser/safe_browsing/safe_browsing_ui_manager.cc",
"browser/safe_browsing/safe_browsing_ui_manager.h",
"browser/safe_browsing/url_checker_delegate_impl.cc",
"browser/safe_browsing/url_checker_delegate_impl.h",
]
}
configs += [ configs += [
"//build/config:precompiled_headers", "//build/config:precompiled_headers",
...@@ -99,9 +107,6 @@ jumbo_static_library("weblayer_lib") { ...@@ -99,9 +107,6 @@ jumbo_static_library("weblayer_lib") {
"//cc", "//cc",
"//components/crash/content/app", "//components/crash/content/app",
"//components/crash/content/browser", "//components/crash/content/browser",
"//components/safe_browsing",
"//components/safe_browsing/browser",
"//components/safe_browsing/db:database_manager",
"//content:resources", "//content:resources",
"//content/app/resources", "//content/app/resources",
"//content/public/app:both", "//content/public/app:both",
...@@ -145,7 +150,11 @@ jumbo_static_library("weblayer_lib") { ...@@ -145,7 +150,11 @@ jumbo_static_library("weblayer_lib") {
] ]
deps += [ deps += [
"//components/crash/android:crashpad_main", "//components/crash/android:crashpad_main",
"//components/safe_browsing",
"//components/safe_browsing/android:remote_database_manager", "//components/safe_browsing/android:remote_database_manager",
"//components/safe_browsing/browser",
"//components/safe_browsing/browser:network_context",
"//components/safe_browsing/db:database_manager",
"//components/version_info/android:channel_getter", "//components/version_info/android:channel_getter",
] ]
} }
......
...@@ -12,8 +12,6 @@ ...@@ -12,8 +12,6 @@
#include "base/path_service.h" #include "base/path_service.h"
#include "base/stl_util.h" #include "base/stl_util.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "components/safe_browsing/android/remote_database_manager.h"
#include "components/safe_browsing/browser/browser_url_loader_throttle.h"
#include "content/public/browser/browser_context.h" #include "content/public/browser/browser_context.h"
#include "content/public/browser/devtools_manager_delegate.h" #include "content/public/browser/devtools_manager_delegate.h"
#include "content/public/browser/network_service_instance.h" #include "content/public/browser/network_service_instance.h"
...@@ -23,13 +21,12 @@ ...@@ -23,13 +21,12 @@
#include "services/network/network_service.h" #include "services/network/network_service.h"
#include "services/network/public/mojom/network_context.mojom.h" #include "services/network/public/mojom/network_context.mojom.h"
#include "services/network/public/mojom/network_service.mojom.h" #include "services/network/public/mojom/network_service.mojom.h"
#include "third_party/blink/public/common/loader/url_loader_throttle.h"
#include "third_party/blink/public/common/user_agent/user_agent_metadata.h" #include "third_party/blink/public/common/user_agent/user_agent_metadata.h"
#include "url/gurl.h" #include "url/gurl.h"
#include "url/origin.h" #include "url/origin.h"
#include "weblayer/browser/browser_controller_impl.h" #include "weblayer/browser/browser_controller_impl.h"
#include "weblayer/browser/browser_main_parts_impl.h" #include "weblayer/browser/browser_main_parts_impl.h"
#include "weblayer/browser/safe_browsing/safe_browsing_ui_manager.h"
#include "weblayer/browser/safe_browsing/url_checker_delegate_impl.h"
#include "weblayer/browser/weblayer_content_browser_overlay_manifest.h" #include "weblayer/browser/weblayer_content_browser_overlay_manifest.h"
#include "weblayer/common/features.h" #include "weblayer/common/features.h"
#include "weblayer/public/fullscreen_delegate.h" #include "weblayer/public/fullscreen_delegate.h"
...@@ -40,6 +37,7 @@ ...@@ -40,6 +37,7 @@
#include "components/crash/content/browser/crash_handler_host_linux.h" #include "components/crash/content/browser/crash_handler_host_linux.h"
#include "ui/base/resource/resource_bundle_android.h" #include "ui/base/resource/resource_bundle_android.h"
#include "weblayer/browser/android_descriptors.h" #include "weblayer/browser/android_descriptors.h"
#include "weblayer/browser/safe_browsing/safe_browsing_service.h"
#endif #endif
#if defined(OS_LINUX) || defined(OS_ANDROID) #if defined(OS_LINUX) || defined(OS_ANDROID)
...@@ -173,33 +171,22 @@ ContentBrowserClientImpl::CreateURLLoaderThrottles( ...@@ -173,33 +171,22 @@ ContentBrowserClientImpl::CreateURLLoaderThrottles(
if (base::FeatureList::IsEnabled(features::kWebLayerSafeBrowsing) && if (base::FeatureList::IsEnabled(features::kWebLayerSafeBrowsing) &&
IsSafebrowsingSupported()) { IsSafebrowsingSupported()) {
result.push_back(safe_browsing::BrowserURLLoaderThrottle::Create(
base::BindOnce(
[](ContentBrowserClientImpl* client, content::ResourceContext*) {
return client->GetSafeBrowsingUrlCheckerDelegate();
},
base::Unretained(this)),
wc_getter, frame_tree_node_id, browser_context->GetResourceContext()));
}
return result;
}
scoped_refptr<safe_browsing::UrlCheckerDelegate>
ContentBrowserClientImpl::GetSafeBrowsingUrlCheckerDelegate() {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
#if defined(OS_ANDROID) #if defined(OS_ANDROID)
if (!safe_browsing_url_checker_delegate_) { if (!safe_browsing_service_) {
// TODO(timvolodine): consider a better place for the database manager and // TODO(timvolodine): consider creating SafeBrowsingService elsewhere
// the ui manager (also w.r.t. future safebrowsing init sequence). // (especially in multiplatform support).
safe_browsing_url_checker_delegate_ = new UrlCheckerDelegateImpl( // Note: Initialize() needs to happen on UI thread.
new safe_browsing::RemoteSafeBrowsingDatabaseManager(), safe_browsing_service_ =
new SafeBrowsingUIManager()); std::make_unique<SafeBrowsingService>(GetUserAgent());
} safe_browsing_service_->Initialize();
}
result.push_back(safe_browsing_service_->CreateURLLoaderThrottle(
browser_context->GetResourceContext(), wc_getter, frame_tree_node_id));
#endif #endif
}
return safe_browsing_url_checker_delegate_; return result;
} }
#if defined(OS_LINUX) || defined(OS_ANDROID) #if defined(OS_LINUX) || defined(OS_ANDROID)
......
...@@ -15,12 +15,9 @@ ...@@ -15,12 +15,9 @@
#include "content/public/browser/content_browser_client.h" #include "content/public/browser/content_browser_client.h"
#include "services/service_manager/public/cpp/binder_registry.h" #include "services/service_manager/public/cpp/binder_registry.h"
namespace safe_browsing {
class UrlCheckerDelegate;
}
namespace weblayer { namespace weblayer {
class SafeBrowsingService;
struct MainParams; struct MainParams;
class ContentBrowserClientImpl : public content::ContentBrowserClient { class ContentBrowserClientImpl : public content::ContentBrowserClient {
...@@ -63,13 +60,11 @@ class ContentBrowserClientImpl : public content::ContentBrowserClient { ...@@ -63,13 +60,11 @@ class ContentBrowserClientImpl : public content::ContentBrowserClient {
#endif // defined(OS_LINUX) || defined(OS_ANDROID) #endif // defined(OS_LINUX) || defined(OS_ANDROID)
private: private:
scoped_refptr<safe_browsing::UrlCheckerDelegate>
GetSafeBrowsingUrlCheckerDelegate();
MainParams* params_; MainParams* params_;
scoped_refptr<safe_browsing::UrlCheckerDelegate> #if defined(OS_ANDROID)
safe_browsing_url_checker_delegate_; std::unique_ptr<SafeBrowsingService> safe_browsing_service_;
#endif
}; };
} // namespace weblayer } // namespace weblayer
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "weblayer/browser/safe_browsing/safe_browsing_service.h"
#include "base/bind.h"
#include "base/path_service.h"
#include "base/task/post_task.h"
#include "components/safe_browsing/android/remote_database_manager.h"
#include "components/safe_browsing/browser/browser_url_loader_throttle.h"
#include "components/safe_browsing/browser/mojo_safe_browsing_impl.h"
#include "components/safe_browsing/browser/safe_browsing_network_context.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/resource_context.h"
#include "services/network/public/mojom/network_service.mojom.h"
#include "third_party/blink/public/common/loader/url_loader_throttle.h"
#include "weblayer/browser/safe_browsing/url_checker_delegate_impl.h"
namespace weblayer {
namespace {
network::mojom::NetworkContextParamsPtr CreateDefaultNetworkContextParams(
const std::string& user_agent) {
network::mojom::NetworkContextParamsPtr network_context_params =
network::mojom::NetworkContextParams::New();
network_context_params->user_agent = user_agent;
return network_context_params;
}
} // namespace
SafeBrowsingService::SafeBrowsingService(const std::string& user_agent)
: user_agent_(user_agent) {}
SafeBrowsingService::~SafeBrowsingService() {}
void SafeBrowsingService::Initialize() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (network_context_) {
// already initialized
return;
}
base::FilePath user_data_dir;
bool result =
base::PathService::Get(base::DIR_ANDROID_APP_DATA, &user_data_dir);
DCHECK(result);
// safebrowsing network context needs to be created on the UI thread.
network_context_ =
std::make_unique<safe_browsing::SafeBrowsingNetworkContext>(
user_data_dir,
base::BindRepeating(CreateDefaultNetworkContextParams, user_agent_));
CreateSafeBrowsingUIManager();
}
std::unique_ptr<blink::URLLoaderThrottle>
SafeBrowsingService::CreateURLLoaderThrottle(
content::ResourceContext* resource_context,
const base::RepeatingCallback<content::WebContents*()>& wc_getter,
int frame_tree_node_id) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
return safe_browsing::BrowserURLLoaderThrottle::Create(
base::BindOnce(
[](SafeBrowsingService* sb_service, content::ResourceContext*) {
return sb_service->GetSafeBrowsingUrlCheckerDelegate();
},
base::Unretained(this)),
wc_getter, frame_tree_node_id, resource_context);
}
scoped_refptr<safe_browsing::UrlCheckerDelegate>
SafeBrowsingService::GetSafeBrowsingUrlCheckerDelegate() {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
if (!safe_browsing_url_checker_delegate_) {
safe_browsing_url_checker_delegate_ = new UrlCheckerDelegateImpl(
GetSafeBrowsingDBManager(), GetSafeBrowsingUIManager());
}
return safe_browsing_url_checker_delegate_;
}
safe_browsing::RemoteSafeBrowsingDatabaseManager*
SafeBrowsingService::GetSafeBrowsingDBManager() {
if (!safe_browsing_db_manager_) {
CreateAndStartSafeBrowsingDBManager();
}
return safe_browsing_db_manager_.get();
}
SafeBrowsingUIManager* SafeBrowsingService::GetSafeBrowsingUIManager() {
return ui_manager_.get();
}
void SafeBrowsingService::CreateSafeBrowsingUIManager() {
DCHECK(!ui_manager_);
ui_manager_ = new SafeBrowsingUIManager();
}
void SafeBrowsingService::CreateAndStartSafeBrowsingDBManager() {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
DCHECK(!safe_browsing_db_manager_);
safe_browsing_db_manager_ =
new safe_browsing::RemoteSafeBrowsingDatabaseManager();
// V4ProtocolConfig is not used. Just create one with empty values.
safe_browsing::V4ProtocolConfig config("", false, "", "");
safe_browsing_db_manager_->StartOnIOThread(GetURLLoaderFactoryOnIOThread(),
config);
}
scoped_refptr<network::SharedURLLoaderFactory>
SafeBrowsingService::GetURLLoaderFactoryOnIOThread() {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
if (!shared_url_loader_factory_on_io_) {
base::PostTask(
FROM_HERE, {content::BrowserThread::UI},
base::BindOnce(&SafeBrowsingService::CreateURLLoaderFactoryForIO,
base::Unretained(this),
MakeRequest(&url_loader_factory_on_io_)));
shared_url_loader_factory_on_io_ =
base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
url_loader_factory_on_io_.get());
}
return shared_url_loader_factory_on_io_;
}
void SafeBrowsingService::CreateURLLoaderFactoryForIO(
mojo::PendingReceiver<network::mojom::URLLoaderFactory> receiver) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
auto url_loader_factory_params =
network::mojom::URLLoaderFactoryParams::New();
url_loader_factory_params->process_id = network::mojom::kBrowserProcessId;
url_loader_factory_params->is_corb_enabled = false;
network_context_->GetNetworkContext()->CreateURLLoaderFactory(
std::move(receiver), std::move(url_loader_factory_params));
}
} // namespace weblayer
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef WEBLAYER_BROWSER_SAFE_BROWSING_SAFE_BROWSING_SERVICE_H_
#define WEBLAYER_BROWSER_SAFE_BROWSING_SAFE_BROWSING_SERVICE_H_
#include "components/safe_browsing/base_ui_manager.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "weblayer/browser/safe_browsing/safe_browsing_ui_manager.h"
namespace content {
class ResourceContext;
}
namespace blink {
class URLLoaderThrottle;
}
namespace network {
class SharedURLLoaderFactory;
}
namespace safe_browsing {
class UrlCheckerDelegate;
class RemoteSafeBrowsingDatabaseManager;
class SafeBrowsingNetworkContext;
} // namespace safe_browsing
namespace weblayer {
// Class for managing safebrowsing related functionality. In particular this
// class owns both the safebrowsing database and UI managers and provides
// support for initialization and construction of these objects.
class SafeBrowsingService {
public:
SafeBrowsingService(const std::string& user_agent);
~SafeBrowsingService();
// Executed on UI thread
void Initialize();
std::unique_ptr<blink::URLLoaderThrottle> CreateURLLoaderThrottle(
content::ResourceContext* resource_context,
const base::RepeatingCallback<content::WebContents*()>& wc_getter,
int frame_tree_node_id);
private:
SafeBrowsingUIManager* GetSafeBrowsingUIManager();
safe_browsing::RemoteSafeBrowsingDatabaseManager* GetSafeBrowsingDBManager();
// Executed on IO thread
scoped_refptr<safe_browsing::UrlCheckerDelegate>
GetSafeBrowsingUrlCheckerDelegate();
void CreateSafeBrowsingUIManager();
void CreateAndStartSafeBrowsingDBManager();
scoped_refptr<network::SharedURLLoaderFactory>
GetURLLoaderFactoryOnIOThread();
void CreateURLLoaderFactoryForIO(
mojo::PendingReceiver<network::mojom::URLLoaderFactory> receiver);
// The UI manager handles showing interstitials. Accessed on both UI and IO
// thread.
scoped_refptr<SafeBrowsingUIManager> ui_manager_;
// This is what owns the URLRequestContext inside the network service. This is
// used by SimpleURLLoader for Safe Browsing requests.
std::unique_ptr<safe_browsing::SafeBrowsingNetworkContext> network_context_;
// Accessed on IO thread only.
scoped_refptr<safe_browsing::RemoteSafeBrowsingDatabaseManager>
safe_browsing_db_manager_;
// A SharedURLLoaderFactory and its interfaceptr used on the IO thread.
network::mojom::URLLoaderFactoryPtr url_loader_factory_on_io_;
scoped_refptr<network::WeakWrapperSharedURLLoaderFactory>
shared_url_loader_factory_on_io_;
scoped_refptr<safe_browsing::UrlCheckerDelegate>
safe_browsing_url_checker_delegate_;
std::string user_agent_;
DISALLOW_COPY_AND_ASSIGN(SafeBrowsingService);
};
} // namespace weblayer
#endif // WEBLAYER_BROWSER_SAFE_BROWSING_SAFE_BROWSING_SERVICE_H_
\ No newline at end of file
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