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.
5 #ifndef CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_STORE_H_
6 #define CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_STORE_H_
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"
18 class CloudExternalDataManager;
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 {
28 // Everything is in good order.
30 // Loading policy from the underlying data store failed.
32 // Failed to store policy to the data store.
34 // Failed to parse the policy read from the data store.
36 // Failed to serialize policy for storage.
37 STATUS_SERIALIZE_ERROR,
39 STATUS_VALIDATION_ERROR,
40 // Store cannot accept policy (e.g. non-enterprise device).
44 // Callbacks for policy store events. Most importantly, policy updates.
49 // Called on changes to store->policy() and/or store->policy_map().
50 virtual void OnStoreLoaded(CloudPolicyStore* store) = 0;
52 // Called upon encountering errors.
53 virtual void OnStoreError(CloudPolicyStore* store) = 0;
57 virtual ~CloudPolicyStore();
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_; }
63 base::WeakPtr<CloudExternalDataManager> external_data_manager() const {
64 return external_data_manager_;
67 const PolicyMap& policy_map() const { return policy_map_; }
68 bool has_policy() const {
69 return policy_.get() != NULL;
71 const enterprise_management::PolicyData* policy() const {
74 bool is_managed() const {
75 return policy_.get() &&
76 policy_->state() == enterprise_management::PolicyData::ACTIVE;
78 Status status() const { return status_; }
79 CloudPolicyValidatorBase::Status validation_status() const {
80 return validation_status_;
83 // Store a new policy blob. Pending load/store operations will be canceled.
84 // The store operation may proceed asynchronously and observers are notified
85 // once the operation finishes. If successful, OnStoreLoaded() will be invoked
86 // on the observers and the updated policy can be read through policy().
87 // Errors generate OnStoreError() notifications.
88 // |invalidation_version| is the invalidation version of the policy to be
91 const enterprise_management::PolicyFetchResponse& policy,
92 int64 invalidation_version);
95 const enterprise_management::PolicyFetchResponse& policy) = 0;
97 // Load the current policy blob from persistent storage. Pending load/store
98 // operations will be canceled. This may trigger asynchronous operations.
99 // Upon success, OnStoreLoaded() will be called on the registered observers.
100 // Otherwise, OnStoreError() reports the reason for failure.
101 virtual void Load() = 0;
103 // Registers an observer to be notified when policy changes.
104 void AddObserver(Observer* observer);
106 // Removes the specified observer.
107 void RemoveObserver(Observer* observer);
109 // The invalidation version of the last policy stored. This value can be read
110 // by observers to determine which version of the policy is now available.
111 int64 invalidation_version() {
112 return invalidation_version_;
115 // Indicate that external data referenced by policies in this store is managed
116 // by |external_data_manager|. The |external_data_manager| will be notified
117 // about policy changes before any other observers.
118 void SetExternalDataManager(
119 base::WeakPtr<CloudExternalDataManager> external_data_manager);
121 // Replaces |policy_map_| and calls the registered observers, simulating a
122 // successful load of |policy_map| from persistent storage.
123 // TODO(bartfab): This override is only needed because there are no policies
124 // that reference external data and therefore, no ExternalDataFetchers in the
125 // |policy_map_|. Once the first such policy is added, use that policy in
126 // tests and remove the override.
127 void SetPolicyMapForTesting(const PolicyMap& policy_map);
130 // Invokes the corresponding callback on all registered observers.
131 void NotifyStoreLoaded();
132 void NotifyStoreError();
134 // Manages external data referenced by policies.
135 base::WeakPtr<CloudExternalDataManager> external_data_manager_;
137 // Decoded version of the currently effective policy.
138 PolicyMap policy_map_;
140 // Currently effective policy.
141 scoped_ptr<enterprise_management::PolicyData> policy_;
143 // Latest status code.
146 // Latest validation status.
147 CloudPolicyValidatorBase::Status validation_status_;
149 // The invalidation version of the last policy stored.
150 int64 invalidation_version_;
153 // Whether the store has completed asynchronous initialization, which is
154 // triggered by calling Load().
155 bool is_initialized_;
157 ObserverList<Observer, true> observers_;
159 DISALLOW_COPY_AND_ASSIGN(CloudPolicyStore);
162 } // namespace policy
164 #endif // CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_STORE_H_