Chromium Code Reviews| Index: chrome/browser/policy/device_token_fetcher.h |
| diff --git a/chrome/browser/policy/device_token_fetcher.h b/chrome/browser/policy/device_token_fetcher.h |
| index ae22f3d500a54efe1b92c82ec0d98a3f9a6bf7ab..1b792eed40bd0157ae1935e196508b6183aff4fe 100644 |
| --- a/chrome/browser/policy/device_token_fetcher.h |
| +++ b/chrome/browser/policy/device_token_fetcher.h |
| @@ -7,20 +7,17 @@ |
| #pragma once |
| #include <string> |
| -#include <vector> |
| -#include "base/file_path.h" |
| #include "base/observer_list.h" |
| -#include "base/ref_counted.h" |
| -#include "base/synchronization/waitable_event.h" |
| +#include "base/scoped_ptr.h" |
| +#include "base/task.h" |
| #include "chrome/browser/policy/device_management_backend.h" |
| -#include "chrome/common/notification_observer.h" |
| -#include "chrome/common/notification_registrar.h" |
| - |
| -class Profile; |
| namespace policy { |
| +class CloudPolicyCache; |
| +class DeviceManagementService; |
| + |
| namespace em = enterprise_management; |
| // Fetches the device token that can be used for policy requests with the device |
| @@ -29,170 +26,104 @@ namespace em = enterprise_management; |
| // fetcher is shared as a singleton by all users of the device management token |
| // to ensure they all get the same token. |
| class DeviceTokenFetcher |
| - : public NotificationObserver, |
| - public DeviceManagementBackend::DeviceRegisterResponseDelegate, |
| - public base::RefCountedThreadSafe<DeviceTokenFetcher> { |
| + : public DeviceManagementBackend::DeviceRegisterResponseDelegate { |
| public: |
| class Observer { |
| public: |
| - virtual void OnTokenSuccess() = 0; |
| - virtual void OnTokenError() = 0; |
| - virtual void OnNotManaged() = 0; |
| virtual ~Observer() {} |
| + virtual void OnTokenAvailable() = 0; |
| }; |
| - class ObserverRegistrar { |
| - public: |
| - ObserverRegistrar(); |
| - ~ObserverRegistrar(); |
| - |
| - void Init(DeviceTokenFetcher* token_fetcher); |
| - void AddObserver(DeviceTokenFetcher::Observer* observer); |
| - void RemoveAll(); |
| - private: |
| - DeviceTokenFetcher* token_fetcher_; |
| - std::vector<DeviceTokenFetcher::Observer*> observers_; |
| - }; |
| - |
| - // Requests to the device management server are sent through |backend|. It |
| - // obtains the authentication token from |token_service|. The fetcher stores |
| - // the device token to |token_path| once it's retrieved from the server. |
| - DeviceTokenFetcher(DeviceManagementBackend* backend, |
| - Profile* profile, |
| - const FilePath& token_path); |
| + // |service| is used to talk to the device management service and |cache| is |
| + // used to persist whether the device is unmanaged. |
| + DeviceTokenFetcher(DeviceManagementService* service, |
| + CloudPolicyCache* cache); |
| + // Version for tests that allows to set timing paramters. |
| + DeviceTokenFetcher(DeviceManagementService* service, |
| + CloudPolicyCache* cache, |
| + int64 token_fetch_error_delay_ms, |
| + int64 unmanaged_device_refresh_rate_ms); |
| virtual ~DeviceTokenFetcher(); |
| - // NotificationObserver method overrides: |
| - virtual void Observe(NotificationType type, |
| - const NotificationSource& source, |
| - const NotificationDetails& details); |
| + // Resets the fetcher. |
| + void Reset(); |
| + |
| + // Starts fetching a token. |
| + void FetchToken(const std::string& auth_token, |
| + const std::string& device_id); |
| + |
| + // Returns the device management token or the empty string if not available. |
| + const std::string& GetDeviceToken(); |
| + |
| + void AddObserver(Observer* observer); |
| + void RemoveObserver(Observer* observer); |
| // DeviceManagementBackend::DeviceRegisterResponseDelegate method overrides: |
| virtual void HandleRegisterResponse( |
| const em::DeviceRegisterResponse& response); |
| virtual void OnError(DeviceManagementBackend::ErrorCode code); |
| - // Re-initializes this DeviceTokenFetcher |
| - void Restart(); |
| - |
| - // Called by subscribers of the device management token to indicate that they |
| - // will need the token in the future. Must be called on the UI thread. |
| - void StartFetching(); |
| - |
| - // Returns true if there is a pending token request to the device management |
| - // server. |
| - bool IsTokenPending(); |
| - |
| - // Returns the device management token for this device, blocking until |
| - // outstanding requests to the device management server are satisfied. In the |
| - // case that the token could not be fetched, an empty string is returned. |
| - std::string GetDeviceToken(); |
| - |
| - // Returns the device ID for this device. If no such ID has been set yet, a |
| - // new ID is generated and returned. |
| - std::string GetDeviceID(); |
| - |
| - // True if the fetcher has a valid AuthToken for the device management server. |
| - bool HasAuthToken() const { return !auth_token_.empty(); } |
| - |
| - // True if the device token has been fetched and is valid. |
| - bool IsTokenValid() const; |
| - |
| - protected: |
| - // Returns the email address of the currently logged-in user. |
| - virtual std::string GetCurrentUser(); |
| - |
| - // Used to identify GOOGLE_SIGNIN_SUCCESSFUL notifications from the owning |
| - // profile, and to query for the current username. |
| - Profile* profile_; // weak |
| - |
| private: |
| friend class DeviceTokenFetcherTest; |
| // The different states that the fetcher can be in during the process of |
| // getting the device token. |
| enum FetcherState { |
| - kStateNotStarted, |
| - kStateLoadDeviceTokenFromDisk, |
| - kStateReadyToRequestDeviceTokenFromServer, |
| - kStateRequestingDeviceTokenFromServer, |
| - kStateHasDeviceToken, |
| - kStateFailure, |
| - kStateNotManaged, |
| + // Fetcher inactive. |
| + STATE_INACTIVE, |
| + // Token available. |
| + STATE_TOKEN_AVAILABLE, |
| + // Device unmanaged. |
| + STATE_UNMANAGED, |
| + // Error, retry later. |
| + STATE_ERROR, |
| }; |
| - // Moves the fetcher into a new state. If the fetcher has the device token |
| - // or is moving into the failure state, callers waiting on WaitForToken |
| - // are unblocked. |
| - void SetState(FetcherState state); |
| - |
| - // Returns the full path to the file that persists the device manager token. |
| - void GetDeviceTokenPath(FilePath* token_path) const; |
| - |
| - // Tries to load the device token from disk. Must be called on the FILE |
| - // thread. |
| - void AttemptTokenLoadFromDisk(); |
| - |
| - // Called if it's not possible to load the device token from disk. Sets the |
| - // fetcher in a state that's ready to register the device with the device |
| - // management server and receive the device token in return. If the AuthToken |
| - // for the device management server is available, initiate the server |
| - // request. |
| - void MakeReadyToRequestDeviceToken(); |
| - |
| - // Issues a registration request to the server if both the fetcher is in the |
| - // ready-to-request state and the device management server AuthToken is |
| - // available. |
| - void SendServerRequestIfPossible(); |
| - |
| - void AddObserver(Observer* obs) { |
| - observer_list_.AddObserver(obs); |
| - } |
| + // Common initialization helper. |
| + void Initialize(DeviceManagementService* service, |
| + CloudPolicyCache* cache, |
| + int64 token_fetch_error_delay_ms, |
| + int64 unmanaged_device_refresh_rate_ms); |
| - void RemoveObserver(Observer* obs) { |
| - observer_list_.RemoveObserver(obs); |
| - } |
| + // Moves the fetcher into a new state. |
| + void SetState(FetcherState state); |
| - void NotifyTokenSuccess() { |
| - FOR_EACH_OBSERVER(Observer, observer_list_, OnTokenSuccess()); |
| - } |
| + // Called back from the delayed work task. |
| + void DoDelayedWork(); |
|
danno
2011/02/04 16:01:33
What kind of work. Why? What's the motivation?
Jakob Kummerow
2011/02/07 16:00:09
Seeing what this method does, I suggest we name it
Jakob Kummerow
2011/02/14 13:50:34
Done.
|
| - void NotifyTokenError() { |
| - FOR_EACH_OBSERVER(Observer, observer_list_, OnTokenError()); |
| - } |
| + // Cancels delayed work. |
| + void CancelDelayedWork(); |
| - void NotifyNotManaged() { |
| - FOR_EACH_OBSERVER(Observer, observer_list_, OnNotManaged()); |
| - } |
| + // Service and backend. A new backend is created whenever the fetcher gets |
| + // reset. |
| + DeviceManagementService* service_; // weak |
| + scoped_ptr<DeviceManagementBackend> backend_; |
| - // Saves the device management token to disk once it has been retrieved from |
| - // the server. Must be called on the FILE thread. |
| - static void WriteDeviceTokenToDisk(const FilePath& path, |
| - const std::string& token, |
| - const std::string& device_id); |
| + // Reference to the cache. Used to persist and read unmanaged state. |
| + CloudPolicyCache* cache_; |
| - // Generates a new device ID used to register the device with the device |
| - // management server and generate the device token. |
| - static std::string GenerateNewDeviceID(); |
| + // Refresh parameters. |
| + int64 token_fetch_error_delay_ms_; |
| + int64 effective_token_fetch_error_delay_ms_; |
| + int64 unmanaged_device_refresh_rate_ms_; |
| - ObserverList<Observer, true> observer_list_; |
| - FilePath token_path_; |
| - DeviceManagementBackend* backend_; // weak |
| + // State the fetcher is currently in. |
| FetcherState state_; |
| + |
| + // Current device token. |
| std::string device_token_; |
| - std::string device_id_; |
| - // Contains the AuthToken for the device management server. Empty if the |
| - // AuthToken hasn't been issued yet or that was an error getting the |
| - // AuthToken. |
| + // Contains the AuthToken for the device management server. |
| std::string auth_token_; |
| + // Device identifier to send to the server. |
| + std::string device_id_; |
| + |
| + // Task that has been scheduled to do delayed work. |
| + CancelableTask* delayed_work_task_; |
| - // An event that is signaled only once the device token has been fetched |
| - // or it has been determined that there was an error during fetching. |
| - base::WaitableEvent device_token_load_complete_event_; |
| + ScopedRunnableMethodFactory<DeviceTokenFetcher> method_factory_; |
| - // Registers the fetcher for notification of successful Gaia logins. |
| - NotificationRegistrar registrar_; |
| + ObserverList<Observer, true> observer_list_; |
| }; |
| } // namespace policy |