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_factory.h"
7 #include "chrome/browser/chromeos/login/users/user.h"
8 #include "chrome/browser/chromeos/login/users/user_manager.h"
9 #include "chrome/browser/chromeos/ownership/owner_settings_service.h"
10 #include "chrome/browser/chromeos/profiles/profile_helper.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "components/keyed_service/content/browser_context_dependency_manager.h"
16 OwnerSettingsServiceFactory::OwnerSettingsServiceFactory()
17 : BrowserContextKeyedServiceFactory(
18 "OwnerSettingsService",
19 BrowserContextDependencyManager::GetInstance()) {
22 OwnerSettingsServiceFactory::~OwnerSettingsServiceFactory() {
26 OwnerSettingsService* OwnerSettingsServiceFactory::GetForProfile(
28 return static_cast<OwnerSettingsService*>(
29 GetInstance()->GetServiceForBrowserContext(profile, true));
33 OwnerSettingsServiceFactory* OwnerSettingsServiceFactory::GetInstance() {
34 return Singleton<OwnerSettingsServiceFactory>::get();
37 void OwnerSettingsServiceFactory::SetUsername(const std::string& username) {
39 if (!UserManager::IsInitialized())
41 const User* user = UserManager::Get()->FindUser(username_);
42 if (!user || !user->is_profile_created())
44 Profile* profile = UserManager::Get()->GetProfileByUser(user);
47 OwnerSettingsService* service = GetForProfile(profile);
49 // It's safe to call ReloadPrivateKey() here, as profile is fully created
52 service->ReloadPrivateKey();
55 std::string OwnerSettingsServiceFactory::GetUsername() const {
60 KeyedService* OwnerSettingsServiceFactory::BuildInstanceFor(
61 content::BrowserContext* browser_context) {
62 Profile* profile = static_cast<Profile*>(browser_context);
63 if (profile->IsGuestSession() || ProfileHelper::IsSigninProfile(profile))
65 return new OwnerSettingsService(profile);
68 bool OwnerSettingsServiceFactory::ServiceIsCreatedWithBrowserContext() const {
72 KeyedService* OwnerSettingsServiceFactory::BuildServiceInstanceFor(
73 content::BrowserContext* context) const {
74 return BuildInstanceFor(context);
77 } // namespace chromeos