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

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: Delimiter + restart test 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..a238f79548cf37d84b28d0f153ae398645e0884d
--- /dev/null
+++ b/chrome/browser/extensions/api/push_messaging/push_messaging_invalidation_handler.cc
@@ -0,0 +1,148 @@
+// 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 <vector>
+
+#include "base/string_number_conversions.h"
+#include "base/string_split.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;
+
+// Chrome push messaging object IDs currently have the following format:
+// <format type>/<GAIA ID>/<extension ID>/<subchannel>
+// <format type> must be 'U', and <GAIA ID> is handled server-side so the client
+// never sees it.
+syncer::ObjectIdSet ExtensionIdToObjectIds(const std::string& extension_id) {
Munjal (Google) 2012/08/20 19:26:00 Nit: Should we pass in ObjectIdSet* instead of ret
akalin 2012/08/20 20:35:01 not really -- it's a microoptimization that's usua
dcheng 2012/08/20 20:58:21 Code that deals with ObjectIdSets (e.g. in sync) g
+ syncer::ObjectIdSet object_ids;
+ for (int i = 0; i < kNumberOfSubchannels; ++i) {
+ std::string name = base::StringPrintf("U/%s/%d", extension_id.c_str(), i);
akalin 2012/08/20 20:35:01 Consider just doing: const std::string& name = "U
dcheng 2012/08/20 20:58:21 Done.
+ // TODO(dcheng): CHROME_COMPONENTS is temporary, we need to update this once
+ // we roll cacheinvalidation.
+ 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) {
+ // TODO(dcheng): CHROME_COMPONENTS is temporary, we need to update this once
+ // we roll cacheinvalidation.
+ 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();
+ std::vector<std::string> components;
+ base::SplitStringDontTrim(name, '/', &components);
+ if (components[0] != "U") {
akalin 2012/08/20 20:35:01 check components size?
dcheng 2012/08/20 20:58:21 Oops. Done.
+ DLOG(WARNING) << "Invalid format type from object name " << name;
+ return false;
+ }
+ if (components[1].size() != 32) {
akalin 2012/08/20 20:35:01 I think there's an IsExtensionIdValid() function s
dcheng 2012/08/20 20:58:21 Done.
+ DLOG(WARNING) << "Invalid extension ID from object name " << name;
+ return false;
+ }
+ *extension_id = components[1];
+ if (!base::StringToInt(components[2], subchannel)) {
+ DLOG(WARNING) << "Subchannel not a number from object name " << name;
+ return false;
+ }
+ if (*subchannel < 0 || *subchannel >= kNumberOfSubchannels) {
+ DLOG(WARNING) << "Subchannel out of range from object name " << name;
+ return false;
+ }
+ return true;
+}
+
+} // namespace
+
+PushMessagingInvalidationHandler::PushMessagingInvalidationHandler(
+ InvalidationService* service,
+ PushMessagingInvalidationHandlerObserver* observer,
Munjal (Google) 2012/08/20 19:26:00 I just realized that "observer" is a more appropri
dcheng 2012/08/20 20:58:21 Done.
+ const std::set<std::string>& extension_ids)
+ : service_(service),
+ registered_extensions_(extension_ids),
+ observer_(observer) {
+ DCHECK(service_);
+ service_->RegisterInvalidationHandler(this);
+ UpdateRegistrations();
+}
+
+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());
Munjal (Google) 2012/08/20 19:26:00 There are few places in this file which use 32 as
akalin 2012/08/20 20:35:01 probably should just use IsExtensionIdValid()
dcheng 2012/08/20 20:58:21 Using Extension::IdIsValid() here and several othe
+ registered_extensions_.insert(extension_id);
+ UpdateRegistrations();
+}
+
+void PushMessagingInvalidationHandler::UnregisterExtension(
+ const std::string& extension_id) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ DCHECK_EQ(32U, extension_id.size());
akalin 2012/08/20 20:35:01 here too
dcheng 2012/08/20 20:58:21 Done.
+ registered_extensions_.erase(extension_id);
+ UpdateRegistrations();
+}
+
+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);
+ }
+ }
+}
+
+void PushMessagingInvalidationHandler::UpdateRegistrations() {
+ syncer::ObjectIdSet ids;
+ for (std::set<std::string>::const_iterator it =
+ registered_extensions_.begin(); it != registered_extensions_.end();
akalin 2012/08/20 20:35:01 i usually try to break after semicolons
dcheng 2012/08/20 20:58:21 It doesn't fit on one line. <embarrassed look>
+ ++it) {
+ const syncer::ObjectIdSet& object_ids = ExtensionIdToObjectIds(*it);
+ ids.insert(object_ids.begin(), object_ids.end());
+ }
+ service_->UpdateRegisteredInvalidationIds(this, ids);
+}
+
+} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698