Update To 11.40.268.0
[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/compiler_specific.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/threading/thread_checker.h"
17 #include "components/invalidation/invalidation.h"
18 #include "components/invalidation/invalidation_handler.h"
19 #include "components/policy/core/common/cloud/cloud_policy_core.h"
20 #include "components/policy/core/common/cloud/cloud_policy_store.h"
21 #include "google/cacheinvalidation/include/types.h"
22 #include "policy/proto/device_management_backend.pb.h"
23
24 namespace base {
25 class Clock;
26 class SequencedTaskRunner;
27 }
28
29 namespace invalidation {
30 class InvalidationService;
31 }
32
33 namespace policy {
34
35 // Listens for and provides policy invalidations.
36 class CloudPolicyInvalidator : public syncer::InvalidationHandler,
37                                public CloudPolicyCore::Observer,
38                                public CloudPolicyStore::Observer {
39  public:
40   // The number of minutes to delay a policy refresh after receiving an
41   // invalidation with no payload.
42   static const int kMissingPayloadDelay;
43
44   // The default, min and max values for max_fetch_delay_.
45   static const int kMaxFetchDelayDefault;
46   static const int kMaxFetchDelayMin;
47   static const int kMaxFetchDelayMax;
48
49   // The grace period, in seconds, to allow for invalidations to be received
50   // once the invalidation service starts up.
51   static const int kInvalidationGracePeriod;
52
53   // Time, in seconds, for which unknown version invalidations are ignored after
54   // fetching a policy.
55   static const int kUnknownVersionIgnorePeriod;
56
57   // The max tolerated discrepancy, in seconds, between policy timestamps and
58   // invalidation timestamps when determining if an invalidation is expired.
59   static const int kMaxInvalidationTimeDelta;
60
61   // |type| indicates the policy type that this invalidator is responsible for.
62   // |core| is the cloud policy core which connects the various policy objects.
63   // It must remain valid until Shutdown is called.
64   // |task_runner| is used for scheduling delayed tasks. It must post tasks to
65   // the main policy thread.
66   // |clock| is used to get the current time.
67   // |highest_handled_invalidation_version| is the highest invalidation version
68   // that was handled already before this invalidator was created.
69   CloudPolicyInvalidator(
70       enterprise_management::DeviceRegisterRequest::Type type,
71       CloudPolicyCore* core,
72       const scoped_refptr<base::SequencedTaskRunner>& task_runner,
73       scoped_ptr<base::Clock> clock,
74       int64 highest_handled_invalidation_version);
75   ~CloudPolicyInvalidator() override;
76
77   // Initializes the invalidator. No invalidations will be generated before this
78   // method is called. This method must only be called once.
79   // |invalidation_service| is the invalidation service to use and must remain
80   // valid until Shutdown is called.
81   void Initialize(invalidation::InvalidationService* invalidation_service);
82
83   // Shuts down and disables invalidations. It must be called before the object
84   // is destroyed.
85   void Shutdown();
86
87   // Whether the invalidator currently has the ability to receive invalidations.
88   bool invalidations_enabled() {
89     return invalidations_enabled_;
90   }
91
92   // The highest invalidation version that was handled already.
93   int64 highest_handled_invalidation_version() const {
94     return highest_handled_invalidation_version_;
95   }
96
97   // syncer::InvalidationHandler:
98   void OnInvalidatorStateChange(syncer::InvalidatorState state) override;
99   void OnIncomingInvalidation(
100       const syncer::ObjectIdInvalidationMap& invalidation_map) override;
101   std::string GetOwnerName() const override;
102
103   // CloudPolicyCore::Observer:
104   void OnCoreConnected(CloudPolicyCore* core) override;
105   void OnRefreshSchedulerStarted(CloudPolicyCore* core) override;
106   void OnCoreDisconnecting(CloudPolicyCore* core) override;
107
108   // CloudPolicyStore::Observer:
109   void OnStoreLoaded(CloudPolicyStore* store) override;
110   void OnStoreError(CloudPolicyStore* store) override;
111
112  private:
113   // Handle an invalidation to the policy.
114   void HandleInvalidation(const syncer::Invalidation& invalidation);
115
116   // Update object registration with the invalidation service based on the
117   // given policy data.
118   void UpdateRegistration(const enterprise_management::PolicyData* policy);
119
120   // Registers the given object with the invalidation service.
121   void Register(const invalidation::ObjectId& object_id);
122
123   // Unregisters the current object with the invalidation service.
124   void Unregister();
125
126   // Update |max_fetch_delay_| based on the given policy map.
127   void UpdateMaxFetchDelay(const PolicyMap& policy_map);
128   void set_max_fetch_delay(int delay);
129
130   // Updates invalidations_enabled_ and calls the invalidation handler if the
131   // value changed.
132   void UpdateInvalidationsEnabled();
133
134   // Refresh the policy.
135   // |is_missing_payload| is set to true if the callback is being invoked in
136   // response to an invalidation with a missing payload.
137   void RefreshPolicy(bool is_missing_payload);
138
139   // Acknowledge the latest invalidation.
140   void AcknowledgeInvalidation();
141
142   // Determines if the given policy is different from the policy passed in the
143   // previous call.
144   bool IsPolicyChanged(const enterprise_management::PolicyData* policy);
145
146   // Determine if an invalidation has expired.
147   // |version| is the version of the invalidation, or zero for unknown.
148   bool IsInvalidationExpired(int64 version);
149
150   // Get the kMetricPolicyRefresh histogram metric which should be incremented
151   // when a policy is stored.
152   int GetPolicyRefreshMetric(bool policy_changed);
153
154   // Get the kMetricPolicyInvalidations histogram metric which should be
155   // incremented when an invalidation is received.
156   int GetInvalidationMetric(bool is_missing_payload, bool is_expired);
157
158   // Determine if invalidations have been enabled longer than the grace period.
159   bool GetInvalidationsEnabled();
160
161   // The state of the object.
162   enum State {
163     UNINITIALIZED,
164     STOPPED,
165     STARTED,
166     SHUT_DOWN
167   };
168   State state_;
169
170   // The policy type this invalidator is responsible for.
171   const enterprise_management::DeviceRegisterRequest::Type type_;
172
173   // The cloud policy core.
174   CloudPolicyCore* core_;
175
176   // Schedules delayed tasks.
177   const scoped_refptr<base::SequencedTaskRunner> task_runner_;
178
179   // The clock.
180   scoped_ptr<base::Clock> clock_;
181
182   // The invalidation service.
183   invalidation::InvalidationService* invalidation_service_;
184
185   // Whether the invalidator currently has the ability to receive invalidations.
186   // This is true if the invalidation service is enabled and the invalidator
187   // has registered for a policy object.
188   bool invalidations_enabled_;
189
190   // The time that invalidations became enabled.
191   base::Time invalidations_enabled_time_;
192
193   // Whether the invalidation service is currently enabled.
194   bool invalidation_service_enabled_;
195
196   // Whether this object has registered for policy invalidations.
197   bool is_registered_;
198
199   // The object id representing the policy in the invalidation service.
200   invalidation::ObjectId object_id_;
201
202   // Whether the policy is current invalid. This is set to true when an
203   // invalidation is received and reset when the policy fetched due to the
204   // invalidation is stored.
205   bool invalid_;
206
207   // The version of the latest invalidation received. This is compared to
208   // the invalidation version of policy stored to determine when the
209   // invalidated policy is up-to-date.
210   int64 invalidation_version_;
211
212   // The number of invalidations with unknown version received. Since such
213   // invalidations do not provide a version number, this count is used to set
214   // invalidation_version_ when such invalidations occur.
215   int unknown_version_invalidation_count_;
216
217   // The highest invalidation version that was handled already.
218   int64 highest_handled_invalidation_version_;
219
220   // The most up to date invalidation.
221   scoped_ptr<syncer::Invalidation> invalidation_;
222
223   // The maximum random delay, in ms, between receiving an invalidation and
224   // fetching the new policy.
225   int max_fetch_delay_;
226
227   // The hash value of the current policy. This is used to determine if a new
228   // policy is different from the current one.
229   uint32 policy_hash_value_;
230
231   // A thread checker to make sure that callbacks are invoked on the correct
232   // thread.
233   base::ThreadChecker thread_checker_;
234
235   // WeakPtrFactory used to create callbacks to this object.
236   base::WeakPtrFactory<CloudPolicyInvalidator> weak_factory_;
237
238   DISALLOW_COPY_AND_ASSIGN(CloudPolicyInvalidator);
239 };
240
241 }  // namespace policy
242
243 #endif  // CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_INVALIDATOR_H_