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/scoped_ptr.h"
     14 #include "base/memory/weak_ptr.h"
     15 #include "chrome/browser/policy/cloud/cloud_policy_client.h"
     16 #include "chrome/browser/policy/cloud/cloud_policy_service.h"
     17 #include "components/browser_context_keyed_service/browser_context_keyed_service.h"
     18 #include "content/public/browser/notification_observer.h"
     19 #include "content/public/browser/notification_registrar.h"
     20 
     21 class Profile;
     22 
     23 namespace policy {
     24 
     25 class UserCloudPolicyManager;
     26 
     27 // The UserPolicySigninService is responsible for interacting with the policy
     28 // infrastructure (mainly UserCloudPolicyManager) to load policy for the signed
     29 // in user. This is the base class that contains shared behavior.
     30 //
     31 // At signin time, this class initializes the UCPM and loads policy before any
     32 // other signed in services are initialized. After each restart, this class
     33 // ensures that the CloudPolicyClient is registered (in case the policy server
     34 // was offline during the initial policy fetch) and if not it initiates a fresh
     35 // registration process.
     36 //
     37 // Finally, if the user signs out, this class is responsible for shutting down
     38 // the policy infrastructure to ensure that any cached policy is cleared.
     39 class UserPolicySigninServiceBase : public BrowserContextKeyedService,
     40                                     public CloudPolicyClient::Observer,
     41                                     public CloudPolicyService::Observer,
     42                                     public content::NotificationObserver {
     43  public:
     44   // The callback invoked once policy registration is complete. Passed
     45   // CloudPolicyClient parameter is null if DMToken fetch failed.
     46   typedef base::Callback<void(scoped_ptr<CloudPolicyClient>)>
     47       PolicyRegistrationCallback;
     48 
     49   // The callback invoked once policy fetch is complete. Passed boolean
     50   // parameter is set to true if the policy fetch succeeded.
     51   typedef base::Callback<void(bool)> PolicyFetchCallback;
     52 
     53   // Creates a UserPolicySigninServiceBase associated with the passed |profile|.
     54   explicit UserPolicySigninServiceBase(Profile* profile);
     55   virtual ~UserPolicySigninServiceBase();
     56 
     57   // Initiates a policy fetch as part of user signin, using a CloudPolicyClient
     58   // previously initialized via RegisterPolicyClient. |callback| is invoked
     59   // once the policy fetch is complete, passing true if the policy fetch
     60   // succeeded.
     61   void FetchPolicyForSignedInUser(scoped_ptr<CloudPolicyClient> client,
     62                                   const PolicyFetchCallback& callback);
     63 
     64   // content::NotificationObserver implementation:
     65   virtual void Observe(int type,
     66                        const content::NotificationSource& source,
     67                        const content::NotificationDetails& details) OVERRIDE;
     68 
     69   // CloudPolicyService::Observer implementation:
     70   virtual void OnInitializationCompleted(CloudPolicyService* service) OVERRIDE;
     71 
     72   // CloudPolicyClient::Observer implementation:
     73   virtual void OnPolicyFetched(CloudPolicyClient* client) OVERRIDE;
     74   virtual void OnRegistrationStateChanged(CloudPolicyClient* client) OVERRIDE;
     75   virtual void OnClientError(CloudPolicyClient* client) OVERRIDE;
     76 
     77   // BrowserContextKeyedService implementation:
     78   virtual void Shutdown() OVERRIDE;
     79 
     80  protected:
     81   // Returns true if policy should be loaded even when Gaia reports that the
     82   // account doesn't have management enabled.
     83   static bool ShouldForceLoadPolicy();
     84 
     85   // Returns a CloudPolicyClient to perform a registration with the DM server,
     86   // or NULL if |username| shouldn't register for policy management.
     87   scoped_ptr<CloudPolicyClient> PrepareToRegister(const std::string& username);
     88 
     89   // Returns false if cloud policy is disabled or if the passed |email_address|
     90   // is definitely not from a hosted domain (according to the blacklist in
     91   // BrowserPolicyConnector::IsNonEnterpriseUser()).
     92   bool ShouldLoadPolicyForUser(const std::string& email_address);
     93 
     94   // Invoked to initialize the UserPolicySigninService once its owning Profile
     95   // becomes ready. If the Profile has a signed-in account associated with it
     96   // at startup then this initializes the cloud policy manager by calling
     97   // InitializeForSignedInUser(); otherwise it clears any stored policies.
     98   void InitializeOnProfileReady();
     99 
    100   // Invoked to initialize the cloud policy service for |username|, which is the
    101   // account associated with the Profile that owns this service. This is invoked
    102   // from InitializeOnProfileReady() if the Profile already has a signed-in
    103   // account at startup, or (on the desktop platforms) as soon as the user
    104   // signs-in and an OAuth2 login refresh token becomes available.
    105   void InitializeForSignedInUser(const std::string& username);
    106 
    107   // Initializes the cloud policy manager with the passed |client|. This is
    108   // called from InitializeForSignedInUser() when the Profile already has a
    109   // signed in account at startup, and from FetchPolicyForSignedInUser() during
    110   // the initial policy fetch after signing in.
    111   virtual void InitializeUserCloudPolicyManager(
    112       scoped_ptr<CloudPolicyClient> client);
    113 
    114   // Shuts down the UserCloudPolicyManager (for example, after the user signs
    115   // out) and deletes any cached policy.
    116   virtual void ShutdownUserCloudPolicyManager();
    117 
    118   // Convenience helper to get the UserCloudPolicyManager for |profile_|.
    119   UserCloudPolicyManager* GetManager();
    120 
    121   Profile* profile() { return profile_; }
    122   content::NotificationRegistrar* registrar() { return &registrar_; }
    123 
    124  private:
    125   // Weak pointer to the profile this service is associated with.
    126   Profile* profile_;
    127 
    128   content::NotificationRegistrar registrar_;
    129 
    130   base::WeakPtrFactory<UserPolicySigninServiceBase> weak_factory_;
    131 
    132   DISALLOW_COPY_AND_ASSIGN(UserPolicySigninServiceBase);
    133 };
    134 
    135 }  // namespace policy
    136 
    137 #endif  // CHROME_BROWSER_POLICY_CLOUD_USER_POLICY_SIGNIN_SERVICE_BASE_H_
    138