1 // Copyright (c) 2011 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 #pragma once 8 9 #include <string> 10 11 #include "base/file_path.h" 12 #include "base/memory/scoped_ptr.h" 13 #include "base/observer_list.h" 14 #include "base/task.h" 15 #include "base/time.h" 16 #include "chrome/browser/policy/cloud_policy_identity_strategy.h" 17 #include "chrome/browser/policy/configuration_policy_provider.h" 18 #include "chrome/browser/policy/device_management_backend.h" 19 #include "chrome/browser/policy/device_token_fetcher.h" 20 21 class Profile; 22 class TokenService; 23 24 namespace policy { 25 26 class CloudPolicyCacheBase; 27 class DeviceManagementBackend; 28 29 // Coordinates the actions of DeviceTokenFetcher, CloudPolicyIdentityStrategy, 30 // DeviceManagementBackend, and CloudPolicyCache: calls their methods and 31 // listens to their callbacks/notifications. 32 class CloudPolicyController 33 : public DeviceManagementBackend::DevicePolicyResponseDelegate, 34 public DeviceTokenFetcher::Observer, 35 public CloudPolicyIdentityStrategy::Observer { 36 public: 37 // Takes ownership of |backend|; the other parameters are weak pointers. 38 CloudPolicyController(DeviceManagementService* service, 39 CloudPolicyCacheBase* cache, 40 DeviceTokenFetcher* token_fetcher, 41 CloudPolicyIdentityStrategy* identity_strategy, 42 PolicyNotifier* notifier); 43 virtual ~CloudPolicyController(); 44 45 // Sets the refresh rate at which to re-fetch policy information. 46 void SetRefreshRate(int64 refresh_rate_milliseconds); 47 48 // Triggers an immediate retry of of the current operation. 49 void Retry(); 50 51 // Stops all auto-retrying error handling behavior inside the policy 52 // subsystem. 53 void StopAutoRetry(); 54 55 // DevicePolicyResponseDelegate implementation: 56 virtual void HandlePolicyResponse( 57 const em::DevicePolicyResponse& response); 58 virtual void OnError(DeviceManagementBackend::ErrorCode code); 59 60 // DeviceTokenFetcher::Observer implementation: 61 virtual void OnDeviceTokenAvailable(); 62 63 // CloudPolicyIdentityStrategy::Observer implementation: 64 virtual void OnDeviceTokenChanged(); 65 virtual void OnCredentialsChanged(); 66 67 private: 68 // Indicates the current state the controller is in. 69 enum ControllerState { 70 // The controller is initializing, policy information not yet available. 71 STATE_TOKEN_UNAVAILABLE, 72 // The device is not managed. Should retry fetching the token after delay. 73 STATE_TOKEN_UNMANAGED, 74 // The token is not valid and should be refetched with exponential back-off. 75 STATE_TOKEN_ERROR, 76 // The token is valid, but policy is yet to be fetched. 77 STATE_TOKEN_VALID, 78 // Policy information is available and valid. 79 STATE_POLICY_VALID, 80 // The service returned an error when requesting policy, will retry. 81 STATE_POLICY_ERROR, 82 // The service returned an error that is not going to go away soon. 83 STATE_POLICY_UNAVAILABLE 84 }; 85 86 friend class CloudPolicyControllerTest; 87 88 // More configurable constructor for use by test cases. 89 CloudPolicyController(DeviceManagementService* service, 90 CloudPolicyCacheBase* cache, 91 DeviceTokenFetcher* token_fetcher, 92 CloudPolicyIdentityStrategy* identity_strategy, 93 PolicyNotifier* notifier, 94 int64 policy_refresh_rate_ms, 95 int policy_refresh_deviation_factor_percent, 96 int64 policy_refresh_deviation_max_ms, 97 int64 policy_refresh_error_delay_ms); 98 99 // Called by constructors to perform shared initialization. 100 void Initialize(DeviceManagementService* service, 101 CloudPolicyCacheBase* cache, 102 DeviceTokenFetcher* token_fetcher, 103 CloudPolicyIdentityStrategy* identity_strategy, 104 PolicyNotifier* notifier, 105 int64 policy_refresh_rate_ms, 106 int policy_refresh_deviation_factor_percent, 107 int64 policy_refresh_deviation_max_ms, 108 int64 policy_refresh_error_delay_ms); 109 110 // Asks the token fetcher to fetch a new token. 111 void FetchToken(); 112 113 // Sends a request to the device management backend to fetch policy if one 114 // isn't already outstanding. 115 void SendPolicyRequest(); 116 117 // Called back from the delayed work task. Calls |DoWork()|. 118 void DoDelayedWork(); 119 120 // Performs whatever action is required in the current state, 121 // e.g. refreshing policy. 122 void DoWork(); 123 124 // Cancels the delayed work task. 125 void CancelDelayedWork(); 126 127 // Switches to a new state and triggers any appropriate actions. 128 void SetState(ControllerState new_state); 129 130 // Computes the policy refresh delay to use. 131 int64 GetRefreshDelay(); 132 133 DeviceManagementService* service_; 134 CloudPolicyCacheBase* cache_; 135 CloudPolicyIdentityStrategy* identity_strategy_; 136 DeviceTokenFetcher* token_fetcher_; 137 scoped_ptr<DeviceManagementBackend> backend_; 138 ControllerState state_; 139 PolicyNotifier* notifier_; 140 141 int64 policy_refresh_rate_ms_; 142 int policy_refresh_deviation_factor_percent_; 143 int64 policy_refresh_deviation_max_ms_; 144 int64 policy_refresh_error_delay_ms_; 145 int64 effective_policy_refresh_error_delay_ms_; 146 147 CancelableTask* delayed_work_task_; 148 ScopedRunnableMethodFactory<CloudPolicyController> method_factory_; 149 150 DISALLOW_COPY_AND_ASSIGN(CloudPolicyController); 151 }; 152 153 } // namespace policy 154 155 #endif // CHROME_BROWSER_POLICY_CLOUD_POLICY_CONTROLLER_H_ 156