Commit 30b86781 authored by Marc Treib's avatar Marc Treib Committed by Commit Bot

InvalidatorRegistrarWithMemory: Terminology cleanup

This renames some methods and updates comments. No behavior changes.

Bug: 1021092
Change-Id: Iab7d289e707a86cfaa79a45a7d2cdf071dbcb87d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1898066
Commit-Queue: Marc Treib <treib@chromium.org>
Reviewed-by: default avatarTatiana Gornak <melandory@chromium.org>
Cr-Commit-Position: refs/heads/master@{#712512}
parent eb974b72
...@@ -136,7 +136,7 @@ bool FCMInvalidationService::UpdateRegisteredInvalidationIds( ...@@ -136,7 +136,7 @@ bool FCMInvalidationService::UpdateRegisteredInvalidationIds(
if (!invalidator_registrar_.UpdateRegisteredTopics(handler, topics)) if (!invalidator_registrar_.UpdateRegisteredTopics(handler, topics))
return false; return false;
DoUpdateRegisteredIdsIfNeeded(); DoUpdateRegisteredIdsIfNeeded();
logger_.OnUpdateTopics(invalidator_registrar_.GetSanitizedHandlersIdsMap()); logger_.OnUpdateTopics(invalidator_registrar_.GetHandlerNameToTopicsMap());
return true; return true;
} }
...@@ -318,7 +318,7 @@ void FCMInvalidationService::OnInstanceIdRecieved(const std::string& id) { ...@@ -318,7 +318,7 @@ void FCMInvalidationService::OnInstanceIdRecieved(const std::string& id) {
DictionaryPrefUpdate update(pref_service_, DictionaryPrefUpdate update(pref_service_,
prefs::kInvalidationClientIDCache); prefs::kInvalidationClientIDCache);
update->SetStringKey(sender_id_, id); update->SetStringKey(sender_id_, id);
invalidator_registrar_.UpdateInvalidatorId(id); invalidator_registrar_.UpdateInvalidatorInstanceId(id);
} }
} }
...@@ -330,8 +330,8 @@ void FCMInvalidationService::OnDeleteIDCompleted( ...@@ -330,8 +330,8 @@ void FCMInvalidationService::OnDeleteIDCompleted(
void FCMInvalidationService::DoUpdateRegisteredIdsIfNeeded() { void FCMInvalidationService::DoUpdateRegisteredIdsIfNeeded() {
if (!invalidation_listener_ || !update_was_requested_) if (!invalidation_listener_ || !update_was_requested_)
return; return;
auto registered_ids = invalidator_registrar_.GetAllRegisteredIds(); auto subscribed_topics = invalidator_registrar_.GetAllSubscribedTopics();
invalidation_listener_->UpdateRegisteredTopics(registered_ids); invalidation_listener_->UpdateRegisteredTopics(subscribed_topics);
update_was_requested_ = false; update_was_requested_ = false;
} }
......
...@@ -73,6 +73,7 @@ InvalidatorRegistrarWithMemory::InvalidatorRegistrarWithMemory( ...@@ -73,6 +73,7 @@ InvalidatorRegistrarWithMemory::InvalidatorRegistrarWithMemory(
update->SetKey(sender_id_, base::DictionaryValue()); update->SetKey(sender_id_, base::DictionaryValue());
return; return;
} }
// Restore |handler_name_to_subscribed_topics_map_| from prefs.
for (const auto& it : pref_data->DictItems()) { for (const auto& it : pref_data->DictItems()) {
Topic topic = it.first; Topic topic = it.first;
if (it.second.is_dict()) { if (it.second.is_dict()) {
...@@ -81,20 +82,20 @@ InvalidatorRegistrarWithMemory::InvalidatorRegistrarWithMemory( ...@@ -81,20 +82,20 @@ InvalidatorRegistrarWithMemory::InvalidatorRegistrarWithMemory(
if (!handler || !is_public) { if (!handler || !is_public) {
continue; continue;
} }
handler_name_to_topics_map_[handler->GetString()].emplace( handler_name_to_subscribed_topics_map_[handler->GetString()].emplace(
topic, TopicMetadata{is_public->GetBool()}); topic, TopicMetadata{is_public->GetBool()});
} else if (it.second.is_string()) { } else if (it.second.is_string()) {
std::string handler_name; std::string handler_name;
it.second.GetAsString(&handler_name); it.second.GetAsString(&handler_name);
handler_name_to_topics_map_[handler_name].emplace(topic, handler_name_to_subscribed_topics_map_[handler_name].emplace(
TopicMetadata{false}); topic, TopicMetadata{false});
} }
} }
} }
InvalidatorRegistrarWithMemory::~InvalidatorRegistrarWithMemory() { InvalidatorRegistrarWithMemory::~InvalidatorRegistrarWithMemory() {
DCHECK(thread_checker_.CalledOnValidThread()); DCHECK(thread_checker_.CalledOnValidThread());
CHECK(handler_to_topics_map_.empty()); CHECK(registered_handler_to_topics_map_.empty());
} }
void InvalidatorRegistrarWithMemory::RegisterHandler( void InvalidatorRegistrarWithMemory::RegisterHandler(
...@@ -111,9 +112,10 @@ void InvalidatorRegistrarWithMemory::UnregisterHandler( ...@@ -111,9 +112,10 @@ void InvalidatorRegistrarWithMemory::UnregisterHandler(
CHECK(handler); CHECK(handler);
CHECK(handlers_.HasObserver(handler)); CHECK(handlers_.HasObserver(handler));
handlers_.RemoveObserver(handler); handlers_.RemoveObserver(handler);
handler_to_topics_map_.erase(handler); registered_handler_to_topics_map_.erase(handler);
// Note: Do *not* remove the entry from |handler_name_to_topics_map_| so that // Note: Do *not* remove the entry from
// GetAllRegisteredIds() still returns the registered topics. // |handler_name_to_subscribed_topics_map_| so that GetAllSubscribedTopics()
// still returns the registered topics.
} }
bool InvalidatorRegistrarWithMemory::IsHandlerRegistered( bool InvalidatorRegistrarWithMemory::IsHandlerRegistered(
...@@ -135,9 +137,9 @@ bool InvalidatorRegistrarWithMemory::UpdateRegisteredTopics( ...@@ -135,9 +137,9 @@ bool InvalidatorRegistrarWithMemory::UpdateRegisteredTopics(
if (success) { if (success) {
if (topics.empty()) { if (topics.empty()) {
handler_to_topics_map_.erase(handler); registered_handler_to_topics_map_.erase(handler);
} else { } else {
handler_to_topics_map_[handler] = topics; registered_handler_to_topics_map_[handler] = topics;
} }
} }
...@@ -149,21 +151,21 @@ bool InvalidatorRegistrarWithMemory::UpdateRegisteredTopics( ...@@ -149,21 +151,21 @@ bool InvalidatorRegistrarWithMemory::UpdateRegisteredTopics(
} }
// TODO(treib): This seems inconsistent - if there's a duplicate, we don't // TODO(treib): This seems inconsistent - if there's a duplicate, we don't
// update |handler_to_topics_map_| but we *do* still update // update |registered_handler_to_topics_map_| but we *do* still update
// |handler_name_to_topics_map_| and the prefs?! // |handler_name_to_subscribed_topics_map_| and the prefs?!
DictionaryPrefUpdate update(local_state_, kTopicsToHandler); DictionaryPrefUpdate update(local_state_, kTopicsToHandler);
base::Value* pref_data = update->FindDictKey(sender_id_); base::Value* pref_data = update->FindDictKey(sender_id_);
auto to_unregister = FindRemovedTopics(old_topics, topics); auto to_unregister = FindRemovedTopics(old_topics, topics);
if (!to_unregister.empty()) { for (const auto& topic : to_unregister) {
for (const auto& topic : to_unregister) { pref_data->RemoveKey(topic);
pref_data->RemoveKey(topic); handler_name_to_subscribed_topics_map_[handler->GetOwnerName()].erase(
handler_name_to_topics_map_[handler->GetOwnerName()].erase(topic); topic);
}
} }
for (const auto& topic : topics) { for (const auto& topic : topics) {
handler_name_to_topics_map_[handler->GetOwnerName()].insert(topic); handler_name_to_subscribed_topics_map_[handler->GetOwnerName()].insert(
topic);
base::DictionaryValue handler_pref; base::DictionaryValue handler_pref;
handler_pref.SetStringKey(kHandler, handler->GetOwnerName()); handler_pref.SetStringKey(kHandler, handler->GetOwnerName());
handler_pref.SetBoolKey(kIsPublic, topic.second.is_public); handler_pref.SetBoolKey(kIsPublic, topic.second.is_public);
...@@ -175,13 +177,14 @@ bool InvalidatorRegistrarWithMemory::UpdateRegisteredTopics( ...@@ -175,13 +177,14 @@ bool InvalidatorRegistrarWithMemory::UpdateRegisteredTopics(
Topics InvalidatorRegistrarWithMemory::GetRegisteredTopics( Topics InvalidatorRegistrarWithMemory::GetRegisteredTopics(
InvalidationHandler* handler) const { InvalidationHandler* handler) const {
DCHECK(thread_checker_.CalledOnValidThread()); DCHECK(thread_checker_.CalledOnValidThread());
auto lookup = handler_to_topics_map_.find(handler); auto lookup = registered_handler_to_topics_map_.find(handler);
return lookup != handler_to_topics_map_.end() ? lookup->second : Topics(); return lookup != registered_handler_to_topics_map_.end() ? lookup->second
: Topics();
} }
Topics InvalidatorRegistrarWithMemory::GetAllRegisteredIds() const { Topics InvalidatorRegistrarWithMemory::GetAllSubscribedTopics() const {
Topics registered_topics; Topics registered_topics;
for (const auto& handler_to_topic : handler_name_to_topics_map_) { for (const auto& handler_to_topic : handler_name_to_subscribed_topics_map_) {
registered_topics.insert(handler_to_topic.second.begin(), registered_topics.insert(handler_to_topic.second.begin(),
handler_to_topic.second.end()); handler_to_topic.second.end());
} }
...@@ -196,18 +199,15 @@ void InvalidatorRegistrarWithMemory::DispatchInvalidationsToHandlers( ...@@ -196,18 +199,15 @@ void InvalidatorRegistrarWithMemory::DispatchInvalidationsToHandlers(
return; return;
} }
for (const auto& handler_and_topics : handler_to_topics_map_) { for (const auto& handler_and_topics : registered_handler_to_topics_map_) {
TopicInvalidationMap to_emit = TopicInvalidationMap topics_to_emit =
invalidation_map.GetSubsetWithTopics(handler_and_topics.second); invalidation_map.GetSubsetWithTopics(handler_and_topics.second);
ObjectIdInvalidationMap object_ids_to_emit; if (topics_to_emit.Empty()) {
std::vector<syncer::Invalidation> invalidations; continue;
to_emit.GetAllInvalidations(&invalidations);
for (const auto& invalidation : invalidations) {
object_ids_to_emit.Insert(invalidation);
}
if (!to_emit.Empty()) {
handler_and_topics.first->OnIncomingInvalidation(object_ids_to_emit);
} }
ObjectIdInvalidationMap object_ids_to_emit =
ConvertTopicInvalidationMapToObjectIdInvalidationMap(topics_to_emit);
handler_and_topics.first->OnIncomingInvalidation(object_ids_to_emit);
} }
} }
...@@ -226,20 +226,21 @@ InvalidatorState InvalidatorRegistrarWithMemory::GetInvalidatorState() const { ...@@ -226,20 +226,21 @@ InvalidatorState InvalidatorRegistrarWithMemory::GetInvalidatorState() const {
return state_; return state_;
} }
void InvalidatorRegistrarWithMemory::UpdateInvalidatorId( void InvalidatorRegistrarWithMemory::UpdateInvalidatorInstanceId(
const std::string& id) { const std::string& instance_id) {
for (auto& observer : handlers_) for (auto& observer : handlers_)
observer.OnInvalidatorClientIdChange(id); observer.OnInvalidatorClientIdChange(instance_id);
} }
std::map<std::string, Topics> std::map<std::string, Topics>
InvalidatorRegistrarWithMemory::GetSanitizedHandlersIdsMap() { InvalidatorRegistrarWithMemory::GetHandlerNameToTopicsMap() {
DCHECK(thread_checker_.CalledOnValidThread()); DCHECK(thread_checker_.CalledOnValidThread());
std::map<std::string, Topics> clean_handlers_to_topics; std::map<std::string, Topics> names_to_topics;
for (const auto& handler_and_topics : handler_to_topics_map_) for (const auto& handler_and_topics : registered_handler_to_topics_map_) {
clean_handlers_to_topics[handler_and_topics.first->GetOwnerName()] = names_to_topics[handler_and_topics.first->GetOwnerName()] =
Topics(handler_and_topics.second); handler_and_topics.second;
return clean_handlers_to_topics; }
return names_to_topics;
} }
void InvalidatorRegistrarWithMemory::RequestDetailedStatus( void InvalidatorRegistrarWithMemory::RequestDetailedStatus(
...@@ -251,7 +252,7 @@ void InvalidatorRegistrarWithMemory::RequestDetailedStatus( ...@@ -251,7 +252,7 @@ void InvalidatorRegistrarWithMemory::RequestDetailedStatus(
bool InvalidatorRegistrarWithMemory::HasDuplicateTopicRegistration( bool InvalidatorRegistrarWithMemory::HasDuplicateTopicRegistration(
InvalidationHandler* handler, InvalidationHandler* handler,
const Topics& topics) const { const Topics& topics) const {
for (const auto& handler_and_topics : handler_to_topics_map_) { for (const auto& handler_and_topics : registered_handler_to_topics_map_) {
if (handler_and_topics.first == handler) { if (handler_and_topics.first == handler) {
continue; continue;
} }
...@@ -270,8 +271,8 @@ bool InvalidatorRegistrarWithMemory::HasDuplicateTopicRegistration( ...@@ -270,8 +271,8 @@ bool InvalidatorRegistrarWithMemory::HasDuplicateTopicRegistration(
base::DictionaryValue InvalidatorRegistrarWithMemory::CollectDebugData() const { base::DictionaryValue InvalidatorRegistrarWithMemory::CollectDebugData() const {
base::DictionaryValue return_value; base::DictionaryValue return_value;
return_value.SetInteger("InvalidatorRegistrarWithMemory.Handlers", return_value.SetInteger("InvalidatorRegistrarWithMemory.Handlers",
handler_name_to_topics_map_.size()); handler_name_to_subscribed_topics_map_.size());
for (const auto& handler_to_topics : handler_name_to_topics_map_) { for (const auto& handler_to_topics : handler_name_to_subscribed_topics_map_) {
const std::string& handler = handler_to_topics.first; const std::string& handler = handler_to_topics.first;
for (const auto& topic : handler_to_topics.second) { for (const auto& topic : handler_to_topics.second) {
return_value.SetString("InvalidatorRegistrarWithMemory." + topic.first, return_value.SetString("InvalidatorRegistrarWithMemory." + topic.first,
......
...@@ -65,10 +65,13 @@ class INVALIDATION_EXPORT InvalidatorRegistrarWithMemory { ...@@ -65,10 +65,13 @@ class INVALIDATION_EXPORT InvalidatorRegistrarWithMemory {
Topics GetRegisteredTopics(InvalidationHandler* handler) const; Topics GetRegisteredTopics(InvalidationHandler* handler) const;
// Returns the set of all IDs that are registered to some handler (even // Returns the set of all topics that (we think) we are subscribed to on the
// handlers that have been unregistered). // server. This is the set of topics which were registered to some handler and
// TODO(treib): Rename "Ids" to "Topics". // not unregistered (via UpdateRegisteredTopics()). This includes topics whose
Topics GetAllRegisteredIds() const; // *handler* has been unregistered without unregistering the topic itself
// first (e.g. because Chrome was restarted and the handler hasn't registered
// itself again yet).
Topics GetAllSubscribedTopics() const;
// Sorts incoming invalidations into a bucket for each handler and then // Sorts incoming invalidations into a bucket for each handler and then
// dispatches the batched invalidations to the corresponding handler. // dispatches the batched invalidations to the corresponding handler.
...@@ -87,15 +90,13 @@ class INVALIDATION_EXPORT InvalidatorRegistrarWithMemory { ...@@ -87,15 +90,13 @@ class INVALIDATION_EXPORT InvalidatorRegistrarWithMemory {
// updated state. // updated state.
InvalidatorState GetInvalidatorState() const; InvalidatorState GetInvalidatorState() const;
// Updates the invalidator id to the given one and then notifies // Notifies all handlers about the new instance ID.
// all handlers. void UpdateInvalidatorInstanceId(const std::string& instance_id);
void UpdateInvalidatorId(const std::string& id);
// Gets a new map for the name of invalidator handlers and their // Gets a new map from the name of invalidation handlers to their topics. This
// objects id. This is used by the InvalidatorLogger to be able // is used by the InvalidatorLogger to be able to display every registered
// to display every registered handler and its topics. // handler and its topics.
// TODO(treib): Rename "Ids" to "Topics". std::map<std::string, Topics> GetHandlerNameToTopicsMap();
std::map<std::string, Topics> GetSanitizedHandlersIdsMap();
void RequestDetailedStatus( void RequestDetailedStatus(
base::RepeatingCallback<void(const base::DictionaryValue&)> callback) base::RepeatingCallback<void(const base::DictionaryValue&)> callback)
...@@ -115,15 +116,18 @@ class INVALIDATION_EXPORT InvalidatorRegistrarWithMemory { ...@@ -115,15 +116,18 @@ class INVALIDATION_EXPORT InvalidatorRegistrarWithMemory {
base::ObserverList<InvalidationHandler, true>::Unchecked handlers_; base::ObserverList<InvalidationHandler, true>::Unchecked handlers_;
// Note: When a handler is unregistered, its entry is removed from // Note: When a handler is unregistered, its entry is removed from
// |handler_to_topics_map_| but NOT from |handler_name_to_topics_map_|. // |registered_handler_to_topics_map_| but NOT from
std::map<InvalidationHandler*, Topics> handler_to_topics_map_; // |handler_name_to_subscribed_topics_map_|.
std::map<std::string, Topics> handler_name_to_topics_map_; std::map<InvalidationHandler*, Topics> registered_handler_to_topics_map_;
std::map<std::string, Topics> handler_name_to_subscribed_topics_map_;
InvalidatorState state_; InvalidatorState state_;
// This can be either a regular (Profile-attached) PrefService or the local // This can be either a regular (Profile-attached) PrefService or the local
// state PrefService. // state PrefService.
PrefService* const local_state_; PrefService* const local_state_;
// The FCM sender ID. May be empty.
const std::string sender_id_; const std::string sender_id_;
DISALLOW_COPY_AND_ASSIGN(InvalidatorRegistrarWithMemory); DISALLOW_COPY_AND_ASSIGN(InvalidatorRegistrarWithMemory);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment