Chromium Code Reviews| Index: chrome/browser/policy/cloud/cloud_policy_invalidator.h |
| diff --git a/chrome/browser/policy/cloud/cloud_policy_invalidator.h b/chrome/browser/policy/cloud/cloud_policy_invalidator.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..34e550da91f4903ad0025fc984eae8a27dae4ca0 |
| --- /dev/null |
| +++ b/chrome/browser/policy/cloud/cloud_policy_invalidator.h |
| @@ -0,0 +1,186 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_INVALIDATOR_H_ |
| +#define CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_INVALIDATOR_H_ |
| + |
| +#include "base/basictypes.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "base/threading/thread_checker.h" |
| +#include "chrome/browser/policy/cloud/cloud_policy_store.h" |
| +#include "sync/notifier/invalidation_handler.h" |
| + |
| +namespace base { |
| +class SequencedTaskRunner; |
| +} |
| + |
| +namespace invalidation { |
| +class InvalidationService; |
| +} |
| + |
| +namespace policy { |
| + |
| +class CloudPolicyClient; |
| +class CloudPolicyInvalidationHandler; |
| + |
| +// Listens for and provides policy invalidations. |
| +class CloudPolicyInvalidator : public syncer::InvalidationHandler, |
| + public CloudPolicyStore::Observer { |
| + public: |
| + // The number of minutes to delay a policy refresh after receiving an |
| + // invalidation with no payload. |
| + static const int kMissingPayloadDelay; |
| + |
| + // The default, min and max values for max_fetch_delay_. |
| + static const int kMaxFetchDelayDefault; |
| + static const int kMaxFetchDelayMin; |
| + static const int kMaxFetchDelayMax; |
| + |
| + // |invalidation_service| is the invalidation service which generates the |
| + // invalidations and must remain valid during the lifetime of this object. |
| + // |client| is cloud policy client. It must remain valid during the lifetime |
| + // of this object. |
| + // |store| is cloud policy store. It must remain valid during the lifetime |
| + // of this object. |
| + // |task_runner| is used for scheduling delayed tasks. It must post tasks to |
| + // the main policy thread. |
| + // |invalidation_handler| handles invalidations provided by this object and |
| + // must remain valid during the lifetime of this object. |
| + CloudPolicyInvalidator( |
| + invalidation::InvalidationService* invalidation_service, |
| + CloudPolicyClient* client, |
| + CloudPolicyStore* store, |
| + const scoped_refptr<base::SequencedTaskRunner>& task_runner, |
| + CloudPolicyInvalidationHandler* invalidation_handler); |
| + virtual ~CloudPolicyInvalidator(); |
| + |
| + // Unregisters the policy object from the invalidation service. This method |
| + // should only be called when policy is no longer applied; it should not be |
| + // called for normal browser shutdown. This object should be destroyed |
| + // immediately after calling this method. |
| + void Unregister(); |
| + |
| + // Whether the invalidator currently has the ability to receive invalidations. |
| + bool invalidations_enabled() { |
| + return invalidations_enabled_; |
| + } |
| + |
| + // syncer::InvalidationHandler: |
| + virtual void OnInvalidatorStateChange( |
| + syncer::InvalidatorState state) OVERRIDE; |
| + virtual void OnIncomingInvalidation( |
| + const syncer::ObjectIdInvalidationMap& invalidation_map) OVERRIDE; |
| + |
| + // CloudPolicyStore::Observer: |
| + virtual void OnStoreLoaded(CloudPolicyStore* store) OVERRIDE; |
| + virtual void OnStoreError(CloudPolicyStore* store) OVERRIDE; |
| + |
| + private: |
| + // Handle an invalidation to the policy. |
| + void HandleInvalidation(const syncer::Invalidation& invalidation); |
| + |
| + // Update object registration with the invalidation service based on the |
| + // given policy data. |
| + void UpdateRegistration(const enterprise_management::PolicyData* policy); |
| + |
| + // Update |max_fetch_delay_| based on the given policy map. |
| + void UpdateMaxFetchDelay(const PolicyMap& policy_map); |
| + void set_max_fetch_delay(int delay); |
| + |
| + // Updates invalidations_enabled_ and calls the invalidation handler if the |
| + // value changed. |
| + void UpdateInvalidationsEnabled(); |
| + |
| + // Run the invalidate callback on the invalidation handler. is_missing_payload |
| + // is set to true if the callback is being invoked in response to an |
| + // invalidation with a missing payload. |
| + void RunInvalidateCallback(bool is_missing_payload); |
| + |
| + // Acknowledge the latest invalidation. |
| + void AcknowledgeInvalidation(); |
| + |
| + // Get the kMetricPolicyRefresh histogram metric which should be incremented |
| + // when a policy is stored. |
| + int GetPolicyRefreshMetric(); |
| + |
| + // The invalidation service. |
| + invalidation::InvalidationService* invalidation_service_; |
| + |
| + // The cloud policy client. |
| + CloudPolicyClient* client_; |
| + |
| + // The cloud policy store. |
| + CloudPolicyStore* store_; |
| + |
| + // Schedules delayed tasks. |
| + const scoped_refptr<base::SequencedTaskRunner> task_runner_; |
| + |
| + // The handler for invalidations provded by this object. |
| + CloudPolicyInvalidationHandler* invalidation_handler_; |
| + |
| + // Whether the invalidator currently has the ability to receive invalidations. |
| + // This is true if the invalidation service is enabled and the invalidator |
| + // has registered for a policy object. |
| + bool invalidations_enabled_; |
| + |
| + // Whether the invalidation service is currently enabled. |
| + bool invalidation_service_enabled_; |
| + |
| + // The timestamp of the PolicyData at which this object registered for policy |
| + // invalidations. Set to zero if the object has not registered yet. |
| + int64 registered_timestamp_; |
| + |
| + // The object id representing the policy in the invalidation service. |
| + invalidation::ObjectId object_id_; |
| + |
| + // Whether the policy is current invalid. This is set to true when an |
| + // invalidation is received and reset when the policy fetched due to the |
| + // invalidation is stored. |
| + bool invalid_; |
| + |
| + // The version of the latest invalidation received. This is compared to |
| + // the invalidation version of policy stored to determine when the |
| + // invalidated policy is up-to-date. |
| + int64 invalidation_version_; |
| + |
| + // The number of invalidations with unknown version received. Since such |
| + // invalidations do not provide a version number, this count is used to set |
| + // invalidation_version_ when such invalidations occur. |
| + int unknown_version_invalidation_count_; |
| + |
| + // The acknowledgment handle for the current invalidation. |
| + syncer::AckHandle ack_handle_; |
| + |
| + // WeakPtrFactory used to create callbacks to this object. |
| + base::WeakPtrFactory<CloudPolicyInvalidator> weak_factory_; |
| + |
| + // The maximum random delay, in ms, between receiving an invalidation and |
| + // fetching the new policy. |
| + int max_fetch_delay_; |
| + |
| + // A thread checker to make sure that callbacks are invoked on the correct |
| + // thread. |
| + base::ThreadChecker thread_checker_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(CloudPolicyInvalidator); |
| +}; |
| + |
| +// Handles invalidations to cloud policy objects. |
| +class CloudPolicyInvalidationHandler { |
| + public: |
|
Joao da Silva
2013/07/25 18:03:53
virtual ~CloudPolicyInvalidatorHandler() {}
Steve Condie
2013/07/26 01:39:25
Done.
|
| + // This method is called when the policy should be refreshed due to an |
| + // invalidation. A policy fetch should be scheduled in the near future. |
| + virtual void InvalidatePolicy() = 0; |
| + |
| + // This method is called when the invalidator determines that the ability to |
| + // receive policy invalidations becomes enabled or disabled. The invalidator |
| + // starts in a disabled state, so the first call to this method is always when |
| + // the invalidator becomes enabled. |
| + virtual void OnInvalidatorStateChanged(bool invalidations_enabled) = 0; |
| +}; |
| + |
| +} // namespace policy |
| + |
| +#endif // CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_INVALIDATOR_H_ |