1 // Copyright 2019 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/security_events/security_event_recorder_factory.h"
6 
7 #include <memory>
8 #include <utility>
9 
10 #include "base/bind.h"
11 #include "base/time/default_clock.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/security_events/security_event_recorder_impl.h"
14 #include "chrome/browser/security_events/security_event_sync_bridge.h"
15 #include "chrome/browser/security_events/security_event_sync_bridge_impl.h"
16 #include "chrome/browser/sync/model_type_store_service_factory.h"
17 #include "chrome/common/channel_info.h"
18 #include "components/keyed_service/content/browser_context_dependency_manager.h"
19 #include "components/sync/base/report_unrecoverable_error.h"
20 #include "components/sync/model/model_type_store_service.h"
21 #include "components/sync/model_impl/client_tag_based_model_type_processor.h"
22 
23 // static
GetInstance()24 SecurityEventRecorderFactory* SecurityEventRecorderFactory::GetInstance() {
25   return base::Singleton<SecurityEventRecorderFactory>::get();
26 }
27 
28 // static
GetForProfile(Profile * profile)29 SecurityEventRecorder* SecurityEventRecorderFactory::GetForProfile(
30     Profile* profile) {
31   DCHECK(!profile->IsOffTheRecord());
32   return static_cast<SecurityEventRecorder*>(
33       GetInstance()->GetServiceForBrowserContext(profile, true));
34 }
SecurityEventRecorderFactory()35 SecurityEventRecorderFactory::SecurityEventRecorderFactory()
36     : BrowserContextKeyedServiceFactory(
37           "SecurityEventRecorder",
38           BrowserContextDependencyManager::GetInstance()) {
39   DependsOn(ModelTypeStoreServiceFactory::GetInstance());
40 }
41 
~SecurityEventRecorderFactory()42 SecurityEventRecorderFactory::~SecurityEventRecorderFactory() {}
43 
BuildServiceInstanceFor(content::BrowserContext * context) const44 KeyedService* SecurityEventRecorderFactory::BuildServiceInstanceFor(
45     content::BrowserContext* context) const {
46   Profile* profile = static_cast<Profile*>(context);
47   syncer::OnceModelTypeStoreFactory store_factory =
48       ModelTypeStoreServiceFactory::GetForProfile(profile)->GetStoreFactory();
49 
50   auto change_processor =
51       std::make_unique<syncer::ClientTagBasedModelTypeProcessor>(
52           syncer::SECURITY_EVENTS,
53           base::BindRepeating(&syncer::ReportUnrecoverableError,
54                               chrome::GetChannel()));
55   auto security_event_sync_bridge =
56       std::make_unique<SecurityEventSyncBridgeImpl>(
57           std::move(store_factory), std::move(change_processor));
58   return new SecurityEventRecorderImpl(std::move(security_event_sync_bridge),
59                                        base::DefaultClock::GetInstance());
60 }
61