OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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> |
10 | 11 |
11 #include "base/file_path.h" | 12 #include "base/file_path.h" |
| 13 #include "base/observer_list.h" |
12 #include "base/ref_counted.h" | 14 #include "base/ref_counted.h" |
13 #include "base/waitable_event.h" | 15 #include "base/waitable_event.h" |
14 #include "chrome/browser/policy/device_management_backend.h" | 16 #include "chrome/browser/policy/device_management_backend.h" |
15 #include "chrome/common/notification_observer.h" | 17 #include "chrome/common/notification_observer.h" |
16 #include "chrome/common/notification_registrar.h" | 18 #include "chrome/common/notification_registrar.h" |
17 | 19 |
18 class TokenService; | 20 class TokenService; |
19 | 21 |
20 namespace policy { | 22 namespace policy { |
21 | 23 |
22 namespace em = enterprise_management; | 24 namespace em = enterprise_management; |
23 | 25 |
24 // 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 |
25 // management server, either from disk if it already has been successfully | 27 // management server, either from disk if it already has been successfully |
26 // requested, otherwise from the device management server. An instance of the | 28 // 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 | 29 // fetcher is shared as a singleton by all users of the device management token |
28 // to ensure they all get the same token. | 30 // to ensure they all get the same token. |
29 class DeviceTokenFetcher | 31 class DeviceTokenFetcher |
30 : public NotificationObserver, | 32 : public NotificationObserver, |
31 public DeviceManagementBackend::DeviceRegisterResponseDelegate, | 33 public DeviceManagementBackend::DeviceRegisterResponseDelegate, |
32 public base::RefCountedThreadSafe<DeviceTokenFetcher> { | 34 public base::RefCountedThreadSafe<DeviceTokenFetcher> { |
33 public: | 35 public: |
| 36 class Observer { |
| 37 public: |
| 38 virtual void OnTokenSuccess() = 0; |
| 39 virtual void OnTokenError() = 0; |
| 40 virtual void OnNotManaged() = 0; |
| 41 virtual ~Observer() {} |
| 42 }; |
| 43 |
| 44 class ObserverRegistrar { |
| 45 public: |
| 46 void Init(DeviceTokenFetcher* token_fetcher) { |
| 47 token_fetcher_ = token_fetcher; |
| 48 } |
| 49 ~ObserverRegistrar() { |
| 50 RemoveAll(); |
| 51 } |
| 52 void AddObserver(DeviceTokenFetcher::Observer* observer) { |
| 53 observers_.push_back(observer); |
| 54 token_fetcher_->AddObserver(observer); |
| 55 } |
| 56 void RemoveAll() { |
| 57 for (std::vector<DeviceTokenFetcher::Observer*>::iterator it = |
| 58 observers_.begin(); it != observers_.end(); ++it) { |
| 59 token_fetcher_->RemoveObserver(*it); |
| 60 } |
| 61 observers_.clear(); |
| 62 } |
| 63 private: |
| 64 DeviceTokenFetcher* token_fetcher_; |
| 65 std::vector<DeviceTokenFetcher::Observer*> observers_; |
| 66 }; |
| 67 |
34 // Requests to the device management server are sent through |backend|. It | 68 // Requests to the device management server are sent through |backend|. It |
35 // obtains the authentication token from |token_service|. The fetcher stores | 69 // obtains the authentication token from |token_service|. The fetcher stores |
36 // the device token to |token_path| once it's retrieved from the server. | 70 // the device token to |token_path| once it's retrieved from the server. |
37 DeviceTokenFetcher(DeviceManagementBackend* backend, | 71 DeviceTokenFetcher(DeviceManagementBackend* backend, |
38 TokenService* token_service, | 72 TokenService* token_service, |
39 const FilePath& token_path); | 73 const FilePath& token_path); |
40 virtual ~DeviceTokenFetcher() {} | 74 virtual ~DeviceTokenFetcher() {} |
41 | 75 |
42 // NotificationObserver method overrides: | 76 // NotificationObserver method overrides: |
43 virtual void Observe(NotificationType type, | 77 virtual void Observe(NotificationType type, |
44 const NotificationSource& source, | 78 const NotificationSource& source, |
45 const NotificationDetails& details); | 79 const NotificationDetails& details); |
46 | 80 |
47 // DeviceManagementBackend::DeviceRegisterResponseDelegate method overrides: | 81 // DeviceManagementBackend::DeviceRegisterResponseDelegate method overrides: |
48 virtual void HandleRegisterResponse( | 82 virtual void HandleRegisterResponse( |
49 const em::DeviceRegisterResponse& response); | 83 const em::DeviceRegisterResponse& response); |
50 virtual void OnError(DeviceManagementBackend::ErrorCode code); | 84 virtual void OnError(DeviceManagementBackend::ErrorCode code); |
51 | 85 |
| 86 // Re-initializes this DeviceTokenFetcher |
| 87 void Restart(); |
| 88 |
52 // Called by subscribers of the device management token to indicate that they | 89 // Called by subscribers of the device management token to indicate that they |
53 // will need the token in the future. Must be called on the UI thread. | 90 // will need the token in the future. Must be called on the UI thread. |
54 void StartFetching(); | 91 void StartFetching(); |
55 | 92 |
56 // Instructs the fetcher to shut down, before the backend and token service | 93 // Instructs the fetcher to shut down, before the backend and token service |
57 // references become stale. | 94 // references become stale. |
58 void Shutdown(); | 95 void Shutdown(); |
59 | 96 |
60 // Returns true if there is a pending token request to the device management | 97 // Returns true if there is a pending token request to the device management |
61 // server. | 98 // server. |
(...skipping 18 matching lines...) Expand all Loading... |
80 friend class DeviceTokenFetcherTest; | 117 friend class DeviceTokenFetcherTest; |
81 | 118 |
82 // The different states that the fetcher can be in during the process of | 119 // The different states that the fetcher can be in during the process of |
83 // getting the device token. | 120 // getting the device token. |
84 enum FetcherState { | 121 enum FetcherState { |
85 kStateNotStarted, | 122 kStateNotStarted, |
86 kStateLoadDeviceTokenFromDisk, | 123 kStateLoadDeviceTokenFromDisk, |
87 kStateReadyToRequestDeviceTokenFromServer, | 124 kStateReadyToRequestDeviceTokenFromServer, |
88 kStateRequestingDeviceTokenFromServer, | 125 kStateRequestingDeviceTokenFromServer, |
89 kStateHasDeviceToken, | 126 kStateHasDeviceToken, |
90 kStateFailure | 127 kStateFailure, |
| 128 kStateNotManaged, |
91 }; | 129 }; |
92 | 130 |
93 // Moves the fetcher into a new state. If the fetcher has the device token | 131 // Moves the fetcher into a new state. If the fetcher has the device token |
94 // or is moving into the failure state, callers waiting on WaitForToken | 132 // or is moving into the failure state, callers waiting on WaitForToken |
95 // are unblocked. | 133 // are unblocked. |
96 void SetState(FetcherState state); | 134 void SetState(FetcherState state); |
97 | 135 |
98 // Returns the full path to the file that persists the device manager token. | 136 // Returns the full path to the file that persists the device manager token. |
99 void GetDeviceTokenPath(FilePath* token_path) const; | 137 void GetDeviceTokenPath(FilePath* token_path) const; |
100 | 138 |
101 // Tries to load the device token from disk. Must be called on the FILE | 139 // Tries to load the device token from disk. Must be called on the FILE |
102 // thread. | 140 // thread. |
103 void AttemptTokenLoadFromDisk(); | 141 void AttemptTokenLoadFromDisk(); |
104 | 142 |
105 // Called if it's not possible to load the device token from disk. Sets the | 143 // Called if it's not possible to load the device token from disk. Sets the |
106 // fetcher in a state that's ready to register the device with the device | 144 // fetcher in a state that's ready to register the device with the device |
107 // management server and receive the device token in return. If the AuthToken | 145 // management server and receive the device token in return. If the AuthToken |
108 // for the device management server is available, initiate the server | 146 // for the device management server is available, initiate the server |
109 // request. | 147 // request. |
110 void MakeReadyToRequestDeviceToken(); | 148 void MakeReadyToRequestDeviceToken(); |
111 | 149 |
112 // Issues a registration request to the server if both the fetcher is in the | 150 // Issues a registration request to the server if both the fetcher is in the |
113 // ready-to-request state and the device management server AuthToken is | 151 // ready-to-request state and the device management server AuthToken is |
114 // available. | 152 // available. |
115 void SendServerRequestIfPossible(); | 153 void SendServerRequestIfPossible(); |
116 | 154 |
| 155 void AddObserver(Observer* obs) { |
| 156 observer_list_.AddObserver(obs); |
| 157 } |
| 158 |
| 159 void RemoveObserver(Observer* obs) { |
| 160 observer_list_.RemoveObserver(obs); |
| 161 } |
| 162 |
| 163 void NotifyTokenSuccess() { |
| 164 FOR_EACH_OBSERVER(Observer, observer_list_, OnTokenSuccess()); |
| 165 } |
| 166 |
| 167 void NotifyTokenError() { |
| 168 FOR_EACH_OBSERVER(Observer, observer_list_, OnTokenError()); |
| 169 } |
| 170 |
| 171 void NotifyNotManaged() { |
| 172 FOR_EACH_OBSERVER(Observer, observer_list_, OnNotManaged()); |
| 173 } |
| 174 |
117 // Saves the device management token to disk once it has been retrieved from | 175 // Saves the device management token to disk once it has been retrieved from |
118 // the server. Must be called on the FILE thread. | 176 // the server. Must be called on the FILE thread. |
119 static void WriteDeviceTokenToDisk(const FilePath& path, | 177 static void WriteDeviceTokenToDisk(const FilePath& path, |
120 const std::string& token, | 178 const std::string& token, |
121 const std::string& device_id); | 179 const std::string& device_id); |
122 | 180 |
123 // Generates a new device ID used to register the device with the device | 181 // Generates a new device ID used to register the device with the device |
124 // management server and generate the device token. | 182 // management server and generate the device token. |
125 static std::string GenerateNewDeviceID(); | 183 static std::string GenerateNewDeviceID(); |
126 | 184 |
| 185 ObserverList<Observer, true> observer_list_; |
127 FilePath token_path_; | 186 FilePath token_path_; |
128 DeviceManagementBackend* backend_; // weak | 187 DeviceManagementBackend* backend_; // weak |
129 TokenService* token_service_; | 188 TokenService* token_service_; |
130 FetcherState state_; | 189 FetcherState state_; |
131 std::string device_token_; | 190 std::string device_token_; |
132 std::string device_id_; | 191 std::string device_id_; |
133 | 192 |
134 // Contains the AuthToken for the device management server. Empty if the | 193 // Contains the AuthToken for the device management server. Empty if the |
135 // AuthToken hasn't been issued yet or that was an error getting the | 194 // AuthToken hasn't been issued yet or that was an error getting the |
136 // AuthToken. | 195 // AuthToken. |
137 std::string auth_token_; | 196 std::string auth_token_; |
138 | 197 |
139 // An event that is signaled only once the device token has been fetched | 198 // An event that is signaled only once the device token has been fetched |
140 // or it has been determined that there was an error during fetching. | 199 // or it has been determined that there was an error during fetching. |
141 base::WaitableEvent device_token_load_complete_event_; | 200 base::WaitableEvent device_token_load_complete_event_; |
142 | 201 |
143 // Registers the fetcher for notification of successful Gaia logins. | 202 // Registers the fetcher for notification of successful Gaia logins. |
144 NotificationRegistrar registrar_; | 203 NotificationRegistrar registrar_; |
145 }; | 204 }; |
146 | 205 |
147 } // namespace policy | 206 } // namespace policy |
148 | 207 |
149 #endif // CHROME_BROWSER_POLICY_DEVICE_TOKEN_FETCHER_H_ | 208 #endif // CHROME_BROWSER_POLICY_DEVICE_TOKEN_FETCHER_H_ |
OLD | NEW |