Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4683)

Unified Diff: chrome/browser/extensions/api/push_messaging/push_messaging_invalidation_handler.cc

Issue 10826156: Plumb invalidations from Tango to the extensions code for the Push Messaging API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Make pushMessaging non-optional Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..b5d4131d6f89de82c38eabacc01d9086bfa2dd57
--- /dev/null
+++ b/chrome/browser/extensions/api/push_messaging/push_messaging_invalidation_handler.cc
@@ -0,0 +1,164 @@
+// 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;
+
+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);
Munjal (Google) 2012/08/14 18:56:37 How about using some separator (e.g. |) in the obj
dcheng 2012/08/14 21:39:59 The Chrome components format didn't allow for non-
+ 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;
+ }
+ if (name.compare(kFormatTypeIndex, kFormatTypeLength, "U")) {
+ DLOG(WARNING) << "Invalid format type for object name " << name;
+ return false;
+ }
+ 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;
Munjal (Google) 2012/08/14 18:56:37 The logic in this method will be much more robust
dcheng 2012/08/14 21:39:59 See above.
+}
+
+} // namespace
+
+PushMessagingInvalidationHandler::PushMessagingInvalidationHandler(
+ InvalidationService* service,
+ PushMessagingInvalidationHandlerObserver* observer,
+ const std::set<std::string>& extension_ids)
+ : service_(service),
+ observer_(observer) {
+ DCHECK(service_);
+
+ service_->RegisterInvalidationHandler(this);
+
+ 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_->UnregisterInvalidationHandler(this);
+}
+
+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_);
Munjal (Google) 2012/08/14 18:56:37 If we are keeping track of registered_ids_ just to
dcheng 2012/08/14 21:39:59 Done.
+}
+
+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

Powered by Google App Engine
This is Rietveld 408576698