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/invalidation_logger.h"
6
7 #include "base/memory/scoped_ptr.h"
8 #include "base/values.h"
9 #include "components/invalidation/invalidation_logger_observer.h"
10 #include "sync/notifier/invalidation_handler.h"
11
12 namespace invalidation {
13 class InvalidationLoggerObserver;
14
InvalidationLogger()15 InvalidationLogger::InvalidationLogger()
16 : last_invalidator_state_(syncer::TRANSIENT_INVALIDATION_ERROR),
17 last_invalidator_state_timestamp_(base::Time::Now()) { }
18
~InvalidationLogger()19 InvalidationLogger::~InvalidationLogger() {}
20
OnRegistration(const std::string & registrar_name)21 void InvalidationLogger::OnRegistration(const std::string& registrar_name) {
22 registered_handlers_.insert(registrar_name);
23 EmitRegisteredHandlers();
24 }
25
OnUnregistration(const std::string & registrar_name)26 void InvalidationLogger::OnUnregistration(const std::string& registrar_name) {
27 DCHECK(registered_handlers_.find(registrar_name) !=
28 registered_handlers_.end());
29 std::multiset<std::string>::iterator it =
30 registered_handlers_.find(registrar_name);
31 // Delete only one instance of registrar_name.
32 registered_handlers_.erase(it);
33 EmitRegisteredHandlers();
34 }
35
EmitRegisteredHandlers()36 void InvalidationLogger::EmitRegisteredHandlers() {
37 FOR_EACH_OBSERVER(InvalidationLoggerObserver, observer_list_,
38 OnRegistrationChange(registered_handlers_));
39 }
40
OnStateChange(const syncer::InvalidatorState & new_state)41 void InvalidationLogger::OnStateChange(
42 const syncer::InvalidatorState& new_state) {
43 // Prevent spurious same state emissions from updating the timestamp.
44 if (new_state != last_invalidator_state_)
45 last_invalidator_state_timestamp_ = base::Time::Now();
46 last_invalidator_state_ = new_state;
47 EmitState();
48 }
49
EmitState()50 void InvalidationLogger::EmitState() {
51 FOR_EACH_OBSERVER(InvalidationLoggerObserver,
52 observer_list_,
53 OnStateChange(last_invalidator_state_,
54 last_invalidator_state_timestamp_));
55 }
56
OnUpdateIds(std::map<std::string,syncer::ObjectIdSet> updated_ids)57 void InvalidationLogger::OnUpdateIds(
58 std::map<std::string, syncer::ObjectIdSet> updated_ids) {
59 for (std::map<std::string, syncer::ObjectIdSet>::const_iterator it =
60 updated_ids.begin(); it != updated_ids.end(); ++it) {
61 latest_ids_[it->first] = syncer::ObjectIdSet(it->second);
62 }
63 EmitUpdatedIds();
64 }
65
EmitUpdatedIds()66 void InvalidationLogger::EmitUpdatedIds() {
67 for (std::map<std::string, syncer::ObjectIdSet>::const_iterator it =
68 latest_ids_.begin(); it != latest_ids_.end(); ++it) {
69 const syncer::ObjectIdSet& object_ids_for_handler = it->second;
70 syncer::ObjectIdCountMap per_object_invalidation_count;
71 for (syncer::ObjectIdSet::const_iterator oid_it =
72 object_ids_for_handler.begin();
73 oid_it != object_ids_for_handler.end();
74 ++oid_it) {
75 per_object_invalidation_count[*oid_it] = invalidation_count_[*oid_it];
76 }
77 FOR_EACH_OBSERVER(InvalidationLoggerObserver,
78 observer_list_,
79 OnUpdateIds(it->first, per_object_invalidation_count));
80 }
81 }
82
OnDebugMessage(const base::DictionaryValue & details)83 void InvalidationLogger::OnDebugMessage(const base::DictionaryValue& details) {
84 FOR_EACH_OBSERVER(
85 InvalidationLoggerObserver, observer_list_, OnDebugMessage(details));
86 }
87
OnInvalidation(const syncer::ObjectIdInvalidationMap & details)88 void InvalidationLogger::OnInvalidation(
89 const syncer::ObjectIdInvalidationMap& details) {
90 std::vector<syncer::Invalidation> internal_invalidations;
91 details.GetAllInvalidations(&internal_invalidations);
92 for (std::vector<syncer::Invalidation>::const_iterator it =
93 internal_invalidations.begin();
94 it != internal_invalidations.end();
95 ++it) {
96 invalidation_count_[it->object_id()]++;
97 }
98 FOR_EACH_OBSERVER(
99 InvalidationLoggerObserver, observer_list_, OnInvalidation(details));
100 }
101
EmitContent()102 void InvalidationLogger::EmitContent() {
103 EmitState();
104 EmitUpdatedIds();
105 EmitRegisteredHandlers();
106 }
107
RegisterObserver(InvalidationLoggerObserver * debug_observer)108 void InvalidationLogger::RegisterObserver(
109 InvalidationLoggerObserver* debug_observer) {
110 observer_list_.AddObserver(debug_observer);
111 }
112
UnregisterObserver(InvalidationLoggerObserver * debug_observer)113 void InvalidationLogger::UnregisterObserver(
114 InvalidationLoggerObserver* debug_observer) {
115 observer_list_.RemoveObserver(debug_observer);
116 }
117
IsObserverRegistered(InvalidationLoggerObserver * debug_observer)118 bool InvalidationLogger::IsObserverRegistered(
119 InvalidationLoggerObserver* debug_observer) {
120 return observer_list_.HasObserver(debug_observer);
121 }
122 } // namespace invalidation
123