- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / policy / cloud / cloud_policy_store.h
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   // 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
89   // stored.
90   void Store(
91       const enterprise_management::PolicyFetchResponse& policy,
92       int64 invalidation_version);
93
94   virtual void Store(
95       const enterprise_management::PolicyFetchResponse& policy) = 0;
96
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;
102
103   // Registers an observer to be notified when policy changes.
104   void AddObserver(Observer* observer);
105
106   // Removes the specified observer.
107   void RemoveObserver(Observer* observer);
108
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_;
113   }
114
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);
120
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);
128
129  protected:
130   // Invokes the corresponding callback on all registered observers.
131   void NotifyStoreLoaded();
132   void NotifyStoreError();
133
134   // Manages external data referenced by policies.
135   base::WeakPtr<CloudExternalDataManager> external_data_manager_;
136
137   // Decoded version of the currently effective policy.
138   PolicyMap policy_map_;
139
140   // Currently effective policy.
141   scoped_ptr<enterprise_management::PolicyData> policy_;
142
143   // Latest status code.
144   Status status_;
145
146   // Latest validation status.
147   CloudPolicyValidatorBase::Status validation_status_;
148
149   // The invalidation version of the last policy stored.
150   int64 invalidation_version_;
151
152  private:
153   // Whether the store has completed asynchronous initialization, which is
154   // triggered by calling Load().
155   bool is_initialized_;
156
157   ObserverList<Observer, true> observers_;
158
159   DISALLOW_COPY_AND_ASSIGN(CloudPolicyStore);
160 };
161
162 }  // namespace policy
163
164 #endif  // CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_STORE_H_