Roll src/third_party/WebKit 9f7fb92:f103b33 (svn 202621:202622)
[chromium-blink-merge.git] / components / invalidation / impl / invalidator_registrar.cc
blob903cf72451df07174d4fe1cb7b4634910bd4eadc
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.
5 #include "components/invalidation/impl/invalidator_registrar.h"
7 #include <cstddef>
8 #include <iterator>
9 #include <utility>
11 #include "base/logging.h"
12 #include "components/invalidation/public/object_id_invalidation_map.h"
14 namespace syncer {
16 InvalidatorRegistrar::InvalidatorRegistrar()
17 : state_(DEFAULT_INVALIDATION_ERROR) {}
19 InvalidatorRegistrar::~InvalidatorRegistrar() {
20 DCHECK(thread_checker_.CalledOnValidThread());
21 CHECK(handler_to_ids_map_.empty());
24 void InvalidatorRegistrar::RegisterHandler(InvalidationHandler* handler) {
25 DCHECK(thread_checker_.CalledOnValidThread());
26 CHECK(handler);
27 CHECK(!handlers_.HasObserver(handler));
28 handlers_.AddObserver(handler);
31 bool InvalidatorRegistrar::UpdateRegisteredIds(InvalidationHandler* handler,
32 const ObjectIdSet& ids) {
33 DCHECK(thread_checker_.CalledOnValidThread());
34 CHECK(handler);
35 CHECK(handlers_.HasObserver(handler));
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;
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;
58 if (ids.empty()) {
59 handler_to_ids_map_.erase(handler);
60 } else {
61 handler_to_ids_map_[handler] = ids;
63 return true;
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);
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();
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());
92 return registered_ids;
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;
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);
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));
122 InvalidatorState InvalidatorRegistrar::GetInvalidatorState() const {
123 DCHECK(thread_checker_.CalledOnValidThread());
124 return state_;
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);
136 return clean_handlers_to_ids;
139 bool InvalidatorRegistrar::IsHandlerRegisteredForTest(
140 const InvalidationHandler* handler) const {
141 DCHECK(thread_checker_.CalledOnValidThread());
142 return handlers_.HasObserver(handler);
145 void InvalidatorRegistrar::DetachFromThreadForTest() {
146 DCHECK(thread_checker_.CalledOnValidThread());
147 thread_checker_.DetachFromThread();
150 } // namespace syncer