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