Home | History | Annotate | Download | only in policy
      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
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROME_BROWSER_POLICY_USER_POLICY_IDENTITY_STRATEGY_H_
      6 #define CHROME_BROWSER_POLICY_USER_POLICY_IDENTITY_STRATEGY_H_
      7 #pragma once
      8 
      9 #include <string>
     10 
     11 #include "base/file_path.h"
     12 #include "base/memory/ref_counted.h"
     13 #include "base/memory/weak_ptr.h"
     14 #include "chrome/browser/policy/cloud_policy_identity_strategy.h"
     15 #include "content/common/notification_observer.h"
     16 #include "content/common/notification_registrar.h"
     17 
     18 class Profile;
     19 
     20 namespace policy {
     21 
     22 class DeviceManagementBackend;
     23 
     24 // A token provider implementation that provides a user device token for the
     25 // user corresponding to a given profile.
     26 class UserPolicyIdentityStrategy : public CloudPolicyIdentityStrategy,
     27                                    public NotificationObserver {
     28  public:
     29   UserPolicyIdentityStrategy(Profile* profile,
     30                              const FilePath& token_cache_file);
     31   virtual ~UserPolicyIdentityStrategy();
     32 
     33   // CloudPolicyIdentityStrategy implementation:
     34   virtual std::string GetDeviceToken() OVERRIDE;
     35   virtual std::string GetDeviceID() OVERRIDE;
     36   virtual std::string GetMachineID() OVERRIDE;
     37   virtual std::string GetMachineModel() OVERRIDE;
     38   virtual em::DeviceRegisterRequest_Type GetPolicyRegisterType() OVERRIDE;
     39   virtual std::string GetPolicyType() OVERRIDE;
     40   virtual bool GetCredentials(std::string* username,
     41                               std::string* auth_token) OVERRIDE;
     42   virtual void OnDeviceTokenAvailable(const std::string& token) OVERRIDE;
     43 
     44  private:
     45   class TokenCache;
     46 
     47   // Checks whether a new token should be fetched and if so, sends out a
     48   // notification.
     49   void CheckAndTriggerFetch();
     50 
     51   // Gets the current user.
     52   std::string GetCurrentUser();
     53 
     54   // Called from the token cache when the token has been loaded.
     55   void OnCacheLoaded(const std::string& token, const std::string& device_id);
     56 
     57   // NotificationObserver method overrides:
     58   virtual void Observe(NotificationType type,
     59                        const NotificationSource& source,
     60                        const NotificationDetails& details);
     61 
     62   // The profile this provider is associated with.
     63   Profile* profile_;
     64 
     65   // Keeps the on-disk copy of the token.
     66   scoped_refptr<TokenCache> cache_;
     67 
     68   // The device ID we use.
     69   std::string device_id_;
     70 
     71   // Current device token. Empty if not available.
     72   std::string device_token_;
     73 
     74   // Registers the provider for notification of successful Gaia logins.
     75   NotificationRegistrar registrar_;
     76 
     77   // Allows to construct weak ptrs.
     78   base::WeakPtrFactory<UserPolicyIdentityStrategy> weak_ptr_factory_;
     79 
     80   DISALLOW_COPY_AND_ASSIGN(UserPolicyIdentityStrategy);
     81 };
     82 
     83 }  // namespace policy
     84 
     85 #endif  // CHROME_BROWSER_POLICY_USER_POLICY_IDENTITY_STRATEGY_H_
     86