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 #include <vector> | |
11 | 10 |
12 #include "base/file_path.h" | |
13 #include "base/observer_list.h" | 11 #include "base/observer_list.h" |
14 #include "base/ref_counted.h" | 12 #include "base/scoped_ptr.h" |
15 #include "base/synchronization/waitable_event.h" | 13 #include "base/task.h" |
16 #include "chrome/browser/policy/device_management_backend.h" | 14 #include "chrome/browser/policy/device_management_backend.h" |
17 #include "chrome/common/notification_observer.h" | |
18 #include "chrome/common/notification_registrar.h" | |
19 | |
20 class Profile; | |
21 | 15 |
22 namespace policy { | 16 namespace policy { |
23 | 17 |
18 class CloudPolicyCache; | |
19 class DeviceManagementService; | |
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 NotificationObserver, | 29 : public DeviceManagementBackend::DeviceRegisterResponseDelegate { |
33 public DeviceManagementBackend::DeviceRegisterResponseDelegate, | |
34 public base::RefCountedThreadSafe<DeviceTokenFetcher> { | |
35 public: | 30 public: |
36 class Observer { | 31 class Observer { |
37 public: | 32 public: |
38 virtual void OnTokenSuccess() = 0; | |
39 virtual void OnTokenError() = 0; | |
40 virtual void OnNotManaged() = 0; | |
41 virtual ~Observer() {} | 33 virtual ~Observer() {} |
34 virtual void OnTokenAvailable() = 0; | |
42 }; | 35 }; |
43 | 36 |
44 class ObserverRegistrar { | 37 // |service| is used to talk to the device management service and |cache| is |
45 public: | 38 // used to persist whether the device is unmanaged. |
46 ObserverRegistrar(); | 39 DeviceTokenFetcher(DeviceManagementService* service, |
47 ~ObserverRegistrar(); | 40 CloudPolicyCache* cache); |
48 | 41 // Version for tests that allows to set timing paramters. |
49 void Init(DeviceTokenFetcher* token_fetcher); | 42 DeviceTokenFetcher(DeviceManagementService* service, |
50 void AddObserver(DeviceTokenFetcher::Observer* observer); | 43 CloudPolicyCache* cache, |
51 void RemoveAll(); | 44 int64 token_fetch_error_delay_ms, |
52 private: | 45 int64 unmanaged_device_refresh_rate_ms); |
53 DeviceTokenFetcher* token_fetcher_; | |
54 std::vector<DeviceTokenFetcher::Observer*> observers_; | |
55 }; | |
56 | |
57 // Requests to the device management server are sent through |backend|. It | |
58 // obtains the authentication token from |token_service|. The fetcher stores | |
59 // the device token to |token_path| once it's retrieved from the server. | |
60 DeviceTokenFetcher(DeviceManagementBackend* backend, | |
61 Profile* profile, | |
62 const FilePath& token_path); | |
63 virtual ~DeviceTokenFetcher(); | 46 virtual ~DeviceTokenFetcher(); |
64 | 47 |
65 // NotificationObserver method overrides: | 48 // Resets the fetcher. |
66 virtual void Observe(NotificationType type, | 49 void Reset(); |
67 const NotificationSource& source, | 50 |
68 const NotificationDetails& details); | 51 // Starts fetching a token. |
52 void FetchToken(const std::string& auth_token, | |
53 const std::string& device_id); | |
54 | |
55 // Returns the device management token or the empty string if not available. | |
56 const std::string& GetDeviceToken(); | |
57 | |
58 void AddObserver(Observer* observer); | |
59 void RemoveObserver(Observer* observer); | |
69 | 60 |
70 // DeviceManagementBackend::DeviceRegisterResponseDelegate method overrides: | 61 // DeviceManagementBackend::DeviceRegisterResponseDelegate method overrides: |
71 virtual void HandleRegisterResponse( | 62 virtual void HandleRegisterResponse( |
72 const em::DeviceRegisterResponse& response); | 63 const em::DeviceRegisterResponse& response); |
73 virtual void OnError(DeviceManagementBackend::ErrorCode code); | 64 virtual void OnError(DeviceManagementBackend::ErrorCode code); |
74 | 65 |
75 // Re-initializes this DeviceTokenFetcher | |
76 void Restart(); | |
77 | |
78 // Called by subscribers of the device management token to indicate that they | |
79 // will need the token in the future. Must be called on the UI thread. | |
80 void StartFetching(); | |
81 | |
82 // Returns true if there is a pending token request to the device management | |
83 // server. | |
84 bool IsTokenPending(); | |
85 | |
86 // Returns the device management token for this device, blocking until | |
87 // outstanding requests to the device management server are satisfied. In the | |
88 // case that the token could not be fetched, an empty string is returned. | |
89 std::string GetDeviceToken(); | |
90 | |
91 // Returns the device ID for this device. If no such ID has been set yet, a | |
92 // new ID is generated and returned. | |
93 std::string GetDeviceID(); | |
94 | |
95 // True if the fetcher has a valid AuthToken for the device management server. | |
96 bool HasAuthToken() const { return !auth_token_.empty(); } | |
97 | |
98 // True if the device token has been fetched and is valid. | |
99 bool IsTokenValid() const; | |
100 | |
101 protected: | |
102 // Returns the email address of the currently logged-in user. | |
103 virtual std::string GetCurrentUser(); | |
104 | |
105 // Used to identify GOOGLE_SIGNIN_SUCCESSFUL notifications from the owning | |
106 // profile, and to query for the current username. | |
107 Profile* profile_; // weak | |
108 | |
109 private: | 66 private: |
110 friend class DeviceTokenFetcherTest; | 67 friend class DeviceTokenFetcherTest; |
111 | 68 |
112 // The different states that the fetcher can be in during the process of | 69 // The different states that the fetcher can be in during the process of |
113 // getting the device token. | 70 // getting the device token. |
114 enum FetcherState { | 71 enum FetcherState { |
115 kStateNotStarted, | 72 // Fetcher inactive. |
116 kStateLoadDeviceTokenFromDisk, | 73 STATE_INACTIVE, |
117 kStateReadyToRequestDeviceTokenFromServer, | 74 // Token available. |
118 kStateRequestingDeviceTokenFromServer, | 75 STATE_TOKEN_AVAILABLE, |
119 kStateHasDeviceToken, | 76 // Device unmanaged. |
120 kStateFailure, | 77 STATE_UNMANAGED, |
121 kStateNotManaged, | 78 // Error, retry later. |
79 STATE_ERROR, | |
122 }; | 80 }; |
123 | 81 |
124 // Moves the fetcher into a new state. If the fetcher has the device token | 82 // Common initialization helper. |
125 // or is moving into the failure state, callers waiting on WaitForToken | 83 void Initialize(DeviceManagementService* service, |
126 // are unblocked. | 84 CloudPolicyCache* cache, |
85 int64 token_fetch_error_delay_ms, | |
86 int64 unmanaged_device_refresh_rate_ms); | |
87 | |
88 // Moves the fetcher into a new state. | |
127 void SetState(FetcherState state); | 89 void SetState(FetcherState state); |
128 | 90 |
129 // Returns the full path to the file that persists the device manager token. | 91 // Called back from the delayed work task. |
130 void GetDeviceTokenPath(FilePath* token_path) const; | 92 void DoDelayedWork(); |
danno
2011/02/04 16:01:33
What kind of work. Why? What's the motivation?
Jakob Kummerow
2011/02/07 16:00:09
Seeing what this method does, I suggest we name it
Jakob Kummerow
2011/02/14 13:50:34
Done.
| |
131 | 93 |
132 // Tries to load the device token from disk. Must be called on the FILE | 94 // Cancels delayed work. |
133 // thread. | 95 void CancelDelayedWork(); |
134 void AttemptTokenLoadFromDisk(); | |
135 | 96 |
136 // Called if it's not possible to load the device token from disk. Sets the | 97 // Service and backend. A new backend is created whenever the fetcher gets |
137 // fetcher in a state that's ready to register the device with the device | 98 // reset. |
138 // management server and receive the device token in return. If the AuthToken | 99 DeviceManagementService* service_; // weak |
139 // for the device management server is available, initiate the server | 100 scoped_ptr<DeviceManagementBackend> backend_; |
140 // request. | |
141 void MakeReadyToRequestDeviceToken(); | |
142 | 101 |
143 // Issues a registration request to the server if both the fetcher is in the | 102 // Reference to the cache. Used to persist and read unmanaged state. |
144 // ready-to-request state and the device management server AuthToken is | 103 CloudPolicyCache* cache_; |
145 // available. | |
146 void SendServerRequestIfPossible(); | |
147 | 104 |
148 void AddObserver(Observer* obs) { | 105 // Refresh parameters. |
149 observer_list_.AddObserver(obs); | 106 int64 token_fetch_error_delay_ms_; |
150 } | 107 int64 effective_token_fetch_error_delay_ms_; |
108 int64 unmanaged_device_refresh_rate_ms_; | |
151 | 109 |
152 void RemoveObserver(Observer* obs) { | 110 // State the fetcher is currently in. |
153 observer_list_.RemoveObserver(obs); | 111 FetcherState state_; |
154 } | |
155 | 112 |
156 void NotifyTokenSuccess() { | 113 // Current device token. |
157 FOR_EACH_OBSERVER(Observer, observer_list_, OnTokenSuccess()); | 114 std::string device_token_; |
158 } | |
159 | 115 |
160 void NotifyTokenError() { | 116 // Contains the AuthToken for the device management server. |
161 FOR_EACH_OBSERVER(Observer, observer_list_, OnTokenError()); | 117 std::string auth_token_; |
162 } | 118 // Device identifier to send to the server. |
119 std::string device_id_; | |
163 | 120 |
164 void NotifyNotManaged() { | 121 // Task that has been scheduled to do delayed work. |
165 FOR_EACH_OBSERVER(Observer, observer_list_, OnNotManaged()); | 122 CancelableTask* delayed_work_task_; |
166 } | |
167 | 123 |
168 // Saves the device management token to disk once it has been retrieved from | 124 ScopedRunnableMethodFactory<DeviceTokenFetcher> method_factory_; |
169 // the server. Must be called on the FILE thread. | |
170 static void WriteDeviceTokenToDisk(const FilePath& path, | |
171 const std::string& token, | |
172 const std::string& device_id); | |
173 | |
174 // Generates a new device ID used to register the device with the device | |
175 // management server and generate the device token. | |
176 static std::string GenerateNewDeviceID(); | |
177 | 125 |
178 ObserverList<Observer, true> observer_list_; | 126 ObserverList<Observer, true> observer_list_; |
179 FilePath token_path_; | |
180 DeviceManagementBackend* backend_; // weak | |
181 FetcherState state_; | |
182 std::string device_token_; | |
183 std::string device_id_; | |
184 | |
185 // Contains the AuthToken for the device management server. Empty if the | |
186 // AuthToken hasn't been issued yet or that was an error getting the | |
187 // AuthToken. | |
188 std::string auth_token_; | |
189 | |
190 // An event that is signaled only once the device token has been fetched | |
191 // or it has been determined that there was an error during fetching. | |
192 base::WaitableEvent device_token_load_complete_event_; | |
193 | |
194 // Registers the fetcher for notification of successful Gaia logins. | |
195 NotificationRegistrar registrar_; | |
196 }; | 127 }; |
197 | 128 |
198 } // namespace policy | 129 } // namespace policy |
199 | 130 |
200 #endif // CHROME_BROWSER_POLICY_DEVICE_TOKEN_FETCHER_H_ | 131 #endif // CHROME_BROWSER_POLICY_DEVICE_TOKEN_FETCHER_H_ |
OLD | NEW |