Roll src/third_party/WebKit 9f7fb92:f103b33 (svn 202621:202622)
[chromium-blink-merge.git] / components / invalidation / impl / invalidation_logger.cc
blob5628a24bdbb0edcc4e9b8de11565c25ff4cfb6d0
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/invalidation_logger.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/values.h"
9 #include "components/invalidation/impl/invalidation_logger_observer.h"
10 #include "components/invalidation/public/invalidation_handler.h"
12 namespace invalidation {
13 class InvalidationLoggerObserver;
15 InvalidationLogger::InvalidationLogger()
16 : last_invalidator_state_(syncer::TRANSIENT_INVALIDATION_ERROR),
17 last_invalidator_state_timestamp_(base::Time::Now()) { }
19 InvalidationLogger::~InvalidationLogger() {}
21 void InvalidationLogger::OnRegistration(const std::string& registrar_name) {
22 registered_handlers_.insert(registrar_name);
23 EmitRegisteredHandlers();
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();
36 void InvalidationLogger::EmitRegisteredHandlers() {
37 FOR_EACH_OBSERVER(InvalidationLoggerObserver, observer_list_,
38 OnRegistrationChange(registered_handlers_));
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();
50 void InvalidationLogger::EmitState() {
51 FOR_EACH_OBSERVER(InvalidationLoggerObserver,
52 observer_list_,
53 OnStateChange(last_invalidator_state_,
54 last_invalidator_state_timestamp_));
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);
63 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];
77 FOR_EACH_OBSERVER(InvalidationLoggerObserver,
78 observer_list_,
79 OnUpdateIds(it->first, per_object_invalidation_count));
83 void InvalidationLogger::OnDebugMessage(const base::DictionaryValue& details) {
84 FOR_EACH_OBSERVER(
85 InvalidationLoggerObserver, observer_list_, OnDebugMessage(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()]++;
98 FOR_EACH_OBSERVER(
99 InvalidationLoggerObserver, observer_list_, OnInvalidation(details));
102 void InvalidationLogger::EmitContent() {
103 EmitState();
104 EmitUpdatedIds();
105 EmitRegisteredHandlers();
108 void InvalidationLogger::RegisterObserver(
109 InvalidationLoggerObserver* debug_observer) {
110 observer_list_.AddObserver(debug_observer);
113 void InvalidationLogger::UnregisterObserver(
114 InvalidationLoggerObserver* debug_observer) {
115 observer_list_.RemoveObserver(debug_observer);
118 bool InvalidationLogger::IsObserverRegistered(
119 const InvalidationLoggerObserver* debug_observer) const {
120 return observer_list_.HasObserver(debug_observer);
122 } // namespace invalidation