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 "chrome/browser/invalidation/invalidation_logger.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/values.h"
9 #include "chrome/browser/invalidation/invalidation_logger_observer.h"
10 #include "sync/notifier/invalidation_handler.h"
12 namespace invalidation {
13 class InvalidationLoggerObserver;
15 InvalidationLogger::InvalidationLogger()
16 : last_invalidator_state_(syncer::TRANSIENT_INVALIDATION_ERROR) {}
18 InvalidationLogger::~InvalidationLogger() {}
20 void InvalidationLogger::OnRegistration(const std::string& registrar_name) {
21 registered_handlers_.insert(registrar_name);
22 EmitRegisteredHandlers();
25 void InvalidationLogger::OnUnregistration(const std::string& registrar_name) {
26 DCHECK(registered_handlers_.find(registrar_name) !=
27 registered_handlers_.end());
28 std::multiset<std::string>::iterator it =
29 registered_handlers_.find(registrar_name);
30 // Delete only one instance of registrar_name.
31 registered_handlers_.erase(it);
32 EmitRegisteredHandlers();
35 void InvalidationLogger::EmitRegisteredHandlers() {
36 FOR_EACH_OBSERVER(InvalidationLoggerObserver, observer_list_,
37 OnRegistrationChange(registered_handlers_));
40 void InvalidationLogger::OnStateChange(
41 const syncer::InvalidatorState& newState) {
42 last_invalidator_state_ = newState;
46 void InvalidationLogger::EmitState() {
47 FOR_EACH_OBSERVER(InvalidationLoggerObserver,
49 OnStateChange(last_invalidator_state_));
52 void InvalidationLogger::OnUpdateIds(
53 std::map<std::string, syncer::ObjectIdSet> updated_ids) {
54 for (std::map<std::string, syncer::ObjectIdSet>::const_iterator it =
55 updated_ids.begin(); it != updated_ids.end(); ++it) {
56 latest_ids_[it->first] = syncer::ObjectIdSet(it->second);
61 void InvalidationLogger::EmitUpdatedIds() {
62 for (std::map<std::string, syncer::ObjectIdSet>::const_iterator it =
63 latest_ids_.begin(); it != latest_ids_.end(); ++it) {
64 FOR_EACH_OBSERVER(InvalidationLoggerObserver,
66 OnUpdateIds(it->first, it->second));
70 void InvalidationLogger::OnDebugMessage(const base::DictionaryValue& details) {
72 InvalidationLoggerObserver, observer_list_, OnDebugMessage(details));
75 void InvalidationLogger::OnInvalidation(
76 const syncer::ObjectIdInvalidationMap& details) {
78 InvalidationLoggerObserver, observer_list_, OnInvalidation(details));
81 void InvalidationLogger::EmitContent() {
84 EmitRegisteredHandlers();
87 void InvalidationLogger::RegisterObserver(
88 InvalidationLoggerObserver* debug_observer) {
89 observer_list_.AddObserver(debug_observer);
92 void InvalidationLogger::UnregisterObserver(
93 InvalidationLoggerObserver* debug_observer) {
94 observer_list_.RemoveObserver(debug_observer);
97 bool InvalidationLogger::IsObserverRegistered(
98 InvalidationLoggerObserver* debug_observer) {
99 return observer_list_.HasObserver(debug_observer);
101 } // namespace invalidation