Upstream version 8.37.180.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / ownership / owner_settings_service_factory.cc
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.
4
5 #include "chrome/browser/chromeos/ownership/owner_settings_service_factory.h"
6
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"
13
14 namespace chromeos {
15
16 OwnerSettingsServiceFactory::OwnerSettingsServiceFactory()
17     : BrowserContextKeyedServiceFactory(
18           "OwnerSettingsService",
19           BrowserContextDependencyManager::GetInstance()) {
20 }
21
22 OwnerSettingsServiceFactory::~OwnerSettingsServiceFactory() {
23 }
24
25 // static
26 OwnerSettingsService* OwnerSettingsServiceFactory::GetForProfile(
27     Profile* profile) {
28   return static_cast<OwnerSettingsService*>(
29       GetInstance()->GetServiceForBrowserContext(profile, true));
30 }
31
32 // static
33 OwnerSettingsServiceFactory* OwnerSettingsServiceFactory::GetInstance() {
34   return Singleton<OwnerSettingsServiceFactory>::get();
35 }
36
37 void OwnerSettingsServiceFactory::SetUsername(const std::string& username) {
38   username_ = username;
39   if (!UserManager::IsInitialized())
40     return;
41   const User* user = UserManager::Get()->FindUser(username_);
42   if (!user || !user->is_profile_created())
43     return;
44   Profile* profile = UserManager::Get()->GetProfileByUser(user);
45   if (!profile)
46     return;
47   OwnerSettingsService* service = GetForProfile(profile);
48
49   // It's safe to call ReloadPrivateKey() here, as profile is fully created
50   // at this time.
51   if (service)
52     service->ReloadPrivateKey();
53 }
54
55 std::string OwnerSettingsServiceFactory::GetUsername() const {
56   return username_;
57 }
58
59 // static
60 KeyedService* OwnerSettingsServiceFactory::BuildInstanceFor(
61     content::BrowserContext* browser_context) {
62   Profile* profile = static_cast<Profile*>(browser_context);
63   if (profile->IsGuestSession() || ProfileHelper::IsSigninProfile(profile))
64     return NULL;
65   return new OwnerSettingsService(profile);
66 }
67
68 bool OwnerSettingsServiceFactory::ServiceIsCreatedWithBrowserContext() const {
69   return true;
70 }
71
72 KeyedService* OwnerSettingsServiceFactory::BuildServiceInstanceFor(
73     content::BrowserContext* context) const {
74   return BuildInstanceFor(context);
75 }
76
77 }  // namespace chromeos