| 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..88b826135f80038528d6677b4a3457c5939eed8c
|
| --- /dev/null
|
| +++ b/chrome/browser/policy/cloud/cloud_policy_invalidator.h
|
| @@ -0,0 +1,213 @@
|
| +// 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 <string>
|
| +
|
| +#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 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 until Shutdown is called.
|
| + // |store| is cloud policy store. It must remain valid until Shutdown is
|
| + // called.
|
| + // |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 until Shutdown is called.
|
| + CloudPolicyInvalidator(
|
| + invalidation::InvalidationService* invalidation_service,
|
| + CloudPolicyStore* store,
|
| + const scoped_refptr<base::SequencedTaskRunner>& task_runner,
|
| + CloudPolicyInvalidationHandler* invalidation_handler);
|
| +
|
| + // Shuts down and disables invalidations. It must be called before the object
|
| + // is destroyed.
|
| + void Shutdown();
|
| +
|
| + // 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);
|
| +
|
| + // Unregisters the current object with the invalidation service.
|
| + void Unregister();
|
| +
|
| + // 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 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:
|
| + // This method is called when the current invalidation info should be set
|
| + // on the cloud policy client.
|
| + virtual void SetInvalidationInfo(
|
| + int64 version,
|
| + const std::string& payload) = 0;
|
| +
|
| + // 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;
|
| +};
|
| +
|
| +// Caches the latest invalidation received and replays it to a
|
| +// CloudPolicyInvalidationHandler. The intent is for this class to be used by
|
| +// CloudPolicyInvalidationHandler objects that may not be ready to respond
|
| +// to invalidations when the policy invalidator starts. The object can pass
|
| +// through the calls to an instance of this class. When the object becomes ready
|
| +// to handle invalidations, it calls Replay to handle the latest invalidation.
|
| +class CloudPolicyInvalidationReplayer {
|
| + public:
|
| + CloudPolicyInvalidationReplayer();
|
| +
|
| + // Methods corresponding to CloudPolicyInvalidationHandler methods.
|
| + void SetInvalidationInfo(int64 version, const std::string& payload);
|
| + void InvalidatePolicy();
|
| + void OnInvalidatorStateChanged(bool invalidations_enabled);
|
| +
|
| + // Replays the latest invalidation (if any) on the given
|
| + // CloudPolicyInvalidationHandler. This method must only be called once.
|
| + void Replay(CloudPolicyInvalidationHandler* handler);
|
| +
|
| + private:
|
| + int64 invalidation_version_;
|
| + std::string invalidation_payload_;
|
| + bool invalidate_policy_called_;
|
| + bool invalidations_enabled_;
|
| +};
|
| +
|
| +} // namespace policy
|
| +
|
| +#endif // CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_INVALIDATOR_H_
|
|
|