1 // Copyright 2020 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "chrome/browser/safe_browsing/chrome_enterprise_url_lookup_service_factory.h"
6 
7 #include "chrome/browser/browser_process.h"
8 #include "chrome/browser/policy/chrome_browser_policy_connector.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/safe_browsing/advanced_protection_status_manager.h"
11 #include "chrome/browser/safe_browsing/advanced_protection_status_manager_factory.h"
12 #include "chrome/browser/safe_browsing/chrome_enterprise_url_lookup_service.h"
13 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
14 #include "chrome/browser/safe_browsing/verdict_cache_manager_factory.h"
15 #include "chrome/browser/sync/profile_sync_service_factory.h"
16 #include "components/keyed_service/content/browser_context_dependency_manager.h"
17 #include "components/safe_browsing/core/common/utils.h"
18 #include "components/safe_browsing/core/features.h"
19 #include "components/safe_browsing/core/verdict_cache_manager.h"
20 #include "content/public/browser/browser_context.h"
21 #include "services/network/public/cpp/cross_thread_pending_shared_url_loader_factory.h"
22 
23 namespace safe_browsing {
24 
25 // static
26 ChromeEnterpriseRealTimeUrlLookupService*
GetForProfile(Profile * profile)27 ChromeEnterpriseRealTimeUrlLookupServiceFactory::GetForProfile(
28     Profile* profile) {
29   return static_cast<ChromeEnterpriseRealTimeUrlLookupService*>(
30       GetInstance()->GetServiceForBrowserContext(profile, /* create= */ true));
31 }
32 
33 // static
34 ChromeEnterpriseRealTimeUrlLookupServiceFactory*
GetInstance()35 ChromeEnterpriseRealTimeUrlLookupServiceFactory::GetInstance() {
36   return base::Singleton<
37       ChromeEnterpriseRealTimeUrlLookupServiceFactory>::get();
38 }
39 
40 ChromeEnterpriseRealTimeUrlLookupServiceFactory::
ChromeEnterpriseRealTimeUrlLookupServiceFactory()41     ChromeEnterpriseRealTimeUrlLookupServiceFactory()
42     : BrowserContextKeyedServiceFactory(
43           "ChromeEnterpriseRealTimeUrlLookupService",
44           BrowserContextDependencyManager::GetInstance()) {
45   DependsOn(VerdictCacheManagerFactory::GetInstance());
46 }
47 
48 KeyedService*
BuildServiceInstanceFor(content::BrowserContext * context) const49 ChromeEnterpriseRealTimeUrlLookupServiceFactory::BuildServiceInstanceFor(
50     content::BrowserContext* context) const {
51   if (!g_browser_process->safe_browsing_service()) {
52     return nullptr;
53   }
54   Profile* profile = Profile::FromBrowserContext(context);
55   auto url_loader_factory =
56       std::make_unique<network::CrossThreadPendingSharedURLLoaderFactory>(
57           g_browser_process->safe_browsing_service()->GetURLLoaderFactory());
58   if (base::FeatureList::IsEnabled(kSafeBrowsingRemoveCookies)) {
59     url_loader_factory =
60         std::make_unique<network::CrossThreadPendingSharedURLLoaderFactory>(
61             profile->GetURLLoaderFactory());
62   }
63   const policy::BrowserPolicyConnector* browser_policy_connector =
64       g_browser_process->browser_policy_connector();
65   bool is_under_advanced_protection =
66       AdvancedProtectionStatusManagerFactory::GetForProfile(profile)
67           ->IsUnderAdvancedProtection();
68   return new ChromeEnterpriseRealTimeUrlLookupService(
69       network::SharedURLLoaderFactory::Create(std::move(url_loader_factory)),
70       VerdictCacheManagerFactory::GetForProfile(profile), profile,
71       ProfileSyncServiceFactory::GetForProfile(profile), profile->GetPrefs(),
72       GetProfileManagementStatus(browser_policy_connector),
73       is_under_advanced_protection, profile->IsOffTheRecord());
74 }
75 
76 }  // namespace safe_browsing
77