| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/api/push_messaging/push_messaging_invalidati
on_handler.h" | 5 #include "chrome/browser/extensions/api/push_messaging/push_messaging_invalidati
on_handler.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/strings/string_split.h" | 11 #include "base/strings/string_split.h" |
| 12 #include "chrome/browser/extensions/api/push_messaging/push_messaging_invalidati
on_handler_delegate.h" | 12 #include "chrome/browser/extensions/api/push_messaging/push_messaging_invalidati
on_handler_delegate.h" |
| 13 #include "chrome/browser/invalidation/invalidation_service.h" | 13 #include "chrome/browser/invalidation/invalidation_service.h" |
| 14 #include "chrome/common/extensions/extension.h" | 14 #include "chrome/common/extensions/extension.h" |
| 15 #include "google/cacheinvalidation/types.pb.h" | 15 #include "google/cacheinvalidation/types.pb.h" |
| 16 #include "sync/notifier/object_id_invalidation_map.h" |
| 16 | 17 |
| 17 namespace extensions { | 18 namespace extensions { |
| 18 | 19 |
| 19 namespace { | 20 namespace { |
| 20 | 21 |
| 21 const int kNumberOfSubchannels = 4; | 22 const int kNumberOfSubchannels = 4; |
| 22 | 23 |
| 23 // Chrome push messaging object IDs currently have the following format: | 24 // Chrome push messaging object IDs currently have the following format: |
| 24 // <format type>/<GAIA ID>/<extension ID>/<subchannel> | 25 // <format type>/<GAIA ID>/<extension ID>/<subchannel> |
| 25 // <format type> must be 'U', and <GAIA ID> is handled server-side so the client | 26 // <format type> must be 'U', and <GAIA ID> is handled server-side so the client |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 | 118 |
| 118 void PushMessagingInvalidationHandler::OnInvalidatorStateChange( | 119 void PushMessagingInvalidationHandler::OnInvalidatorStateChange( |
| 119 syncer::InvalidatorState state) { | 120 syncer::InvalidatorState state) { |
| 120 DCHECK(thread_checker_.CalledOnValidThread()); | 121 DCHECK(thread_checker_.CalledOnValidThread()); |
| 121 // Nothing to do. | 122 // Nothing to do. |
| 122 } | 123 } |
| 123 | 124 |
| 124 void PushMessagingInvalidationHandler::OnIncomingInvalidation( | 125 void PushMessagingInvalidationHandler::OnIncomingInvalidation( |
| 125 const syncer::ObjectIdInvalidationMap& invalidation_map) { | 126 const syncer::ObjectIdInvalidationMap& invalidation_map) { |
| 126 DCHECK(thread_checker_.CalledOnValidThread()); | 127 DCHECK(thread_checker_.CalledOnValidThread()); |
| 127 for (syncer::ObjectIdInvalidationMap::const_iterator it = | 128 invalidation_map.AcknowledgeAll(); |
| 128 invalidation_map.begin(); it != invalidation_map.end(); ++it) { | |
| 129 service_->AcknowledgeInvalidation(it->first, it->second.ack_handle); | |
| 130 | 129 |
| 131 syncer::ObjectIdSet::iterator suppressed_id = | 130 syncer::ObjectIdSet ids = invalidation_map.GetObjectIds(); |
| 132 suppressed_ids_.find(it->first); | 131 for (syncer::ObjectIdSet::const_iterator it = ids.begin(); |
| 132 it != ids.end(); ++it) { |
| 133 const syncer::OrderedInvalidationList& list = |
| 134 invalidation_map.ForObject(*it); |
| 135 const syncer::Invalidation& invalidation = list.back(); |
| 136 |
| 137 std::string payload; |
| 138 if (invalidation.IsUnknownVersion()) { |
| 139 payload = std::string(); |
| 140 } else { |
| 141 payload = list.back().GetPayload(); |
| 142 } |
| 143 |
| 144 syncer::ObjectIdSet::iterator suppressed_id = suppressed_ids_.find(*it); |
| 133 if (suppressed_id != suppressed_ids_.end()) { | 145 if (suppressed_id != suppressed_ids_.end()) { |
| 134 suppressed_ids_.erase(suppressed_id); | 146 suppressed_ids_.erase(suppressed_id); |
| 135 continue; | 147 continue; |
| 136 } | 148 } |
| 137 DVLOG(2) << "Incoming push message, id is: " | 149 DVLOG(2) << "Incoming push message, id is: " |
| 138 << syncer::ObjectIdToString(it->first) | 150 << syncer::ObjectIdToString(*it) |
| 139 << " and payload is:" << it->second.payload; | 151 << " and payload is:" << payload; |
| 140 | 152 |
| 141 std::string extension_id; | 153 std::string extension_id; |
| 142 int subchannel; | 154 int subchannel; |
| 143 if (ObjectIdToExtensionAndSubchannel(it->first, | 155 if (ObjectIdToExtensionAndSubchannel(*it, &extension_id, &subchannel)) { |
| 144 &extension_id, | 156 const syncer::OrderedInvalidationList& invalidation_list = |
| 145 &subchannel)) { | 157 invalidation_map.ForObject(*it); |
| 146 DVLOG(2) << "Sending push message to reciever, extension is " | 158 |
| 147 << extension_id << ", subchannel is " << subchannel | 159 // We always forward unknown version invalidation when we receive one. |
| 148 << ", and payload is " << it->second.payload; | 160 if (invalidation_list.StartsWithUnknownVersion()) { |
| 149 delegate_->OnMessage(extension_id, subchannel, it->second.payload); | 161 DVLOG(2) << "Sending push message to reciever, extension is " |
| 162 << extension_id << ", subchannel is " << subchannel |
| 163 << "and payload was lost"; |
| 164 delegate_->OnMessage(extension_id, subchannel, std::string()); |
| 165 } |
| 166 |
| 167 // If we receive a new max version for this object, forward its payload. |
| 168 const syncer::Invalidation& max_invalidation = invalidation_list.back(); |
| 169 if (!max_invalidation.IsUnknownVersion() && |
| 170 max_invalidation.GetVersion() > max_object_version_map_[*it]) { |
| 171 max_object_version_map_[*it] = max_invalidation.GetVersion(); |
| 172 DVLOG(2) << "Sending push message to reciever, extension is " |
| 173 << extension_id << ", subchannel is " << subchannel |
| 174 << ", and payload is " << max_invalidation.GetPayload(); |
| 175 delegate_->OnMessage(extension_id, |
| 176 subchannel, |
| 177 max_invalidation.GetPayload()); |
| 178 } |
| 150 } | 179 } |
| 151 } | 180 } |
| 152 } | 181 } |
| 153 | 182 |
| 154 void PushMessagingInvalidationHandler::UpdateRegistrations() { | 183 void PushMessagingInvalidationHandler::UpdateRegistrations() { |
| 155 syncer::ObjectIdSet ids; | 184 syncer::ObjectIdSet ids; |
| 156 for (std::set<std::string>::const_iterator it = | 185 for (std::set<std::string>::const_iterator it = |
| 157 registered_extensions_.begin(); it != registered_extensions_.end(); | 186 registered_extensions_.begin(); it != registered_extensions_.end(); |
| 158 ++it) { | 187 ++it) { |
| 159 const syncer::ObjectIdSet& object_ids = ExtensionIdToObjectIds(*it); | 188 const syncer::ObjectIdSet& object_ids = ExtensionIdToObjectIds(*it); |
| 160 ids.insert(object_ids.begin(), object_ids.end()); | 189 ids.insert(object_ids.begin(), object_ids.end()); |
| 161 } | 190 } |
| 162 service_->UpdateRegisteredInvalidationIds(this, ids); | 191 service_->UpdateRegisteredInvalidationIds(this, ids); |
| 163 } | 192 } |
| 164 | 193 |
| 165 } // namespace extensions | 194 } // namespace extensions |
| OLD | NEW |