OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "chrome/browser/policy/cloud/remote_commands_invalidator.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "components/invalidation/invalidation.h" |
| 11 #include "components/invalidation/invalidation_service.h" |
| 12 #include "components/invalidation/invalidation_util.h" |
| 13 #include "components/invalidation/invalidator_state.h" |
| 14 #include "components/invalidation/object_id_invalidation_map.h" |
| 15 #include "components/invalidation/single_object_invalidation_set.h" |
| 16 |
| 17 namespace policy { |
| 18 |
| 19 RemoteCommandsInvalidator::RemoteCommandsInvalidator() { |
| 20 } |
| 21 |
| 22 RemoteCommandsInvalidator::~RemoteCommandsInvalidator() { |
| 23 DCHECK_EQ(SHUT_DOWN, state_); |
| 24 } |
| 25 |
| 26 void RemoteCommandsInvalidator::Initialize( |
| 27 invalidation::InvalidationService* invalidation_service) { |
| 28 DCHECK_EQ(SHUT_DOWN, state_); |
| 29 DCHECK(thread_checker_.CalledOnValidThread()); |
| 30 |
| 31 DCHECK(invalidation_service); |
| 32 invalidation_service_ = invalidation_service; |
| 33 |
| 34 state_ = STOPPED; |
| 35 OnInitialize(); |
| 36 } |
| 37 |
| 38 void RemoteCommandsInvalidator::Shutdown() { |
| 39 DCHECK_NE(SHUT_DOWN, state_); |
| 40 DCHECK(thread_checker_.CalledOnValidThread()); |
| 41 |
| 42 Stop(); |
| 43 |
| 44 state_ = SHUT_DOWN; |
| 45 OnShutdown(); |
| 46 } |
| 47 |
| 48 void RemoteCommandsInvalidator::Start() { |
| 49 DCHECK_EQ(STOPPED, state_); |
| 50 DCHECK(thread_checker_.CalledOnValidThread()); |
| 51 state_ = STARTED; |
| 52 |
| 53 OnStart(); |
| 54 } |
| 55 |
| 56 void RemoteCommandsInvalidator::Stop() { |
| 57 DCHECK_NE(SHUT_DOWN, state_); |
| 58 DCHECK(thread_checker_.CalledOnValidThread()); |
| 59 |
| 60 if (state_ == STARTED) { |
| 61 Unregister(); |
| 62 state_ = STOPPED; |
| 63 |
| 64 OnStop(); |
| 65 } |
| 66 } |
| 67 |
| 68 void RemoteCommandsInvalidator::OnInvalidatorStateChange( |
| 69 syncer::InvalidatorState state) { |
| 70 DCHECK_EQ(STARTED, state_); |
| 71 DCHECK(thread_checker_.CalledOnValidThread()); |
| 72 |
| 73 invalidation_service_enabled_ = state == syncer::INVALIDATIONS_ENABLED; |
| 74 UpdateInvalidationsEnabled(); |
| 75 } |
| 76 |
| 77 void RemoteCommandsInvalidator::OnIncomingInvalidation( |
| 78 const syncer::ObjectIdInvalidationMap& invalidation_map) { |
| 79 DCHECK_EQ(STARTED, state_); |
| 80 DCHECK(thread_checker_.CalledOnValidThread()); |
| 81 |
| 82 if (!invalidation_service_enabled_) |
| 83 LOG(WARNING) << "Unexpected invalidation received."; |
| 84 |
| 85 const syncer::SingleObjectInvalidationSet& list = |
| 86 invalidation_map.ForObject(object_id_); |
| 87 if (list.IsEmpty()) { |
| 88 NOTREACHED(); |
| 89 return; |
| 90 } |
| 91 |
| 92 // Acknowledge all invalidations. |
| 93 for (const auto& it : list) |
| 94 it.Acknowledge(); |
| 95 |
| 96 DoRemoteCommandsFetch(); |
| 97 } |
| 98 |
| 99 std::string RemoteCommandsInvalidator::GetOwnerName() const { |
| 100 return "RemoteCommands"; |
| 101 } |
| 102 |
| 103 void RemoteCommandsInvalidator::ReloadPolicyData( |
| 104 const enterprise_management::PolicyData* policy) { |
| 105 DCHECK(thread_checker_.CalledOnValidThread()); |
| 106 |
| 107 if (state_ != STARTED) |
| 108 return; |
| 109 |
| 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()) { |
| 114 Unregister(); |
| 115 return; |
| 116 } |
| 117 const invalidation::ObjectId object_id(policy->command_invalidation_source(), |
| 118 policy->command_invalidation_name()); |
| 119 |
| 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_)) |
| 123 Register(object_id); |
| 124 } |
| 125 |
| 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; |
| 133 } |
| 134 |
| 135 object_id_ = object_id; |
| 136 UpdateInvalidationsEnabled(); |
| 137 |
| 138 // Update registration with the invalidation service. |
| 139 syncer::ObjectIdSet ids; |
| 140 ids.insert(object_id); |
| 141 invalidation_service_->UpdateRegisteredInvalidationIds(this, ids); |
| 142 } |
| 143 |
| 144 void RemoteCommandsInvalidator::Unregister() { |
| 145 if (is_registered_) { |
| 146 invalidation_service_->UpdateRegisteredInvalidationIds( |
| 147 this, syncer::ObjectIdSet()); |
| 148 invalidation_service_->UnregisterInvalidationHandler(this); |
| 149 is_registered_ = false; |
| 150 UpdateInvalidationsEnabled(); |
| 151 } |
| 152 } |
| 153 |
| 154 void RemoteCommandsInvalidator::UpdateInvalidationsEnabled() { |
| 155 invalidations_enabled_ = invalidation_service_enabled_ && is_registered_; |
| 156 } |
| 157 |
| 158 } // namespace policy |
OLD | NEW |