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/bind.h" |
| 10 #include "base/location.h" |
| 11 #include "base/logging.h" |
| 12 #include "base/sequenced_task_runner.h" |
| 13 #include "components/invalidation/invalidation.h" |
| 14 #include "components/invalidation/invalidation_service.h" |
| 15 #include "components/invalidation/invalidation_util.h" |
| 16 #include "components/invalidation/invalidator_state.h" |
| 17 #include "components/invalidation/object_id_invalidation_map.h" |
| 18 #include "components/invalidation/single_object_invalidation_set.h" |
| 19 |
| 20 namespace policy { |
| 21 |
| 22 RemoteCommandsInvalidator::RemoteCommandsInvalidator( |
| 23 const scoped_refptr<base::SequencedTaskRunner>& task_runner) |
| 24 : task_runner_(task_runner), weak_factory_(this) { |
| 25 DCHECK(task_runner); |
| 26 } |
| 27 |
| 28 RemoteCommandsInvalidator::~RemoteCommandsInvalidator() { |
| 29 DCHECK_EQ(SHUT_DOWN, state_); |
| 30 } |
| 31 |
| 32 void RemoteCommandsInvalidator::Initialize( |
| 33 invalidation::InvalidationService* invalidation_service) { |
| 34 DCHECK_EQ(SHUT_DOWN, state_); |
| 35 DCHECK(thread_checker_.CalledOnValidThread()); |
| 36 |
| 37 DCHECK(invalidation_service); |
| 38 invalidation_service_ = invalidation_service; |
| 39 |
| 40 state_ = STOPPED; |
| 41 OnInitialize(); |
| 42 } |
| 43 |
| 44 void RemoteCommandsInvalidator::Shutdown() { |
| 45 DCHECK_NE(SHUT_DOWN, state_); |
| 46 DCHECK(thread_checker_.CalledOnValidThread()); |
| 47 |
| 48 Stop(); |
| 49 |
| 50 state_ = SHUT_DOWN; |
| 51 weak_factory_.InvalidateWeakPtrs(); |
| 52 OnShutdown(); |
| 53 } |
| 54 |
| 55 void RemoteCommandsInvalidator::Start() { |
| 56 DCHECK_EQ(STOPPED, state_); |
| 57 DCHECK(thread_checker_.CalledOnValidThread()); |
| 58 state_ = STARTED; |
| 59 |
| 60 OnStart(); |
| 61 } |
| 62 |
| 63 void RemoteCommandsInvalidator::Stop() { |
| 64 DCHECK_NE(SHUT_DOWN, state_); |
| 65 DCHECK(thread_checker_.CalledOnValidThread()); |
| 66 |
| 67 if (state_ == STARTED) { |
| 68 Unregister(); |
| 69 state_ = STOPPED; |
| 70 |
| 71 OnStop(); |
| 72 } |
| 73 } |
| 74 |
| 75 void RemoteCommandsInvalidator::OnInvalidatorStateChange( |
| 76 syncer::InvalidatorState state) { |
| 77 DCHECK_EQ(STARTED, state_); |
| 78 DCHECK(thread_checker_.CalledOnValidThread()); |
| 79 |
| 80 invalidation_service_enabled_ = state == syncer::INVALIDATIONS_ENABLED; |
| 81 UpdateInvalidationsEnabled(); |
| 82 } |
| 83 |
| 84 void RemoteCommandsInvalidator::OnIncomingInvalidation( |
| 85 const syncer::ObjectIdInvalidationMap& invalidation_map) { |
| 86 DCHECK_EQ(STARTED, state_); |
| 87 DCHECK(thread_checker_.CalledOnValidThread()); |
| 88 |
| 89 if (!invalidation_service_enabled_) |
| 90 LOG(WARNING) << "Unexpected invalidation received."; |
| 91 |
| 92 const syncer::SingleObjectInvalidationSet& list = |
| 93 invalidation_map.ForObject(object_id_); |
| 94 if (list.IsEmpty()) { |
| 95 NOTREACHED(); |
| 96 return; |
| 97 } |
| 98 |
| 99 // Acknowledge all invalidations. |
| 100 for (const auto& it : list) |
| 101 it.Acknowledge(); |
| 102 |
| 103 task_runner_->PostTask( |
| 104 FROM_HERE, base::Bind(&RemoteCommandsInvalidator::DoRemoteCommandsFetch, |
| 105 weak_factory_.GetWeakPtr())); |
| 106 } |
| 107 |
| 108 std::string RemoteCommandsInvalidator::GetOwnerName() const { |
| 109 return "RemoteCommands"; |
| 110 } |
| 111 |
| 112 void RemoteCommandsInvalidator::ReloadPolicyData( |
| 113 const enterprise_management::PolicyData* policy) { |
| 114 DCHECK(thread_checker_.CalledOnValidThread()); |
| 115 |
| 116 if (state_ != STARTED) |
| 117 return; |
| 118 |
| 119 // Create the ObjectId based on the policy data. |
| 120 // If the policy does not specify an the ObjectId, then unregister. |
| 121 if (!policy || !policy->has_command_invalidation_source() || |
| 122 !policy->has_command_invalidation_name()) { |
| 123 Unregister(); |
| 124 return; |
| 125 } |
| 126 const invalidation::ObjectId object_id(policy->command_invalidation_source(), |
| 127 policy->command_invalidation_name()); |
| 128 |
| 129 // If the policy object id in the policy data is different from the currently |
| 130 // registered object id, update the object registration. |
| 131 if (!is_registered_ || !(object_id == object_id_)) |
| 132 Register(object_id); |
| 133 } |
| 134 |
| 135 void RemoteCommandsInvalidator::Register( |
| 136 const invalidation::ObjectId& object_id) { |
| 137 // Register this handler with the invalidation service if needed. |
| 138 if (!is_registered_) { |
| 139 OnInvalidatorStateChange(invalidation_service_->GetInvalidatorState()); |
| 140 invalidation_service_->RegisterInvalidationHandler(this); |
| 141 is_registered_ = true; |
| 142 } |
| 143 |
| 144 object_id_ = object_id; |
| 145 UpdateInvalidationsEnabled(); |
| 146 |
| 147 // Update registration with the invalidation service. |
| 148 syncer::ObjectIdSet ids; |
| 149 ids.insert(object_id); |
| 150 invalidation_service_->UpdateRegisteredInvalidationIds(this, ids); |
| 151 } |
| 152 |
| 153 void RemoteCommandsInvalidator::Unregister() { |
| 154 if (is_registered_) { |
| 155 invalidation_service_->UpdateRegisteredInvalidationIds( |
| 156 this, syncer::ObjectIdSet()); |
| 157 invalidation_service_->UnregisterInvalidationHandler(this); |
| 158 is_registered_ = false; |
| 159 UpdateInvalidationsEnabled(); |
| 160 } |
| 161 } |
| 162 |
| 163 void RemoteCommandsInvalidator::UpdateInvalidationsEnabled() { |
| 164 invalidations_enabled_ = invalidation_service_enabled_ && is_registered_; |
| 165 } |
| 166 |
| 167 } // namespace policy |
OLD | NEW |