1 // Copyright 2013 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/policy/policy_cert_service_factory.h"
7 #include "base/memory/singleton.h"
8 #include "base/prefs/pref_registry_simple.h"
9 #include "base/prefs/pref_service.h"
10 #include "base/prefs/scoped_user_pref_update.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/chromeos/login/user_manager.h"
13 #include "chrome/browser/chromeos/policy/policy_cert_service.h"
14 #include "chrome/browser/chromeos/policy/policy_cert_verifier.h"
15 #include "chrome/browser/chromeos/policy/user_network_configuration_updater_factory.h"
16 #include "chrome/browser/lifetime/application_lifetime.h"
17 #include "chrome/browser/profiles/incognito_helpers.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/common/pref_names.h"
20 #include "components/browser_context_keyed_service/browser_context_dependency_manager.h"
21 #include "components/user_prefs/pref_registry_syncable.h"
26 PolicyCertService
* PolicyCertServiceFactory::GetForProfile(Profile
* profile
) {
27 return static_cast<PolicyCertService
*>(
28 GetInstance()->GetServiceForBrowserContext(profile
, false));
32 scoped_ptr
<PolicyCertVerifier
> PolicyCertServiceFactory::CreateForProfile(
34 DCHECK(!GetInstance()->GetServiceForBrowserContext(profile
, false));
35 PolicyCertService
* service
= static_cast<PolicyCertService
*>(
36 GetInstance()->GetServiceForBrowserContext(profile
, true));
38 return scoped_ptr
<PolicyCertVerifier
>();
39 return service
->CreatePolicyCertVerifier();
43 PolicyCertServiceFactory
* PolicyCertServiceFactory::GetInstance() {
44 return Singleton
<PolicyCertServiceFactory
>::get();
48 void PolicyCertServiceFactory::SetUsedPolicyCertificates(
49 const std::string
& user_id
) {
50 if (UsedPolicyCertificates(user_id
))
52 ListPrefUpdate
update(g_browser_process
->local_state(),
53 prefs::kUsedPolicyCertificates
);
54 update
->AppendString(user_id
);
58 void PolicyCertServiceFactory::ClearUsedPolicyCertificates(
59 const std::string
& user_id
) {
60 ListPrefUpdate
update(g_browser_process
->local_state(),
61 prefs::kUsedPolicyCertificates
);
62 update
->Remove(base::StringValue(user_id
), NULL
);
66 bool PolicyCertServiceFactory::UsedPolicyCertificates(
67 const std::string
& user_id
) {
68 base::StringValue
value(user_id
);
69 const base::ListValue
* list
=
70 g_browser_process
->local_state()->GetList(prefs::kUsedPolicyCertificates
);
75 return list
->Find(value
) != list
->end();
79 void PolicyCertServiceFactory::RegisterPrefs(PrefRegistrySimple
* local_state
) {
80 local_state
->RegisterListPref(prefs::kUsedPolicyCertificates
);
83 PolicyCertServiceFactory::PolicyCertServiceFactory()
84 : BrowserContextKeyedServiceFactory(
86 BrowserContextDependencyManager::GetInstance()) {
87 DependsOn(UserNetworkConfigurationUpdaterFactory::GetInstance());
90 PolicyCertServiceFactory::~PolicyCertServiceFactory() {}
92 BrowserContextKeyedService
* PolicyCertServiceFactory::BuildServiceInstanceFor(
93 content::BrowserContext
* context
) const {
94 Profile
* profile
= static_cast<Profile
*>(context
);
96 chromeos::UserManager
* user_manager
= chromeos::UserManager::Get();
97 chromeos::User
* user
=
98 user_manager
->GetUserByProfile(profile
->GetOriginalProfile());
102 // Backwards compatibility: profiles that used policy-pushed certificates used
103 // to have this condition marked in their prefs. This signal has moved to
104 // local_state though, to support checking it before the profile is loaded.
105 // Check the profile here and update the local_state, if appropriate.
106 // TODO(joaodasilva): remove this, eventually.
107 PrefService
* prefs
= profile
->GetOriginalProfile()->GetPrefs();
108 if (prefs
->GetBoolean(prefs::kUsedPolicyCertificatesOnce
)) {
109 SetUsedPolicyCertificates(user
->email());
110 prefs
->ClearPref(prefs::kUsedPolicyCertificatesOnce
);
112 if (user_manager
->GetLoggedInUsers().size() > 1u) {
113 // This login should not have been allowed. After rebooting, local_state
114 // will contain the updated list of users that used policy-pushed
115 // certificates and this won't happen again.
116 // Note that a user becomes logged in before his profile is created.
117 LOG(ERROR
) << "Shutdown session because a tainted profile was added.";
118 g_browser_process
->local_state()->CommitPendingWrite();
119 prefs
->CommitPendingWrite();
120 chrome::AttemptUserExit();
124 UserNetworkConfigurationUpdater
* net_conf_updater
=
125 UserNetworkConfigurationUpdaterFactory::GetForProfile(profile
);
126 if (!net_conf_updater
)
129 return new PolicyCertService(user
->email(), net_conf_updater
, user_manager
);
132 content::BrowserContext
* PolicyCertServiceFactory::GetBrowserContextToUse(
133 content::BrowserContext
* context
) const {
134 return chrome::GetBrowserContextOwnInstanceInIncognito(context
);
137 void PolicyCertServiceFactory::RegisterProfilePrefs(
138 user_prefs::PrefRegistrySyncable
* registry
) {
139 // TODO(joaodasilva): this is used for backwards compatibility.
140 // Remove once it's not necessary anymore.
141 registry
->RegisterBooleanPref(
142 prefs::kUsedPolicyCertificatesOnce
,
144 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
147 bool PolicyCertServiceFactory::ServiceIsNULLWhileTesting() const {
151 } // namespace policy