Commit 0abc17b0 authored by Xinghui Lu's avatar Xinghui Lu Committed by Commit Bot

Create url_lookup_service_factory to manage url_lookup_service.

url_lookup_service is currently managed in database_manager, which
does not operate at profile level. To make each profile have its own
url_lookup_service, create a singleton which owns all
url_lookup_service, and make url_lookup_service a keyed service.

This is the first CL of refactoring url_lookup_service to be profile
based. The actual service change will be implemented in a follow-up
CL. For more details, see: go/chrome-protego-refactor-profile.

Bug: 1050859
Change-Id: If1862546c2cf8fc9a23375e680c790c72a5913e4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2061119Reviewed-by: default avatarVarun Khaneja <vakh@chromium.org>
Reviewed-by: default avatarColin Blundell <blundell@chromium.org>
Commit-Queue: Xinghui Lu <xinghuilu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#743299}
parent 07fbc516
...@@ -4232,6 +4232,9 @@ ChromeContentBrowserClient::CreateURLLoaderThrottles( ...@@ -4232,6 +4232,9 @@ ChromeContentBrowserClient::CreateURLLoaderThrottles(
? IdentityManagerFactory::GetForProfile(profile) ? IdentityManagerFactory::GetForProfile(profile)
: nullptr; : nullptr;
// TODO(crbug.com/1050859): Get url_lookup_service from
// url_lookup_service_factory and pass it into the throttle.
result.push_back(safe_browsing::BrowserURLLoaderThrottle::Create( result.push_back(safe_browsing::BrowserURLLoaderThrottle::Create(
base::BindOnce( base::BindOnce(
&ChromeContentBrowserClient::GetSafeBrowsingUrlCheckerDelegate, &ChromeContentBrowserClient::GetSafeBrowsingUrlCheckerDelegate,
......
...@@ -99,6 +99,7 @@ jumbo_static_library("safe_browsing") { ...@@ -99,6 +99,7 @@ jumbo_static_library("safe_browsing") {
"ui_manager.h", "ui_manager.h",
] ]
deps += [ deps += [
":url_lookup_service_factory",
"//chrome/browser/engagement:mojo_bindings", "//chrome/browser/engagement:mojo_bindings",
"//chrome/common/safe_browsing:proto", "//chrome/common/safe_browsing:proto",
"//components/safe_browsing/content", "//components/safe_browsing/content",
...@@ -271,6 +272,19 @@ jumbo_static_library("safe_browsing") { ...@@ -271,6 +272,19 @@ jumbo_static_library("safe_browsing") {
} }
} }
source_set("url_lookup_service_factory") {
sources = [
"url_lookup_service_factory.cc",
"url_lookup_service_factory.h",
]
deps = [
"//components/keyed_service/content",
"//components/safe_browsing/core/realtime:url_lookup_service",
"//content/public/browser",
]
}
static_library("advanced_protection") { static_library("advanced_protection") {
sources = [ sources = [
"advanced_protection_status_manager.cc", "advanced_protection_status_manager.cc",
......
// Copyright 2020 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 "chrome/browser/safe_browsing/url_lookup_service_factory.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/safe_browsing/safe_browsing_service.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/safe_browsing/core/realtime/url_lookup_service.h"
#include "content/public/browser/browser_context.h"
#include "services/network/public/cpp/cross_thread_pending_shared_url_loader_factory.h"
namespace safe_browsing {
// static
RealTimeUrlLookupService* RealTimeUrlLookupServiceFactory::GetForProfile(
Profile* profile) {
return static_cast<RealTimeUrlLookupService*>(
GetInstance()->GetServiceForBrowserContext(profile, /* create= */ true));
}
// static
RealTimeUrlLookupServiceFactory*
RealTimeUrlLookupServiceFactory::GetInstance() {
return base::Singleton<RealTimeUrlLookupServiceFactory>::get();
}
RealTimeUrlLookupServiceFactory::RealTimeUrlLookupServiceFactory()
: BrowserContextKeyedServiceFactory(
"RealTimeUrlLookupService",
BrowserContextDependencyManager::GetInstance()) {}
KeyedService* RealTimeUrlLookupServiceFactory::BuildServiceInstanceFor(
content::BrowserContext* context) const {
if (!g_browser_process->safe_browsing_service()) {
return nullptr;
}
auto url_loader_factory =
std::make_unique<network::CrossThreadPendingSharedURLLoaderFactory>(
g_browser_process->safe_browsing_service()->GetURLLoaderFactory());
return new RealTimeUrlLookupService(
network::SharedURLLoaderFactory::Create(std::move(url_loader_factory)));
}
} // namespace safe_browsing
// Copyright 2020 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 CHROME_BROWSER_SAFE_BROWSING_URL_LOOKUP_SERVICE_FACTORY_H_
#define CHROME_BROWSER_SAFE_BROWSING_URL_LOOKUP_SERVICE_FACTORY_H_
#include "base/memory/singleton.h"
#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
class KeyedService;
class Profile;
namespace content {
class BrowserContext;
}
namespace safe_browsing {
class RealTimeUrlLookupService;
// Singleton that owns RealTimeUrlLookupService objects, one for each active
// Profile. It listens to profile destroy events and destroy its associated
// service. It returns nullptr if the profile is in the Incognito mode.
class RealTimeUrlLookupServiceFactory
: public BrowserContextKeyedServiceFactory {
public:
// Creates the service if it doesn't exist already for the given |profile|.
// If the service already exists, return its pointer.
static RealTimeUrlLookupService* GetForProfile(Profile* profile);
// Get the singleton instance.
static RealTimeUrlLookupServiceFactory* GetInstance();
private:
friend struct base::DefaultSingletonTraits<RealTimeUrlLookupServiceFactory>;
RealTimeUrlLookupServiceFactory();
~RealTimeUrlLookupServiceFactory() override = default;
// BrowserContextKeyedServiceFactory:
KeyedService* BuildServiceInstanceFor(
content::BrowserContext* context) const override;
DISALLOW_COPY_AND_ASSIGN(RealTimeUrlLookupServiceFactory);
};
} // namespace safe_browsing
#endif // CHROME_BROWSER_SAFE_BROWSING_URL_LOOKUP_SERVICE_FACTORY_H_
include_rules = [ include_rules = [
"+components/content_settings/core/browser", "+components/content_settings/core/browser",
"+components/history/core/browser", "+components/history/core/browser",
"+components/keyed_service/core",
"+components/password_manager/core/browser/password_manager_metrics_util.h", "+components/password_manager/core/browser/password_manager_metrics_util.h",
"+components/prefs", "+components/prefs",
"+components/security_interstitials/content", "+components/security_interstitials/content",
......
...@@ -117,7 +117,7 @@ void RealTimeUrlLookupService::StartLookup( ...@@ -117,7 +117,7 @@ void RealTimeUrlLookupService::StartLookup(
std::move(request_callback).Run(std::move(request)); std::move(request_callback).Run(std::move(request));
} }
RealTimeUrlLookupService::~RealTimeUrlLookupService() { void RealTimeUrlLookupService::Shutdown() {
for (auto& pending : pending_requests_) { for (auto& pending : pending_requests_) {
// An empty response is treated as safe. // An empty response is treated as safe.
auto response = std::make_unique<RTLookupResponse>(); auto response = std::make_unique<RTLookupResponse>();
...@@ -127,6 +127,8 @@ RealTimeUrlLookupService::~RealTimeUrlLookupService() { ...@@ -127,6 +127,8 @@ RealTimeUrlLookupService::~RealTimeUrlLookupService() {
pending_requests_.clear(); pending_requests_.clear();
} }
RealTimeUrlLookupService::~RealTimeUrlLookupService() {}
void RealTimeUrlLookupService::OnURLLoaderComplete( void RealTimeUrlLookupService::OnURLLoaderComplete(
network::SimpleURLLoader* url_loader, network::SimpleURLLoader* url_loader,
base::TimeTicks request_start_time, base::TimeTicks request_start_time,
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "base/timer/timer.h" #include "base/timer/timer.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/safe_browsing/core/db/v4_protocol_manager_util.h" #include "components/safe_browsing/core/db/v4_protocol_manager_util.h"
#include "components/safe_browsing/core/proto/realtimeapi.pb.h" #include "components/safe_browsing/core/proto/realtimeapi.pb.h"
#include "url/gurl.h" #include "url/gurl.h"
...@@ -36,11 +37,11 @@ using RTLookupResponseCallback = ...@@ -36,11 +37,11 @@ using RTLookupResponseCallback =
// This class implements the logic to decide whether the real time lookup // This class implements the logic to decide whether the real time lookup
// feature is enabled for a given user/profile. // feature is enabled for a given user/profile.
class RealTimeUrlLookupService { class RealTimeUrlLookupService : public KeyedService {
public: public:
explicit RealTimeUrlLookupService( explicit RealTimeUrlLookupService(
scoped_refptr<network::SharedURLLoaderFactory>); scoped_refptr<network::SharedURLLoaderFactory>);
~RealTimeUrlLookupService(); ~RealTimeUrlLookupService() override;
// Returns true if |url|'s scheme can be checked. // Returns true if |url|'s scheme can be checked.
bool CanCheckUrl(const GURL& url) const; bool CanCheckUrl(const GURL& url) const;
...@@ -64,6 +65,10 @@ class RealTimeUrlLookupService { ...@@ -64,6 +65,10 @@ class RealTimeUrlLookupService {
// object will finish all pending requests and delete itself. // object will finish all pending requests and delete itself.
void WaitForPendingRequestsOrDelete(); void WaitForPendingRequestsOrDelete();
// KeyedService:
// Called before the actual deletion of the object.
void Shutdown() override;
// Returns the SBThreatType for a given // Returns the SBThreatType for a given
// RTLookupResponse::ThreatInfo::ThreatType // RTLookupResponse::ThreatInfo::ThreatType
static SBThreatType GetSBThreatTypeForRTThreatType( static SBThreatType GetSBThreatTypeForRTThreatType(
......
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