| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "base/basictypes.h" |
| 9 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 10 #include "chrome/browser/policy/device_management_backend.h" | 11 #include "chrome/browser/policy/cloud_policy_constants.h" |
| 11 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | 12 |
| 13 namespace enterprise_management { |
| 14 class DeviceManagementResponse; |
| 15 } |
| 12 | 16 |
| 13 namespace policy { | 17 namespace policy { |
| 14 | 18 |
| 15 class CloudPolicyCacheBase; | 19 class CloudPolicyCacheBase; |
| 16 class CloudPolicyDataStore; | 20 class CloudPolicyDataStore; |
| 17 class DelayedWorkScheduler; | 21 class DelayedWorkScheduler; |
| 22 class DeviceManagementRequestJob; |
| 18 class DeviceManagementService; | 23 class DeviceManagementService; |
| 19 class PolicyNotifier; | 24 class PolicyNotifier; |
| 20 | 25 |
| 21 // Fetches the device token that can be used for policy requests with the device | 26 // Fetches the device token that can be used for policy requests with the device |
| 22 // management server, either from disk if it already has been successfully | 27 // management server, either from disk if it already has been successfully |
| 23 // requested, otherwise from the device management server. An instance of the | 28 // requested, otherwise from the device management server. An instance of the |
| 24 // fetcher is shared as a singleton by all users of the device management token | 29 // fetcher is shared as a singleton by all users of the device management token |
| 25 // to ensure they all get the same token. | 30 // to ensure they all get the same token. |
| 26 class DeviceTokenFetcher | 31 class DeviceTokenFetcher { |
| 27 : public DeviceManagementBackend::DeviceRegisterResponseDelegate { | |
| 28 public: | 32 public: |
| 29 // |service| is used to talk to the device management service and |cache| is | 33 // |service| is used to talk to the device management service and |cache| is |
| 30 // used to persist whether the device is unmanaged. | 34 // used to persist whether the device is unmanaged. |
| 31 DeviceTokenFetcher(DeviceManagementService* service, | 35 DeviceTokenFetcher(DeviceManagementService* service, |
| 32 CloudPolicyCacheBase* cache, | 36 CloudPolicyCacheBase* cache, |
| 33 CloudPolicyDataStore* data_store, | 37 CloudPolicyDataStore* data_store, |
| 34 PolicyNotifier* notifier); | 38 PolicyNotifier* notifier); |
| 35 // Version for tests that allows to set timing parameters. | 39 // Version for tests that allows to set timing parameters. |
| 36 // Takes ownership of |scheduler|. | 40 // Takes ownership of |scheduler|. |
| 37 DeviceTokenFetcher(DeviceManagementService* service, | 41 DeviceTokenFetcher(DeviceManagementService* service, |
| 38 CloudPolicyCacheBase* cache, | 42 CloudPolicyCacheBase* cache, |
| 39 CloudPolicyDataStore* data_store, | 43 CloudPolicyDataStore* data_store, |
| 40 PolicyNotifier* notifier, | 44 PolicyNotifier* notifier, |
| 41 DelayedWorkScheduler* scheduler); | 45 DelayedWorkScheduler* scheduler); |
| 42 virtual ~DeviceTokenFetcher(); | 46 virtual ~DeviceTokenFetcher(); |
| 43 | 47 |
| 44 // Starts fetching a token. | 48 // Starts fetching a token. |
| 45 // Declared virtual so it can be overridden by mocks. | 49 // Declared virtual so it can be overridden by mocks. |
| 46 virtual void FetchToken(); | 50 virtual void FetchToken(); |
| 47 | 51 |
| 48 virtual void SetUnmanagedState(); | 52 virtual void SetUnmanagedState(); |
| 49 virtual void SetSerialNumberInvalidState(); | 53 virtual void SetSerialNumberInvalidState(); |
| 50 | 54 |
| 51 // Cancels any pending work on this fetcher and resets it to inactive state. | 55 // Cancels any pending work on this fetcher and resets it to inactive state. |
| 52 void Reset(); | 56 void Reset(); |
| 53 | 57 |
| 54 // DeviceManagementBackend::DeviceRegisterResponseDelegate method overrides: | |
| 55 virtual void HandleRegisterResponse( | |
| 56 const enterprise_management::DeviceRegisterResponse& response) OVERRIDE; | |
| 57 virtual void OnError(DeviceManagementBackend::ErrorCode code) OVERRIDE; | |
| 58 | |
| 59 private: | 58 private: |
| 60 friend class DeviceTokenFetcherTest; | 59 friend class DeviceTokenFetcherTest; |
| 61 | 60 |
| 62 // The different states that the fetcher can be in during the process of | 61 // The different states that the fetcher can be in during the process of |
| 63 // getting the device token. |state_| is initialized to INACTIVE, depending | 62 // getting the device token. |state_| is initialized to INACTIVE, depending |
| 64 // on the result of a token fetching attempt can transition to either of | 63 // on the result of a token fetching attempt can transition to either of |
| 65 // TOKEN_AVAILABLE, UNMANAGED, or ERROR. The first attempt must be triggered | 64 // TOKEN_AVAILABLE, UNMANAGED, or ERROR. The first attempt must be triggered |
| 66 // externally. When |state_| is UNMANAGED, a new fetching attempt is | 65 // externally. When |state_| is UNMANAGED, a new fetching attempt is |
| 67 // performed every |unmanaged_device_refresh_rate_ms_|; when it's ERROR, | 66 // performed every |unmanaged_device_refresh_rate_ms_|; when it's ERROR, |
| 68 // a new attempt is done after |effective_token_fetch_error_delay_ms_|. | 67 // a new attempt is done after |effective_token_fetch_error_delay_ms_|. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 83 STATE_BAD_AUTH | 82 STATE_BAD_AUTH |
| 84 }; | 83 }; |
| 85 | 84 |
| 86 // Common initialization helper. | 85 // Common initialization helper. |
| 87 void Initialize(DeviceManagementService* service, | 86 void Initialize(DeviceManagementService* service, |
| 88 CloudPolicyCacheBase* cache, | 87 CloudPolicyCacheBase* cache, |
| 89 CloudPolicyDataStore* data, | 88 CloudPolicyDataStore* data, |
| 90 PolicyNotifier* notifier, | 89 PolicyNotifier* notifier, |
| 91 DelayedWorkScheduler* scheduler); | 90 DelayedWorkScheduler* scheduler); |
| 92 | 91 |
| 93 // Moves the fetcher into a new state. | |
| 94 void SetState(FetcherState state); | |
| 95 | |
| 96 // Resets |backend_|, then uses |auth_token_| and |device_id_| to perform | 92 // Resets |backend_|, then uses |auth_token_| and |device_id_| to perform |
| 97 // an actual token fetch. | 93 // an actual token fetch. |
| 98 void FetchTokenInternal(); | 94 void FetchTokenInternal(); |
| 99 | 95 |
| 96 // Handles token fetch request completion. |
| 97 void OnTokenFetchCompleted( |
| 98 DeviceManagementStatus status, |
| 99 const enterprise_management::DeviceManagementResponse& response); |
| 100 |
| 101 // Moves the fetcher into a new state. |
| 102 void SetState(FetcherState state); |
| 103 |
| 100 // DelayedWorkScheduler::Client: | 104 // DelayedWorkScheduler::Client: |
| 101 virtual void DoWork(); | 105 virtual void DoWork(); |
| 102 | 106 |
| 103 // Service and backend. A new backend is created whenever the fetcher gets | 107 // Service and backend. A new backend is created whenever the fetcher gets |
| 104 // reset. | 108 // reset. |
| 105 DeviceManagementService* service_; // weak | 109 DeviceManagementService* service_; // weak |
| 106 scoped_ptr<DeviceManagementBackend> backend_; | 110 scoped_ptr<DeviceManagementRequestJob> request_job_; |
| 107 | 111 |
| 108 // Reference to the cache. Used to persist and read unmanaged state. | 112 // Reference to the cache. Used to persist and read unmanaged state. |
| 109 CloudPolicyCacheBase* cache_; | 113 CloudPolicyCacheBase* cache_; |
| 110 | 114 |
| 111 PolicyNotifier* notifier_; | 115 PolicyNotifier* notifier_; |
| 112 | 116 |
| 113 // Refresh parameters. | 117 // Refresh parameters. |
| 114 int64 token_fetch_error_delay_ms_; | 118 int64 token_fetch_error_delay_ms_; |
| 115 int64 token_fetch_error_max_delay_ms_; | 119 int64 token_fetch_error_max_delay_ms_; |
| 116 int64 effective_token_fetch_error_delay_ms_; | 120 int64 effective_token_fetch_error_delay_ms_; |
| 117 int64 unmanaged_device_refresh_rate_ms_; | 121 int64 unmanaged_device_refresh_rate_ms_; |
| 118 | 122 |
| 119 // State the fetcher is currently in. | 123 // State the fetcher is currently in. |
| 120 FetcherState state_; | 124 FetcherState state_; |
| 121 | 125 |
| 122 CloudPolicyDataStore* data_store_; | 126 CloudPolicyDataStore* data_store_; |
| 123 | 127 |
| 124 scoped_ptr<DelayedWorkScheduler> scheduler_; | 128 scoped_ptr<DelayedWorkScheduler> scheduler_; |
| 125 | 129 |
| 126 DISALLOW_COPY_AND_ASSIGN(DeviceTokenFetcher); | 130 DISALLOW_COPY_AND_ASSIGN(DeviceTokenFetcher); |
| 127 }; | 131 }; |
| 128 | 132 |
| 129 } // namespace policy | 133 } // namespace policy |
| 130 | 134 |
| 131 #endif // CHROME_BROWSER_POLICY_DEVICE_TOKEN_FETCHER_H_ | 135 #endif // CHROME_BROWSER_POLICY_DEVICE_TOKEN_FETCHER_H_ |
| OLD | NEW |