summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/ownership/owner_settings_service_factory.cc
blob: 606e0e30925e50eb7f67d24cc721c2b8d84b7538 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/chromeos/ownership/owner_settings_service_factory.h"

#include "chrome/browser/chromeos/login/users/user.h"
#include "chrome/browser/chromeos/login/users/user_manager.h"
#include "chrome/browser/chromeos/ownership/owner_settings_service.h"
#include "chrome/browser/chromeos/profiles/profile_helper.h"
#include "chrome/browser/profiles/profile.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"

namespace chromeos {

OwnerSettingsServiceFactory::OwnerSettingsServiceFactory()
    : BrowserContextKeyedServiceFactory(
          "OwnerSettingsService",
          BrowserContextDependencyManager::GetInstance()) {
}

OwnerSettingsServiceFactory::~OwnerSettingsServiceFactory() {
}

// static
OwnerSettingsService* OwnerSettingsServiceFactory::GetForProfile(
    Profile* profile) {
  return static_cast<OwnerSettingsService*>(
      GetInstance()->GetServiceForBrowserContext(profile, true));
}

// static
OwnerSettingsServiceFactory* OwnerSettingsServiceFactory::GetInstance() {
  return Singleton<OwnerSettingsServiceFactory>::get();
}

void OwnerSettingsServiceFactory::SetUsername(const std::string& username) {
  username_ = username;
  if (!UserManager::IsInitialized())
    return;
  const User* user = UserManager::Get()->FindUser(username_);
  if (!user || !user->is_profile_created())
    return;
  Profile* profile = UserManager::Get()->GetProfileByUser(user);
  if (!profile)
    return;
  OwnerSettingsService* service = GetForProfile(profile);

  // It's safe to call ReloadOwnerKey() here, as profile is fully created
  // at this time.
  if (service)
    service->ReloadOwnerKey();
}

std::string OwnerSettingsServiceFactory::GetUsername() const {
  return username_;
}

// static
KeyedService* OwnerSettingsServiceFactory::BuildInstanceFor(
    content::BrowserContext* browser_context) {
  Profile* profile = static_cast<Profile*>(browser_context);
  if (profile->IsGuestSession() || ProfileHelper::IsSigninProfile(profile))
    return NULL;
  return new OwnerSettingsService(profile);
}

bool OwnerSettingsServiceFactory::ServiceIsCreatedWithBrowserContext() const {
  return true;
}

KeyedService* OwnerSettingsServiceFactory::BuildServiceInstanceFor(
    content::BrowserContext* context) const {
  return BuildInstanceFor(context);
}

}  // namespace chromeos