summaryrefslogtreecommitdiffstats
path: root/chrome/browser/policy/device_management_policy_provider.cc
blob: 1a1f0c240cb48a5fc38af218cb50a21b7457cae8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/policy/device_management_policy_provider.h"

#include "base/command_line.h"
#include "base/file_util.h"
#include "base/path_service.h"
#include "base/rand_util.h"
#include "base/task.h"
#include "chrome/browser/browser_thread.h"
#include "chrome/browser/policy/device_management_backend.h"
#include "chrome/browser/policy/device_management_policy_cache.h"
#include "chrome/browser/policy/proto/device_management_constants.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/notification_service.h"
#include "chrome/common/notification_type.h"

namespace policy {

namespace em = enterprise_management;

const int64 kPolicyRefreshRateInMilliseconds = 3 * 60 * 60 * 1000;  // 3 hours
const int64 kPolicyRefreshMaxEarlierInMilliseconds = 20 * 60 * 1000;  // 20 mins
// These are the base values for delays before retrying after an error. They
// will be doubled each time they are used.
const int64 kPolicyRefreshErrorDelayInMilliseconds = 3 * 1000;  // 3 seconds
const int64 kDeviceTokenRefreshErrorDelayInMilliseconds = 3 * 1000;
// For unmanaged devices, check once per day whether they're still unmanaged.
const int64 kPolicyRefreshUnmanagedDeviceInMilliseconds = 24 * 60 * 60 * 1000;

const FilePath::StringType kDeviceTokenFilename = FILE_PATH_LITERAL("Token");
const FilePath::StringType kPolicyFilename = FILE_PATH_LITERAL("Policy");

// Ensures that the portion of the policy provider implementation that requires
// the IOThread is deferred until the IOThread is fully initialized. The policy
// provider posts this task on the UI thread during its constructor, thereby
// guaranteeing that the code won't get executed until after the UI and IO
// threads are fully constructed.
class DeviceManagementPolicyProvider::InitializeAfterIOThreadExistsTask
    : public Task {
 public:
  explicit InitializeAfterIOThreadExistsTask(
      base::WeakPtr<DeviceManagementPolicyProvider> provider)
      : provider_(provider) {
  }

  // Task implementation:
  virtual void Run() {
    DeviceManagementPolicyProvider* provider = provider_.get();
    if (provider)
      provider->InitializeAfterIOThreadExists();
  }

 private:
  base::WeakPtr<DeviceManagementPolicyProvider> provider_;
};

class DeviceManagementPolicyProvider::RefreshTask : public Task {
 public:
  explicit RefreshTask(base::WeakPtr<DeviceManagementPolicyProvider> provider)
      : provider_(provider) {}

  // Task implementation:
  virtual void Run() {
    DeviceManagementPolicyProvider* provider = provider_.get();
    if (provider)
      provider->RefreshTaskExecute();
  }

 private:
  base::WeakPtr<DeviceManagementPolicyProvider> provider_;
};

DeviceManagementPolicyProvider::DeviceManagementPolicyProvider(
    const ConfigurationPolicyProvider::PolicyDefinitionList* policy_list,
    DeviceManagementBackend* backend,
    Profile* profile)
    : ConfigurationPolicyProvider(policy_list) {
  Initialize(backend,
             profile,
             kPolicyRefreshRateInMilliseconds,
             kPolicyRefreshMaxEarlierInMilliseconds,
             kPolicyRefreshErrorDelayInMilliseconds,
             kDeviceTokenRefreshErrorDelayInMilliseconds,
             kPolicyRefreshUnmanagedDeviceInMilliseconds);
}

DeviceManagementPolicyProvider::DeviceManagementPolicyProvider(
    const PolicyDefinitionList* policy_list,
    DeviceManagementBackend* backend,
    Profile* profile,
    int64 policy_refresh_rate_ms,
    int64 policy_refresh_max_earlier_ms,
    int64 policy_refresh_error_delay_ms,
    int64 token_fetch_error_delay_ms,
    int64 unmanaged_device_refresh_rate_ms)
    : ConfigurationPolicyProvider(policy_list) {
  Initialize(backend,
             profile,
             policy_refresh_rate_ms,
             policy_refresh_max_earlier_ms,
             policy_refresh_error_delay_ms,
             token_fetch_error_delay_ms,
             unmanaged_device_refresh_rate_ms);
}

DeviceManagementPolicyProvider::~DeviceManagementPolicyProvider() {}

bool DeviceManagementPolicyProvider::Provide(
    ConfigurationPolicyStoreInterface* policy_store) {
  scoped_ptr<DictionaryValue> policies(cache_->GetPolicy());
  DecodePolicyValueTree(policies.get(), policy_store);
  return true;
}

bool DeviceManagementPolicyProvider::IsInitializationComplete() const {
  return !waiting_for_initial_policies_;
}

void DeviceManagementPolicyProvider::HandlePolicyResponse(
    const em::DevicePolicyResponse& response) {
  if (cache_->SetPolicy(response))
    NotifyCloudPolicyUpdate();
  policy_request_pending_ = false;
  // Reset the error delay since policy fetching succeeded this time.
  policy_refresh_error_delay_ms_ = kPolicyRefreshErrorDelayInMilliseconds;
  ScheduleRefreshTask(GetRefreshTaskDelay());
  // Update this provider's internal waiting state, but don't notify anyone
  // else yet (that's done by the PrefValueStore that receives the policy).
  waiting_for_initial_policies_ = false;
}

void DeviceManagementPolicyProvider::OnError(
    DeviceManagementBackend::ErrorCode code) {
  policy_request_pending_ = false;
  if (code == DeviceManagementBackend::kErrorServiceDeviceNotFound ||
      code == DeviceManagementBackend::kErrorServiceManagementTokenInvalid) {
    LOG(WARNING) << "The device token was either invalid or unknown to the "
                 << "device manager, re-registering device.";
    token_fetcher_->Restart();
  } else if (code ==
             DeviceManagementBackend::kErrorServiceManagementNotSupported) {
    VLOG(1) << "The device is no longer managed, resetting device token.";
    token_fetcher_->Restart();
  } else {
    LOG(WARNING) << "Could not provide policy from the device manager (error = "
                 << code << "), will retry in "
                 << (policy_refresh_error_delay_ms_/1000) << " seconds.";
    ScheduleRefreshTask(policy_refresh_error_delay_ms_);
    policy_refresh_error_delay_ms_ *= 2;
    if (policy_refresh_rate_ms_ &&
        policy_refresh_rate_ms_ < policy_refresh_error_delay_ms_) {
      policy_refresh_error_delay_ms_ = policy_refresh_rate_ms_;
    }
  }
  StopWaitingForInitialPolicies();
}

void DeviceManagementPolicyProvider::OnTokenSuccess() {
  if (policy_request_pending_)
    return;
  cache_->SetDeviceUnmanaged(false);
  SendPolicyRequest();
}

void DeviceManagementPolicyProvider::OnTokenError() {
  LOG(WARNING) << "Could not retrieve device token.";
  ScheduleRefreshTask(token_fetch_error_delay_ms_);
  token_fetch_error_delay_ms_ *= 2;
  if (token_fetch_error_delay_ms_ > policy_refresh_rate_ms_)
    token_fetch_error_delay_ms_ = policy_refresh_rate_ms_;
  StopWaitingForInitialPolicies();
}

void DeviceManagementPolicyProvider::OnNotManaged() {
  VLOG(1) << "This device is not managed.";
  cache_->SetDeviceUnmanaged(true);
  ScheduleRefreshTask(unmanaged_device_refresh_rate_ms_);
  StopWaitingForInitialPolicies();
}

void DeviceManagementPolicyProvider::Shutdown() {
  profile_ = NULL;
  if (token_fetcher_)
    token_fetcher_->Shutdown();
}

void DeviceManagementPolicyProvider::AddObserver(
    ConfigurationPolicyProvider::Observer* observer) {
  observer_list_.AddObserver(observer);
}

void DeviceManagementPolicyProvider::RemoveObserver(
    ConfigurationPolicyProvider::Observer* observer) {
  observer_list_.RemoveObserver(observer);
}

void DeviceManagementPolicyProvider::NotifyCloudPolicyUpdate() {
  FOR_EACH_OBSERVER(ConfigurationPolicyProvider::Observer,
                    observer_list_,
                    OnUpdatePolicy());
}

void DeviceManagementPolicyProvider::Initialize(
    DeviceManagementBackend* backend,
    Profile* profile,
    int64 policy_refresh_rate_ms,
    int64 policy_refresh_max_earlier_ms,
    int64 policy_refresh_error_delay_ms,
    int64 token_fetch_error_delay_ms,
    int64 unmanaged_device_refresh_rate_ms) {
  backend_.reset(backend);
  profile_ = profile;
  storage_dir_ = GetOrCreateDeviceManagementDir(profile_->GetPath());
  policy_request_pending_ = false;
  refresh_task_pending_ = false;
  waiting_for_initial_policies_ = true;
  policy_refresh_rate_ms_ = policy_refresh_rate_ms;
  policy_refresh_max_earlier_ms_ = policy_refresh_max_earlier_ms;
  policy_refresh_error_delay_ms_ = policy_refresh_error_delay_ms;
  token_fetch_error_delay_ms_ = token_fetch_error_delay_ms;
  unmanaged_device_refresh_rate_ms_ = unmanaged_device_refresh_rate_ms;

  const FilePath policy_path = storage_dir_.Append(kPolicyFilename);
  cache_.reset(new DeviceManagementPolicyCache(policy_path));
  cache_->LoadPolicyFromFile();

  if (cache_->is_device_unmanaged()) {
    // This is a non-first login on an unmanaged device.
    waiting_for_initial_policies_ = false;
    // Defer token_fetcher_ initialization until this device should ask for
    // a device token again.
    base::Time unmanaged_timestamp = cache_->last_policy_refresh_time();
    int64 delay = unmanaged_device_refresh_rate_ms_ -
        (base::Time::NowFromSystemTime().ToInternalValue() -
            unmanaged_timestamp.ToInternalValue());
    if (delay < 0)
      delay = 0;
    BrowserThread::PostDelayedTask(
        BrowserThread::UI, FROM_HERE,
        new InitializeAfterIOThreadExistsTask(AsWeakPtr()),
        delay);
  } else {
    if (file_util::PathExists(
        storage_dir_.Append(kDeviceTokenFilename))) {
      // This is a non-first login on a managed device.
      waiting_for_initial_policies_ = false;
    }
    // Defer initialization that requires the IOThread until after the IOThread
    // has been initialized.
    BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
                            new InitializeAfterIOThreadExistsTask(AsWeakPtr()));
  }
}

void DeviceManagementPolicyProvider::InitializeAfterIOThreadExists() {
  if (profile_) {
    if (!token_fetcher_) {
      token_fetcher_ = new DeviceTokenFetcher(
          backend_.get(), profile_, GetTokenPath());
    }
    registrar_.Init(token_fetcher_);
    registrar_.AddObserver(this);
    token_fetcher_->StartFetching();
  }
}

void DeviceManagementPolicyProvider::SendPolicyRequest() {
  if (policy_request_pending_)
    return;

  policy_request_pending_ = true;
  em::DevicePolicyRequest policy_request;
  policy_request.set_policy_scope(kChromePolicyScope);
  em::DevicePolicySettingRequest* setting =
      policy_request.add_setting_request();
  setting->set_key(kChromeDevicePolicySettingKey);
  setting->set_watermark("");
  backend_->ProcessPolicyRequest(token_fetcher_->GetDeviceToken(),
                                 token_fetcher_->GetDeviceID(),
                                 policy_request, this);
}

void DeviceManagementPolicyProvider::RefreshTaskExecute() {
  DCHECK(refresh_task_pending_);
  refresh_task_pending_ = false;
  // If there is no valid device token, the token_fetcher_ apparently failed,
  // so it must be restarted.
  if (!token_fetcher_->IsTokenValid()) {
    if (token_fetcher_->IsTokenPending()) {
      NOTREACHED();
      return;
    }
    token_fetcher_->Restart();
    return;
  }
  // If there is a device token, just refresh policies.
  SendPolicyRequest();
}

void DeviceManagementPolicyProvider::ScheduleRefreshTask(
    int64 delay_in_milliseconds) {
  // This check is simply a safeguard, the situation currently cannot happen.
  if (refresh_task_pending_) {
    NOTREACHED();
    return;
  }
  refresh_task_pending_ = true;
  BrowserThread::PostDelayedTask(BrowserThread::UI, FROM_HERE,
                                 new RefreshTask(AsWeakPtr()),
                                 delay_in_milliseconds);
}

int64 DeviceManagementPolicyProvider::GetRefreshTaskDelay() {
  int64 delay = policy_refresh_rate_ms_;
  if (policy_refresh_max_earlier_ms_)
    delay -= base::RandGenerator(policy_refresh_max_earlier_ms_);
  return delay;
}

FilePath DeviceManagementPolicyProvider::GetTokenPath() {
  return storage_dir_.Append(kDeviceTokenFilename);
}

void DeviceManagementPolicyProvider::SetDeviceTokenFetcher(
    DeviceTokenFetcher* token_fetcher) {
  DCHECK(!token_fetcher_);
  token_fetcher_ = token_fetcher;
}

void DeviceManagementPolicyProvider::StopWaitingForInitialPolicies() {
  waiting_for_initial_policies_ = false;
  // Notify observers that initial policy fetch is complete.
  NotifyCloudPolicyUpdate();
}

// static
std::string DeviceManagementPolicyProvider::GetDeviceManagementURL() {
  return CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
      switches::kDeviceManagementUrl);
}

// static
FilePath DeviceManagementPolicyProvider::GetOrCreateDeviceManagementDir(
    const FilePath& user_data_dir) {
  const FilePath device_management_dir = user_data_dir.Append(
      FILE_PATH_LITERAL("Device Management"));
  if (!file_util::DirectoryExists(device_management_dir)) {
    if (!file_util::CreateDirectory(device_management_dir))
      NOTREACHED();
  }
  return device_management_dir;
}

}  // namespace policy