Home | History | Annotate | Download | only in cloud
      1 // Copyright 2013 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_CLOUD_USER_POLICY_SIGNIN_SERVICE_BASE_H_
      6 #define CHROME_BROWSER_POLICY_CLOUD_USER_POLICY_SIGNIN_SERVICE_BASE_H_
      7 
      8 #include <string>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/callback.h"
     12 #include "base/compiler_specific.h"
     13 #include "base/memory/ref_counted.h"
     14 #include "base/memory/scoped_ptr.h"
     15 #include "base/memory/weak_ptr.h"
     16 #include "components/browser_context_keyed_service/browser_context_keyed_service.h"
     17 #include "components/policy/core/common/cloud/cloud_policy_client.h"
     18 #include "components/policy/core/common/cloud/cloud_policy_service.h"
     19 #include "content/public/browser/notification_observer.h"
     20 #include "content/public/browser/notification_registrar.h"
     21 
     22 class PrefService;
     23 class Profile;
     24 class SigninManager;
     25 
     26 namespace net {
     27 class URLRequestContextGetter;
     28 }
     29 
     30 namespace policy {
     31 
     32 class DeviceManagementService;
     33 class UserCloudPolicyManager;
     34 
     35 // The UserPolicySigninService is responsible for interacting with the policy
     36 // infrastructure (mainly UserCloudPolicyManager) to load policy for the signed
     37 // in user. This is the base class that contains shared behavior.
     38 //
     39 // At signin time, this class initializes the UCPM and loads policy before any
     40 // other signed in services are initialized. After each restart, this class
     41 // ensures that the CloudPolicyClient is registered (in case the policy server
     42 // was offline during the initial policy fetch) and if not it initiates a fresh
     43 // registration process.
     44 //
     45 // Finally, if the user signs out, this class is responsible for shutting down
     46 // the policy infrastructure to ensure that any cached policy is cleared.
     47 class UserPolicySigninServiceBase : public BrowserContextKeyedService,
     48                                     public CloudPolicyClient::Observer,
     49                                     public CloudPolicyService::Observer,
     50                                     public content::NotificationObserver {
     51  public:
     52   // The callback invoked once policy registration is complete. Passed
     53   // |dm_token| and |client_id| parameters are empty if policy registration
     54   // failed.
     55   typedef base::Callback<void(const std::string& dm_token,
     56                               const std::string& client_id)>
     57       PolicyRegistrationCallback;
     58 
     59   // The callback invoked once policy fetch is complete. Passed boolean
     60   // parameter is set to true if the policy fetch succeeded.
     61   typedef base::Callback<void(bool)> PolicyFetchCallback;
     62 
     63   // Creates a UserPolicySigninServiceBase associated with the passed |profile|.
     64   UserPolicySigninServiceBase(
     65       Profile* profile,
     66       PrefService* local_state,
     67       DeviceManagementService* device_management_service,
     68       scoped_refptr<net::URLRequestContextGetter> system_request_context);
     69   virtual ~UserPolicySigninServiceBase();
     70 
     71   // Initiates a policy fetch as part of user signin, using a |dm_token| and
     72   // |client_id| fetched via RegisterForPolicy(). |callback| is invoked
     73   // once the policy fetch is complete, passing true if the policy fetch
     74   // succeeded.
     75   void FetchPolicyForSignedInUser(const std::string& username,
     76                                   const std::string& dm_token,
     77                                   const std::string& client_id,
     78                                   const PolicyFetchCallback& callback);
     79 
     80   // content::NotificationObserver implementation:
     81   virtual void Observe(int type,
     82                        const content::NotificationSource& source,
     83                        const content::NotificationDetails& details) OVERRIDE;
     84 
     85   // CloudPolicyService::Observer implementation:
     86   virtual void OnInitializationCompleted(CloudPolicyService* service) OVERRIDE;
     87 
     88   // CloudPolicyClient::Observer implementation:
     89   virtual void OnPolicyFetched(CloudPolicyClient* client) OVERRIDE;
     90   virtual void OnRegistrationStateChanged(CloudPolicyClient* client) OVERRIDE;
     91   virtual void OnClientError(CloudPolicyClient* client) OVERRIDE;
     92 
     93   // BrowserContextKeyedService implementation:
     94   virtual void Shutdown() OVERRIDE;
     95 
     96   void SetSystemRequestContext(
     97       scoped_refptr<net::URLRequestContextGetter> request_context);
     98 
     99  protected:
    100   net::URLRequestContextGetter* system_request_context() {
    101     return system_request_context_;
    102   }
    103 
    104   // Returns true if policy should be loaded even when Gaia reports that the
    105   // account doesn't have management enabled.
    106   static bool ShouldForceLoadPolicy();
    107 
    108   // Returns a CloudPolicyClient to perform a registration with the DM server,
    109   // or NULL if |username| shouldn't register for policy management.
    110   scoped_ptr<CloudPolicyClient> CreateClientForRegistrationOnly(
    111       const std::string& username);
    112 
    113   // Returns false if cloud policy is disabled or if the passed |email_address|
    114   // is definitely not from a hosted domain (according to the blacklist in
    115   // BrowserPolicyConnector::IsNonEnterpriseUser()).
    116   bool ShouldLoadPolicyForUser(const std::string& email_address);
    117 
    118   // Invoked to initialize the UserPolicySigninService once its owning Profile
    119   // becomes ready. If the Profile has a signed-in account associated with it
    120   // at startup then this initializes the cloud policy manager by calling
    121   // InitializeForSignedInUser(); otherwise it clears any stored policies.
    122   void InitializeOnProfileReady();
    123 
    124   // Invoked to initialize the cloud policy service for |username|, which is the
    125   // account associated with the Profile that owns this service. This is invoked
    126   // from InitializeOnProfileReady() if the Profile already has a signed-in
    127   // account at startup, or (on the desktop platforms) as soon as the user
    128   // signs-in and an OAuth2 login refresh token becomes available.
    129   void InitializeForSignedInUser(const std::string& username);
    130 
    131   // Initializes the cloud policy manager with the passed |client|. This is
    132   // called from InitializeForSignedInUser() when the Profile already has a
    133   // signed in account at startup, and from FetchPolicyForSignedInUser() during
    134   // the initial policy fetch after signing in.
    135   virtual void InitializeUserCloudPolicyManager(
    136       const std::string& username,
    137       scoped_ptr<CloudPolicyClient> client);
    138 
    139   // Prepares for the UserCloudPolicyManager to be shutdown due to
    140   // user signout or profile destruction.
    141   virtual void PrepareForUserCloudPolicyManagerShutdown();
    142 
    143   // Shuts down the UserCloudPolicyManager (for example, after the user signs
    144   // out) and deletes any cached policy.
    145   virtual void ShutdownUserCloudPolicyManager();
    146 
    147   // Convenience helper to get the UserCloudPolicyManager for |profile_|.
    148   UserCloudPolicyManager* GetManager();
    149 
    150   Profile* profile() { return profile_; }
    151   content::NotificationRegistrar* registrar() { return &registrar_; }
    152   SigninManager* GetSigninManager();
    153 
    154  private:
    155   // Helper functions to create a request context for use by CloudPolicyClients.
    156   scoped_refptr<net::URLRequestContextGetter> CreateUserRequestContext();
    157   scoped_refptr<net::URLRequestContextGetter> CreateSystemRequestContext();
    158 
    159   // Weak pointer to the profile this service is associated with.
    160   Profile* profile_;
    161 
    162   content::NotificationRegistrar registrar_;
    163 
    164   PrefService* local_state_;
    165   DeviceManagementService* device_management_service_;
    166   scoped_refptr<net::URLRequestContextGetter> system_request_context_;
    167 
    168   base::WeakPtrFactory<UserPolicySigninServiceBase> weak_factory_;
    169 
    170   DISALLOW_COPY_AND_ASSIGN(UserPolicySigninServiceBase);
    171 };
    172 
    173 }  // namespace policy
    174 
    175 #endif  // CHROME_BROWSER_POLICY_CLOUD_USER_POLICY_SIGNIN_SERVICE_BASE_H_
    176