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

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: port to ToT/5198006-ps14 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/profile.h" 13 #include "chrome/browser/profile.h"
14 #include "chrome/browser/policy/device_management_backend.h" 14 #include "chrome/browser/policy/device_management_backend.h"
15 #include "chrome/browser/policy/device_management_policy_cache.h" 15 #include "chrome/browser/policy/device_management_policy_cache.h"
16 #include "chrome/browser/policy/proto/device_management_constants.h" 16 #include "chrome/browser/policy/proto/device_management_constants.h"
17 #include "chrome/common/chrome_paths.h" 17 #include "chrome/common/chrome_paths.h"
18 #include "chrome/common/chrome_switches.h" 18 #include "chrome/common/chrome_switches.h"
19 #include "chrome/common/notification_service.h" 19 #include "chrome/common/notification_service.h"
20 #include "chrome/common/notification_type.h" 20 #include "chrome/common/notification_type.h"
21 21
22 namespace policy { 22 namespace policy {
23 23
24 namespace em = enterprise_management;
25
24 const int64 kPolicyRefreshRateInMilliseconds = 3 * 60 * 60 * 1000; // 3 hours 26 const int64 kPolicyRefreshRateInMilliseconds = 3 * 60 * 60 * 1000; // 3 hours
25 const int64 kPolicyRefreshMaxEarlierInMilliseconds = 20 * 60 * 1000; // 20 mins 27 const int64 kPolicyRefreshMaxEarlierInMilliseconds = 20 * 60 * 1000; // 20 mins
26 // 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
27 // will be doubled each time they are used. 29 // will be doubled each time they are used.
28 const int64 kPolicyRefreshErrorDelayInMilliseconds = 3 * 1000; // 3 seconds 30 const int64 kPolicyRefreshErrorDelayInMilliseconds = 3 * 1000; // 3 seconds
29 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";
30 37
31 // Ensures that the portion of the policy provider implementation that requires 38 // Ensures that the portion of the policy provider implementation that requires
32 // 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
33 // 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
34 // 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
35 // threads are fully constructed. 42 // threads are fully constructed.
36 class DeviceManagementPolicyProvider::InitializeAfterIOThreadExistsTask 43 class DeviceManagementPolicyProvider::InitializeAfterIOThreadExistsTask
37 : public Task { 44 : public Task {
38 public: 45 public:
39 explicit InitializeAfterIOThreadExistsTask( 46 explicit InitializeAfterIOThreadExistsTask(
(...skipping 25 matching lines...) Expand all
65 } 72 }
66 73
67 private: 74 private:
68 base::WeakPtr<DeviceManagementPolicyProvider> provider_; 75 base::WeakPtr<DeviceManagementPolicyProvider> provider_;
69 }; 76 };
70 77
71 DeviceManagementPolicyProvider::DeviceManagementPolicyProvider( 78 DeviceManagementPolicyProvider::DeviceManagementPolicyProvider(
72 const ConfigurationPolicyProvider::PolicyDefinitionList* policy_list, 79 const ConfigurationPolicyProvider::PolicyDefinitionList* policy_list,
73 DeviceManagementBackend* backend, 80 DeviceManagementBackend* backend,
74 Profile* profile) 81 Profile* profile)
75 : ConfigurationPolicyProvider(policy_list), 82 : ConfigurationPolicyProvider(policy_list) {
76 backend_(backend), 83 Initialize(backend,
77 profile_(profile), 84 profile,
78 storage_dir_(GetOrCreateDeviceManagementDir(profile_->GetPath())), 85 kPolicyRefreshRateInMilliseconds,
79 policy_request_pending_(false), 86 kPolicyRefreshMaxEarlierInMilliseconds,
80 refresh_task_pending_(false), 87 kPolicyRefreshErrorDelayInMilliseconds,
81 policy_refresh_rate_ms_(kPolicyRefreshRateInMilliseconds), 88 kDeviceTokenRefreshErrorDelayInMilliseconds,
82 policy_refresh_max_earlier_ms_(kPolicyRefreshMaxEarlierInMilliseconds), 89 kPolicyRefreshUnmanagedDeviceInMilliseconds);
83 policy_refresh_error_delay_ms_(kPolicyRefreshErrorDelayInMilliseconds), 90 }
84 token_fetch_error_delay_ms_(kDeviceTokenRefreshErrorDelayInMilliseconds) { 91
85 Initialize(); 92 DeviceManagementPolicyProvider::DeviceManagementPolicyProvider(
93 const PolicyDefinitionList* policy_list,
94 DeviceManagementBackend* backend,
95 Profile* profile,
96 int64 policy_refresh_rate_ms,
97 int64 policy_refresh_max_earlier_ms,
98 int64 policy_refresh_error_delay_ms,
99 int64 token_fetch_error_delay_ms,
100 int64 unmanaged_device_refresh_rate_ms)
101 : ConfigurationPolicyProvider(policy_list) {
102 Initialize(backend,
103 profile,
104 policy_refresh_rate_ms,
105 policy_refresh_max_earlier_ms,
106 policy_refresh_error_delay_ms,
107 token_fetch_error_delay_ms,
108 unmanaged_device_refresh_rate_ms);
86 } 109 }
87 110
88 DeviceManagementPolicyProvider::~DeviceManagementPolicyProvider() {} 111 DeviceManagementPolicyProvider::~DeviceManagementPolicyProvider() {}
89 112
90 bool DeviceManagementPolicyProvider::Provide( 113 bool DeviceManagementPolicyProvider::Provide(
91 ConfigurationPolicyStoreInterface* policy_store) { 114 ConfigurationPolicyStoreInterface* policy_store) {
92 scoped_ptr<DictionaryValue> policies(cache_->GetPolicy()); 115 scoped_ptr<DictionaryValue> policies(cache_->GetPolicy());
93 DecodePolicyValueTree(policies.get(), policy_store); 116 DecodePolicyValueTree(policies.get(), policy_store);
94 return true; 117 return true;
95 } 118 }
96 119
97 void DeviceManagementPolicyProvider::HandlePolicyResponse( 120 void DeviceManagementPolicyProvider::HandlePolicyResponse(
98 const em::DevicePolicyResponse& response) { 121 const em::DevicePolicyResponse& response) {
99 if (cache_->SetPolicy(response)) 122 if (cache_->SetPolicy(response))
100 NotifyStoreOfPolicyChange(); 123 NotifyStoreOfPolicyChange();
101 policy_request_pending_ = false; 124 policy_request_pending_ = false;
102 // Reset the error delay since policy fetching succeeded this time. 125 // Reset the error delay since policy fetching succeeded this time.
103 policy_refresh_error_delay_ms_ = kPolicyRefreshErrorDelayInMilliseconds; 126 policy_refresh_error_delay_ms_ = kPolicyRefreshErrorDelayInMilliseconds;
104 ScheduleRefreshTask(GetRefreshTaskDelay()); 127 ScheduleRefreshTask(GetRefreshTaskDelay());
128 // Update this provider's internal waiting state, but don't notify anyone
129 // else yet (that's done by the PrefValueStore that receives the policy).
130 waiting_for_initial_policies_ = false;
105 } 131 }
106 132
107 void DeviceManagementPolicyProvider::OnError( 133 void DeviceManagementPolicyProvider::OnError(
108 DeviceManagementBackend::ErrorCode code) { 134 DeviceManagementBackend::ErrorCode code) {
109 policy_request_pending_ = false; 135 policy_request_pending_ = false;
110 if (code == DeviceManagementBackend::kErrorServiceDeviceNotFound || 136 if (code == DeviceManagementBackend::kErrorServiceDeviceNotFound ||
111 code == DeviceManagementBackend::kErrorServiceManagementTokenInvalid) { 137 code == DeviceManagementBackend::kErrorServiceManagementTokenInvalid) {
112 LOG(WARNING) << "The device token was either invalid or unknown to the " 138 LOG(WARNING) << "The device token was either invalid or unknown to the "
113 << "device manager, re-registering device."; 139 << "device manager, re-registering device.";
114 token_fetcher_->Restart(); 140 token_fetcher_->Restart();
115 } else { 141 } else {
116 LOG(WARNING) << "Could not provide policy from the device manager (error = " 142 LOG(WARNING) << "Could not provide policy from the device manager (error = "
117 << code << "), will retry in " 143 << code << "), will retry in "
118 << (policy_refresh_error_delay_ms_/1000) << " seconds."; 144 << (policy_refresh_error_delay_ms_/1000) << " seconds.";
119 ScheduleRefreshTask(policy_refresh_error_delay_ms_); 145 ScheduleRefreshTask(policy_refresh_error_delay_ms_);
120 policy_refresh_error_delay_ms_ *= 2; 146 policy_refresh_error_delay_ms_ *= 2;
121 if (policy_refresh_rate_ms_ && 147 if (policy_refresh_rate_ms_ &&
122 policy_refresh_rate_ms_ < policy_refresh_error_delay_ms_) { 148 policy_refresh_rate_ms_ < policy_refresh_error_delay_ms_) {
123 policy_refresh_error_delay_ms_ = policy_refresh_rate_ms_; 149 policy_refresh_error_delay_ms_ = policy_refresh_rate_ms_;
124 } 150 }
125 } 151 }
126 #if defined(OS_CHROMEOS) 152 StopWaitingForInitialPolicies();
127 // Send a CLOUD_POLICY_UPDATE notification to unblock ChromeOS logins that
128 // are waiting for an initial policy fetch to complete.
129 NotifyCloudPolicyUpdate();
130 #endif
131 } 153 }
132 154
133 #if defined(OS_CHROMEOS)
134 void DeviceManagementPolicyProvider::NotifyCloudPolicyUpdate() const {
135 NotificationService::current()->Notify(
136 NotificationType::CLOUD_POLICY_UPDATE,
137 Source<DeviceManagementPolicyProvider>(this),
138 NotificationService::NoDetails());
139 }
140 #endif
141
142 void DeviceManagementPolicyProvider::OnTokenSuccess() { 155 void DeviceManagementPolicyProvider::OnTokenSuccess() {
143 if (policy_request_pending_) 156 cache_->SetDeviceUnmanaged(false);
144 return;
145 SendPolicyRequest(); 157 SendPolicyRequest();
146 } 158 }
147 159
148 void DeviceManagementPolicyProvider::OnTokenError() { 160 void DeviceManagementPolicyProvider::OnTokenError() {
149 LOG(WARNING) << "Could not retrieve device token."; 161 LOG(WARNING) << "Could not retrieve device token.";
150 ScheduleRefreshTask(token_fetch_error_delay_ms_); 162 ScheduleRefreshTask(token_fetch_error_delay_ms_);
151 token_fetch_error_delay_ms_ *= 2; 163 token_fetch_error_delay_ms_ *= 2;
152 if (token_fetch_error_delay_ms_ > policy_refresh_rate_ms_) 164 if (token_fetch_error_delay_ms_ > policy_refresh_rate_ms_)
153 token_fetch_error_delay_ms_ = policy_refresh_rate_ms_; 165 token_fetch_error_delay_ms_ = policy_refresh_rate_ms_;
166 StopWaitingForInitialPolicies();
154 } 167 }
155 168
156 void DeviceManagementPolicyProvider::OnNotManaged() { 169 void DeviceManagementPolicyProvider::OnNotManaged() {
157 VLOG(1) << "This device is not managed."; 170 VLOG(1) << "This device is not managed.";
171 cache_->SetDeviceUnmanaged(true);
172 ScheduleRefreshTask(unmanaged_device_refresh_rate_ms_);
173 StopWaitingForInitialPolicies();
158 } 174 }
159 175
160 void DeviceManagementPolicyProvider::Shutdown() { 176 void DeviceManagementPolicyProvider::Shutdown() {
161 profile_ = NULL; 177 profile_ = NULL;
162 if (token_fetcher_) 178 if (token_fetcher_)
163 token_fetcher_->Shutdown(); 179 token_fetcher_->Shutdown();
164 } 180 }
165 181
166 void DeviceManagementPolicyProvider::Initialize() { 182 void DeviceManagementPolicyProvider::Initialize(
183 DeviceManagementBackend* backend,
184 Profile* profile,
185 int64 policy_refresh_rate_ms,
186 int64 policy_refresh_max_earlier_ms,
187 int64 policy_refresh_error_delay_ms,
188 int64 token_fetch_error_delay_ms,
189 int64 unmanaged_device_refresh_rate_ms) {
190 backend_.reset(backend);
191 profile_ = profile;
192 storage_dir_ = GetOrCreateDeviceManagementDir(profile_->GetPath());
193 policy_request_pending_ = false;
194 refresh_task_pending_ = false;
195 waiting_for_initial_policies_ = true;
196 policy_refresh_rate_ms_ = policy_refresh_rate_ms;
197 policy_refresh_max_earlier_ms_ = policy_refresh_max_earlier_ms;
198 policy_refresh_error_delay_ms_ = policy_refresh_error_delay_ms;
199 token_fetch_error_delay_ms_ = token_fetch_error_delay_ms;
200 unmanaged_device_refresh_rate_ms_ = unmanaged_device_refresh_rate_ms;
201
Mattias Nissler (ping if slow) 2010/11/26 15:07:30 whitespace error?
Jakob Kummerow 2010/11/26 16:04:03 Done.
167 const FilePath policy_path = storage_dir_.Append( 202 const FilePath policy_path = storage_dir_.Append(
168 FILE_PATH_LITERAL("Policy")); 203 FILE_PATH_LITERAL("Policy"));
169 cache_.reset(new DeviceManagementPolicyCache(policy_path)); 204 cache_.reset(new DeviceManagementPolicyCache(policy_path));
170 cache_->LoadPolicyFromFile(); 205 cache_->LoadPolicyFromFile();
171 206
172 // Defer initialization that requires the IOThread until after the IOThread 207 if (cache_->is_device_unmanaged()) {
173 // has been initialized. 208 // This is a non-first login on an unmanaged device.
174 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 209 waiting_for_initial_policies_ = false;
175 new InitializeAfterIOThreadExistsTask(AsWeakPtr())); 210 // Defer token_fetcher_ initialization until this device should ask for
211 // a device token again.
212 base::Time unmanaged_timestamp = cache_->last_policy_refresh_time();
213 int64 delay = unmanaged_device_refresh_rate_ms_ -
214 (base::Time::NowFromSystemTime().ToInternalValue() -
215 unmanaged_timestamp.ToInternalValue());
216 if (delay < 0)
217 delay = 0;
218 BrowserThread::PostDelayedTask(
219 BrowserThread::UI, FROM_HERE,
220 new InitializeAfterIOThreadExistsTask(AsWeakPtr()),
221 delay);
222 } else {
223 if (file_util::PathExists(
224 storage_dir_.Append(FILE_PATH_LITERAL(kDeviceTokenFilename)))) {
225 // This is a non-first login on a managed device.
226 waiting_for_initial_policies_ = false;
227 }
228 // Defer initialization that requires the IOThread until after the IOThread
229 // has been initialized.
230 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
231 new InitializeAfterIOThreadExistsTask(AsWeakPtr()));
232 }
176 } 233 }
177 234
178 void DeviceManagementPolicyProvider::InitializeAfterIOThreadExists() { 235 void DeviceManagementPolicyProvider::InitializeAfterIOThreadExists() {
179 if (profile_) { 236 if (profile_) {
180 if (!token_fetcher_) { 237 if (!token_fetcher_) {
181 token_fetcher_ = new DeviceTokenFetcher( 238 token_fetcher_ = new DeviceTokenFetcher(
182 backend_.get(), profile_, GetTokenPath()); 239 backend_.get(), profile_, GetTokenPath());
183 } 240 }
184 registrar_.Init(token_fetcher_); 241 registrar_.Init(token_fetcher_);
185 registrar_.AddObserver(this); 242 registrar_.AddObserver(this);
186 token_fetcher_->StartFetching(); 243 token_fetcher_->StartFetching();
187 } 244 }
188 } 245 }
189 246
190 void DeviceManagementPolicyProvider::SendPolicyRequest() { 247 void DeviceManagementPolicyProvider::SendPolicyRequest() {
191 if (!policy_request_pending_) { 248 if (policy_request_pending_)
192 policy_request_pending_ = true; 249 » return;
Mattias Nissler (ping if slow) 2010/11/26 15:07:30 remove tab.
Jakob Kummerow 2010/11/26 16:04:03 Done.
193 em::DevicePolicyRequest policy_request; 250
194 policy_request.set_policy_scope(kChromePolicyScope); 251 policy_request_pending_ = true;
195 em::DevicePolicySettingRequest* setting = 252 em::DevicePolicyRequest policy_request;
196 policy_request.add_setting_request(); 253 policy_request.set_policy_scope(kChromePolicyScope);
197 setting->set_key(kChromeDevicePolicySettingKey); 254 em::DevicePolicySettingRequest* setting =
198 setting->set_watermark(""); 255 policy_request.add_setting_request();
199 backend_->ProcessPolicyRequest(token_fetcher_->GetDeviceToken(), 256 setting->set_key(kChromeDevicePolicySettingKey);
200 token_fetcher_->GetDeviceID(), 257 setting->set_watermark("");
201 policy_request, this); 258 backend_->ProcessPolicyRequest(token_fetcher_->GetDeviceToken(),
202 } 259 token_fetcher_->GetDeviceID(),
260 policy_request, this);
203 } 261 }
204 262
205 void DeviceManagementPolicyProvider::RefreshTaskExecute() { 263 void DeviceManagementPolicyProvider::RefreshTaskExecute() {
206 DCHECK(refresh_task_pending_); 264 DCHECK(refresh_task_pending_);
207 refresh_task_pending_ = false; 265 refresh_task_pending_ = false;
208 // If there is no valid device token, the token_fetcher_ apparently failed, 266 // If there is no valid device token, the token_fetcher_ apparently failed,
209 // so it must be restarted. 267 // so it must be restarted.
210 if (!token_fetcher_->IsTokenValid()) { 268 if (!token_fetcher_->IsTokenValid()) {
211 if (token_fetcher_->IsTokenPending()) { 269 if (token_fetcher_->IsTokenPending()) {
212 NOTREACHED(); 270 NOTREACHED();
(...skipping 29 matching lines...) Expand all
242 FilePath DeviceManagementPolicyProvider::GetTokenPath() { 300 FilePath DeviceManagementPolicyProvider::GetTokenPath() {
243 return storage_dir_.Append(FILE_PATH_LITERAL("Token")); 301 return storage_dir_.Append(FILE_PATH_LITERAL("Token"));
244 } 302 }
245 303
246 void DeviceManagementPolicyProvider::SetDeviceTokenFetcher( 304 void DeviceManagementPolicyProvider::SetDeviceTokenFetcher(
247 DeviceTokenFetcher* token_fetcher) { 305 DeviceTokenFetcher* token_fetcher) {
248 DCHECK(!token_fetcher_); 306 DCHECK(!token_fetcher_);
249 token_fetcher_ = token_fetcher; 307 token_fetcher_ = token_fetcher;
250 } 308 }
251 309
310 void DeviceManagementPolicyProvider::StopWaitingForInitialPolicies() {
311 waiting_for_initial_policies_ = false;
312 #if defined(OS_CHROMEOS)
313 // Send a CLOUD_POLICY_UPDATE notification to unblock ChromeOS logins that
314 // are waiting for an initial policy fetch to complete.
315 NotifyCloudPolicyUpdate();
316 #endif
317 }
318
319 #if defined(OS_CHROMEOS)
320 void DeviceManagementPolicyProvider::NotifyCloudPolicyUpdate() const {
321 NotificationService::current()->Notify(
322 NotificationType::CLOUD_POLICY_UPDATE,
323 Source<DeviceManagementPolicyProvider>(this),
324 NotificationService::NoDetails());
325 }
326 #endif
327
252 // static 328 // static
253 std::string DeviceManagementPolicyProvider::GetDeviceManagementURL() { 329 std::string DeviceManagementPolicyProvider::GetDeviceManagementURL() {
254 return CommandLine::ForCurrentProcess()->GetSwitchValueASCII( 330 return CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
255 switches::kDeviceManagementUrl); 331 switches::kDeviceManagementUrl);
256 } 332 }
257 333
258 // static 334 // static
259 FilePath DeviceManagementPolicyProvider::GetOrCreateDeviceManagementDir( 335 FilePath DeviceManagementPolicyProvider::GetOrCreateDeviceManagementDir(
260 const FilePath& user_data_dir) { 336 const FilePath& user_data_dir) {
261 const FilePath device_management_dir = user_data_dir.Append( 337 const FilePath device_management_dir = user_data_dir.Append(
262 FILE_PATH_LITERAL("Device Management")); 338 FILE_PATH_LITERAL("Device Management"));
263 if (!file_util::DirectoryExists(device_management_dir)) { 339 if (!file_util::DirectoryExists(device_management_dir)) {
264 if (!file_util::CreateDirectory(device_management_dir)) 340 if (!file_util::CreateDirectory(device_management_dir))
265 NOTREACHED(); 341 NOTREACHED();
266 } 342 }
267 return device_management_dir; 343 return device_management_dir;
268 } 344 }
269 345
270 bool DeviceManagementPolicyProvider::IsPolicyCacheEmpty() const {
271 return cache_->last_policy_refresh_time().is_null();
272 }
273
274 bool DeviceManagementPolicyProvider::WaitingForInitialPolicies() const { 346 bool DeviceManagementPolicyProvider::WaitingForInitialPolicies() const {
275 return (IsPolicyCacheEmpty() && IsPolicyRequestPending()); 347 return waiting_for_initial_policies_;
276 } 348 }
277 349
278 } // namespace policy 350 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698