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..c2df96a38d4723754716f1c8fe1affb9bfe81c28 |
--- /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 "base/stringprintf.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; |
+ |
Pete Williamson
2012/08/13 18:24:09
Nit - the "kXxxIndex" constants below might be a b
dcheng
2012/08/13 20:44:14
It's also the index into a string. I named it "Ind
|
+const int kFormatTypeIndex = 0; |
+const int kFormatTypeLength = 1; |
+const int kProjectIdIndex = 1; |
+const int kProjectIdLength = 2; |
+const int kExtensionIdIndex = 3; |
+const int kExtensionIdLength = 32; |
+const int kSubchannelIndex = 35; |
+const int kSubchannelLength = 1; |
+ |
+// 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. |
+syncer::ObjectIdSet ExtensionIdToObjectIds(const std::string& extension_id) { |
+ syncer::ObjectIdSet object_ids; |
+ for (int i = 0; i < kNumberOfSubchannels; ++i) { |
+ std::string name = base::StringPrintf("UPM%s%d", extension_id.c_str(), 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 = kFormatTypeLength + kProjectIdLength + |
+ kExtensionIdLength + kSubchannelLength; |
+ if (name.size() != kValidObjectIdLength) { |
+ DLOG(WARNING) << "Invalid length for object name " << name; |
+ return false; |
+ } |
Pete Williamson
2012/08/13 18:24:09
Might be good to add a comment here:
// TODO: Modi
dcheng
2012/08/13 20:44:14
I avoided making that comment since that would req
|
+ if (name.compare(kFormatTypeIndex, kFormatTypeLength, "U")) { |
+ DLOG(WARNING) << "Invalid format type for object name " << name; |
+ return false; |
+ } |
Pete Williamson
2012/08/13 18:24:09
Does this "PM" need to be registered somewhere so
dcheng
2012/08/13 20:44:14
This is an ad-hoc registration. We rely on good ci
|
+ if (name.compare(kProjectIdIndex, kProjectIdLength, "PM")) { |
+ DLOG(WARNING) << "Invalid project ID for object name " << name; |
+ return false; |
+ } |
+ *extension_id = name.substr(kExtensionIdIndex, kExtensionIdLength); |
+ if (!base::StringToInt(name.substr(kSubchannelIndex, kSubchannelLength), |
+ 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 |
+ |
Pete Williamson
2012/08/13 18:24:09
Coding standards question: I personally think it
dcheng
2012/08/13 20:44:14
I've updated the header with comments about the li
|
+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_); |
+} |
+ |
Pete Williamson
2012/08/13 18:24:09
Is this removing the registration when the DTOR fi
dcheng
2012/08/13 20:44:14
I've rebased to akalin's latest set of changes. It
|
+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. |
Pete Williamson
2012/08/13 18:24:09
Should we DCHECK(false); here since this shouldn't
dcheng
2012/08/13 20:44:14
That just means that sync has been disabled. In th
|
+} |
+ |
+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 |