summaryrefslogtreecommitdiffstats
path: root/sync/notifier/invalidator_registrar.cc
blob: 0c8d0f74392b100d2f4c0c51bd3d567977fbeeaf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "sync/notifier/invalidator_registrar.h"

#include <cstddef>
#include <utility>

#include "base/logging.h"

namespace syncer {

InvalidatorRegistrar::InvalidatorRegistrar()
    : state_(DEFAULT_INVALIDATION_ERROR) {}

InvalidatorRegistrar::~InvalidatorRegistrar() {
  DCHECK(thread_checker_.CalledOnValidThread());
  CHECK(!handlers_.might_have_observers());
  // |id_to_handler_map_| may be non-empty but that's okay.
}

void InvalidatorRegistrar::RegisterHandler(InvalidationHandler* handler) {
  DCHECK(thread_checker_.CalledOnValidThread());
  CHECK(handler);
  CHECK(!handlers_.HasObserver(handler));
  handlers_.AddObserver(handler);
}

void InvalidatorRegistrar::UpdateRegisteredIds(
    InvalidationHandler* handler,
    const ObjectIdSet& ids) {
  DCHECK(thread_checker_.CalledOnValidThread());
  CHECK(handler);
  CHECK(handlers_.HasObserver(handler));
  // Remove all existing entries for |handler|.
  for (IdHandlerMap::iterator it = id_to_handler_map_.begin();
       it != id_to_handler_map_.end(); ) {
    if (it->second == handler) {
      IdHandlerMap::iterator erase_it = it;
      ++it;
      id_to_handler_map_.erase(erase_it);
    } else {
      ++it;
    }
  }

  // Now add the entries for |handler|. We keep track of the last insertion
  // point so we only traverse the map once to insert all the new entries.
  IdHandlerMap::iterator insert_it = id_to_handler_map_.begin();
  for (ObjectIdSet::const_iterator it = ids.begin(); it != ids.end(); ++it) {
    insert_it =
        id_to_handler_map_.insert(insert_it, std::make_pair(*it, handler));
    CHECK_EQ(handler, insert_it->second)
        << "Duplicate registration: trying to register "
        << ObjectIdToString(insert_it->first) << " for "
        << handler << " when it's already registered for "
        << insert_it->second;
  }
}

void InvalidatorRegistrar::UnregisterHandler(InvalidationHandler* handler) {
  DCHECK(thread_checker_.CalledOnValidThread());
  CHECK(handler);
  CHECK(handlers_.HasObserver(handler));
  handlers_.RemoveObserver(handler);
}

ObjectIdSet InvalidatorRegistrar::GetRegisteredIds(
    InvalidationHandler* handler) const {
  DCHECK(thread_checker_.CalledOnValidThread());
  ObjectIdSet registered_ids;
  for (IdHandlerMap::const_iterator it = id_to_handler_map_.begin();
       it != id_to_handler_map_.end(); ++it) {
    if (it->second == handler) {
      registered_ids.insert(it->first);
    }
  }
  return registered_ids;
}

ObjectIdSet InvalidatorRegistrar::GetAllRegisteredIds() const {
  DCHECK(thread_checker_.CalledOnValidThread());
  ObjectIdSet registered_ids;
  for (IdHandlerMap::const_iterator it = id_to_handler_map_.begin();
       it != id_to_handler_map_.end(); ++it) {
    registered_ids.insert(it->first);
  }
  return registered_ids;
}

void InvalidatorRegistrar::DispatchInvalidationsToHandlers(
    const ObjectIdInvalidationMap& invalidation_map,
    IncomingInvalidationSource source) {
  DCHECK(thread_checker_.CalledOnValidThread());
  // If we have no handlers, there's nothing to do.
  if (!handlers_.might_have_observers()) {
    return;
  }

  typedef std::map<InvalidationHandler*, ObjectIdInvalidationMap> DispatchMap;
  DispatchMap dispatch_map;
  for (ObjectIdInvalidationMap::const_iterator it = invalidation_map.begin();
       it != invalidation_map.end(); ++it) {
    InvalidationHandler* const handler = ObjectIdToHandler(it->first);
    // Filter out invalidations for IDs with no handler.
    if (handler)
      dispatch_map[handler].insert(*it);
  }

  // Emit invalidations only for handlers in |handlers_|.
  ObserverListBase<InvalidationHandler>::Iterator it(handlers_);
  InvalidationHandler* handler = NULL;
  while ((handler = it.GetNext()) != NULL) {
    DispatchMap::const_iterator dispatch_it = dispatch_map.find(handler);
    if (dispatch_it != dispatch_map.end())
      handler->OnIncomingInvalidation(dispatch_it->second, source);
  }
}

void InvalidatorRegistrar::UpdateInvalidatorState(InvalidatorState state) {
  DCHECK(thread_checker_.CalledOnValidThread());
  state_ = state;
  FOR_EACH_OBSERVER(InvalidationHandler, handlers_,
                    OnInvalidatorStateChange(state));
}

InvalidatorState InvalidatorRegistrar::GetInvalidatorState() const {
  DCHECK(thread_checker_.CalledOnValidThread());
  return state_;
}

bool InvalidatorRegistrar::IsHandlerRegisteredForTest(
    InvalidationHandler* handler) const {
  DCHECK(thread_checker_.CalledOnValidThread());
  return handlers_.HasObserver(handler);
}

void InvalidatorRegistrar::DetachFromThreadForTest() {
  DCHECK(thread_checker_.CalledOnValidThread());
  thread_checker_.DetachFromThread();
}

InvalidationHandler* InvalidatorRegistrar::ObjectIdToHandler(
    const invalidation::ObjectId& id) {
  DCHECK(thread_checker_.CalledOnValidThread());
  IdHandlerMap::const_iterator it = id_to_handler_map_.find(id);
  return (it == id_to_handler_map_.end()) ? NULL : it->second;
}

}  // namespace syncer