| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef CHROME_BROWSER_POLICY_DEVICE_TOKEN_FETCHER_H_ | 5 #ifndef CHROME_BROWSER_POLICY_DEVICE_TOKEN_FETCHER_H_ |
| 6 #define CHROME_BROWSER_POLICY_DEVICE_TOKEN_FETCHER_H_ | 6 #define CHROME_BROWSER_POLICY_DEVICE_TOKEN_FETCHER_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/observer_list.h" | |
| 13 #include "base/task.h" | |
| 14 #include "chrome/browser/policy/delayed_work_scheduler.h" | 10 #include "chrome/browser/policy/delayed_work_scheduler.h" |
| 15 #include "chrome/browser/policy/device_management_backend.h" | 11 #include "chrome/browser/policy/device_management_backend.h" |
| 16 #include "chrome/browser/policy/policy_notifier.h" | 12 #include "chrome/browser/policy/policy_notifier.h" |
| 17 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | 13 #include "chrome/browser/policy/proto/device_management_backend.pb.h" |
| 18 | 14 |
| 19 namespace policy { | 15 namespace policy { |
| 20 | 16 |
| 21 class CloudPolicyCacheBase; | 17 class CloudPolicyCacheBase; |
| 18 class CloudPolicyDataStore; |
| 22 class DeviceManagementService; | 19 class DeviceManagementService; |
| 23 | 20 |
| 24 namespace em = enterprise_management; | 21 namespace em = enterprise_management; |
| 25 | 22 |
| 26 // Fetches the device token that can be used for policy requests with the device | 23 // Fetches the device token that can be used for policy requests with the device |
| 27 // management server, either from disk if it already has been successfully | 24 // management server, either from disk if it already has been successfully |
| 28 // requested, otherwise from the device management server. An instance of the | 25 // requested, otherwise from the device management server. An instance of the |
| 29 // fetcher is shared as a singleton by all users of the device management token | 26 // fetcher is shared as a singleton by all users of the device management token |
| 30 // to ensure they all get the same token. | 27 // to ensure they all get the same token. |
| 31 class DeviceTokenFetcher | 28 class DeviceTokenFetcher |
| 32 : public DeviceManagementBackend::DeviceRegisterResponseDelegate { | 29 : public DeviceManagementBackend::DeviceRegisterResponseDelegate { |
| 33 public: | 30 public: |
| 34 class Observer { | |
| 35 public: | |
| 36 virtual ~Observer() {} | |
| 37 virtual void OnDeviceTokenAvailable() = 0; | |
| 38 }; | |
| 39 | |
| 40 // |service| is used to talk to the device management service and |cache| is | 31 // |service| is used to talk to the device management service and |cache| is |
| 41 // used to persist whether the device is unmanaged. | 32 // used to persist whether the device is unmanaged. |
| 42 DeviceTokenFetcher(DeviceManagementService* service, | 33 DeviceTokenFetcher(DeviceManagementService* service, |
| 43 CloudPolicyCacheBase* cache, | 34 CloudPolicyCacheBase* cache, |
| 35 CloudPolicyDataStore* data_store, |
| 44 PolicyNotifier* notifier); | 36 PolicyNotifier* notifier); |
| 45 // Version for tests that allows to set timing parameters. | 37 // Version for tests that allows to set timing parameters. |
| 46 // Takes ownership of |scheduler|. | 38 // Takes ownership of |scheduler|. |
| 47 DeviceTokenFetcher(DeviceManagementService* service, | 39 DeviceTokenFetcher(DeviceManagementService* service, |
| 48 CloudPolicyCacheBase* cache, | 40 CloudPolicyCacheBase* cache, |
| 41 CloudPolicyDataStore* data_store, |
| 49 PolicyNotifier* notifier, | 42 PolicyNotifier* notifier, |
| 50 DelayedWorkScheduler* scheduler); | 43 DelayedWorkScheduler* scheduler); |
| 51 virtual ~DeviceTokenFetcher(); | 44 virtual ~DeviceTokenFetcher(); |
| 52 | 45 |
| 53 // Starts fetching a token. | 46 // Starts fetching a token. |
| 54 // Declared virtual so it can be overridden by mocks. | 47 // Declared virtual so it can be overridden by mocks. |
| 55 virtual void FetchToken(const std::string& auth_token, | 48 virtual void FetchToken(); |
| 56 const std::string& device_id, | |
| 57 em::DeviceRegisterRequest_Type policy_type, | |
| 58 const std::string& machine_id, | |
| 59 const std::string& machine_model); | |
| 60 | 49 |
| 61 virtual void SetUnmanagedState(); | 50 virtual void SetUnmanagedState(); |
| 62 | 51 |
| 63 // Returns the device management token or the empty string if not available. | |
| 64 // Declared virtual so it can be overridden by mocks. | |
| 65 virtual const std::string& GetDeviceToken(); | |
| 66 | |
| 67 // Disables the auto-retry-on-error behavior of this token fetcher. | 52 // Disables the auto-retry-on-error behavior of this token fetcher. |
| 68 void StopAutoRetry(); | 53 void StopAutoRetry(); |
| 69 | 54 |
| 70 void AddObserver(Observer* observer); | |
| 71 void RemoveObserver(Observer* observer); | |
| 72 | |
| 73 // DeviceManagementBackend::DeviceRegisterResponseDelegate method overrides: | 55 // DeviceManagementBackend::DeviceRegisterResponseDelegate method overrides: |
| 74 virtual void HandleRegisterResponse( | 56 virtual void HandleRegisterResponse( |
| 75 const em::DeviceRegisterResponse& response); | 57 const em::DeviceRegisterResponse& response) OVERRIDE; |
| 76 virtual void OnError(DeviceManagementBackend::ErrorCode code); | 58 virtual void OnError(DeviceManagementBackend::ErrorCode code) OVERRIDE; |
| 77 | 59 |
| 78 private: | 60 private: |
| 79 friend class DeviceTokenFetcherTest; | 61 friend class DeviceTokenFetcherTest; |
| 80 | 62 |
| 81 // The different states that the fetcher can be in during the process of | 63 // The different states that the fetcher can be in during the process of |
| 82 // getting the device token. |state_| is initialized to INACTIVE, depending | 64 // getting the device token. |state_| is initialized to INACTIVE, depending |
| 83 // on the result of a token fetching attempt can transition to either of | 65 // on the result of a token fetching attempt can transition to either of |
| 84 // TOKEN_AVAILABLE, UNMANAGED, or ERROR. The first attempt must be triggered | 66 // TOKEN_AVAILABLE, UNMANAGED, or ERROR. The first attempt must be triggered |
| 85 // externally. When |state_| is UNMANAGED, a new fetching attempt is | 67 // externally. When |state_| is UNMANAGED, a new fetching attempt is |
| 86 // performed every |unmanaged_device_refresh_rate_ms_|; when it's ERROR, | 68 // performed every |unmanaged_device_refresh_rate_ms_|; when it's ERROR, |
| 87 // a new attempt is done after |effective_token_fetch_error_delay_ms_|. | 69 // a new attempt is done after |effective_token_fetch_error_delay_ms_|. |
| 88 enum FetcherState { | 70 enum FetcherState { |
| 89 // Fetcher inactive. | 71 // Fetcher inactive. |
| 90 STATE_INACTIVE, | 72 STATE_INACTIVE, |
| 91 // Token available. | 73 // Token available. |
| 92 STATE_TOKEN_AVAILABLE, | 74 STATE_TOKEN_AVAILABLE, |
| 93 // Device unmanaged. | 75 // Device unmanaged. |
| 94 STATE_UNMANAGED, | 76 STATE_UNMANAGED, |
| 95 // Error, retry later. | 77 // Error, retry later. |
| 96 STATE_ERROR, | 78 STATE_ERROR, |
| 97 // Temporary error. Retry sooner. | 79 // Temporary error. Retry sooner. |
| 98 STATE_TEMPORARY_ERROR, | 80 STATE_TEMPORARY_ERROR, |
| 99 // Server rejected the auth token. | 81 // Server rejected the auth token. |
| 100 STATE_BAD_AUTH | 82 STATE_BAD_AUTH |
| 101 }; | 83 }; |
| 102 | 84 |
| 103 // Common initialization helper. | 85 // Common initialization helper. |
| 104 void Initialize(DeviceManagementService* service, | 86 void Initialize(DeviceManagementService* service, |
| 105 CloudPolicyCacheBase* cache, | 87 CloudPolicyCacheBase* cache, |
| 88 CloudPolicyDataStore* data, |
| 106 PolicyNotifier* notifier, | 89 PolicyNotifier* notifier, |
| 107 DelayedWorkScheduler* scheduler); | 90 DelayedWorkScheduler* scheduler); |
| 108 | 91 |
| 109 // Moves the fetcher into a new state. | 92 // Moves the fetcher into a new state. |
| 110 void SetState(FetcherState state); | 93 void SetState(FetcherState state); |
| 111 | 94 |
| 112 // Resets |backend_|, then uses |auth_token_| and |device_id_| to perform | 95 // Resets |backend_|, then uses |auth_token_| and |device_id_| to perform |
| 113 // an actual token fetch. | 96 // an actual token fetch. |
| 114 void FetchTokenInternal(); | 97 void FetchTokenInternal(); |
| 115 | 98 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 128 | 111 |
| 129 // Refresh parameters. | 112 // Refresh parameters. |
| 130 int64 token_fetch_error_delay_ms_; | 113 int64 token_fetch_error_delay_ms_; |
| 131 int64 token_fetch_error_max_delay_ms_; | 114 int64 token_fetch_error_max_delay_ms_; |
| 132 int64 effective_token_fetch_error_delay_ms_; | 115 int64 effective_token_fetch_error_delay_ms_; |
| 133 int64 unmanaged_device_refresh_rate_ms_; | 116 int64 unmanaged_device_refresh_rate_ms_; |
| 134 | 117 |
| 135 // State the fetcher is currently in. | 118 // State the fetcher is currently in. |
| 136 FetcherState state_; | 119 FetcherState state_; |
| 137 | 120 |
| 138 // Current device token. | 121 CloudPolicyDataStore* data_store_; |
| 139 std::string device_token_; | |
| 140 | |
| 141 // Contains the AuthToken for the device management server. | |
| 142 std::string auth_token_; | |
| 143 // Device identifier to send to the server. | |
| 144 std::string device_id_; | |
| 145 // Contains policy type to send to the server. | |
| 146 em::DeviceRegisterRequest_Type policy_type_; | |
| 147 // Contains physical machine id to send to the server. | |
| 148 std::string machine_id_; | |
| 149 // Contains physical machine model to send to server. | |
| 150 std::string machine_model_; | |
| 151 | 122 |
| 152 scoped_ptr<DelayedWorkScheduler> scheduler_; | 123 scoped_ptr<DelayedWorkScheduler> scheduler_; |
| 153 | |
| 154 ObserverList<Observer, true> observer_list_; | |
| 155 }; | 124 }; |
| 156 | 125 |
| 157 } // namespace policy | 126 } // namespace policy |
| 158 | 127 |
| 159 #endif // CHROME_BROWSER_POLICY_DEVICE_TOKEN_FETCHER_H_ | 128 #endif // CHROME_BROWSER_POLICY_DEVICE_TOKEN_FETCHER_H_ |
| OLD | NEW |