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_DEVICE_TOKEN_FETCHER_H_ | |
6 #define CHROME_BROWSER_POLICY_DEVICE_TOKEN_FETCHER_H_ | |
7 | |
8 #include "base/basictypes.h" | |
9 #include "base/memory/scoped_ptr.h" | |
10 #include "chrome/browser/policy/cloud_policy_constants.h" | |
11 | |
12 namespace enterprise_management { | |
13 class DeviceManagementResponse; | |
14 } | |
15 | |
16 namespace policy { | |
17 | |
18 class CloudPolicyCacheBase; | |
19 class CloudPolicyDataStore; | |
20 class DelayedWorkScheduler; | |
21 class DeviceManagementRequestJob; | |
22 class DeviceManagementService; | |
23 class PolicyNotifier; | |
24 | |
25 // Fetches the device token that can be used for policy requests with the device | |
26 // management server, either from disk if it already has been successfully | |
27 // requested, otherwise from the device management server. An instance of the | |
28 // fetcher is shared as a singleton by all users of the device management token | |
29 // to ensure they all get the same token. | |
30 class DeviceTokenFetcher { | |
31 public: | |
32 // |service| is used to talk to the device management service and |cache| is | |
33 // used to persist whether the device is unmanaged. | |
34 DeviceTokenFetcher(DeviceManagementService* service, | |
35 CloudPolicyCacheBase* cache, | |
36 CloudPolicyDataStore* data_store, | |
37 PolicyNotifier* notifier); | |
38 // Version for tests that allows to set timing parameters. | |
39 // Takes ownership of |scheduler|. | |
40 DeviceTokenFetcher(DeviceManagementService* service, | |
41 CloudPolicyCacheBase* cache, | |
42 CloudPolicyDataStore* data_store, | |
43 PolicyNotifier* notifier, | |
44 DelayedWorkScheduler* scheduler); | |
45 virtual ~DeviceTokenFetcher(); | |
46 | |
47 // Starts fetching a token. | |
48 // Declared virtual so it can be overridden by mocks. | |
49 virtual void FetchToken(); | |
50 | |
51 virtual void SetUnmanagedState(); | |
52 virtual void SetSerialNumberInvalidState(); | |
53 virtual void SetMissingLicensesState(); | |
54 | |
55 // Cancels any pending work on this fetcher and resets it to inactive state. | |
56 void Reset(); | |
57 | |
58 private: | |
59 friend class DeviceTokenFetcherTest; | |
60 | |
61 // The different states that the fetcher can be in during the process of | |
62 // getting the device token. |state_| is initialized to INACTIVE, depending | |
63 // on the result of a token fetching attempt can transition to either of | |
64 // TOKEN_AVAILABLE, UNMANAGED, or ERROR. The first attempt must be triggered | |
65 // externally. When |state_| is UNMANAGED, a new fetching attempt is | |
66 // performed every |unmanaged_device_refresh_rate_ms_|; when it's ERROR, | |
67 // a new attempt is done after |effective_token_fetch_error_delay_ms_|. | |
68 enum FetcherState { | |
69 // Fetcher inactive. | |
70 STATE_INACTIVE, | |
71 // Token available. | |
72 STATE_TOKEN_AVAILABLE, | |
73 // Device unmanaged. | |
74 STATE_UNMANAGED, | |
75 // The device is not enlisted for the domain. | |
76 STATE_BAD_SERIAL, | |
77 // The licenses for the domain have expired or have been exhausted. | |
78 STATE_MISSING_LICENSES, | |
79 // Error, retry later. | |
80 STATE_ERROR, | |
81 // Temporary error. Retry sooner. | |
82 STATE_TEMPORARY_ERROR, | |
83 // Server rejected the auth token. | |
84 STATE_BAD_AUTH, | |
85 // Server didn't send enrollment mode or the enrollment mode is not known to | |
86 // the client. | |
87 STATE_BAD_ENROLLMENT_MODE, | |
88 }; | |
89 | |
90 // Common initialization helper. | |
91 void Initialize(DeviceManagementService* service, | |
92 CloudPolicyCacheBase* cache, | |
93 CloudPolicyDataStore* data, | |
94 PolicyNotifier* notifier, | |
95 DelayedWorkScheduler* scheduler); | |
96 | |
97 // Resets |backend_|, then uses |auth_token_| and |device_id_| to perform | |
98 // an actual token fetch. | |
99 void FetchTokenInternal(); | |
100 | |
101 // Handles token fetch request completion. | |
102 void OnTokenFetchCompleted( | |
103 DeviceManagementStatus status, | |
104 const enterprise_management::DeviceManagementResponse& response); | |
105 | |
106 // Moves the fetcher into a new state. | |
107 void SetState(FetcherState state); | |
108 | |
109 // DelayedWorkScheduler::Client: | |
110 virtual void DoWork(); | |
111 | |
112 // Service and backend. A new backend is created whenever the fetcher gets | |
113 // reset. | |
114 DeviceManagementService* service_; // weak | |
115 scoped_ptr<DeviceManagementRequestJob> request_job_; | |
116 | |
117 // Reference to the cache. Used to persist and read unmanaged state. | |
118 CloudPolicyCacheBase* cache_; | |
119 | |
120 PolicyNotifier* notifier_; | |
121 | |
122 // Refresh parameters. | |
123 int64 effective_token_fetch_error_delay_ms_; | |
124 | |
125 // State the fetcher is currently in. | |
126 FetcherState state_; | |
127 | |
128 CloudPolicyDataStore* data_store_; | |
129 | |
130 scoped_ptr<DelayedWorkScheduler> scheduler_; | |
131 | |
132 DISALLOW_COPY_AND_ASSIGN(DeviceTokenFetcher); | |
133 }; | |
134 | |
135 } // namespace policy | |
136 | |
137 #endif // CHROME_BROWSER_POLICY_DEVICE_TOKEN_FETCHER_H_ | |
OLD | NEW |