Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(207)

Side by Side Diff: chrome/browser/policy/user_cloud_policy_manager.cc

Issue 10449071: Enable user policy handling through the new cloud policy stack. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments. Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 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/policy/user_cloud_policy_manager.h"
6
7 #include <string>
8
9 #include "base/command_line.h"
10 #include "base/file_path.h"
11 #include "base/message_loop.h"
12 #include "base/path_service.h"
13 #include "chrome/browser/policy/cloud_policy_refresh_scheduler.h"
14 #include "chrome/browser/policy/cloud_policy_service.h"
15 #include "chrome/browser/policy/policy_constants.h"
16 #include "chrome/common/chrome_paths.h"
17 #include "chrome/common/chrome_switches.h"
18 #include "chrome/common/pref_names.h"
19 #include "policy/policy_constants.h"
20
21 #if defined(OS_CHROMEOS)
22 #include "chrome/browser/policy/user_cloud_policy_store_chromeos.h"
23 #include "chromeos/dbus/dbus_thread_manager.h"
24 #include "chromeos/dbus/session_manager_client.h"
25 #endif
26
27 namespace policy {
28
29 namespace {
30
31 #if defined(OS_CHROMEOS)
32 // Paths for the legacy policy caches in the profile directory.
33 // TODO(mnissler): Remove once the number of pre-M20 clients becomes negligible.
34
35 // Subdirectory in the user's profile for storing user policies.
36 const FilePath::CharType kPolicyDir[] = FILE_PATH_LITERAL("Device Management");
37 // File in the above directory for stroing user policy dmtokens.
38 const FilePath::CharType kTokenCacheFile[] = FILE_PATH_LITERAL("Token");
39 // File in the above directory for storing user policy data.
40 const FilePath::CharType kPolicyCacheFile[] = FILE_PATH_LITERAL("Policy");
41 #endif
42
43 } // namespace
44
45 UserCloudPolicyManager::UserCloudPolicyManager(
46 const PolicyDefinitionList* policy_list,
47 scoped_ptr<CloudPolicyStore> store,
48 bool wait_for_policy_fetch)
49 : ConfigurationPolicyProvider(policy_list),
50 wait_for_policy_fetch_(wait_for_policy_fetch),
51 wait_for_policy_refresh_(false),
52 store_(store.Pass()) {
53 store_->Load();
54 store_->AddObserver(this);
55 }
56
57 UserCloudPolicyManager::~UserCloudPolicyManager() {
58 Shutdown();
59 store_->RemoveObserver(this);
60 }
61
62 #if defined(OS_CHROMEOS)
63 // static
64 scoped_ptr<UserCloudPolicyManager> UserCloudPolicyManager::Create(
65 bool wait_for_policy_fetch) {
66 FilePath profile_dir;
67 CHECK(PathService::Get(chrome::DIR_USER_DATA, &profile_dir));
68 CommandLine* command_line = CommandLine::ForCurrentProcess();
69 const FilePath policy_dir =
70 profile_dir
71 .Append(command_line->GetSwitchValuePath(switches::kLoginProfile))
72 .Append(kPolicyDir);
73 const FilePath policy_cache_file = policy_dir.Append(kPolicyCacheFile);
74 const FilePath token_cache_file = policy_dir.Append(kTokenCacheFile);
75
76 scoped_ptr<CloudPolicyStore> store(
77 new UserCloudPolicyStoreChromeOS(
78 chromeos::DBusThreadManager::Get()->GetSessionManagerClient(),
79 token_cache_file, policy_cache_file));
80 return scoped_ptr<UserCloudPolicyManager>(
81 new UserCloudPolicyManager(GetChromePolicyDefinitionList(),
82 store.Pass(), wait_for_policy_fetch));
83 }
84 #endif
85
86 void UserCloudPolicyManager::Initialize(PrefService* prefs,
87 DeviceManagementService* service,
88 UserAffiliation user_affiliation) {
89 DCHECK(!service_.get());
90 prefs_ = prefs;
91 scoped_ptr<CloudPolicyClient> client(
92 new CloudPolicyClient(std::string(), std::string(), user_affiliation,
93 POLICY_SCOPE_USER, NULL, service));
94 service_.reset(new CloudPolicyService(client.Pass(), store_.get()));
95 service_->client()->AddObserver(this);
96
97 if (wait_for_policy_fetch_) {
98 // If we are supposed to wait for a policy fetch, we trigger an explicit
99 // policy refresh at startup that allows us to unblock initialization once
100 // done. The refresh scheduler only gets started once that refresh
101 // completes. Note that we might have to wait for registration to happen,
102 // see OnRegistrationStateChanged() below.
103 if (service_->client()->is_registered()) {
104 service_->RefreshPolicy(
105 base::Bind(&UserCloudPolicyManager::OnInitialPolicyFetchComplete,
106 base::Unretained(this)));
107 }
108 } else {
109 CancelWaitForPolicyFetch();
110 }
111 }
112
113 void UserCloudPolicyManager::Shutdown() {
114 refresh_scheduler_.reset();
115 if (service_.get())
116 service_->client()->RemoveObserver(this);
117 service_.reset();
118 prefs_ = NULL;
119 }
120
121 void UserCloudPolicyManager::CancelWaitForPolicyFetch() {
122 wait_for_policy_fetch_ = false;
123 CheckAndPublishPolicy();
124
125 // Now that |wait_for_policy_fetch_| is guaranteed to be false, the scheduler
126 // can be started.
127 if (service_.get() && !refresh_scheduler_.get() && prefs_) {
128 refresh_scheduler_.reset(
129 new CloudPolicyRefreshScheduler(
130 service_->client(), store_.get(), prefs_,
131 prefs::kUserPolicyRefreshRate,
132 MessageLoop::current()->message_loop_proxy()));
133 }
134 }
135
136 bool UserCloudPolicyManager::IsInitializationComplete() const {
137 return store_->is_initialized() && !wait_for_policy_fetch_;
138 }
139
140 void UserCloudPolicyManager::RefreshPolicies() {
141 if (service_.get()) {
142 wait_for_policy_refresh_ = true;
143 service_->RefreshPolicy(
144 base::Bind(&UserCloudPolicyManager::OnRefreshComplete,
145 base::Unretained(this)));
146 } else {
147 OnRefreshComplete();
148 }
149 }
150
151 void UserCloudPolicyManager::OnPolicyFetched(CloudPolicyClient* client) {
152 // No action required. If we're blocked on a policy fetch, we'll learn about
153 // completion of it through OnInitialPolicyFetchComplete().
154 }
155
156 void UserCloudPolicyManager::OnRegistrationStateChanged(
157 CloudPolicyClient* client) {
158 if (wait_for_policy_fetch_) {
159 // If we're blocked on the policy fetch, now is a good time to issue it.
160 if (service_->client()->is_registered()) {
161 service_->RefreshPolicy(
162 base::Bind(&UserCloudPolicyManager::OnInitialPolicyFetchComplete,
163 base::Unretained(this)));
164 } else {
165 // If the client has switched to not registered, we bail out as this
166 // indicates the cloud policy setup flow has been aborted.
167 CancelWaitForPolicyFetch();
168 }
169 }
170 }
171
172 void UserCloudPolicyManager::OnClientError(CloudPolicyClient* client) {
173 CancelWaitForPolicyFetch();
174 }
175
176 void UserCloudPolicyManager::CheckAndPublishPolicy() {
177 if (IsInitializationComplete() && !wait_for_policy_refresh_) {
178 scoped_ptr<PolicyBundle> bundle(new PolicyBundle());
179 bundle->Get(POLICY_DOMAIN_CHROME, std::string()).CopyFrom(
180 store_->policy_map());
181 UpdatePolicy(bundle.Pass());
182 }
183 }
184
185 void UserCloudPolicyManager::OnStoreLoaded(CloudPolicyStore* store) {
186 CheckAndPublishPolicy();
187 }
188
189 void UserCloudPolicyManager::OnStoreError(CloudPolicyStore* store) {
190 // No action required, the old policy is still valid.
191 }
192
193 void UserCloudPolicyManager::OnInitialPolicyFetchComplete() {
194 CancelWaitForPolicyFetch();
195 }
196
197 void UserCloudPolicyManager::OnRefreshComplete() {
198 wait_for_policy_refresh_ = false;
199 CheckAndPublishPolicy();
200 }
201
202 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/policy/user_cloud_policy_manager.h ('k') | chrome/browser/policy/user_cloud_policy_manager_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698