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