summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/policy/affiliated_cloud_policy_invalidator.cc
blob: 952cab4c1e8de972429db4c4318acc9c79f95a77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright 2014 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.

#include "chrome/browser/chromeos/policy/affiliated_cloud_policy_invalidator.h"

#include "base/logging.h"
#include "base/thread_task_runner_handle.h"
#include "base/time/clock.h"
#include "base/time/default_clock.h"
#include "chrome/browser/policy/cloud/cloud_policy_invalidator.h"

namespace policy {

AffiliatedCloudPolicyInvalidator::AffiliatedCloudPolicyInvalidator(
    enterprise_management::DeviceRegisterRequest::Type type,
    CloudPolicyCore* core,
    AffiliatedInvalidationServiceProvider* invalidation_service_provider)
    : type_(type),
      core_(core),
      invalidation_service_provider_(invalidation_service_provider),
      highest_handled_invalidation_version_(0) {
  invalidation_service_provider_->RegisterConsumer(this);
}

AffiliatedCloudPolicyInvalidator::~AffiliatedCloudPolicyInvalidator() {
  DestroyInvalidator();
  invalidation_service_provider_->UnregisterConsumer(this);
}

void AffiliatedCloudPolicyInvalidator::OnInvalidationServiceSet(
    invalidation::InvalidationService* invalidation_service) {
  DestroyInvalidator();
  if (invalidation_service)
    CreateInvalidator(invalidation_service);
}

CloudPolicyInvalidator*
AffiliatedCloudPolicyInvalidator::GetInvalidatorForTest() const {
  return invalidator_.get();
}

void AffiliatedCloudPolicyInvalidator::CreateInvalidator(
    invalidation::InvalidationService* invalidation_service) {
  DCHECK(!invalidator_);
  invalidator_.reset(new CloudPolicyInvalidator(
      type_,
      core_,
      base::ThreadTaskRunnerHandle::Get(),
      scoped_ptr<base::Clock>(new base::DefaultClock()),
      highest_handled_invalidation_version_));
  invalidator_->Initialize(invalidation_service);
}

void AffiliatedCloudPolicyInvalidator::DestroyInvalidator() {
  if (!invalidator_)
    return;

  highest_handled_invalidation_version_ =
      invalidator_->highest_handled_invalidation_version();
  invalidator_->Shutdown();
  invalidator_.reset();
}

}  // namespace policy