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 an affiliated
27// user with a connected invalidation service is logged in, that service is used
28// to conserve server resources. If there are no logged-in users matching these
29// criteria, a device-global |TiclInvalidationService| is spun up.
30// The class monitors the status of the 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 no connected invalidation service is available for
58  // use, a |CloudPolicyInvalidator| will be created later when a connected
59  // service becomes available.
60  // Further ensures that a device-global invalidation service is running iff
61  // there is no other connected service available for use.
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 highest invalidation version that was handled already.
92  int64 highest_handled_invalidation_version_;
93
94  // The current |CloudPolicyInvalidator|. NULL if no connected invalidation
95  // service is available.
96  scoped_ptr<CloudPolicyInvalidator> invalidator_;
97
98  DISALLOW_COPY_AND_ASSIGN(DeviceCloudPolicyInvalidator);
99};
100
101}  // namespace policy
102
103#endif  // CHROME_BROWSER_CHROMEOS_POLICY_DEVICE_CLOUD_POLICY_INVALIDATOR_H_
104