1 // Copyright 2015 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 #include "chrome/browser/policy/cloud/remote_commands_invalidator.h"
9 #include "base/logging.h"
10 #include "components/invalidation/public/invalidation.h"
11 #include "components/invalidation/public/invalidation_service.h"
12 #include "components/invalidation/public/invalidation_util.h"
13 #include "components/invalidation/public/invalidator_state.h"
14 #include "components/invalidation/public/object_id_invalidation_map.h"
15 #include "components/invalidation/public/single_object_invalidation_set.h"
19 RemoteCommandsInvalidator::RemoteCommandsInvalidator() {
22 RemoteCommandsInvalidator::~RemoteCommandsInvalidator() {
23 DCHECK_EQ(SHUT_DOWN
, state_
);
26 void RemoteCommandsInvalidator::Initialize(
27 invalidation::InvalidationService
* invalidation_service
) {
28 DCHECK_EQ(SHUT_DOWN
, state_
);
29 DCHECK(thread_checker_
.CalledOnValidThread());
31 DCHECK(invalidation_service
);
32 invalidation_service_
= invalidation_service
;
38 void RemoteCommandsInvalidator::Shutdown() {
39 DCHECK_NE(SHUT_DOWN
, state_
);
40 DCHECK(thread_checker_
.CalledOnValidThread());
48 void RemoteCommandsInvalidator::Start() {
49 DCHECK_EQ(STOPPED
, state_
);
50 DCHECK(thread_checker_
.CalledOnValidThread());
56 void RemoteCommandsInvalidator::Stop() {
57 DCHECK_NE(SHUT_DOWN
, state_
);
58 DCHECK(thread_checker_
.CalledOnValidThread());
60 if (state_
== STARTED
) {
68 void RemoteCommandsInvalidator::OnInvalidatorStateChange(
69 syncer::InvalidatorState state
) {
70 DCHECK_EQ(STARTED
, state_
);
71 DCHECK(thread_checker_
.CalledOnValidThread());
73 invalidation_service_enabled_
= state
== syncer::INVALIDATIONS_ENABLED
;
74 UpdateInvalidationsEnabled();
77 void RemoteCommandsInvalidator::OnIncomingInvalidation(
78 const syncer::ObjectIdInvalidationMap
& invalidation_map
) {
79 DCHECK_EQ(STARTED
, state_
);
80 DCHECK(thread_checker_
.CalledOnValidThread());
82 if (!invalidation_service_enabled_
)
83 LOG(WARNING
) << "Unexpected invalidation received.";
85 const syncer::SingleObjectInvalidationSet
& list
=
86 invalidation_map
.ForObject(object_id_
);
92 // Acknowledge all invalidations.
93 for (const auto& it
: list
)
96 DoRemoteCommandsFetch();
99 std::string
RemoteCommandsInvalidator::GetOwnerName() const {
100 return "RemoteCommands";
103 void RemoteCommandsInvalidator::ReloadPolicyData(
104 const enterprise_management::PolicyData
* policy
) {
105 DCHECK(thread_checker_
.CalledOnValidThread());
107 if (state_
!= STARTED
)
110 // Create the ObjectId based on the policy data.
111 // If the policy does not specify an the ObjectId, then unregister.
112 if (!policy
|| !policy
->has_command_invalidation_source() ||
113 !policy
->has_command_invalidation_name()) {
117 const invalidation::ObjectId
object_id(policy
->command_invalidation_source(),
118 policy
->command_invalidation_name());
120 // If the policy object id in the policy data is different from the currently
121 // registered object id, update the object registration.
122 if (!is_registered_
|| !(object_id
== object_id_
))
126 void RemoteCommandsInvalidator::Register(
127 const invalidation::ObjectId
& object_id
) {
128 // Register this handler with the invalidation service if needed.
129 if (!is_registered_
) {
130 OnInvalidatorStateChange(invalidation_service_
->GetInvalidatorState());
131 invalidation_service_
->RegisterInvalidationHandler(this);
132 is_registered_
= true;
135 object_id_
= object_id
;
136 UpdateInvalidationsEnabled();
138 // Update registration with the invalidation service.
139 syncer::ObjectIdSet ids
;
140 ids
.insert(object_id
);
141 CHECK(invalidation_service_
->UpdateRegisteredInvalidationIds(this, ids
));
144 void RemoteCommandsInvalidator::Unregister() {
145 if (is_registered_
) {
146 CHECK(invalidation_service_
->UpdateRegisteredInvalidationIds(
147 this, syncer::ObjectIdSet()));
148 invalidation_service_
->UnregisterInvalidationHandler(this);
149 is_registered_
= false;
150 UpdateInvalidationsEnabled();
154 void RemoteCommandsInvalidator::UpdateInvalidationsEnabled() {
155 invalidations_enabled_
= invalidation_service_enabled_
&& is_registered_
;
158 } // namespace policy