- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / policy / cloud / cloud_policy_invalidator.h
1 // Copyright 2013 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_INVALIDATOR_H_
6 #define CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_INVALIDATOR_H_
7
8 #include <string>
9
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/threading/thread_checker.h"
15 #include "chrome/browser/policy/cloud/cloud_policy_core.h"
16 #include "chrome/browser/policy/cloud/cloud_policy_store.h"
17 #include "sync/notifier/invalidation_handler.h"
18
19 namespace base {
20 class SequencedTaskRunner;
21 }
22
23 namespace invalidation {
24 class InvalidationService;
25 }
26
27 namespace policy {
28
29 // Listens for and provides policy invalidations.
30 class CloudPolicyInvalidator : public syncer::InvalidationHandler,
31                                public CloudPolicyCore::Observer,
32                                public CloudPolicyStore::Observer {
33  public:
34   // The number of minutes to delay a policy refresh after receiving an
35   // invalidation with no payload.
36   static const int kMissingPayloadDelay;
37
38   // The default, min and max values for max_fetch_delay_.
39   static const int kMaxFetchDelayDefault;
40   static const int kMaxFetchDelayMin;
41   static const int kMaxFetchDelayMax;
42
43   // |core| is the cloud policy core which connects the various policy objects.
44   // It must remain valid until Shutdown is called.
45   // |task_runner| is used for scheduling delayed tasks. It must post tasks to
46   // the main policy thread.
47   CloudPolicyInvalidator(
48       CloudPolicyCore* core,
49       const scoped_refptr<base::SequencedTaskRunner>& task_runner);
50   virtual ~CloudPolicyInvalidator();
51
52   // Initializes the invalidator. No invalidations will be generated before this
53   // method is called. This method must only be called once.
54   // |invalidation_service| is the invalidation service to use and must remain
55   // valid until Shutdown is called.
56   void Initialize(invalidation::InvalidationService* invalidation_service);
57
58   // Shuts down and disables invalidations. It must be called before the object
59   // is destroyed.
60   void Shutdown();
61
62   // Whether the invalidator currently has the ability to receive invalidations.
63   bool invalidations_enabled() {
64     return invalidations_enabled_;
65   }
66
67   // syncer::InvalidationHandler:
68   virtual void OnInvalidatorStateChange(
69       syncer::InvalidatorState state) OVERRIDE;
70   virtual void OnIncomingInvalidation(
71       const syncer::ObjectIdInvalidationMap& invalidation_map) OVERRIDE;
72
73   // CloudPolicyCore::Observer:
74   virtual void OnCoreConnected(CloudPolicyCore* core) OVERRIDE;
75   virtual void OnRefreshSchedulerStarted(CloudPolicyCore* core) OVERRIDE;
76   virtual void OnCoreDisconnecting(CloudPolicyCore* core) OVERRIDE;
77
78   // CloudPolicyStore::Observer:
79   virtual void OnStoreLoaded(CloudPolicyStore* store) OVERRIDE;
80   virtual void OnStoreError(CloudPolicyStore* store) OVERRIDE;
81
82  private:
83   // Handle an invalidation to the policy.
84   void HandleInvalidation(const syncer::Invalidation& invalidation);
85
86   // Update object registration with the invalidation service based on the
87   // given policy data.
88   void UpdateRegistration(const enterprise_management::PolicyData* policy);
89
90   // Registers the given object with the invalidation service.
91   void Register(int64 timestamp, const invalidation::ObjectId& object_id);
92
93   // Unregisters the current object with the invalidation service.
94   void Unregister();
95
96   // Update |max_fetch_delay_| based on the given policy map.
97   void UpdateMaxFetchDelay(const PolicyMap& policy_map);
98   void set_max_fetch_delay(int delay);
99
100   // Updates invalidations_enabled_ and calls the invalidation handler if the
101   // value changed.
102   void UpdateInvalidationsEnabled();
103
104   // Refresh the policy.
105   // |is_missing_payload| is set to true if the callback is being invoked in
106   // response to an invalidation with a missing payload.
107   void RefreshPolicy(bool is_missing_payload);
108
109   // Acknowledge the latest invalidation.
110   void AcknowledgeInvalidation();
111
112   // Determines if the given policy is different from the policy passed in the
113   // previous call.
114   bool IsPolicyChanged(const enterprise_management::PolicyData* policy);
115
116   // Get the kMetricPolicyRefresh histogram metric which should be incremented
117   // when a policy is stored.
118   int GetPolicyRefreshMetric(bool policy_changed);
119
120   // The state of the object.
121   enum State {
122     UNINITIALIZED,
123     STOPPED,
124     STARTED,
125     SHUT_DOWN
126   };
127   State state_;
128
129   // The cloud policy core.
130   CloudPolicyCore* core_;
131
132   // Schedules delayed tasks.
133   const scoped_refptr<base::SequencedTaskRunner> task_runner_;
134
135   // The invalidation service.
136   invalidation::InvalidationService* invalidation_service_;
137
138   // Whether the invalidator currently has the ability to receive invalidations.
139   // This is true if the invalidation service is enabled and the invalidator
140   // has registered for a policy object.
141   bool invalidations_enabled_;
142
143   // Whether the invalidation service is currently enabled.
144   bool invalidation_service_enabled_;
145
146   // The timestamp of the PolicyData at which this object registered for policy
147   // invalidations. Set to zero if the object has not registered yet.
148   int64 registered_timestamp_;
149
150   // The object id representing the policy in the invalidation service.
151   invalidation::ObjectId object_id_;
152
153   // Whether the policy is current invalid. This is set to true when an
154   // invalidation is received and reset when the policy fetched due to the
155   // invalidation is stored.
156   bool invalid_;
157
158   // The version of the latest invalidation received. This is compared to
159   // the invalidation version of policy stored to determine when the
160   // invalidated policy is up-to-date.
161   int64 invalidation_version_;
162
163   // The number of invalidations with unknown version received. Since such
164   // invalidations do not provide a version number, this count is used to set
165   // invalidation_version_ when such invalidations occur.
166   int unknown_version_invalidation_count_;
167
168   // The acknowledgment handle for the current invalidation.
169   syncer::AckHandle ack_handle_;
170
171   // WeakPtrFactory used to create callbacks to this object.
172   base::WeakPtrFactory<CloudPolicyInvalidator> weak_factory_;
173
174   // The maximum random delay, in ms, between receiving an invalidation and
175   // fetching the new policy.
176   int max_fetch_delay_;
177
178   // The hash value of the current policy. This is used to determine if a new
179   // policy is different from the current one.
180   uint32 policy_hash_value_;
181
182   // A thread checker to make sure that callbacks are invoked on the correct
183   // thread.
184   base::ThreadChecker thread_checker_;
185
186   DISALLOW_COPY_AND_ASSIGN(CloudPolicyInvalidator);
187 };
188
189 }  // namespace policy
190
191 #endif  // CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_INVALIDATOR_H_