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.
5 #ifndef CHROME_BROWSER_CHROMEOS_POLICY_DEVICE_CLOUD_POLICY_INVALIDATOR_H_
6 #define CHROME_BROWSER_CHROMEOS_POLICY_DEVICE_CLOUD_POLICY_INVALIDATOR_H_
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"
16 namespace invalidation {
17 class InvalidationService;
18 class TiclInvalidationService;
23 class CloudPolicyInvalidator;
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
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
35 class DeviceCloudPolicyInvalidator : public content::NotificationObserver {
37 DeviceCloudPolicyInvalidator();
38 virtual ~DeviceCloudPolicyInvalidator();
40 // content::NotificationObserver:
41 virtual void Observe(int type,
42 const content::NotificationSource& source,
43 const content::NotificationDetails& details) OVERRIDE;
46 friend class DeviceCloudPolicyInvalidatorTest;
48 // Helper that monitors the status of a single |InvalidationService|.
49 class InvalidationServiceObserver;
51 // Status updates received from |InvalidationServiceObserver|s.
52 void OnInvalidationServiceConnected(
53 invalidation::InvalidationService* invalidation_service);
54 void OnInvalidationServiceDisconnected(
55 invalidation::InvalidationService* invalidation_service);
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
61 // Further ensures that a device-global invalidation service is running iff
62 // there is no other connected service.
63 void TryToCreateInvalidator();
65 // Create a |CloudPolicyInvalidator| backed by the |invalidation_service|.
66 void CreateInvalidator(
67 invalidation::InvalidationService* invalidation_service);
69 // Destroy the current |CloudPolicyInvalidator|, if any.
70 void DestroyInvalidator();
72 // Destroy the device-global invalidation service, if any.
73 void DestroyDeviceInvalidationService();
75 content::NotificationRegistrar registrar_;
77 // Device-global invalidation service.
78 scoped_ptr<invalidation::TiclInvalidationService>
79 device_invalidation_service_;
81 // State observer for the device-global invalidation service.
82 scoped_ptr<InvalidationServiceObserver> device_invalidation_service_observer_;
84 // State observers for logged-in users' invalidation services.
85 ScopedVector<InvalidationServiceObserver>
86 profile_invalidation_service_observers_;
88 // The invalidation service backing the current |CloudPolicyInvalidator|. NULL
89 // if no |CloudPolicyInvalidator| exists.
90 invalidation::InvalidationService* invalidation_service_;
92 // The highest invalidation version that was handled already.
93 int64 highest_handled_invalidation_version_;
95 // The current |CloudPolicyInvalidator|. NULL if no connected invalidation
96 // service is available.
97 scoped_ptr<CloudPolicyInvalidator> invalidator_;
99 DISALLOW_COPY_AND_ASSIGN(DeviceCloudPolicyInvalidator);
102 } // namespace policy
104 #endif // CHROME_BROWSER_CHROMEOS_POLICY_DEVICE_CLOUD_POLICY_INVALIDATOR_H_