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

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: Fix Android build 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..1b45bd1b319f0214503950c4a6a8fc11143f7d22
--- /dev/null
+++ b/chrome/browser/extensions/api/push_messaging/push_messaging_invalidation_handler.cc
@@ -0,0 +1,155 @@
+// 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 "chrome/browser/extensions/api/push_messaging/push_messaging_invalidation_handler_delegate.h"
+#include "chrome/browser/sync/invalidation_frontend.h"
+#include "chrome/common/extensions/extension.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) {
+ syncer::ObjectIdSet object_ids;
+ for (int i = 0; i < kNumberOfSubchannels; ++i) {
+ std::string name("U/");
+ name += extension_id;
+ name += "/";
+ name += base::IntToString(i);
+ // 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.size() < 3) {
+ DLOG(WARNING) << "Invalid format type from object name " << name;
+ return false;
+ }
+ if (components[0] != "U") {
+ DLOG(WARNING) << "Invalid format type from object name " << name;
+ return false;
+ }
+ if (!Extension::IdIsValid(components[1])) {
+ 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(
+ InvalidationFrontend* service,
+ PushMessagingInvalidationHandlerDelegate* delegate,
+ const std::set<std::string>& extension_ids)
+ : service_(service),
+ registered_extensions_(extension_ids),
+ delegate_(delegate) {
+ 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(Extension::IdIsValid(extension_id));
+ registered_extensions_.insert(extension_id);
+ UpdateRegistrations();
+}
+
+void PushMessagingInvalidationHandler::UnregisterExtension(
+ const std::string& extension_id) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ DCHECK(Extension::IdIsValid(extension_id));
+ 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)) {
+ delegate_->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();
+ ++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