device_cloud_policy_invalidator.h revision 6e8cce623b6e4fe0c9e4af605d675dd9d0338c38
1// Copyright 2014 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_CHROMEOS_POLICY_DEVICE_CLOUD_POLICY_INVALIDATOR_H_ 6#define CHROME_BROWSER_CHROMEOS_POLICY_DEVICE_CLOUD_POLICY_INVALIDATOR_H_ 7 8#include "base/basictypes.h" 9#include "base/compiler_specific.h" 10#include "base/macros.h" 11#include "base/memory/scoped_ptr.h" 12#include "base/memory/scoped_vector.h" 13#include "content/public/browser/notification_observer.h" 14#include "content/public/browser/notification_registrar.h" 15 16namespace invalidation { 17class InvalidationService; 18class TiclInvalidationService; 19} 20 21namespace policy { 22 23class CloudPolicyInvalidator; 24 25// This class provides invalidations for device policy via a 26// |CloudPolicyInvalidator| backed by an |InvalidationService|. If a user with a 27// connected invalidation service is logged in, that service is used to conserve 28// server resources. If there are no logged-in users or none of them have 29// connected invalidation services, a device-global |TiclInvalidationService| is 30// spun up. 31// The class monitors the status of all invalidation services and switches 32// between them whenever the service currently in use disconnects or the 33// device-global invalidation service can be replaced with another service that 34// just connected. 35class DeviceCloudPolicyInvalidator : public content::NotificationObserver { 36 public: 37 DeviceCloudPolicyInvalidator(); 38 virtual ~DeviceCloudPolicyInvalidator(); 39 40 // content::NotificationObserver: 41 virtual void Observe(int type, 42 const content::NotificationSource& source, 43 const content::NotificationDetails& details) OVERRIDE; 44 45 private: 46 friend class DeviceCloudPolicyInvalidatorTest; 47 48 // Helper that monitors the status of a single |InvalidationService|. 49 class InvalidationServiceObserver; 50 51 // Status updates received from |InvalidationServiceObserver|s. 52 void OnInvalidationServiceConnected( 53 invalidation::InvalidationService* invalidation_service); 54 void OnInvalidationServiceDisconnected( 55 invalidation::InvalidationService* invalidation_service); 56 57 // Attempt to create a |CloudPolicyInvalidator| backed by a connected 58 // invalidation service. If there is no connected invalidation service, a 59 // |CloudPolicyInvalidator| will be created later when a connected service 60 // becomes available. 61 // Further ensures that a device-global invalidation service is running iff 62 // there is no other connected service. 63 void TryToCreateInvalidator(); 64 65 // Create a |CloudPolicyInvalidator| backed by the |invalidation_service|. 66 void CreateInvalidator( 67 invalidation::InvalidationService* invalidation_service); 68 69 // Destroy the current |CloudPolicyInvalidator|, if any. 70 void DestroyInvalidator(); 71 72 // Destroy the device-global invalidation service, if any. 73 void DestroyDeviceInvalidationService(); 74 75 content::NotificationRegistrar registrar_; 76 77 // Device-global invalidation service. 78 scoped_ptr<invalidation::TiclInvalidationService> 79 device_invalidation_service_; 80 81 // State observer for the device-global invalidation service. 82 scoped_ptr<InvalidationServiceObserver> device_invalidation_service_observer_; 83 84 // State observers for logged-in users' invalidation services. 85 ScopedVector<InvalidationServiceObserver> 86 profile_invalidation_service_observers_; 87 88 // The invalidation service backing the current |CloudPolicyInvalidator|. NULL 89 // if no |CloudPolicyInvalidator| exists. 90 invalidation::InvalidationService* invalidation_service_; 91 92 // The highest invalidation version that was handled already. 93 int64 highest_handled_invalidation_version_; 94 95 // The current |CloudPolicyInvalidator|. NULL if no connected invalidation 96 // service is available. 97 scoped_ptr<CloudPolicyInvalidator> invalidator_; 98 99 DISALLOW_COPY_AND_ASSIGN(DeviceCloudPolicyInvalidator); 100}; 101 102} // namespace policy 103 104#endif // CHROME_BROWSER_CHROMEOS_POLICY_DEVICE_CLOUD_POLICY_INVALIDATOR_H_ 105