Home | History | Annotate | Download | only in cloud
      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_POLICY_CLOUD_CLOUD_POLICY_SERVICE_H_
      6 #define CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_SERVICE_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/callback_forward.h"
     13 #include "base/compiler_specific.h"
     14 #include "base/observer_list.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_store.h"
     18 
     19 namespace policy {
     20 
     21 // Coordinates cloud policy handling, moving downloaded policy from the client
     22 // to the store, and setting up client registrations from cached data in the
     23 // store. Also coordinates actions on policy refresh triggers.
     24 class CloudPolicyService : public CloudPolicyClient::Observer,
     25                            public CloudPolicyStore::Observer {
     26  public:
     27   // Callback invoked once the policy refresh attempt has completed. Passed
     28   // bool parameter is true if the refresh was successful (no error).
     29   typedef base::Callback<void(bool)> RefreshPolicyCallback;
     30 
     31   class Observer {
     32    public:
     33     // Invoked when CloudPolicyService has finished initializing (any initial
     34     // policy load activity has completed and the CloudPolicyClient has
     35     // been registered, if possible).
     36     virtual void OnInitializationCompleted(CloudPolicyService* service) = 0;
     37     virtual ~Observer() {}
     38   };
     39 
     40   // |client| and |store| must remain valid for the object life time.
     41   CloudPolicyService(const PolicyNamespaceKey& policy_ns_key,
     42                      CloudPolicyClient* client,
     43                      CloudPolicyStore* store);
     44   virtual ~CloudPolicyService();
     45 
     46   // Returns the domain that manages this user/device, according to the current
     47   // policy blob. Empty if not managed/not available.
     48   std::string ManagedBy() const;
     49 
     50   // Refreshes policy. |callback| will be invoked after the operation completes
     51   // or aborts because of errors.
     52   void RefreshPolicy(const RefreshPolicyCallback& callback);
     53 
     54   // Adds/Removes an Observer for this object.
     55   void AddObserver(Observer* observer);
     56   void RemoveObserver(Observer* observer);
     57 
     58   // CloudPolicyClient::Observer:
     59   virtual void OnPolicyFetched(CloudPolicyClient* client) OVERRIDE;
     60   virtual void OnRegistrationStateChanged(CloudPolicyClient* client) OVERRIDE;
     61   virtual void OnClientError(CloudPolicyClient* client) OVERRIDE;
     62 
     63   // CloudPolicyStore::Observer:
     64   virtual void OnStoreLoaded(CloudPolicyStore* store) OVERRIDE;
     65   virtual void OnStoreError(CloudPolicyStore* store) OVERRIDE;
     66 
     67   bool IsInitializationComplete() const { return initialization_complete_; }
     68 
     69  private:
     70   // Helper function that is called when initialization may be complete, and
     71   // which is responsible for notifying observers.
     72   void CheckInitializationCompleted();
     73 
     74   // Invokes the refresh callbacks and clears refresh state. The |success| flag
     75   // is passed through to the refresh callbacks.
     76   void RefreshCompleted(bool success);
     77 
     78   // The policy namespace fetched by |client_| and expected by |store_|.
     79   PolicyNamespaceKey policy_ns_key_;
     80 
     81   // The client used to talk to the cloud.
     82   CloudPolicyClient* client_;
     83 
     84   // Takes care of persisting and decoding cloud policy.
     85   CloudPolicyStore* store_;
     86 
     87   // Tracks the state of a pending refresh operation, if any.
     88   enum {
     89     // No refresh pending.
     90     REFRESH_NONE,
     91     // Policy fetch is pending.
     92     REFRESH_POLICY_FETCH,
     93     // Policy store is pending.
     94     REFRESH_POLICY_STORE,
     95   } refresh_state_;
     96 
     97   // Callbacks to invoke upon policy refresh.
     98   std::vector<RefreshPolicyCallback> refresh_callbacks_;
     99 
    100   // Set to true once the service is initialized (initial policy load/refresh
    101   // is complete).
    102   bool initialization_complete_;
    103 
    104   // Observers who will receive notifications when the service has finished
    105   // initializing.
    106   ObserverList<Observer, true> observers_;
    107 
    108   DISALLOW_COPY_AND_ASSIGN(CloudPolicyService);
    109 };
    110 
    111 }  // namespace policy
    112 
    113 #endif  // CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_SERVICE_H_
    114