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