Index: chrome/browser/extensions/api/push_messaging/push_messaging_invalidation_handler.cc |
diff --git a/chrome/browser/extensions/api/push_messaging/push_messaging_invalidation_handler.cc b/chrome/browser/extensions/api/push_messaging/push_messaging_invalidation_handler.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f52f49a1ddd84ac2bd18a19241fa905bd2b940d8 |
--- /dev/null |
+++ b/chrome/browser/extensions/api/push_messaging/push_messaging_invalidation_handler.cc |
@@ -0,0 +1,162 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/extensions/api/push_messaging/push_messaging_invalidation_handler.h" |
+ |
+#include <algorithm> |
+ |
+#include "base/string_number_conversions.h" |
+#include "chrome/browser/extensions/api/push_messaging/push_messaging_invalidation_handler_observer.h" |
+#include "chrome/browser/sync/invalidation_service.h" |
+#include "google/cacheinvalidation/types.pb.h" |
+ |
+namespace extensions { |
+ |
+namespace { |
+ |
+const int kNumberOfSubchannels = 4; |
+ |
+// Chrome Components object IDs currently have the following format: |
+// <1 byte format type> |
+// Currently the only format type defined is 'U' but we'll need to create a |
+// new one. |
+// <8 byte GAIA ID> |
+// Handled entirely server-side. The client never see this. |
+// <2 byte project ID> |
+// We use "PM" for Push Messaging. |
+// <4-48 byte project object ID> |
+// We use the extension ID concatenated with the subchannel. |
+// TODO(dcheng): This serialization/deserialization is pretty ugly. Factor out |
+// shared constants and clean it up. |
+syncer::ObjectIdSet ExtensionIdToObjectIds(const std::string& extension_id) { |
+ syncer::ObjectIdSet object_ids; |
+ for (int i = 0; i < kNumberOfSubchannels; ++i) { |
+ std::string name; |
+ name += 'U'; |
+ name += "PM"; |
+ name += extension_id; |
+ name += base::IntToString(i); |
+ object_ids.insert(invalidation::ObjectId( |
+ ipc::invalidation::ObjectSource::CHROME_COMPONENTS, |
+ name)); |
+ } |
+ return object_ids; |
+} |
+ |
+// Returns true iff the conversion was successful. |
+bool ObjectIdToExtensionAndSubchannel(const invalidation::ObjectId& object_id, |
+ std::string* extension_id, |
+ int* subchannel) { |
+ if (object_id.source() != |
+ ipc::invalidation::ObjectSource::CHROME_COMPONENTS) { |
+ DLOG(WARNING) << "Invalid source: " << object_id.source(); |
+ return false; |
+ } |
+ |
+ const std::string& name = object_id.name(); |
+ const size_t kValidObjectIdLength = |
+ 1 + // format type |
+ 2 + // project ID |
+ 32 + // length of extension ID |
+ 1; // length of subchannel as a string. |
+ if (name.size() != kValidObjectIdLength) { |
+ DLOG(WARNING) << "Invalid length for object name " << name; |
+ return false; |
+ } |
+ if (name.compare(0, 1, "U")) { |
+ DLOG(WARNING) << "Invalid format type for object name " << name; |
+ return false; |
+ } |
+ if (name.compare(1, 2, "PM")) { |
+ DLOG(WARNING) << "Invalid project ID for object name " << name; |
+ return false; |
+ } |
+ // TODO(dcheng): We should check that the extension ID in question is actually |
+ // installed. |
+ *extension_id = name.substr(3, 32); |
+ if (!base::StringToInt(name.substr(35), subchannel)) { |
+ DLOG(WARNING) << "Subchannel not a number for object name " << name; |
+ return false; |
+ } |
+ |
+ // We don't need to check for negative since the length check makes it |
+ // impossible. |
+ return *subchannel <= 3; |
+} |
+ |
+} // namespace |
+ |
+PushMessagingInvalidationHandler::PushMessagingInvalidationHandler( |
+ InvalidationService* service, |
+ PushMessagingInvalidationHandlerObserver* observer, |
+ const std::set<std::string>& extension_ids) |
+ : service_(service), |
+ observer_(observer) { |
+ DCHECK(service_); |
+ |
+ for (std::set<std::string>::const_iterator it = extension_ids.begin(); |
+ it != extension_ids.end(); ++it) { |
+ const syncer::ObjectIdSet& object_ids = ExtensionIdToObjectIds(*it); |
+ registered_ids_.insert(object_ids.begin(), object_ids.end()); |
+ } |
+ service_->UpdateRegisteredInvalidationIds(this, registered_ids_); |
+} |
+ |
+PushMessagingInvalidationHandler::~PushMessagingInvalidationHandler() { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ service_->UpdateRegisteredInvalidationIds(this, syncer::ObjectIdSet()); |
+} |
+ |
+void PushMessagingInvalidationHandler::RegisterExtension( |
+ const std::string& extension_id) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ DCHECK_EQ(32U, extension_id.size()); |
+ const syncer::ObjectIdSet& object_ids = ExtensionIdToObjectIds(extension_id); |
+ registered_ids_.insert(object_ids.begin(), object_ids.end()); |
+ service_->UpdateRegisteredInvalidationIds(this, registered_ids_); |
+} |
+ |
+void PushMessagingInvalidationHandler::UnregisterExtension( |
+ const std::string& extension_id) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ DCHECK_EQ(32U, extension_id.size()); |
+ const syncer::ObjectIdSet& object_ids = ExtensionIdToObjectIds(extension_id); |
+ syncer::ObjectIdSet updated_registered_ids; |
+ std::set_difference(registered_ids_.begin(), registered_ids_.end(), |
+ object_ids.begin(), object_ids.end(), |
+ std::inserter(updated_registered_ids, |
+ updated_registered_ids.begin()), |
+ syncer::ObjectIdLessThan()); |
+ registered_ids_.swap(updated_registered_ids); |
+ service_->UpdateRegisteredInvalidationIds(this, registered_ids_); |
+} |
+ |
+void PushMessagingInvalidationHandler::OnNotificationsEnabled() { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ // Nothing to do. |
+} |
+ |
+void PushMessagingInvalidationHandler::OnNotificationsDisabled( |
+ syncer::NotificationsDisabledReason reason) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ // Nothing to do. |
+} |
+ |
+void PushMessagingInvalidationHandler::OnIncomingNotification( |
+ const syncer::ObjectIdPayloadMap& id_payloads, |
+ syncer::IncomingNotificationSource source) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ for (syncer::ObjectIdPayloadMap::const_iterator it = id_payloads.begin(); |
+ it != id_payloads.end(); ++it) { |
+ std::string extension_id; |
+ int subchannel; |
+ if (ObjectIdToExtensionAndSubchannel(it->first, |
+ &extension_id, |
+ &subchannel)) { |
+ observer_->OnMessage(extension_id, subchannel, it->second); |
+ } |
+ } |
+} |
+ |
+} // namespace extensions |