cryptohome: Move stateless wrapper functions out of CryptohomeLibrary
[chromium-blink-merge.git] / chrome / browser / chromeos / policy / user_cloud_policy_manager_factory_chromeos.cc
bloba208b9cfb36a91f76f6c52a7f6b16b75a8c57320
1 // Copyright (c) 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/user_cloud_policy_manager_factory_chromeos.h"
7 #include "base/command_line.h"
8 #include "base/files/file_path.h"
9 #include "base/logging.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/message_loop/message_loop_proxy.h"
12 #include "base/path_service.h"
13 #include "base/sequenced_task_runner.h"
14 #include "base/threading/sequenced_worker_pool.h"
15 #include "base/time/time.h"
16 #include "chrome/browser/browser_process.h"
17 #include "chrome/browser/chromeos/login/user.h"
18 #include "chrome/browser/chromeos/login/user_manager.h"
19 #include "chrome/browser/chromeos/policy/user_cloud_external_data_manager.h"
20 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_chromeos.h"
21 #include "chrome/browser/chromeos/policy/user_cloud_policy_store_chromeos.h"
22 #include "chrome/browser/chromeos/profiles/profile_helper.h"
23 #include "chrome/browser/policy/browser_policy_connector.h"
24 #include "chrome/browser/policy/cloud/cloud_external_data_manager.h"
25 #include "chrome/browser/policy/cloud/device_management_service.h"
26 #include "chrome/browser/policy/cloud/resource_cache.h"
27 #include "chrome/browser/profiles/profile.h"
28 #include "chrome/common/chrome_switches.h"
29 #include "chromeos/chromeos_paths.h"
30 #include "chromeos/chromeos_switches.h"
31 #include "chromeos/dbus/dbus_thread_manager.h"
32 #include "components/browser_context_keyed_service/browser_context_dependency_manager.h"
33 #include "content/public/browser/browser_thread.h"
34 #include "net/url_request/url_request_context_getter.h"
35 #include "policy/policy_constants.h"
37 namespace policy {
39 namespace {
41 // Subdirectory in the user's profile for storing legacy user policies.
42 const base::FilePath::CharType kDeviceManagementDir[] =
43 FILE_PATH_LITERAL("Device Management");
44 // File in the above directory for storing legacy user policy dmtokens.
45 const base::FilePath::CharType kToken[] = FILE_PATH_LITERAL("Token");
46 // This constant is used to build two different paths. It can be a file inside
47 // kDeviceManagementDir where legacy user policy data is stored, and it can be
48 // a directory inside the profile directory where other resources are stored.
49 const base::FilePath::CharType kPolicy[] = FILE_PATH_LITERAL("Policy");
50 // Directory under kPolicy, in the user's profile dir, where external policy
51 // resources are stored.
52 const base::FilePath::CharType kResourceDir[] = FILE_PATH_LITERAL("Resources");
53 // Directory in which to store external policy data. This is specified relative
54 // to kPolicy.
55 const base::FilePath::CharType kPolicyExternalDataDir[] =
56 FILE_PATH_LITERAL("External Data");
58 // Timeout in seconds after which to abandon the initial policy fetch and start
59 // the session regardless.
60 const int kInitialPolicyFetchTimeoutSeconds = 10;
62 } // namespace
64 // static
65 UserCloudPolicyManagerFactoryChromeOS*
66 UserCloudPolicyManagerFactoryChromeOS::GetInstance() {
67 return Singleton<UserCloudPolicyManagerFactoryChromeOS>::get();
70 // static
71 UserCloudPolicyManagerChromeOS*
72 UserCloudPolicyManagerFactoryChromeOS::GetForProfile(
73 Profile* profile) {
74 return GetInstance()->GetManagerForProfile(profile);
77 // static
78 scoped_ptr<UserCloudPolicyManagerChromeOS>
79 UserCloudPolicyManagerFactoryChromeOS::CreateForProfile(
80 Profile* profile,
81 bool force_immediate_load) {
82 return GetInstance()->CreateManagerForProfile(profile, force_immediate_load);
85 UserCloudPolicyManagerFactoryChromeOS::UserCloudPolicyManagerFactoryChromeOS()
86 : BrowserContextKeyedBaseFactory(
87 "UserCloudPolicyManagerChromeOS",
88 BrowserContextDependencyManager::GetInstance()) {}
90 UserCloudPolicyManagerFactoryChromeOS::
91 ~UserCloudPolicyManagerFactoryChromeOS() {}
93 UserCloudPolicyManagerChromeOS*
94 UserCloudPolicyManagerFactoryChromeOS::GetManagerForProfile(
95 Profile* profile) {
96 // Get the manager for the original profile, since the PolicyService is
97 // also shared between the incognito Profile and the original Profile.
98 ManagerMap::const_iterator it = managers_.find(profile->GetOriginalProfile());
99 return it != managers_.end() ? it->second : NULL;
102 scoped_ptr<UserCloudPolicyManagerChromeOS>
103 UserCloudPolicyManagerFactoryChromeOS::CreateManagerForProfile(
104 Profile* profile,
105 bool force_immediate_load) {
106 const CommandLine* command_line = CommandLine::ForCurrentProcess();
107 // Don't initialize cloud policy for the signin profile.
108 if (chromeos::ProfileHelper::IsSigninProfile(profile))
109 return scoped_ptr<UserCloudPolicyManagerChromeOS>();
111 // |user| should never be NULL except for the signin profile. This object is
112 // created as part of the Profile creation, which happens right after
113 // sign-in. The just-signed-in User is the active user during that time.
114 chromeos::UserManager* user_manager = chromeos::UserManager::Get();
115 chromeos::User* user = user_manager->GetActiveUser();
116 CHECK(user);
118 // Only USER_TYPE_REGULAR users have user cloud policy.
119 // USER_TYPE_RETAIL_MODE, USER_TYPE_KIOSK_APP, USER_TYPE_GUEST and
120 // USER_TYPE_LOCALLY_MANAGED are not signed in and can't authenticate the
121 // policy registration.
122 // USER_TYPE_PUBLIC_ACCOUNT gets its policy from the
123 // DeviceLocalAccountPolicyService.
124 const std::string& username = user->email();
125 if (user->GetType() != chromeos::User::USER_TYPE_REGULAR ||
126 BrowserPolicyConnector::IsNonEnterpriseUser(username)) {
127 return scoped_ptr<UserCloudPolicyManagerChromeOS>();
130 BrowserPolicyConnector* connector =
131 g_browser_process->browser_policy_connector();
132 UserAffiliation affiliation = connector->GetUserAffiliation(username);
133 const bool is_managed_user = affiliation == USER_AFFILIATION_MANAGED;
134 const bool is_browser_restart =
135 command_line->HasSwitch(chromeos::switches::kLoginUser) &&
136 !command_line->HasSwitch(chromeos::switches::kLoginPassword);
137 const bool wait_for_initial_policy = is_managed_user && !is_browser_restart;
139 DeviceManagementService* device_management_service =
140 connector->device_management_service();
141 if (wait_for_initial_policy)
142 device_management_service->ScheduleInitialization(0);
144 base::FilePath profile_dir = profile->GetPath();
145 const base::FilePath legacy_dir = profile_dir.Append(kDeviceManagementDir);
146 const base::FilePath policy_cache_file = legacy_dir.Append(kPolicy);
147 const base::FilePath token_cache_file = legacy_dir.Append(kToken);
148 const base::FilePath resource_cache_dir =
149 profile_dir.Append(kPolicy).Append(kResourceDir);
150 const base::FilePath external_data_dir =
151 profile_dir.Append(kPolicy).Append(kPolicyExternalDataDir);
152 base::FilePath policy_key_dir;
153 CHECK(PathService::Get(chromeos::DIR_USER_POLICY_KEYS, &policy_key_dir));
155 scoped_ptr<UserCloudPolicyStoreChromeOS> store(
156 new UserCloudPolicyStoreChromeOS(
157 chromeos::DBusThreadManager::Get()->GetCryptohomeClient(),
158 chromeos::DBusThreadManager::Get()->GetSessionManagerClient(),
159 username, policy_key_dir, token_cache_file, policy_cache_file));
161 scoped_refptr<base::SequencedTaskRunner> backend_task_runner =
162 content::BrowserThread::GetBlockingPool()->GetSequencedTaskRunner(
163 content::BrowserThread::GetBlockingPool()->GetSequenceToken());
164 scoped_refptr<base::SequencedTaskRunner> io_task_runner =
165 content::BrowserThread::GetMessageLoopProxyForThread(
166 content::BrowserThread::IO);
167 scoped_ptr<CloudExternalDataManager> external_data_manager(
168 new UserCloudExternalDataManager(GetChromePolicyDefinitionList(),
169 backend_task_runner,
170 io_task_runner,
171 external_data_dir,
172 store.get()));
173 if (force_immediate_load)
174 store->LoadImmediately();
176 scoped_ptr<ResourceCache> resource_cache;
177 if (command_line->HasSwitch(switches::kEnableComponentCloudPolicy)) {
178 resource_cache.reset(new ResourceCache(
179 resource_cache_dir,
180 content::BrowserThread::GetMessageLoopProxyForThread(
181 content::BrowserThread::FILE)));
184 scoped_ptr<UserCloudPolicyManagerChromeOS> manager(
185 new UserCloudPolicyManagerChromeOS(
186 store.PassAs<CloudPolicyStore>(),
187 external_data_manager.Pass(),
188 base::MessageLoopProxy::current(),
189 resource_cache.Pass(),
190 wait_for_initial_policy,
191 base::TimeDelta::FromSeconds(kInitialPolicyFetchTimeoutSeconds)));
192 manager->Init();
193 manager->Connect(g_browser_process->local_state(),
194 device_management_service,
195 g_browser_process->system_request_context(),
196 affiliation);
198 DCHECK(managers_.find(profile) == managers_.end());
199 managers_[profile] = manager.get();
200 return manager.Pass();
203 void UserCloudPolicyManagerFactoryChromeOS::BrowserContextShutdown(
204 content::BrowserContext* context) {
205 Profile* profile = static_cast<Profile*>(context);
206 if (profile->IsOffTheRecord())
207 return;
208 UserCloudPolicyManagerChromeOS* manager = GetManagerForProfile(profile);
209 if (manager)
210 manager->Shutdown();
213 void UserCloudPolicyManagerFactoryChromeOS::BrowserContextDestroyed(
214 content::BrowserContext* context) {
215 Profile* profile = static_cast<Profile*>(context);
216 managers_.erase(profile);
217 BrowserContextKeyedBaseFactory::BrowserContextDestroyed(context);
220 void UserCloudPolicyManagerFactoryChromeOS::SetEmptyTestingFactory(
221 content::BrowserContext* context) {}
223 void UserCloudPolicyManagerFactoryChromeOS::CreateServiceNow(
224 content::BrowserContext* context) {}
226 } // namespace policy