| 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> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/observer_list.h" | 11 #include "base/observer_list.h" |
| 12 #include "base/scoped_ptr.h" | 12 #include "base/scoped_ptr.h" |
| 13 #include "base/task.h" | 13 #include "base/task.h" |
| 14 #include "chrome/browser/policy/device_management_backend.h" | 14 #include "chrome/browser/policy/device_management_backend.h" |
| 15 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | 15 #include "chrome/browser/policy/proto/device_management_backend.pb.h" |
| 16 | 16 |
| 17 namespace policy { | 17 namespace policy { |
| 18 | 18 |
| 19 class CloudPolicyCache; | 19 class CloudPolicyCacheBase; |
| 20 class DeviceManagementService; | 20 class DeviceManagementService; |
| 21 | 21 |
| 22 namespace em = enterprise_management; | 22 namespace em = enterprise_management; |
| 23 | 23 |
| 24 // Fetches the device token that can be used for policy requests with the device | 24 // Fetches the device token that can be used for policy requests with the device |
| 25 // management server, either from disk if it already has been successfully | 25 // management server, either from disk if it already has been successfully |
| 26 // requested, otherwise from the device management server. An instance of the | 26 // requested, otherwise from the device management server. An instance of the |
| 27 // fetcher is shared as a singleton by all users of the device management token | 27 // fetcher is shared as a singleton by all users of the device management token |
| 28 // to ensure they all get the same token. | 28 // to ensure they all get the same token. |
| 29 class DeviceTokenFetcher | 29 class DeviceTokenFetcher |
| 30 : public DeviceManagementBackend::DeviceRegisterResponseDelegate { | 30 : public DeviceManagementBackend::DeviceRegisterResponseDelegate { |
| 31 public: | 31 public: |
| 32 class Observer { | 32 class Observer { |
| 33 public: | 33 public: |
| 34 virtual ~Observer() {} | 34 virtual ~Observer() {} |
| 35 virtual void OnDeviceTokenAvailable() = 0; | 35 virtual void OnDeviceTokenAvailable() = 0; |
| 36 }; | 36 }; |
| 37 | 37 |
| 38 // |service| is used to talk to the device management service and |cache| is | 38 // |service| is used to talk to the device management service and |cache| is |
| 39 // used to persist whether the device is unmanaged. | 39 // used to persist whether the device is unmanaged. |
| 40 DeviceTokenFetcher(DeviceManagementService* service, | 40 DeviceTokenFetcher(DeviceManagementService* service, |
| 41 CloudPolicyCache* cache); | 41 CloudPolicyCacheBase* cache); |
| 42 // Version for tests that allows to set timing paramters. | 42 // Version for tests that allows to set timing paramters. |
| 43 DeviceTokenFetcher(DeviceManagementService* service, | 43 DeviceTokenFetcher(DeviceManagementService* service, |
| 44 CloudPolicyCache* cache, | 44 CloudPolicyCacheBase* cache, |
| 45 int64 token_fetch_error_delay_ms, | 45 int64 token_fetch_error_delay_ms, |
| 46 int64 unmanaged_device_refresh_rate_ms); | 46 int64 unmanaged_device_refresh_rate_ms); |
| 47 virtual ~DeviceTokenFetcher(); | 47 virtual ~DeviceTokenFetcher(); |
| 48 | 48 |
| 49 // Starts fetching a token. | 49 // Starts fetching a token. |
| 50 // Declared virtual so it can be overridden by mocks. | 50 // Declared virtual so it can be overridden by mocks. |
| 51 virtual void FetchToken(const std::string& auth_token, | 51 virtual void FetchToken(const std::string& auth_token, |
| 52 const std::string& device_id, | 52 const std::string& device_id, |
| 53 em::DeviceRegisterRequest_Type policy_type, | 53 em::DeviceRegisterRequest_Type policy_type, |
| 54 const std::string& machine_id); | 54 const std::string& machine_id); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 81 // Token available. | 81 // Token available. |
| 82 STATE_TOKEN_AVAILABLE, | 82 STATE_TOKEN_AVAILABLE, |
| 83 // Device unmanaged. | 83 // Device unmanaged. |
| 84 STATE_UNMANAGED, | 84 STATE_UNMANAGED, |
| 85 // Error, retry later. | 85 // Error, retry later. |
| 86 STATE_ERROR, | 86 STATE_ERROR, |
| 87 }; | 87 }; |
| 88 | 88 |
| 89 // Common initialization helper. | 89 // Common initialization helper. |
| 90 void Initialize(DeviceManagementService* service, | 90 void Initialize(DeviceManagementService* service, |
| 91 CloudPolicyCache* cache, | 91 CloudPolicyCacheBase* cache, |
| 92 int64 token_fetch_error_delay_ms, | 92 int64 token_fetch_error_delay_ms, |
| 93 int64 unmanaged_device_refresh_rate_ms); | 93 int64 unmanaged_device_refresh_rate_ms); |
| 94 | 94 |
| 95 // Moves the fetcher into a new state. | 95 // Moves the fetcher into a new state. |
| 96 void SetState(FetcherState state); | 96 void SetState(FetcherState state); |
| 97 | 97 |
| 98 // Resets |backend_|, then uses |auth_token_| and |device_id_| to perform | 98 // Resets |backend_|, then uses |auth_token_| and |device_id_| to perform |
| 99 // an actual token fetch. | 99 // an actual token fetch. |
| 100 void FetchTokenInternal(); | 100 void FetchTokenInternal(); |
| 101 | 101 |
| 102 // Called back from the |retry_task_|. | 102 // Called back from the |retry_task_|. |
| 103 void ExecuteRetryTask(); | 103 void ExecuteRetryTask(); |
| 104 | 104 |
| 105 // Cancels the |retry_task_|. | 105 // Cancels the |retry_task_|. |
| 106 void CancelRetryTask(); | 106 void CancelRetryTask(); |
| 107 | 107 |
| 108 // Service and backend. A new backend is created whenever the fetcher gets | 108 // Service and backend. A new backend is created whenever the fetcher gets |
| 109 // reset. | 109 // reset. |
| 110 DeviceManagementService* service_; // weak | 110 DeviceManagementService* service_; // weak |
| 111 scoped_ptr<DeviceManagementBackend> backend_; | 111 scoped_ptr<DeviceManagementBackend> backend_; |
| 112 | 112 |
| 113 // Reference to the cache. Used to persist and read unmanaged state. | 113 // Reference to the cache. Used to persist and read unmanaged state. |
| 114 CloudPolicyCache* cache_; | 114 CloudPolicyCacheBase* cache_; |
| 115 | 115 |
| 116 // Refresh parameters. | 116 // Refresh parameters. |
| 117 int64 token_fetch_error_delay_ms_; | 117 int64 token_fetch_error_delay_ms_; |
| 118 int64 effective_token_fetch_error_delay_ms_; | 118 int64 effective_token_fetch_error_delay_ms_; |
| 119 int64 unmanaged_device_refresh_rate_ms_; | 119 int64 unmanaged_device_refresh_rate_ms_; |
| 120 | 120 |
| 121 // State the fetcher is currently in. | 121 // State the fetcher is currently in. |
| 122 FetcherState state_; | 122 FetcherState state_; |
| 123 | 123 |
| 124 // Current device token. | 124 // Current device token. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 137 CancelableTask* retry_task_; | 137 CancelableTask* retry_task_; |
| 138 | 138 |
| 139 ScopedRunnableMethodFactory<DeviceTokenFetcher> method_factory_; | 139 ScopedRunnableMethodFactory<DeviceTokenFetcher> method_factory_; |
| 140 | 140 |
| 141 ObserverList<Observer, true> observer_list_; | 141 ObserverList<Observer, true> observer_list_; |
| 142 }; | 142 }; |
| 143 | 143 |
| 144 } // namespace policy | 144 } // namespace policy |
| 145 | 145 |
| 146 #endif // CHROME_BROWSER_POLICY_DEVICE_TOKEN_FETCHER_H_ | 146 #endif // CHROME_BROWSER_POLICY_DEVICE_TOKEN_FETCHER_H_ |
| OLD | NEW |