summaryrefslogtreecommitdiffstats
path: root/chrome/browser/policy/cloud_policy_controller.h
blob: d7c971b734421aece334af6d065beb66a5d3a262 (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
// Copyright (c) 2012 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.

#ifndef CHROME_BROWSER_POLICY_CLOUD_POLICY_CONTROLLER_H_
#define CHROME_BROWSER_POLICY_CLOUD_POLICY_CONTROLLER_H_
#pragma once

#include "base/basictypes.h"
#include "base/memory/scoped_ptr.h"
#include "chrome/browser/policy/cloud_policy_constants.h"
#include "chrome/browser/policy/cloud_policy_data_store.h"

namespace enterprise_management {
class DeviceManagementResponse;
}

namespace policy {

class CloudPolicyCacheBase;
class DelayedWorkScheduler;
class DeviceManagementRequestJob;
class DeviceManagementService;
class DeviceTokenFetcher;
class PolicyNotifier;

// Coordinates the actions of DeviceTokenFetcher, CloudPolicyDataStore,
// and CloudPolicyCache: calls their methods and listens to their
// callbacks/notifications.
class CloudPolicyController : public CloudPolicyDataStore::Observer {
 public:
  // All parameters are weak pointers.
  CloudPolicyController(DeviceManagementService* service,
                        CloudPolicyCacheBase* cache,
                        DeviceTokenFetcher* token_fetcher,
                        CloudPolicyDataStore* data_store,
                        PolicyNotifier* notifier);
  virtual ~CloudPolicyController();

  // Sets the refresh rate at which to re-fetch policy information.
  void SetRefreshRate(int64 refresh_rate_milliseconds);

  // Triggers an immediate retry of the current operation.
  void Retry();

  // Stops any pending activity and resets the controller to unenrolled state.
  void Reset();

  // Attempts to fetch policies again, if possible. The cache is notified that
  // a fetch was attempted.
  void RefreshPolicies();

  // Policy request response handler.
  void OnPolicyFetchCompleted(
      DeviceManagementStatus status,
      const enterprise_management::DeviceManagementResponse& response);

  // CloudPolicyDataStore::Observer implementation:
  virtual void OnDeviceTokenChanged() OVERRIDE;
  virtual void OnCredentialsChanged() OVERRIDE;

 private:
  // Indicates the current state the controller is in.
  enum ControllerState {
    // The controller is initializing, policy information not yet available.
    STATE_TOKEN_UNAVAILABLE,
    // The device is not managed. Should retry fetching the token after delay.
    STATE_TOKEN_UNMANAGED,
    // The token is not valid and should be refetched with exponential back-off.
    STATE_TOKEN_ERROR,
    // The token is valid, but policy is yet to be fetched.
    STATE_TOKEN_VALID,
    // Policy information is available and valid.
    STATE_POLICY_VALID,
    // The service returned an error when requesting policy, will retry.
    STATE_POLICY_ERROR,
    // The service returned an error that is not going to go away soon.
    STATE_POLICY_UNAVAILABLE
  };

  friend class CloudPolicyControllerTest;
  friend class TestingCloudPolicySubsystem;

  // More configurable constructor for use by test cases.
  // Takes ownership of |scheduler|; the other parameters are weak pointers.
  CloudPolicyController(DeviceManagementService* service,
                        CloudPolicyCacheBase* cache,
                        DeviceTokenFetcher* token_fetcher,
                        CloudPolicyDataStore* data_store,
                        PolicyNotifier* notifier,
                        DelayedWorkScheduler* scheduler);

  // Called by constructors to perform shared initialization.
  void Initialize(DeviceManagementService* service,
                  CloudPolicyCacheBase* cache,
                  DeviceTokenFetcher* token_fetcher,
                  CloudPolicyDataStore* data_store,
                  PolicyNotifier* notifier,
                  DelayedWorkScheduler* scheduler);

  // Checks if the controller is ready to fetch the DMToken.
  bool ReadyToFetchToken();

  // Asks the token fetcher to fetch a new token.
  void FetchToken();

  // Sends a request to the device management backend to fetch policy if one
  // isn't already outstanding.
  void SendPolicyRequest();

  // Called back from |scheduler_|.
  // Performs whatever action is required in the current state,
  // e.g. refreshing policy.
  void DoWork();

  // Switches to a new state and triggers any appropriate actions.
  void SetState(ControllerState new_state);

  // Computes the policy refresh delay to use.
  int64 GetRefreshDelay();

  DeviceManagementService* service_;
  CloudPolicyCacheBase* cache_;
  CloudPolicyDataStore* data_store_;
  DeviceTokenFetcher* token_fetcher_;
  scoped_ptr<DeviceManagementRequestJob> request_job_;
  ControllerState state_;
  PolicyNotifier* notifier_;

  int64 policy_refresh_rate_ms_;
  int64 effective_policy_refresh_error_delay_ms_;

  scoped_ptr<DelayedWorkScheduler> scheduler_;

  DISALLOW_COPY_AND_ASSIGN(CloudPolicyController);
};

}  // namespace policy

#endif  // CHROME_BROWSER_POLICY_CLOUD_POLICY_CONTROLLER_H_