Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(99)

Side by Side Diff: chrome/browser/policy/device_token_fetcher.h

Issue 6520008: Device policy infrastructure (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix nits Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 OnDeviceTokenAvailable() = 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 // Starts fetching a token.
66 virtual void Observe(NotificationType type, 49 // Declared virtual so it can be overridden by mocks.
67 const NotificationSource& source, 50 virtual void FetchToken(const std::string& auth_token,
68 const NotificationDetails& details); 51 const std::string& device_id);
52
53 // Returns the device management token or the empty string if not available.
54 // Declared virtual so it can be overridden by mocks.
55 virtual const std::string& GetDeviceToken();
56
57 void AddObserver(Observer* observer);
58 void RemoveObserver(Observer* observer);
69 59
70 // DeviceManagementBackend::DeviceRegisterResponseDelegate method overrides: 60 // DeviceManagementBackend::DeviceRegisterResponseDelegate method overrides:
71 virtual void HandleRegisterResponse( 61 virtual void HandleRegisterResponse(
72 const em::DeviceRegisterResponse& response); 62 const em::DeviceRegisterResponse& response);
73 virtual void OnError(DeviceManagementBackend::ErrorCode code); 63 virtual void OnError(DeviceManagementBackend::ErrorCode code);
74 64
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: 65 private:
110 friend class DeviceTokenFetcherTest; 66 friend class DeviceTokenFetcherTest;
111 67
112 // The different states that the fetcher can be in during the process of 68 // The different states that the fetcher can be in during the process of
113 // getting the device token. 69 // getting the device token. |state_| is initialized to INACTIVE, depending
70 // on the result of a token fetching attempt can transition to either of
71 // TOKEN_AVAILABLE, UNMANAGED, or ERROR. The first attempt must be triggered
72 // externally. When |state_| is UNMANAGED, a new fetching attempt is
73 // performed every |unmanaged_device_refresh_rate_ms_|; when it's ERROR,
74 // a new attempt is done after |effective_token_fetch_error_delay_ms_|.
114 enum FetcherState { 75 enum FetcherState {
115 kStateNotStarted, 76 // Fetcher inactive.
116 kStateLoadDeviceTokenFromDisk, 77 STATE_INACTIVE,
117 kStateReadyToRequestDeviceTokenFromServer, 78 // Token available.
118 kStateRequestingDeviceTokenFromServer, 79 STATE_TOKEN_AVAILABLE,
119 kStateHasDeviceToken, 80 // Device unmanaged.
120 kStateFailure, 81 STATE_UNMANAGED,
121 kStateNotManaged, 82 // Error, retry later.
83 STATE_ERROR,
122 }; 84 };
123 85
124 // Moves the fetcher into a new state. If the fetcher has the device token 86 // Common initialization helper.
125 // or is moving into the failure state, callers waiting on WaitForToken 87 void Initialize(DeviceManagementService* service,
126 // are unblocked. 88 CloudPolicyCache* cache,
89 int64 token_fetch_error_delay_ms,
90 int64 unmanaged_device_refresh_rate_ms);
91
92 // Moves the fetcher into a new state.
127 void SetState(FetcherState state); 93 void SetState(FetcherState state);
128 94
129 // Returns the full path to the file that persists the device manager token. 95 // Resets |backend_|, then uses |auth_token_| and |device_id_| to perform
130 void GetDeviceTokenPath(FilePath* token_path) const; 96 // an actual token fetch.
97 void FetchTokenInternal();
131 98
132 // Tries to load the device token from disk. Must be called on the FILE 99 // Called back from the |retry_task_|.
133 // thread. 100 void ExecuteRetryTask();
134 void AttemptTokenLoadFromDisk();
135 101
136 // Called if it's not possible to load the device token from disk. Sets the 102 // Cancels the |retry_task_|.
137 // fetcher in a state that's ready to register the device with the device 103 void CancelRetryTask();
138 // management server and receive the device token in return. If the AuthToken
139 // for the device management server is available, initiate the server
140 // request.
141 void MakeReadyToRequestDeviceToken();
142 104
143 // Issues a registration request to the server if both the fetcher is in the 105 // Service and backend. A new backend is created whenever the fetcher gets
144 // ready-to-request state and the device management server AuthToken is 106 // reset.
145 // available. 107 DeviceManagementService* service_; // weak
146 void SendServerRequestIfPossible(); 108 scoped_ptr<DeviceManagementBackend> backend_;
147 109
148 void AddObserver(Observer* obs) { 110 // Reference to the cache. Used to persist and read unmanaged state.
149 observer_list_.AddObserver(obs); 111 CloudPolicyCache* cache_;
150 }
151 112
152 void RemoveObserver(Observer* obs) { 113 // Refresh parameters.
153 observer_list_.RemoveObserver(obs); 114 int64 token_fetch_error_delay_ms_;
154 } 115 int64 effective_token_fetch_error_delay_ms_;
116 int64 unmanaged_device_refresh_rate_ms_;
155 117
156 void NotifyTokenSuccess() { 118 // State the fetcher is currently in.
157 FOR_EACH_OBSERVER(Observer, observer_list_, OnTokenSuccess()); 119 FetcherState state_;
158 }
159 120
160 void NotifyTokenError() { 121 // Current device token.
161 FOR_EACH_OBSERVER(Observer, observer_list_, OnTokenError()); 122 std::string device_token_;
162 }
163 123
164 void NotifyNotManaged() { 124 // Contains the AuthToken for the device management server.
165 FOR_EACH_OBSERVER(Observer, observer_list_, OnNotManaged()); 125 std::string auth_token_;
166 } 126 // Device identifier to send to the server.
127 std::string device_id_;
167 128
168 // Saves the device management token to disk once it has been retrieved from 129 // Task that has been scheduled to retry fetching a token.
169 // the server. Must be called on the FILE thread. 130 CancelableTask* retry_task_;
170 static void WriteDeviceTokenToDisk(const FilePath& path,
171 const std::string& token,
172 const std::string& device_id);
173 131
174 // Generates a new device ID used to register the device with the device 132 ScopedRunnableMethodFactory<DeviceTokenFetcher> method_factory_;
175 // management server and generate the device token.
176 static std::string GenerateNewDeviceID();
177 133
178 ObserverList<Observer, true> observer_list_; 134 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 }; 135 };
197 136
198 } // namespace policy 137 } // namespace policy
199 138
200 #endif // CHROME_BROWSER_POLICY_DEVICE_TOKEN_FETCHER_H_ 139 #endif // CHROME_BROWSER_POLICY_DEVICE_TOKEN_FETCHER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698