| 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/ref_counted.h" | 12 #include "base/ref_counted.h" |
| 13 #include "base/waitable_event.h" | 13 #include "base/waitable_event.h" |
| 14 #include "chrome/browser/policy/device_management_backend.h" | 14 #include "chrome/browser/policy/device_management_backend.h" |
| 15 #include "chrome/common/notification_observer.h" | 15 #include "chrome/common/notification_observer.h" |
| 16 #include "chrome/common/notification_registrar.h" | 16 #include "chrome/common/notification_registrar.h" |
| 17 | 17 |
| 18 class TokenService; | 18 class Profile; |
| 19 | 19 |
| 20 namespace policy { | 20 namespace policy { |
| 21 | 21 |
| 22 namespace em = enterprise_management; | 22 namespace em = enterprise_management; |
| 23 | 23 |
| 24 // Fetches the device token that can be used for policy requests with the device | 24 // 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 | 25 // management server, either from disk if it already has been successfully |
| 26 // requested, otherwise from the device management server. An instance of the | 26 // 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 | 27 // fetcher is shared as a singleton by all users of the device management token |
| 28 // to ensure they all get the same token. | 28 // to ensure they all get the same token. |
| 29 class DeviceTokenFetcher | 29 class DeviceTokenFetcher |
| 30 : public NotificationObserver, | 30 : public NotificationObserver, |
| 31 public DeviceManagementBackend::DeviceRegisterResponseDelegate, | 31 public DeviceManagementBackend::DeviceRegisterResponseDelegate, |
| 32 public base::RefCountedThreadSafe<DeviceTokenFetcher> { | 32 public base::RefCountedThreadSafe<DeviceTokenFetcher> { |
| 33 public: | 33 public: |
| 34 // Requests to the device management server are sent through |backend|. It | 34 // Requests to the device management server are sent through |backend|. It |
| 35 // obtains the authentication token from |token_service|. The fetcher stores | 35 // obtains the authentication token from |token_service|. The fetcher stores |
| 36 // the device token to |token_path| once it's retrieved from the server. | 36 // the device token to |token_path| once it's retrieved from the server. |
| 37 DeviceTokenFetcher(DeviceManagementBackend* backend, | 37 DeviceTokenFetcher(DeviceManagementBackend* backend, |
| 38 TokenService* token_service, | 38 Profile* profile, |
| 39 const FilePath& token_path); | 39 const FilePath& token_path); |
| 40 virtual ~DeviceTokenFetcher() {} | 40 virtual ~DeviceTokenFetcher() {} |
| 41 | 41 |
| 42 // NotificationObserver method overrides: | 42 // NotificationObserver method overrides: |
| 43 virtual void Observe(NotificationType type, | 43 virtual void Observe(NotificationType type, |
| 44 const NotificationSource& source, | 44 const NotificationSource& source, |
| 45 const NotificationDetails& details); | 45 const NotificationDetails& details); |
| 46 | 46 |
| 47 // DeviceManagementBackend::DeviceRegisterResponseDelegate method overrides: | 47 // DeviceManagementBackend::DeviceRegisterResponseDelegate method overrides: |
| 48 virtual void HandleRegisterResponse( | 48 virtual void HandleRegisterResponse( |
| (...skipping 20 matching lines...) Expand all Loading... |
| 69 // Returns the device ID for this device. If no such ID has been set yet, a | 69 // Returns the device ID for this device. If no such ID has been set yet, a |
| 70 // new ID is generated and returned. | 70 // new ID is generated and returned. |
| 71 std::string GetDeviceID(); | 71 std::string GetDeviceID(); |
| 72 | 72 |
| 73 // True if the fetcher has a valid AuthToken for the device management server. | 73 // True if the fetcher has a valid AuthToken for the device management server. |
| 74 bool HasAuthToken() const { return !auth_token_.empty(); } | 74 bool HasAuthToken() const { return !auth_token_.empty(); } |
| 75 | 75 |
| 76 // True if the device token has been fetched and is valid. | 76 // True if the device token has been fetched and is valid. |
| 77 bool IsTokenValid() const; | 77 bool IsTokenValid() const; |
| 78 | 78 |
| 79 protected: |
| 80 // Returns the email address of the currently logged-in user. |
| 81 virtual std::string GetCurrentUser(); |
| 82 |
| 83 // Used to identify GOOGLE_SIGNIN_SUCCESSFUL notifications from the owning |
| 84 // profile, and to query for the current username. |
| 85 Profile* profile_; // weak |
| 86 |
| 79 private: | 87 private: |
| 80 friend class DeviceTokenFetcherTest; | 88 friend class DeviceTokenFetcherTest; |
| 81 | 89 |
| 82 // The different states that the fetcher can be in during the process of | 90 // The different states that the fetcher can be in during the process of |
| 83 // getting the device token. | 91 // getting the device token. |
| 84 enum FetcherState { | 92 enum FetcherState { |
| 85 kStateNotStarted, | 93 kStateNotStarted, |
| 86 kStateLoadDeviceTokenFromDisk, | 94 kStateLoadDeviceTokenFromDisk, |
| 87 kStateReadyToRequestDeviceTokenFromServer, | 95 kStateReadyToRequestDeviceTokenFromServer, |
| 88 kStateRequestingDeviceTokenFromServer, | 96 kStateRequestingDeviceTokenFromServer, |
| (...skipping 30 matching lines...) Expand all Loading... |
| 119 static void WriteDeviceTokenToDisk(const FilePath& path, | 127 static void WriteDeviceTokenToDisk(const FilePath& path, |
| 120 const std::string& token, | 128 const std::string& token, |
| 121 const std::string& device_id); | 129 const std::string& device_id); |
| 122 | 130 |
| 123 // Generates a new device ID used to register the device with the device | 131 // Generates a new device ID used to register the device with the device |
| 124 // management server and generate the device token. | 132 // management server and generate the device token. |
| 125 static std::string GenerateNewDeviceID(); | 133 static std::string GenerateNewDeviceID(); |
| 126 | 134 |
| 127 FilePath token_path_; | 135 FilePath token_path_; |
| 128 DeviceManagementBackend* backend_; // weak | 136 DeviceManagementBackend* backend_; // weak |
| 129 TokenService* token_service_; | |
| 130 FetcherState state_; | 137 FetcherState state_; |
| 131 std::string device_token_; | 138 std::string device_token_; |
| 132 std::string device_id_; | 139 std::string device_id_; |
| 133 | 140 |
| 134 // Contains the AuthToken for the device management server. Empty if the | 141 // 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 | 142 // AuthToken hasn't been issued yet or that was an error getting the |
| 136 // AuthToken. | 143 // AuthToken. |
| 137 std::string auth_token_; | 144 std::string auth_token_; |
| 138 | 145 |
| 139 // An event that is signaled only once the device token has been fetched | 146 // 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. | 147 // or it has been determined that there was an error during fetching. |
| 141 base::WaitableEvent device_token_load_complete_event_; | 148 base::WaitableEvent device_token_load_complete_event_; |
| 142 | 149 |
| 143 // Registers the fetcher for notification of successful Gaia logins. | 150 // Registers the fetcher for notification of successful Gaia logins. |
| 144 NotificationRegistrar registrar_; | 151 NotificationRegistrar registrar_; |
| 145 }; | 152 }; |
| 146 | 153 |
| 147 } // namespace policy | 154 } // namespace policy |
| 148 | 155 |
| 149 #endif // CHROME_BROWSER_POLICY_DEVICE_TOKEN_FETCHER_H_ | 156 #endif // CHROME_BROWSER_POLICY_DEVICE_TOKEN_FETCHER_H_ |
| OLD | NEW |