1 // Copyright 2014 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.
5 #include "chrome/browser/chromeos/ownership/owner_settings_service_chromeos_factory.h"
7 #include "base/path_service.h"
8 #include "chrome/browser/chromeos/ownership/owner_settings_service_chromeos.h"
9 #include "chrome/browser/chromeos/profiles/profile_helper.h"
10 #include "chrome/browser/chromeos/settings/device_settings_service.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chromeos/chromeos_paths.h"
13 #include "components/keyed_service/content/browser_context_dependency_manager.h"
14 #include "components/ownership/owner_key_util.h"
15 #include "components/ownership/owner_key_util_impl.h"
21 DeviceSettingsService* g_device_settings_service_for_testing_ = nullptr;
23 DeviceSettingsService* GetDeviceSettingsService() {
24 if (g_device_settings_service_for_testing_)
25 return g_device_settings_service_for_testing_;
26 return DeviceSettingsService::IsInitialized() ? DeviceSettingsService::Get()
32 OwnerSettingsServiceChromeOSFactory::OwnerSettingsServiceChromeOSFactory()
33 : BrowserContextKeyedServiceFactory(
34 "OwnerSettingsService",
35 BrowserContextDependencyManager::GetInstance()) {
38 OwnerSettingsServiceChromeOSFactory::~OwnerSettingsServiceChromeOSFactory() {
42 OwnerSettingsServiceChromeOS*
43 OwnerSettingsServiceChromeOSFactory::GetForProfile(Profile* profile) {
44 return static_cast<OwnerSettingsServiceChromeOS*>(
45 GetInstance()->GetServiceForBrowserContext(profile, true));
49 OwnerSettingsServiceChromeOSFactory*
50 OwnerSettingsServiceChromeOSFactory::GetInstance() {
51 return Singleton<OwnerSettingsServiceChromeOSFactory>::get();
55 void OwnerSettingsServiceChromeOSFactory::SetDeviceSettingsServiceForTesting(
56 DeviceSettingsService* device_settings_service) {
57 g_device_settings_service_for_testing_ = device_settings_service;
60 scoped_refptr<ownership::OwnerKeyUtil>
61 OwnerSettingsServiceChromeOSFactory::GetOwnerKeyUtil() {
62 if (owner_key_util_.get())
63 return owner_key_util_;
64 base::FilePath public_key_path;
65 if (!PathService::Get(chromeos::FILE_OWNER_KEY, &public_key_path))
67 owner_key_util_ = new ownership::OwnerKeyUtilImpl(public_key_path);
68 return owner_key_util_;
71 void OwnerSettingsServiceChromeOSFactory::SetOwnerKeyUtilForTesting(
72 const scoped_refptr<ownership::OwnerKeyUtil>& owner_key_util) {
73 owner_key_util_ = owner_key_util;
77 KeyedService* OwnerSettingsServiceChromeOSFactory::BuildInstanceFor(
78 content::BrowserContext* browser_context) {
79 Profile* profile = static_cast<Profile*>(browser_context);
80 if (profile->IsGuestSession() || ProfileHelper::IsSigninProfile(profile))
82 return new OwnerSettingsServiceChromeOS(
83 GetDeviceSettingsService(),
85 GetInstance()->GetOwnerKeyUtil());
88 bool OwnerSettingsServiceChromeOSFactory::ServiceIsCreatedWithBrowserContext()
93 KeyedService* OwnerSettingsServiceChromeOSFactory::BuildServiceInstanceFor(
94 content::BrowserContext* context) const {
95 return BuildInstanceFor(context);
98 } // namespace chromeos