OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/extensions/api/push_messaging/push_messaging_invalidati
on_handler.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "base/string_number_conversions.h" |
| 10 #include "base/stringprintf.h" |
| 11 #include "chrome/browser/extensions/api/push_messaging/push_messaging_invalidati
on_handler_observer.h" |
| 12 #include "chrome/browser/sync/invalidation_service.h" |
| 13 #include "google/cacheinvalidation/types.pb.h" |
| 14 |
| 15 namespace extensions { |
| 16 |
| 17 namespace { |
| 18 |
| 19 const int kNumberOfSubchannels = 4; |
| 20 |
| 21 const int kFormatTypeIndex = 0; |
| 22 const int kFormatTypeLength = 1; |
| 23 const int kProjectIdIndex = 1; |
| 24 const int kProjectIdLength = 2; |
| 25 const int kExtensionIdIndex = 3; |
| 26 const int kExtensionIdLength = 32; |
| 27 const int kSubchannelIndex = 35; |
| 28 const int kSubchannelLength = 1; |
| 29 |
| 30 // Chrome Components object IDs currently have the following format: |
| 31 // <1 byte format type> |
| 32 // Currently the only format type defined is 'U' but we'll need to create a |
| 33 // new one. |
| 34 // <8 byte GAIA ID> |
| 35 // Handled entirely server-side. The client never see this. |
| 36 // <2 byte project ID> |
| 37 // We use "PM" for Push Messaging. |
| 38 // <4-48 byte project object ID> |
| 39 // We use the extension ID concatenated with the subchannel. |
| 40 syncer::ObjectIdSet ExtensionIdToObjectIds(const std::string& extension_id) { |
| 41 syncer::ObjectIdSet object_ids; |
| 42 for (int i = 0; i < kNumberOfSubchannels; ++i) { |
| 43 std::string name = base::StringPrintf("UPM%s%d", extension_id.c_str(), i); |
| 44 object_ids.insert(invalidation::ObjectId( |
| 45 ipc::invalidation::ObjectSource::CHROME_COMPONENTS, |
| 46 name)); |
| 47 } |
| 48 return object_ids; |
| 49 } |
| 50 |
| 51 // Returns true iff the conversion was successful. |
| 52 bool ObjectIdToExtensionAndSubchannel(const invalidation::ObjectId& object_id, |
| 53 std::string* extension_id, |
| 54 int* subchannel) { |
| 55 if (object_id.source() != |
| 56 ipc::invalidation::ObjectSource::CHROME_COMPONENTS) { |
| 57 DLOG(WARNING) << "Invalid source: " << object_id.source(); |
| 58 return false; |
| 59 } |
| 60 |
| 61 const std::string& name = object_id.name(); |
| 62 const size_t kValidObjectIdLength = kFormatTypeLength + kProjectIdLength + |
| 63 kExtensionIdLength + kSubchannelLength; |
| 64 if (name.size() != kValidObjectIdLength) { |
| 65 DLOG(WARNING) << "Invalid length for object name " << name; |
| 66 return false; |
| 67 } |
| 68 if (name.compare(kFormatTypeIndex, kFormatTypeLength, "U")) { |
| 69 DLOG(WARNING) << "Invalid format type for object name " << name; |
| 70 return false; |
| 71 } |
| 72 if (name.compare(kProjectIdIndex, kProjectIdLength, "PM")) { |
| 73 DLOG(WARNING) << "Invalid project ID for object name " << name; |
| 74 return false; |
| 75 } |
| 76 *extension_id = name.substr(kExtensionIdIndex, kExtensionIdLength); |
| 77 if (!base::StringToInt(name.substr(kSubchannelIndex, kSubchannelLength), |
| 78 subchannel)) { |
| 79 DLOG(WARNING) << "Subchannel not a number for object name " << name; |
| 80 return false; |
| 81 } |
| 82 |
| 83 // We don't need to check for negative since the length check makes it |
| 84 // impossible. |
| 85 return *subchannel <= 3; |
| 86 } |
| 87 |
| 88 } // namespace |
| 89 |
| 90 PushMessagingInvalidationHandler::PushMessagingInvalidationHandler( |
| 91 InvalidationService* service, |
| 92 PushMessagingInvalidationHandlerObserver* observer, |
| 93 const std::set<std::string>& extension_ids) |
| 94 : service_(service), |
| 95 registered_extensions_(extension_ids), |
| 96 observer_(observer) { |
| 97 DCHECK(service_); |
| 98 service_->RegisterInvalidationHandler(this); |
| 99 UpdateRegistrations(); |
| 100 } |
| 101 |
| 102 PushMessagingInvalidationHandler::~PushMessagingInvalidationHandler() { |
| 103 DCHECK(thread_checker_.CalledOnValidThread()); |
| 104 service_->UnregisterInvalidationHandler(this); |
| 105 } |
| 106 |
| 107 void PushMessagingInvalidationHandler::RegisterExtension( |
| 108 const std::string& extension_id) { |
| 109 DCHECK(thread_checker_.CalledOnValidThread()); |
| 110 DCHECK_EQ(32U, extension_id.size()); |
| 111 registered_extensions_.insert(extension_id); |
| 112 UpdateRegistrations(); |
| 113 } |
| 114 |
| 115 void PushMessagingInvalidationHandler::UnregisterExtension( |
| 116 const std::string& extension_id) { |
| 117 DCHECK(thread_checker_.CalledOnValidThread()); |
| 118 DCHECK_EQ(32U, extension_id.size()); |
| 119 registered_extensions_.erase(extension_id); |
| 120 UpdateRegistrations(); |
| 121 } |
| 122 |
| 123 void PushMessagingInvalidationHandler::OnNotificationsEnabled() { |
| 124 DCHECK(thread_checker_.CalledOnValidThread()); |
| 125 // Nothing to do. |
| 126 } |
| 127 |
| 128 void PushMessagingInvalidationHandler::OnNotificationsDisabled( |
| 129 syncer::NotificationsDisabledReason reason) { |
| 130 DCHECK(thread_checker_.CalledOnValidThread()); |
| 131 // Nothing to do. |
| 132 } |
| 133 |
| 134 void PushMessagingInvalidationHandler::OnIncomingNotification( |
| 135 const syncer::ObjectIdPayloadMap& id_payloads, |
| 136 syncer::IncomingNotificationSource source) { |
| 137 DCHECK(thread_checker_.CalledOnValidThread()); |
| 138 for (syncer::ObjectIdPayloadMap::const_iterator it = id_payloads.begin(); |
| 139 it != id_payloads.end(); ++it) { |
| 140 std::string extension_id; |
| 141 int subchannel; |
| 142 if (ObjectIdToExtensionAndSubchannel(it->first, |
| 143 &extension_id, |
| 144 &subchannel)) { |
| 145 observer_->OnMessage(extension_id, subchannel, it->second); |
| 146 } |
| 147 } |
| 148 } |
| 149 |
| 150 void PushMessagingInvalidationHandler::UpdateRegistrations() { |
| 151 syncer::ObjectIdSet ids; |
| 152 for (std::set<std::string>::const_iterator it = |
| 153 registered_extensions_.begin(); it != registered_extensions_.end(); |
| 154 ++it) { |
| 155 const syncer::ObjectIdSet& object_ids = ExtensionIdToObjectIds(*it); |
| 156 ids.insert(object_ids.begin(), object_ids.end()); |
| 157 } |
| 158 service_->UpdateRegisteredInvalidationIds(this, ids); |
| 159 } |
| 160 |
| 161 } // namespace extensions |
OLD | NEW |