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

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

Issue 5331008: Persist 'this device is not managed' information (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: move 'unmanaged' timestamp into DMPCache's file; introduce private DMPProvider constructor Created 10 years 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
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/policy/device_management_policy_provider.h" 5 #include "chrome/browser/policy/device_management_policy_provider.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/file_util.h" 8 #include "base/file_util.h"
9 #include "base/path_service.h" 9 #include "base/path_service.h"
10 #include "base/rand_util.h" 10 #include "base/rand_util.h"
11 #include "base/task.h" 11 #include "base/task.h"
12 #include "chrome/browser/browser_thread.h" 12 #include "chrome/browser/browser_thread.h"
13 #include "chrome/browser/policy/device_management_backend.h" 13 #include "chrome/browser/policy/device_management_backend.h"
14 #include "chrome/browser/policy/device_management_policy_cache.h" 14 #include "chrome/browser/policy/device_management_policy_cache.h"
15 #include "chrome/browser/policy/proto/device_management_constants.h" 15 #include "chrome/browser/policy/proto/device_management_constants.h"
16 #include "chrome/browser/policy/proto/device_management_local.pb.h"
Mattias Nissler (ping if slow) 2010/11/26 10:37:45 do you still need this header?
Jakob Kummerow 2010/11/26 11:07:42 No, I don't. Good catch. Done.
16 #include "chrome/common/chrome_paths.h" 17 #include "chrome/common/chrome_paths.h"
17 #include "chrome/common/chrome_switches.h" 18 #include "chrome/common/chrome_switches.h"
18 #include "chrome/common/notification_service.h" 19 #include "chrome/common/notification_service.h"
19 #include "chrome/common/notification_type.h" 20 #include "chrome/common/notification_type.h"
20 21
21 namespace policy { 22 namespace policy {
22 23
24 namespace em = enterprise_management;
25
23 const int64 kPolicyRefreshRateInMilliseconds = 3 * 60 * 60 * 1000; // 3 hours 26 const int64 kPolicyRefreshRateInMilliseconds = 3 * 60 * 60 * 1000; // 3 hours
24 const int64 kPolicyRefreshMaxEarlierInMilliseconds = 20 * 60 * 1000; // 20 mins 27 const int64 kPolicyRefreshMaxEarlierInMilliseconds = 20 * 60 * 1000; // 20 mins
25 // These are the base values for delays before retrying after an error. They 28 // These are the base values for delays before retrying after an error. They
26 // will be doubled each time they are used. 29 // will be doubled each time they are used.
27 const int64 kPolicyRefreshErrorDelayInMilliseconds = 3 * 1000; // 3 seconds 30 const int64 kPolicyRefreshErrorDelayInMilliseconds = 3 * 1000; // 3 seconds
28 const int64 kDeviceTokenRefreshErrorDelayInMilliseconds = 3 * 1000; 31 const int64 kDeviceTokenRefreshErrorDelayInMilliseconds = 3 * 1000;
32 // For unmanaged devices, check once per day whether they're still unmanaged.
33 const int64 kPolicyRefreshUnmanagedDeviceInMilliseconds = 24 * 60 * 60 * 1000;
34
35 const char* kDeviceTokenFilename = "Token";
36 const char* kPolicyFilename = "Policy";
29 37
30 // Ensures that the portion of the policy provider implementation that requires 38 // Ensures that the portion of the policy provider implementation that requires
31 // the IOThread is deferred until the IOThread is fully initialized. The policy 39 // the IOThread is deferred until the IOThread is fully initialized. The policy
32 // provider posts this task on the UI thread during its constructor, thereby 40 // provider posts this task on the UI thread during its constructor, thereby
33 // guaranteeing that the code won't get executed until after the UI and IO 41 // guaranteeing that the code won't get executed until after the UI and IO
34 // threads are fully constructed. 42 // threads are fully constructed.
35 class DeviceManagementPolicyProvider::InitializeAfterIOThreadExistsTask 43 class DeviceManagementPolicyProvider::InitializeAfterIOThreadExistsTask
36 : public Task { 44 : public Task {
37 public: 45 public:
38 explicit InitializeAfterIOThreadExistsTask( 46 explicit InitializeAfterIOThreadExistsTask(
(...skipping 26 matching lines...) Expand all
65 73
66 private: 74 private:
67 base::WeakPtr<DeviceManagementPolicyProvider> provider_; 75 base::WeakPtr<DeviceManagementPolicyProvider> provider_;
68 }; 76 };
69 77
70 DeviceManagementPolicyProvider::DeviceManagementPolicyProvider( 78 DeviceManagementPolicyProvider::DeviceManagementPolicyProvider(
71 const ConfigurationPolicyProvider::PolicyDefinitionList* policy_list, 79 const ConfigurationPolicyProvider::PolicyDefinitionList* policy_list,
72 DeviceManagementBackend* backend, 80 DeviceManagementBackend* backend,
73 TokenService* token_service, 81 TokenService* token_service,
74 const FilePath& storage_dir) 82 const FilePath& storage_dir)
75 : ConfigurationPolicyProvider(policy_list), 83 : ConfigurationPolicyProvider(policy_list) {
76 backend_(backend), 84 Initialize(backend,
77 token_service_(token_service), 85 token_service,
78 storage_dir_(GetOrCreateDeviceManagementDir(storage_dir)), 86 storage_dir,
79 policy_request_pending_(false), 87 kPolicyRefreshRateInMilliseconds,
80 refresh_task_pending_(false), 88 kPolicyRefreshMaxEarlierInMilliseconds,
81 policy_refresh_rate_ms_(kPolicyRefreshRateInMilliseconds), 89 kPolicyRefreshErrorDelayInMilliseconds,
82 policy_refresh_max_earlier_ms_(kPolicyRefreshMaxEarlierInMilliseconds), 90 kDeviceTokenRefreshErrorDelayInMilliseconds,
83 policy_refresh_error_delay_ms_(kPolicyRefreshErrorDelayInMilliseconds), 91 kPolicyRefreshUnmanagedDeviceInMilliseconds);
84 token_fetch_error_delay_ms_(kDeviceTokenRefreshErrorDelayInMilliseconds) { 92 }
85 Initialize(); 93
94 DeviceManagementPolicyProvider::DeviceManagementPolicyProvider(
95 const PolicyDefinitionList* policy_list,
96 DeviceManagementBackend* backend,
97 TokenService* token_service,
98 const FilePath& storage_dir,
99 int64 policy_refresh_rate_ms,
100 int64 policy_refresh_max_earlier_ms,
101 int64 policy_refresh_error_delay_ms,
102 int64 token_fetch_error_delay_ms,
103 int64 unmanaged_device_refresh_rate_ms)
104 : ConfigurationPolicyProvider(policy_list) {
105 Initialize(backend,
106 token_service,
107 storage_dir,
108 policy_refresh_rate_ms,
109 policy_refresh_max_earlier_ms,
110 policy_refresh_error_delay_ms,
111 token_fetch_error_delay_ms,
112 unmanaged_device_refresh_rate_ms);
86 } 113 }
87 114
88 DeviceManagementPolicyProvider::~DeviceManagementPolicyProvider() {} 115 DeviceManagementPolicyProvider::~DeviceManagementPolicyProvider() {}
89 116
90 bool DeviceManagementPolicyProvider::Provide( 117 bool DeviceManagementPolicyProvider::Provide(
91 ConfigurationPolicyStoreInterface* policy_store) { 118 ConfigurationPolicyStoreInterface* policy_store) {
92 scoped_ptr<DictionaryValue> policies(cache_->GetPolicy()); 119 scoped_ptr<DictionaryValue> policies(cache_->GetPolicy());
93 DecodePolicyValueTree(policies.get(), policy_store); 120 DecodePolicyValueTree(policies.get(), policy_store);
94 return true; 121 return true;
95 } 122 }
96 123
97 void DeviceManagementPolicyProvider::HandlePolicyResponse( 124 void DeviceManagementPolicyProvider::HandlePolicyResponse(
98 const em::DevicePolicyResponse& response) { 125 const em::DevicePolicyResponse& response) {
99 if (cache_->SetPolicy(response)) 126 if (cache_->SetPolicy(response))
100 NotifyStoreOfPolicyChange(); 127 NotifyStoreOfPolicyChange();
101 policy_request_pending_ = false; 128 policy_request_pending_ = false;
102 // Reset the error delay since policy fetching succeeded this time. 129 // Reset the error delay since policy fetching succeeded this time.
103 policy_refresh_error_delay_ms_ = kPolicyRefreshErrorDelayInMilliseconds; 130 policy_refresh_error_delay_ms_ = kPolicyRefreshErrorDelayInMilliseconds;
104 ScheduleRefreshTask(GetRefreshTaskDelay()); 131 ScheduleRefreshTask(GetRefreshTaskDelay());
132 // Update this provider's internal waiting state, but don't notify anyone
133 // else yet (that's done by the PrefValueStore that receives the policy).
134 waiting_for_initial_policies_ = false;
105 } 135 }
106 136
107 void DeviceManagementPolicyProvider::OnError( 137 void DeviceManagementPolicyProvider::OnError(
108 DeviceManagementBackend::ErrorCode code) { 138 DeviceManagementBackend::ErrorCode code) {
109 policy_request_pending_ = false; 139 policy_request_pending_ = false;
110 LOG(WARNING) << "Could not provide policy from the device manager (error = " 140 LOG(WARNING) << "Could not provide policy from the device manager (error = "
111 << code << "), will retry in " 141 << code << "), will retry in "
112 << (policy_refresh_error_delay_ms_/1000) << " seconds."; 142 << (policy_refresh_error_delay_ms_/1000) << " seconds.";
113 ScheduleRefreshTask(policy_refresh_error_delay_ms_); 143 ScheduleRefreshTask(policy_refresh_error_delay_ms_);
114 policy_refresh_error_delay_ms_ *= 2; 144 policy_refresh_error_delay_ms_ *= 2;
115 if (policy_refresh_rate_ms_ && 145 if (policy_refresh_rate_ms_ &&
116 policy_refresh_rate_ms_ < policy_refresh_error_delay_ms_) { 146 policy_refresh_rate_ms_ < policy_refresh_error_delay_ms_) {
117 policy_refresh_error_delay_ms_ = policy_refresh_rate_ms_; 147 policy_refresh_error_delay_ms_ = policy_refresh_rate_ms_;
118 } 148 }
119 #if defined(OS_CHROMEOS) 149 StopWaitingForInitialPolicies();
120 // Send a CLOUD_POLICY_UPDATE notification to unblock ChromeOS logins that
121 // are waiting for an initial policy fetch to complete.
122 NotifyCloudPolicyUpdate();
123 #endif
124 } 150 }
125 151
126 #if defined(OS_CHROMEOS)
127 void DeviceManagementPolicyProvider::NotifyCloudPolicyUpdate() const {
128 NotificationService::current()->Notify(
129 NotificationType::CLOUD_POLICY_UPDATE,
130 Source<DeviceManagementPolicyProvider>(this),
131 NotificationService::NoDetails());
132 }
133 #endif
134
135 void DeviceManagementPolicyProvider::OnTokenSuccess() { 152 void DeviceManagementPolicyProvider::OnTokenSuccess() {
136 if (policy_request_pending_) 153 cache_->SetDeviceUnmanaged(false);
137 return;
138 SendPolicyRequest(); 154 SendPolicyRequest();
139 } 155 }
140 156
141 void DeviceManagementPolicyProvider::OnTokenError() { 157 void DeviceManagementPolicyProvider::OnTokenError() {
142 LOG(WARNING) << "Could not retrieve device token."; 158 LOG(WARNING) << "Could not retrieve device token.";
143 ScheduleRefreshTask(token_fetch_error_delay_ms_); 159 ScheduleRefreshTask(token_fetch_error_delay_ms_);
144 token_fetch_error_delay_ms_ *= 2; 160 token_fetch_error_delay_ms_ *= 2;
145 if (token_fetch_error_delay_ms_ > policy_refresh_rate_ms_) 161 if (token_fetch_error_delay_ms_ > policy_refresh_rate_ms_)
146 token_fetch_error_delay_ms_ = policy_refresh_rate_ms_; 162 token_fetch_error_delay_ms_ = policy_refresh_rate_ms_;
163 StopWaitingForInitialPolicies();
147 } 164 }
148 165
149 void DeviceManagementPolicyProvider::OnNotManaged() { 166 void DeviceManagementPolicyProvider::OnNotManaged() {
150 VLOG(1) << "This device is not managed."; 167 VLOG(1) << "This device is not managed.";
168 cache_->SetDeviceUnmanaged(true);
169 ScheduleRefreshTask(unmanaged_device_refresh_rate_ms_);
170 StopWaitingForInitialPolicies();
151 } 171 }
152 172
153 void DeviceManagementPolicyProvider::Shutdown() { 173 void DeviceManagementPolicyProvider::Shutdown() {
154 token_service_ = NULL; 174 token_service_ = NULL;
155 if (token_fetcher_) 175 if (token_fetcher_)
156 token_fetcher_->Shutdown(); 176 token_fetcher_->Shutdown();
157 } 177 }
158 178
159 void DeviceManagementPolicyProvider::Initialize() { 179 void DeviceManagementPolicyProvider::Initialize(
180 DeviceManagementBackend* backend,
181 TokenService* token_service,
182 const FilePath& storage_dir,
183 int64 policy_refresh_rate_ms,
184 int64 policy_refresh_max_earlier_ms,
185 int64 policy_refresh_error_delay_ms,
186 int64 token_fetch_error_delay_ms,
187 int64 unmanaged_device_refresh_rate_ms) {
188 backend_.reset(backend);
189 token_service_ = token_service;
190 storage_dir_ = GetOrCreateDeviceManagementDir(storage_dir);
191 policy_request_pending_ = false;
192 refresh_task_pending_ = false;
193 waiting_for_initial_policies_ = true;
194 policy_refresh_rate_ms_ = policy_refresh_rate_ms;
195 policy_refresh_max_earlier_ms_ = policy_refresh_max_earlier_ms;
196 policy_refresh_error_delay_ms_ = policy_refresh_error_delay_ms;
197 token_fetch_error_delay_ms_ = token_fetch_error_delay_ms;
198 unmanaged_device_refresh_rate_ms_ = unmanaged_device_refresh_rate_ms;
199
160 const FilePath policy_path = storage_dir_.Append( 200 const FilePath policy_path = storage_dir_.Append(
161 FILE_PATH_LITERAL("Policy")); 201 FILE_PATH_LITERAL(kPolicyFilename));
162 cache_.reset(new DeviceManagementPolicyCache(policy_path)); 202 cache_.reset(new DeviceManagementPolicyCache(policy_path));
163 cache_->LoadPolicyFromFile(); 203 cache_->LoadPolicyFromFile();
164 204 if (cache_->is_device_unmanaged()) {
165 // Defer initialization that requires the IOThread until after the IOThread 205 // This is a non-first login on an unmanaged device.
166 // has been initialized. 206 waiting_for_initial_policies_ = false;
167 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 207 // Defer token_fetcher_ initialization until this device should ask for
168 new InitializeAfterIOThreadExistsTask(AsWeakPtr())); 208 // a device token again.
209 base::Time unmanaged_timestamp = cache_->last_policy_refresh_time();
210 int64 delay = unmanaged_device_refresh_rate_ms_ -
211 (base::Time::NowFromSystemTime().ToInternalValue() -
212 unmanaged_timestamp.ToInternalValue());
213 if (delay < 0)
214 delay = 0;
215 BrowserThread::PostDelayedTask(
216 BrowserThread::UI, FROM_HERE,
217 new InitializeAfterIOThreadExistsTask(AsWeakPtr()),
218 delay);
219 } else {
220 if (file_util::PathExists(
221 storage_dir_.Append(FILE_PATH_LITERAL(kDeviceTokenFilename)))) {
222 // This is a non-first login on a managed device.
223 waiting_for_initial_policies_ = false;
224 }
Mattias Nissler (ping if slow) 2010/11/26 10:37:45 Why should we check for the file here? Shouldn't w
Jakob Kummerow 2010/11/26 11:07:42 The idea is that the existence of a device token i
225 // Defer initialization that requires the IOThread until after the IOThread
226 // has been initialized.
227 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
228 new InitializeAfterIOThreadExistsTask(AsWeakPtr()));
229 }
169 } 230 }
170 231
171 void DeviceManagementPolicyProvider::InitializeAfterIOThreadExists() { 232 void DeviceManagementPolicyProvider::InitializeAfterIOThreadExists() {
172 const FilePath token_path = storage_dir_.Append( 233 const FilePath token_path = storage_dir_.Append(
173 FILE_PATH_LITERAL("Token")); 234 FILE_PATH_LITERAL(kDeviceTokenFilename));
174 if (token_service_) { 235 if (token_service_) {
175 token_fetcher_ = 236 token_fetcher_ =
176 new DeviceTokenFetcher(backend_.get(), token_service_, token_path); 237 new DeviceTokenFetcher(backend_.get(), token_service_, token_path);
177 registrar_.Init(token_fetcher_); 238 registrar_.Init(token_fetcher_);
178 registrar_.AddObserver(this); 239 registrar_.AddObserver(this);
179 token_fetcher_->StartFetching(); 240 token_fetcher_->StartFetching();
180 } 241 }
181 } 242 }
182 243
183 void DeviceManagementPolicyProvider::SendPolicyRequest() { 244 void DeviceManagementPolicyProvider::SendPolicyRequest() {
184 if (!policy_request_pending_) { 245 if (policy_request_pending_)
185 policy_request_pending_ = true; 246 return;
Mattias Nissler (ping if slow) 2010/11/26 10:37:45 I like newlines after early returns, but that's a
Jakob Kummerow 2010/11/26 11:07:42 Done.
186 em::DevicePolicyRequest policy_request; 247 policy_request_pending_ = true;
187 policy_request.set_policy_scope(kChromePolicyScope); 248 em::DevicePolicyRequest policy_request;
188 em::DevicePolicySettingRequest* setting = 249 policy_request.set_policy_scope(kChromePolicyScope);
189 policy_request.add_setting_request(); 250 em::DevicePolicySettingRequest* setting =
190 setting->set_key(kChromeDevicePolicySettingKey); 251 policy_request.add_setting_request();
191 backend_->ProcessPolicyRequest(token_fetcher_->GetDeviceToken(), 252 setting->set_key(kChromeDevicePolicySettingKey);
192 token_fetcher_->GetDeviceID(), 253 backend_->ProcessPolicyRequest(token_fetcher_->GetDeviceToken(),
193 policy_request, this); 254 token_fetcher_->GetDeviceID(),
194 } 255 policy_request, this);
195 } 256 }
196 257
197 void DeviceManagementPolicyProvider::RefreshTaskExecute() { 258 void DeviceManagementPolicyProvider::RefreshTaskExecute() {
198 DCHECK(refresh_task_pending_); 259 DCHECK(refresh_task_pending_);
199 refresh_task_pending_ = false; 260 refresh_task_pending_ = false;
200 // If there is no valid device token, the token_fetcher_ apparently failed, 261 // If there is no valid device token, the token_fetcher_ apparently failed,
201 // so it must be restarted. 262 // so it must be restarted.
202 if (!token_fetcher_->IsTokenValid()) { 263 if (!token_fetcher_->IsTokenValid()) {
203 if (token_fetcher_->IsTokenPending()) { 264 if (token_fetcher_->IsTokenPending()) {
204 NOTREACHED(); 265 NOTREACHED();
(...skipping 19 matching lines...) Expand all
224 delay_in_milliseconds); 285 delay_in_milliseconds);
225 } 286 }
226 287
227 int64 DeviceManagementPolicyProvider::GetRefreshTaskDelay() { 288 int64 DeviceManagementPolicyProvider::GetRefreshTaskDelay() {
228 int64 delay = policy_refresh_rate_ms_; 289 int64 delay = policy_refresh_rate_ms_;
229 if (policy_refresh_max_earlier_ms_) 290 if (policy_refresh_max_earlier_ms_)
230 delay -= base::RandGenerator(policy_refresh_max_earlier_ms_); 291 delay -= base::RandGenerator(policy_refresh_max_earlier_ms_);
231 return delay; 292 return delay;
232 } 293 }
233 294
295 void DeviceManagementPolicyProvider::StopWaitingForInitialPolicies() {
296 waiting_for_initial_policies_ = false;
297 #if defined(OS_CHROMEOS)
298 // Send a CLOUD_POLICY_UPDATE notification to unblock ChromeOS logins that
299 // are waiting for an initial policy fetch to complete.
300 NotifyCloudPolicyUpdate();
301 #endif
302 }
303
304 #if defined(OS_CHROMEOS)
305 void DeviceManagementPolicyProvider::NotifyCloudPolicyUpdate() const {
306 NotificationService::current()->Notify(
307 NotificationType::CLOUD_POLICY_UPDATE,
308 Source<DeviceManagementPolicyProvider>(this),
309 NotificationService::NoDetails());
310 }
311 #endif
312
234 // static 313 // static
235 std::string DeviceManagementPolicyProvider::GetDeviceManagementURL() { 314 std::string DeviceManagementPolicyProvider::GetDeviceManagementURL() {
236 return CommandLine::ForCurrentProcess()->GetSwitchValueASCII( 315 return CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
237 switches::kDeviceManagementUrl); 316 switches::kDeviceManagementUrl);
238 } 317 }
239 318
240 // static 319 // static
241 FilePath DeviceManagementPolicyProvider::GetOrCreateDeviceManagementDir( 320 FilePath DeviceManagementPolicyProvider::GetOrCreateDeviceManagementDir(
242 const FilePath& user_data_dir) { 321 const FilePath& user_data_dir) {
243 const FilePath device_management_dir = user_data_dir.Append( 322 const FilePath device_management_dir = user_data_dir.Append(
244 FILE_PATH_LITERAL("Device Management")); 323 FILE_PATH_LITERAL("Device Management"));
245 if (!file_util::DirectoryExists(device_management_dir)) { 324 if (!file_util::DirectoryExists(device_management_dir)) {
246 if (!file_util::CreateDirectory(device_management_dir)) 325 if (!file_util::CreateDirectory(device_management_dir))
247 NOTREACHED(); 326 NOTREACHED();
248 } 327 }
249 return device_management_dir; 328 return device_management_dir;
250 } 329 }
251 330
252 bool DeviceManagementPolicyProvider::IsPolicyCacheEmpty() const {
253 return cache_->last_policy_refresh_time().is_null();
254 }
255
256 bool DeviceManagementPolicyProvider::WaitingForInitialPolicies() const { 331 bool DeviceManagementPolicyProvider::WaitingForInitialPolicies() const {
257 return (IsPolicyCacheEmpty() && IsPolicyRequestPending()); 332 return waiting_for_initial_policies_;
Mattias Nissler (ping if slow) 2010/11/26 10:37:45 would this be equivalent to !cache->IsUnmanaged()
danno 2010/11/26 10:48:36 I almost gave the same feedback last time, but nop
258 } 333 }
259 334
260 } // namespace policy 335 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698