Home | History | Annotate | Download | only in policy
      1 // Copyright (c) 2012 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_CHROMEOS_POLICY_USER_CLOUD_POLICY_MANAGER_CHROMEOS_H_
      6 #define CHROME_BROWSER_CHROMEOS_POLICY_USER_CLOUD_POLICY_MANAGER_CHROMEOS_H_
      7 
      8 #include <string>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/compiler_specific.h"
     12 #include "base/memory/ref_counted.h"
     13 #include "base/memory/scoped_ptr.h"
     14 #include "base/timer/timer.h"
     15 #include "chrome/browser/policy/cloud/cloud_policy_client.h"
     16 #include "chrome/browser/policy/cloud/cloud_policy_constants.h"
     17 #include "chrome/browser/policy/cloud/cloud_policy_manager.h"
     18 #include "chrome/browser/policy/cloud/cloud_policy_service.h"
     19 #include "chrome/browser/policy/cloud/component_cloud_policy_service.h"
     20 #include "components/browser_context_keyed_service/browser_context_keyed_service.h"
     21 
     22 class GoogleServiceAuthError;
     23 class PrefService;
     24 
     25 namespace net {
     26 class URLRequestContextGetter;
     27 }
     28 
     29 namespace policy {
     30 
     31 class DeviceManagementService;
     32 class PolicyOAuth2TokenFetcher;
     33 class ResourceCache;
     34 
     35 // UserCloudPolicyManagerChromeOS implements logic for initializing user policy
     36 // on Chrome OS.
     37 class UserCloudPolicyManagerChromeOS
     38     : public CloudPolicyManager,
     39       public CloudPolicyClient::Observer,
     40       public CloudPolicyService::Observer,
     41       public ComponentCloudPolicyService::Delegate,
     42       public BrowserContextKeyedService {
     43  public:
     44   // If |wait_for_policy_fetch| is true, IsInitializationComplete() will return
     45   // false as long as there hasn't been a successful policy fetch.
     46   UserCloudPolicyManagerChromeOS(
     47       scoped_ptr<CloudPolicyStore> store,
     48       scoped_ptr<ResourceCache> resource_cache,
     49       bool wait_for_policy_fetch,
     50       base::TimeDelta initial_policy_fetch_timeout);
     51   virtual ~UserCloudPolicyManagerChromeOS();
     52 
     53   // Initializes the cloud connection. |local_state| and
     54   // |device_management_service| must stay valid until this object is deleted.
     55   void Connect(PrefService* local_state,
     56                DeviceManagementService* device_management_service,
     57                scoped_refptr<net::URLRequestContextGetter> request_context,
     58                UserAffiliation user_affiliation);
     59 
     60   // This class is one of the policy providers, and must be ready for the
     61   // creation of the Profile's PrefService; all the other
     62   // BrowserContextKeyedServices depend on the PrefService, so this class can't
     63   // depend on other BCKS to avoid a circular dependency. So instead of using
     64   // the ProfileOAuth2TokenService directly to get the access token, a 3rd
     65   // service (UserCloudPolicyTokenForwarder) will fetch it later and pass it
     66   // to this method once available.
     67   // The |access_token| can then be used to authenticate the registration
     68   // request to the DMServer.
     69   void OnAccessTokenAvailable(const std::string& access_token);
     70 
     71   // Returns true if the underlying CloudPolicyClient is already registered.
     72   bool IsClientRegistered() const;
     73 
     74   // ConfigurationPolicyProvider:
     75   virtual void Shutdown() OVERRIDE;
     76   virtual bool IsInitializationComplete(PolicyDomain domain) const OVERRIDE;
     77   virtual void RegisterPolicyDomain(
     78       scoped_refptr<const PolicyDomainDescriptor> descriptor) OVERRIDE;
     79 
     80   // CloudPolicyManager:
     81   virtual scoped_ptr<PolicyBundle> CreatePolicyBundle() OVERRIDE;
     82 
     83   // CloudPolicyService::Observer:
     84   virtual void OnInitializationCompleted(CloudPolicyService* service) OVERRIDE;
     85 
     86   // CloudPolicyClient::Observer:
     87   virtual void OnPolicyFetched(CloudPolicyClient* client) OVERRIDE;
     88   virtual void OnRegistrationStateChanged(CloudPolicyClient* client) OVERRIDE;
     89   virtual void OnClientError(CloudPolicyClient* client) OVERRIDE;
     90 
     91   // ComponentCloudPolicyService::Delegate:
     92   virtual void OnComponentCloudPolicyRefreshNeeded() OVERRIDE;
     93   virtual void OnComponentCloudPolicyUpdated() OVERRIDE;
     94 
     95  private:
     96   // Fetches a policy token using the authentication context of the signin
     97   // Profile, and calls back to OnOAuth2PolicyTokenFetched when done.
     98   void FetchPolicyOAuthTokenUsingSigninProfile();
     99 
    100   // Called once the policy access token is available, and starts the
    101   // registration with the policy server if the token was successfully fetched.
    102   void OnOAuth2PolicyTokenFetched(const std::string& policy_token,
    103                                   const GoogleServiceAuthError& error);
    104 
    105   // Completion handler for the explicit policy fetch triggered on startup in
    106   // case |wait_for_policy_fetch_| is true. |success| is true if the fetch was
    107   // successful.
    108   void OnInitialPolicyFetchComplete(bool success);
    109 
    110   // Cancels waiting for the policy fetch and flags the
    111   // ConfigurationPolicyProvider ready (assuming all other initialization tasks
    112   // have completed).
    113   void CancelWaitForPolicyFetch();
    114 
    115   void StartRefreshSchedulerIfReady();
    116 
    117   // Owns the store, note that CloudPolicyManager just keeps a plain pointer.
    118   scoped_ptr<CloudPolicyStore> store_;
    119 
    120   // Handles fetching and storing cloud policy for components. It uses the
    121   // |store_|, so destroy it first.
    122   scoped_ptr<ComponentCloudPolicyService> component_policy_service_;
    123 
    124   // Whether to wait for a policy fetch to complete before reporting
    125   // IsInitializationComplete().
    126   bool wait_for_policy_fetch_;
    127 
    128   // A timer that puts a hard limit on the maximum time to wait for the intial
    129   // policy fetch.
    130   base::Timer policy_fetch_timeout_;
    131 
    132   // The pref service to pass to the refresh scheduler on initialization.
    133   PrefService* local_state_;
    134 
    135   // Used to fetch the policy OAuth token, when necessary. This object holds
    136   // a callback with an unretained reference to the manager, when it exists.
    137   scoped_ptr<PolicyOAuth2TokenFetcher> token_fetcher_;
    138 
    139   // The access token passed to OnAccessTokenAvailable. It is stored here so
    140   // that it can be used if OnInitializationCompleted is called later.
    141   std::string access_token_;
    142 
    143   DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyManagerChromeOS);
    144 };
    145 
    146 }  // namespace policy
    147 
    148 #endif  // CHROME_BROWSER_CHROMEOS_POLICY_USER_CLOUD_POLICY_MANAGER_CHROMEOS_H_
    149