OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CHROME_BROWSER_POLICY_DEVICE_TOKEN_FETCHER_H_ |
| 6 #define CHROME_BROWSER_POLICY_DEVICE_TOKEN_FETCHER_H_ |
| 7 #pragma once |
| 8 |
| 9 #include <string> |
| 10 |
| 11 #include "base/file_path.h" |
| 12 #include "base/non_thread_safe.h" |
| 13 #include "base/waitable_event.h" |
| 14 #include "chrome/common/notification_observer.h" |
| 15 #include "chrome/common/notification_registrar.h" |
| 16 #include "chrome/browser/policy/device_management_backend.h" |
| 17 |
| 18 namespace policy { |
| 19 |
| 20 namespace em = enterprise_management; |
| 21 |
| 22 // Abstracts how the path is determined where the DeviceTokenFetcher stores the |
| 23 // device token once it has been returned from the server. Tests provide a mock |
| 24 // implementation to the DeviceTokenFetcher that doesn't write to DIR_USER_DATA. |
| 25 class StoredDeviceTokenPathProvider { |
| 26 public: |
| 27 virtual ~StoredDeviceTokenPathProvider() {} |
| 28 |
| 29 // Sets |path| to contain the path at which to use to store the device |
| 30 // management token file. Returns true if successful, otherwise false. |
| 31 virtual bool GetPath(FilePath* path) const = 0; |
| 32 protected: |
| 33 StoredDeviceTokenPathProvider() {} |
| 34 private: |
| 35 DISALLOW_COPY_AND_ASSIGN(StoredDeviceTokenPathProvider); |
| 36 }; |
| 37 |
| 38 // Provides a path to the device token that's inside DIR_USER_DATA. |
| 39 class UserDirDeviceTokenPathProvider : public StoredDeviceTokenPathProvider { |
| 40 public: |
| 41 UserDirDeviceTokenPathProvider() {} |
| 42 virtual ~UserDirDeviceTokenPathProvider() {} |
| 43 virtual bool GetPath(FilePath* path) const; |
| 44 private: |
| 45 DISALLOW_COPY_AND_ASSIGN(UserDirDeviceTokenPathProvider); |
| 46 }; |
| 47 |
| 48 // Fetches the device token that can be used for policy requests with the device |
| 49 // management server, either from disk if it already has been successfully |
| 50 // requested, otherwise from the device management server. An instance of the |
| 51 // fetcher is shared as a singleton by all users of the device management token |
| 52 // to ensure they all get the same token. |
| 53 class DeviceTokenFetcher |
| 54 : public NonThreadSafe, |
| 55 public NotificationObserver, |
| 56 public DeviceManagementBackend::DeviceRegisterResponseDelegate { |
| 57 public: |
| 58 // Requests to the device management server are sent through |backend|. The |
| 59 // DeviceTokenFetcher assumes ownership of |backend|, which is passed in |
| 60 // explicitly to simplify mocking of the backend for unit testing. The |
| 61 // fetcher uses |path_provider| to determine the directory in which the device |
| 62 // token is stored once it's retrieved from the server. The fetcher assumes |
| 63 // ownership of |path_provider|. |
| 64 DeviceTokenFetcher(DeviceManagementBackend* backend, |
| 65 StoredDeviceTokenPathProvider* path_provider); |
| 66 virtual ~DeviceTokenFetcher() {} |
| 67 |
| 68 // NotificationObserver method overrides: |
| 69 virtual void Observe(NotificationType type, |
| 70 const NotificationSource& source, |
| 71 const NotificationDetails& details); |
| 72 |
| 73 // DeviceManagementBackend::DeviceRegisterResponseDelegate method overrides: |
| 74 virtual void HandleRegisterResponse( |
| 75 const em::DeviceRegisterResponse& response); |
| 76 virtual void OnError(DeviceManagementBackend::ErrorCode code); |
| 77 |
| 78 // Called by subscribers of the device management token to indicate that they |
| 79 // will need the token in the future. |
| 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 // True if the device token has been fetched and is valid. |
| 92 bool IsTokenValid() const; |
| 93 |
| 94 private: |
| 95 // The different states that the fetcher can be in during the process of |
| 96 // getting the device token. |
| 97 enum FetcherState { |
| 98 kStateLoadDeviceTokenFromDisk, |
| 99 kStateFetchingAuthToken, |
| 100 kStateHasAuthToken, |
| 101 kStateHasDeviceToken, |
| 102 kStateFailure |
| 103 }; |
| 104 |
| 105 // Moves the fetcher into a new state. If the fetcher has the device token |
| 106 // or is moving into the failure state, callers waiting on WaitForToken |
| 107 // are unblocked. |
| 108 void SetState(FetcherState state); |
| 109 |
| 110 // Saves the device management token to disk once it has been retrieved from |
| 111 // the server. Must be called on the FILE thread. |
| 112 static void WriteDeviceTokenToDisk(const FilePath& path, |
| 113 const std::string& token); |
| 114 |
| 115 // Returns the device ID used to register the device with the device |
| 116 // management server and generate the device token. |
| 117 static std::string GetDeviceID(); |
| 118 |
| 119 scoped_ptr<DeviceManagementBackend> backend_; |
| 120 scoped_ptr<StoredDeviceTokenPathProvider> path_provider_; |
| 121 FetcherState state_; |
| 122 std::string device_token_; |
| 123 |
| 124 // An event that is signaled only once the device token has been fetched |
| 125 // or it has been determined that there was an error during fetching. |
| 126 base::WaitableEvent device_token_load_complete_event_; |
| 127 |
| 128 // Registers the fetcher for notification of successful Gaia logins. |
| 129 NotificationRegistrar registrar_; |
| 130 }; |
| 131 |
| 132 } // namespace policy |
| 133 |
| 134 #endif // CHROME_BROWSER_POLICY_DEVICE_TOKEN_FETCHER_H_ |
OLD | NEW |