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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
// Copyright 2015 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/policy/cloud/remote_commands_invalidator.h"
#include <string>
#include "base/logging.h"
#include "components/invalidation/public/invalidation.h"
#include "components/invalidation/public/invalidation_service.h"
#include "components/invalidation/public/invalidation_util.h"
#include "components/invalidation/public/invalidator_state.h"
#include "components/invalidation/public/object_id_invalidation_map.h"
#include "components/invalidation/public/single_object_invalidation_set.h"
namespace policy {
RemoteCommandsInvalidator::RemoteCommandsInvalidator() {
}
RemoteCommandsInvalidator::~RemoteCommandsInvalidator() {
DCHECK_EQ(SHUT_DOWN, state_);
}
void RemoteCommandsInvalidator::Initialize(
invalidation::InvalidationService* invalidation_service) {
DCHECK_EQ(SHUT_DOWN, state_);
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(invalidation_service);
invalidation_service_ = invalidation_service;
state_ = STOPPED;
OnInitialize();
}
void RemoteCommandsInvalidator::Shutdown() {
DCHECK_NE(SHUT_DOWN, state_);
DCHECK(thread_checker_.CalledOnValidThread());
Stop();
state_ = SHUT_DOWN;
OnShutdown();
}
void RemoteCommandsInvalidator::Start() {
DCHECK_EQ(STOPPED, state_);
DCHECK(thread_checker_.CalledOnValidThread());
state_ = STARTED;
OnStart();
}
void RemoteCommandsInvalidator::Stop() {
DCHECK_NE(SHUT_DOWN, state_);
DCHECK(thread_checker_.CalledOnValidThread());
if (state_ == STARTED) {
Unregister();
state_ = STOPPED;
OnStop();
}
}
void RemoteCommandsInvalidator::OnInvalidatorStateChange(
syncer::InvalidatorState state) {
DCHECK_EQ(STARTED, state_);
DCHECK(thread_checker_.CalledOnValidThread());
invalidation_service_enabled_ = state == syncer::INVALIDATIONS_ENABLED;
UpdateInvalidationsEnabled();
}
void RemoteCommandsInvalidator::OnIncomingInvalidation(
const syncer::ObjectIdInvalidationMap& invalidation_map) {
DCHECK_EQ(STARTED, state_);
DCHECK(thread_checker_.CalledOnValidThread());
if (!invalidation_service_enabled_)
LOG(WARNING) << "Unexpected invalidation received.";
const syncer::SingleObjectInvalidationSet& list =
invalidation_map.ForObject(object_id_);
if (list.IsEmpty()) {
NOTREACHED();
return;
}
// Acknowledge all invalidations.
for (const auto& it : list)
it.Acknowledge();
DoRemoteCommandsFetch();
}
std::string RemoteCommandsInvalidator::GetOwnerName() const {
return "RemoteCommands";
}
void RemoteCommandsInvalidator::ReloadPolicyData(
const enterprise_management::PolicyData* policy) {
DCHECK(thread_checker_.CalledOnValidThread());
if (state_ != STARTED)
return;
// Create the ObjectId based on the policy data.
// If the policy does not specify an the ObjectId, then unregister.
if (!policy || !policy->has_command_invalidation_source() ||
!policy->has_command_invalidation_name()) {
Unregister();
return;
}
const invalidation::ObjectId object_id(policy->command_invalidation_source(),
policy->command_invalidation_name());
// If the policy object id in the policy data is different from the currently
// registered object id, update the object registration.
if (!is_registered_ || !(object_id == object_id_))
Register(object_id);
}
void RemoteCommandsInvalidator::Register(
const invalidation::ObjectId& object_id) {
// Register this handler with the invalidation service if needed.
if (!is_registered_) {
OnInvalidatorStateChange(invalidation_service_->GetInvalidatorState());
invalidation_service_->RegisterInvalidationHandler(this);
is_registered_ = true;
}
object_id_ = object_id;
UpdateInvalidationsEnabled();
// Update registration with the invalidation service.
syncer::ObjectIdSet ids;
ids.insert(object_id);
CHECK(invalidation_service_->UpdateRegisteredInvalidationIds(this, ids));
}
void RemoteCommandsInvalidator::Unregister() {
if (is_registered_) {
CHECK(invalidation_service_->UpdateRegisteredInvalidationIds(
this, syncer::ObjectIdSet()));
invalidation_service_->UnregisterInvalidationHandler(this);
is_registered_ = false;
UpdateInvalidationsEnabled();
}
}
void RemoteCommandsInvalidator::UpdateInvalidationsEnabled() {
invalidations_enabled_ = invalidation_service_enabled_ && is_registered_;
}
} // namespace policy
|