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_STORE_H_
      6 #define CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_STORE_H_
      7 
      8 #include "base/basictypes.h"
      9 #include "base/memory/scoped_ptr.h"
     10 #include "base/memory/weak_ptr.h"
     11 #include "base/observer_list.h"
     12 #include "chrome/browser/policy/cloud/cloud_policy_validator.h"
     13 #include "chrome/browser/policy/policy_map.h"
     14 #include "chrome/browser/policy/proto/cloud/device_management_backend.pb.h"
     15 
     16 namespace policy {
     17 
     18 class CloudExternalDataManager;
     19 
     20 // Defines the low-level interface used by the cloud policy code to:
     21 //   1. Validate policy blobs that should be applied locally
     22 //   2. Persist policy blobs
     23 //   3. Decode policy blobs to PolicyMap representation
     24 class CloudPolicyStore {
     25  public:
     26   // Status codes.
     27   enum Status {
     28     // Everything is in good order.
     29     STATUS_OK,
     30     // Loading policy from the underlying data store failed.
     31     STATUS_LOAD_ERROR,
     32     // Failed to store policy to the data store.
     33     STATUS_STORE_ERROR,
     34     // Failed to parse the policy read from the data store.
     35     STATUS_PARSE_ERROR,
     36     // Failed to serialize policy for storage.
     37     STATUS_SERIALIZE_ERROR,
     38     // Validation error.
     39     STATUS_VALIDATION_ERROR,
     40     // Store cannot accept policy (e.g. non-enterprise device).
     41     STATUS_BAD_STATE,
     42   };
     43 
     44   // Callbacks for policy store events. Most importantly, policy updates.
     45   class Observer {
     46    public:
     47     virtual ~Observer();
     48 
     49     // Called on changes to store->policy() and/or store->policy_map().
     50     virtual void OnStoreLoaded(CloudPolicyStore* store) = 0;
     51 
     52     // Called upon encountering errors.
     53     virtual void OnStoreError(CloudPolicyStore* store) = 0;
     54   };
     55 
     56   CloudPolicyStore();
     57   virtual ~CloudPolicyStore();
     58 
     59   // Indicates whether the store has been fully initialized. This is
     60   // accomplished by calling Load() after startup.
     61   bool is_initialized() const { return is_initialized_; }
     62 
     63   base::WeakPtr<CloudExternalDataManager> external_data_manager() const {
     64     return external_data_manager_;
     65   }
     66 
     67   const PolicyMap& policy_map() const { return policy_map_; }
     68   bool has_policy() const {
     69     return policy_.get() != NULL;
     70   }
     71   const enterprise_management::PolicyData* policy() const {
     72     return policy_.get();
     73   }
     74   bool is_managed() const {
     75     return policy_.get() &&
     76            policy_->state() == enterprise_management::PolicyData::ACTIVE;
     77   }
     78   Status status() const { return status_; }
     79   CloudPolicyValidatorBase::Status validation_status() const {
     80     return validation_status_;
     81   }
     82 
     83   // Returns true if the latest policy loaded was different from the previous
     84   // policy.
     85   bool policy_changed() const {
     86     return policy_changed_;
     87   }
     88 
     89   // Store a new policy blob. Pending load/store operations will be canceled.
     90   // The store operation may proceed asynchronously and observers are notified
     91   // once the operation finishes. If successful, OnStoreLoaded() will be invoked
     92   // on the observers and the updated policy can be read through policy().
     93   // Errors generate OnStoreError() notifications.
     94   // |invalidation_version| is the invalidation version of the policy to be
     95   // stored.
     96   void Store(
     97       const enterprise_management::PolicyFetchResponse& policy,
     98       int64 invalidation_version);
     99 
    100   virtual void Store(
    101       const enterprise_management::PolicyFetchResponse& policy) = 0;
    102 
    103   // Load the current policy blob from persistent storage. Pending load/store
    104   // operations will be canceled. This may trigger asynchronous operations.
    105   // Upon success, OnStoreLoaded() will be called on the registered observers.
    106   // Otherwise, OnStoreError() reports the reason for failure.
    107   virtual void Load() = 0;
    108 
    109   // Registers an observer to be notified when policy changes.
    110   void AddObserver(Observer* observer);
    111 
    112   // Removes the specified observer.
    113   void RemoveObserver(Observer* observer);
    114 
    115   // The invalidation version of the last policy stored. This value can be read
    116   // by observers to determine which version of the policy is now available.
    117   int64 invalidation_version() {
    118     return invalidation_version_;
    119   }
    120 
    121   // Indicate that external data referenced by policies in this store is managed
    122   // by |external_data_manager|. The |external_data_manager| will be notified
    123   // about policy changes before any other observers.
    124   void SetExternalDataManager(
    125       base::WeakPtr<CloudExternalDataManager> external_data_manager);
    126 
    127  protected:
    128   // Invokes the corresponding callback on all registered observers.
    129   void NotifyStoreLoaded();
    130   void NotifyStoreError();
    131 
    132   // Manages external data referenced by policies.
    133   base::WeakPtr<CloudExternalDataManager> external_data_manager_;
    134 
    135   // Decoded version of the currently effective policy.
    136   PolicyMap policy_map_;
    137 
    138   // Currently effective policy.
    139   scoped_ptr<enterprise_management::PolicyData> policy_;
    140 
    141   // Latest status code.
    142   Status status_;
    143 
    144   // Latest validation status.
    145   CloudPolicyValidatorBase::Status validation_status_;
    146 
    147   // The invalidation version of the last policy stored.
    148   int64 invalidation_version_;
    149 
    150  private:
    151   // Whether the store has completed asynchronous initialization, which is
    152   // triggered by calling Load().
    153   bool is_initialized_;
    154 
    155   // Whether latest policy loaded was different from the previous policy.
    156   bool policy_changed_;
    157 
    158   // The hash value of the current policy. This is used to determine when the
    159   // policy changes.
    160   uint32 hash_value_;
    161 
    162   ObserverList<Observer, true> observers_;
    163 
    164   DISALLOW_COPY_AND_ASSIGN(CloudPolicyStore);
    165 };
    166 
    167 }  // namespace policy
    168 
    169 #endif  // CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_STORE_H_
    170