Commit f4fe4f64 authored by Tim Volodine's avatar Tim Volodine Committed by Commit Bot

[WebLayer] Create and init pref service for safebrowsing to work.

Currently safebrowsing is crashing due to the lack
of pref service in weblayer (see crbug.com/1025257).
This patch creates and initializes the pref service to
avoid this crash.

BUG=1015418,1025257
TBR=gab@chromium.org

Change-Id: I11e2ab3bc6cebbd973ad816152dfc27a090c5124
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1926321
Commit-Queue: Tim Volodine <timvolodine@chromium.org>
Reviewed-by: default avatarRichard Coles <torne@chromium.org>
Cr-Commit-Position: refs/heads/master@{#717770}
parent 3319d4ae
......@@ -278,12 +278,14 @@ jumbo_static_library("weblayer_lib") {
"//android_webview:generate_aw_resources",
"//android_webview:generate_aw_strings",
"//components/crash/android:crashpad_main",
"//components/prefs",
"//components/safe_browsing",
"//components/safe_browsing/android:remote_database_manager",
"//components/safe_browsing/android:safe_browsing_api_handler",
"//components/safe_browsing/browser",
"//components/safe_browsing/browser:network_context",
"//components/safe_browsing/db:database_manager",
"//components/user_prefs",
"//components/version_info/android:channel_getter",
]
}
......
......@@ -7,6 +7,8 @@ include_rules = [
"+cc",
"+components/crash/content/browser",
"+components/embedder_support",
"+components/prefs",
"+components/user_prefs",
"+components/safe_browsing",
"+components/security_interstitials",
"+components/version_info",
......
......@@ -214,7 +214,7 @@ ContentBrowserClientImpl::CreateURLLoaderThrottles(
// Note: Initialize() needs to happen on UI thread.
safe_browsing_service_ =
std::make_unique<SafeBrowsingService>(GetUserAgent());
safe_browsing_service_->Initialize();
safe_browsing_service_->Initialize(browser_context);
}
result.push_back(safe_browsing_service_->CreateURLLoaderThrottle(
......
......@@ -7,11 +7,17 @@
#include "base/bind.h"
#include "base/path_service.h"
#include "base/task/post_task.h"
#include "components/prefs/in_memory_pref_store.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/pref_service_factory.h"
#include "components/safe_browsing/android/remote_database_manager.h"
#include "components/safe_browsing/android/safe_browsing_api_handler_bridge.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 "components/user_prefs/user_prefs.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/resource_context.h"
......@@ -38,7 +44,7 @@ SafeBrowsingService::SafeBrowsingService(const std::string& user_agent)
SafeBrowsingService::~SafeBrowsingService() {}
void SafeBrowsingService::Initialize() {
void SafeBrowsingService::Initialize(content::BrowserContext* browser_context) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (network_context_) {
......@@ -46,6 +52,23 @@ void SafeBrowsingService::Initialize() {
return;
}
DCHECK(browser_context);
// Create a simple in-memory pref service, and register safe-browsing
// preferences. Currently this is only required for safebrowsing to work (due
// to RealTimePolicyEngine::IsEnabledByPolicy being checked during
// safebrowsing throttle creation).
// TODO(timvolodine): separate this out into general persistent pref service.
auto pref_registry = base::MakeRefCounted<PrefRegistrySimple>();
safe_browsing::RegisterProfilePrefs(pref_registry.get());
PrefServiceFactory pref_service_factory;
pref_service_factory.set_user_prefs(
base::MakeRefCounted<InMemoryPrefStore>());
user_pref_service_ = pref_service_factory.Create(pref_registry);
// Note: UserPrefs::Set also ensures that the user_pref_service_ has not been
// set previously.
user_prefs::UserPrefs::Set(browser_context, user_pref_service_.get());
safe_browsing_api_handler_.reset(
new safe_browsing::SafeBrowsingApiHandlerBridge());
safe_browsing::SafeBrowsingApiHandler::SetInstance(
......
......@@ -12,6 +12,7 @@
#include "weblayer/browser/safe_browsing/safe_browsing_ui_manager.h"
namespace content {
class BrowserContext;
class ResourceContext;
}
......@@ -41,7 +42,7 @@ class SafeBrowsingService {
~SafeBrowsingService();
// Executed on UI thread
void Initialize();
void Initialize(content::BrowserContext* browser_context);
std::unique_ptr<blink::URLLoaderThrottle> CreateURLLoaderThrottle(
content::ResourceContext* resource_context,
const base::RepeatingCallback<content::WebContents*()>& wc_getter,
......@@ -85,6 +86,8 @@ class SafeBrowsingService {
std::unique_ptr<safe_browsing::SafeBrowsingApiHandler>
safe_browsing_api_handler_;
std::unique_ptr<PrefService> user_pref_service_;
std::string user_agent_;
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