| OLD | NEW |
| (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 #ifndef CHROME_BROWSER_POLICY_CLOUD_POLICY_CONTROLLER_H_ | |
| 6 #define CHROME_BROWSER_POLICY_CLOUD_POLICY_CONTROLLER_H_ | |
| 7 | |
| 8 #include "base/basictypes.h" | |
| 9 #include "base/memory/scoped_ptr.h" | |
| 10 #include "base/time.h" | |
| 11 #include "chrome/browser/policy/cloud_policy_constants.h" | |
| 12 #include "chrome/browser/policy/cloud_policy_data_store.h" | |
| 13 | |
| 14 namespace enterprise_management { | |
| 15 class DeviceManagementResponse; | |
| 16 } | |
| 17 | |
| 18 namespace policy { | |
| 19 | |
| 20 class CloudPolicyCacheBase; | |
| 21 class DelayedWorkScheduler; | |
| 22 class DeviceManagementRequestJob; | |
| 23 class DeviceManagementService; | |
| 24 class DeviceTokenFetcher; | |
| 25 class PolicyNotifier; | |
| 26 | |
| 27 // Coordinates the actions of DeviceTokenFetcher, CloudPolicyDataStore, | |
| 28 // and CloudPolicyCache: calls their methods and listens to their | |
| 29 // callbacks/notifications. | |
| 30 class CloudPolicyController : public CloudPolicyDataStore::Observer { | |
| 31 public: | |
| 32 // All parameters are weak pointers. | |
| 33 CloudPolicyController(DeviceManagementService* service, | |
| 34 CloudPolicyCacheBase* cache, | |
| 35 DeviceTokenFetcher* token_fetcher, | |
| 36 CloudPolicyDataStore* data_store, | |
| 37 PolicyNotifier* notifier); | |
| 38 virtual ~CloudPolicyController(); | |
| 39 | |
| 40 // Sets the refresh rate at which to re-fetch policy information. | |
| 41 void SetRefreshRate(int64 refresh_rate_milliseconds); | |
| 42 | |
| 43 // Triggers an immediate retry of the current operation. | |
| 44 void Retry(); | |
| 45 | |
| 46 // Stops any pending activity and resets the controller to unenrolled state. | |
| 47 void Reset(); | |
| 48 | |
| 49 // Attempts to fetch policies again, if possible. The cache is notified that | |
| 50 // a fetch was attempted. If there is no DMToken yet and no material to fetch | |
| 51 // it, the cache is immediately notified that a fetch was attempted unless | |
| 52 // |wait_for_auth_token| is true. In that case, an asynchronous auth token | |
| 53 // fetch should be in progress that will eventually make the controller retry. | |
| 54 void RefreshPolicies(bool wait_for_auth_token); | |
| 55 | |
| 56 // Policy request response handler. | |
| 57 void OnPolicyFetchCompleted( | |
| 58 DeviceManagementStatus status, | |
| 59 const enterprise_management::DeviceManagementResponse& response); | |
| 60 | |
| 61 // CloudPolicyDataStore::Observer implementation: | |
| 62 virtual void OnDeviceTokenChanged() OVERRIDE; | |
| 63 virtual void OnCredentialsChanged() OVERRIDE; | |
| 64 | |
| 65 private: | |
| 66 // Indicates the current state the controller is in. | |
| 67 enum ControllerState { | |
| 68 // The controller is initializing, policy information not yet available. | |
| 69 STATE_TOKEN_UNAVAILABLE, | |
| 70 // The device is not managed. Should retry fetching the token after delay. | |
| 71 STATE_TOKEN_UNMANAGED, | |
| 72 // The token is not valid and should be refetched with exponential back-off. | |
| 73 STATE_TOKEN_ERROR, | |
| 74 // The token is valid, but policy is yet to be fetched. | |
| 75 STATE_TOKEN_VALID, | |
| 76 // Policy information is available and valid. | |
| 77 STATE_POLICY_VALID, | |
| 78 // The service returned an error when requesting policy, will retry. | |
| 79 STATE_POLICY_ERROR, | |
| 80 // The service returned an error that is not going to go away soon. | |
| 81 STATE_POLICY_UNAVAILABLE | |
| 82 }; | |
| 83 | |
| 84 friend class CloudPolicyControllerTest; | |
| 85 friend class TestingCloudPolicySubsystem; | |
| 86 | |
| 87 // More configurable constructor for use by test cases. | |
| 88 // Takes ownership of |scheduler|; the other parameters are weak pointers. | |
| 89 CloudPolicyController(DeviceManagementService* service, | |
| 90 CloudPolicyCacheBase* cache, | |
| 91 DeviceTokenFetcher* token_fetcher, | |
| 92 CloudPolicyDataStore* data_store, | |
| 93 PolicyNotifier* notifier, | |
| 94 DelayedWorkScheduler* scheduler); | |
| 95 | |
| 96 // Called by constructors to perform shared initialization. | |
| 97 void Initialize(DeviceManagementService* service, | |
| 98 CloudPolicyCacheBase* cache, | |
| 99 DeviceTokenFetcher* token_fetcher, | |
| 100 CloudPolicyDataStore* data_store, | |
| 101 PolicyNotifier* notifier, | |
| 102 DelayedWorkScheduler* scheduler); | |
| 103 | |
| 104 // Checks if the controller is ready to fetch the DMToken. | |
| 105 bool ReadyToFetchToken(); | |
| 106 | |
| 107 // Asks the token fetcher to fetch a new token. | |
| 108 void FetchToken(); | |
| 109 | |
| 110 // Sends a request to the device management backend to fetch policy if one | |
| 111 // isn't already outstanding. | |
| 112 void SendPolicyRequest(); | |
| 113 | |
| 114 // Called back from |scheduler_|. | |
| 115 // Performs whatever action is required in the current state, | |
| 116 // e.g. refreshing policy. | |
| 117 void DoWork(); | |
| 118 | |
| 119 // Switches to a new state and triggers any appropriate actions. | |
| 120 void SetState(ControllerState new_state); | |
| 121 | |
| 122 // Computes the policy refresh delay to use. | |
| 123 base::TimeDelta GetRefreshDelay(); | |
| 124 | |
| 125 // Schedules a DoWork() invocation |delay| time units later. | |
| 126 void ScheduleDelayedWorkTask(const base::TimeDelta& delay); | |
| 127 | |
| 128 // Gets the last refresh time, defaulting to |now| if there is none. | |
| 129 base::Time GetLastRefreshTime(const base::Time& now); | |
| 130 | |
| 131 DeviceManagementService* service_; | |
| 132 CloudPolicyCacheBase* cache_; | |
| 133 CloudPolicyDataStore* data_store_; | |
| 134 DeviceTokenFetcher* token_fetcher_; | |
| 135 scoped_ptr<DeviceManagementRequestJob> request_job_; | |
| 136 ControllerState state_; | |
| 137 PolicyNotifier* notifier_; | |
| 138 | |
| 139 int64 policy_refresh_rate_ms_; | |
| 140 int64 effective_policy_refresh_error_delay_ms_; | |
| 141 | |
| 142 scoped_ptr<DelayedWorkScheduler> scheduler_; | |
| 143 | |
| 144 DISALLOW_COPY_AND_ASSIGN(CloudPolicyController); | |
| 145 }; | |
| 146 | |
| 147 } // namespace policy | |
| 148 | |
| 149 #endif // CHROME_BROWSER_POLICY_CLOUD_POLICY_CONTROLLER_H_ | |
| OLD | NEW |