Chromium Code Reviews| 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..8e956c733a6b275170c18b7dc56038c1c341c694 |
| --- /dev/null |
| +++ b/chrome/browser/extensions/api/push_messaging/push_messaging_invalidation_handler.cc |
| @@ -0,0 +1,151 @@ |
| +// 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) { |
|
Pete Williamson
2012/08/20 22:41:05
Should we add unit tests for this and for ObjectId
dcheng
2012/08/20 22:45:28
This is tested through the RegisterUnregister and
|
| + 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[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 |