| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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 "components/invalidation/invalidator_registrar.h" | |
| 6 | |
| 7 #include <cstddef> | |
| 8 #include <iterator> | |
| 9 #include <utility> | |
| 10 | |
| 11 #include "base/logging.h" | |
| 12 #include "components/invalidation/object_id_invalidation_map.h" | |
| 13 | |
| 14 namespace syncer { | |
| 15 | |
| 16 InvalidatorRegistrar::InvalidatorRegistrar() | |
| 17 : state_(DEFAULT_INVALIDATION_ERROR) {} | |
| 18 | |
| 19 InvalidatorRegistrar::~InvalidatorRegistrar() { | |
| 20 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 21 CHECK(handler_to_ids_map_.empty()); | |
| 22 } | |
| 23 | |
| 24 void InvalidatorRegistrar::RegisterHandler(InvalidationHandler* handler) { | |
| 25 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 26 CHECK(handler); | |
| 27 CHECK(!handlers_.HasObserver(handler)); | |
| 28 handlers_.AddObserver(handler); | |
| 29 } | |
| 30 | |
| 31 bool InvalidatorRegistrar::UpdateRegisteredIds(InvalidationHandler* handler, | |
| 32 const ObjectIdSet& ids) { | |
| 33 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 34 CHECK(handler); | |
| 35 CHECK(handlers_.HasObserver(handler)); | |
| 36 | |
| 37 for (HandlerIdsMap::const_iterator it = handler_to_ids_map_.begin(); | |
| 38 it != handler_to_ids_map_.end(); ++it) { | |
| 39 if (it->first == handler) { | |
| 40 continue; | |
| 41 } | |
| 42 | |
| 43 std::vector<invalidation::ObjectId> intersection; | |
| 44 std::set_intersection( | |
| 45 it->second.begin(), it->second.end(), | |
| 46 ids.begin(), ids.end(), | |
| 47 std::inserter(intersection, intersection.end()), | |
| 48 ObjectIdLessThan()); | |
| 49 if (!intersection.empty()) { | |
| 50 LOG(ERROR) << "Duplicate registration: trying to register " | |
| 51 << ObjectIdToString(*intersection.begin()) << " for " | |
| 52 << handler << " when it's already registered for " | |
| 53 << it->first; | |
| 54 return false; | |
| 55 } | |
| 56 } | |
| 57 | |
| 58 if (ids.empty()) { | |
| 59 handler_to_ids_map_.erase(handler); | |
| 60 } else { | |
| 61 handler_to_ids_map_[handler] = ids; | |
| 62 } | |
| 63 return true; | |
| 64 } | |
| 65 | |
| 66 void InvalidatorRegistrar::UnregisterHandler(InvalidationHandler* handler) { | |
| 67 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 68 CHECK(handler); | |
| 69 CHECK(handlers_.HasObserver(handler)); | |
| 70 handlers_.RemoveObserver(handler); | |
| 71 handler_to_ids_map_.erase(handler); | |
| 72 } | |
| 73 | |
| 74 ObjectIdSet InvalidatorRegistrar::GetRegisteredIds( | |
| 75 InvalidationHandler* handler) const { | |
| 76 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 77 HandlerIdsMap::const_iterator lookup = handler_to_ids_map_.find(handler); | |
| 78 if (lookup != handler_to_ids_map_.end()) { | |
| 79 return lookup->second; | |
| 80 } else { | |
| 81 return ObjectIdSet(); | |
| 82 } | |
| 83 } | |
| 84 | |
| 85 ObjectIdSet InvalidatorRegistrar::GetAllRegisteredIds() const { | |
| 86 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 87 ObjectIdSet registered_ids; | |
| 88 for (HandlerIdsMap::const_iterator it = handler_to_ids_map_.begin(); | |
| 89 it != handler_to_ids_map_.end(); ++it) { | |
| 90 registered_ids.insert(it->second.begin(), it->second.end()); | |
| 91 } | |
| 92 return registered_ids; | |
| 93 } | |
| 94 | |
| 95 void InvalidatorRegistrar::DispatchInvalidationsToHandlers( | |
| 96 const ObjectIdInvalidationMap& invalidation_map) { | |
| 97 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 98 // If we have no handlers, there's nothing to do. | |
| 99 if (!handlers_.might_have_observers()) { | |
| 100 return; | |
| 101 } | |
| 102 | |
| 103 for (HandlerIdsMap::iterator it = handler_to_ids_map_.begin(); | |
| 104 it != handler_to_ids_map_.end(); ++it) { | |
| 105 ObjectIdInvalidationMap to_emit = | |
| 106 invalidation_map.GetSubsetWithObjectIds(it->second); | |
| 107 if (!to_emit.Empty()) { | |
| 108 it->first->OnIncomingInvalidation(to_emit); | |
| 109 } | |
| 110 } | |
| 111 } | |
| 112 | |
| 113 void InvalidatorRegistrar::UpdateInvalidatorState(InvalidatorState state) { | |
| 114 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 115 DVLOG(1) << "New invalidator state: " << InvalidatorStateToString(state_) | |
| 116 << " -> " << InvalidatorStateToString(state); | |
| 117 state_ = state; | |
| 118 FOR_EACH_OBSERVER(InvalidationHandler, handlers_, | |
| 119 OnInvalidatorStateChange(state)); | |
| 120 } | |
| 121 | |
| 122 InvalidatorState InvalidatorRegistrar::GetInvalidatorState() const { | |
| 123 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 124 return state_; | |
| 125 } | |
| 126 | |
| 127 std::map<std::string, ObjectIdSet> | |
| 128 InvalidatorRegistrar::GetSanitizedHandlersIdsMap() { | |
| 129 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 130 std::map<std::string, ObjectIdSet> clean_handlers_to_ids; | |
| 131 for (HandlerIdsMap::const_iterator it = handler_to_ids_map_.begin(); | |
| 132 it != handler_to_ids_map_.end(); | |
| 133 ++it) { | |
| 134 clean_handlers_to_ids[it->first->GetOwnerName()] = ObjectIdSet(it->second); | |
| 135 } | |
| 136 return clean_handlers_to_ids; | |
| 137 } | |
| 138 | |
| 139 bool InvalidatorRegistrar::IsHandlerRegisteredForTest( | |
| 140 const InvalidationHandler* handler) const { | |
| 141 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 142 return handlers_.HasObserver(handler); | |
| 143 } | |
| 144 | |
| 145 void InvalidatorRegistrar::DetachFromThreadForTest() { | |
| 146 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 147 thread_checker_.DetachFromThread(); | |
| 148 } | |
| 149 | |
| 150 } // namespace syncer | |
| OLD | NEW |