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 "chrome/browser/extensions/api/push_messaging/push_messaging_invalidati
on_handler_observer.h" |
| 11 #include "chrome/browser/sync/invalidation_service.h" |
| 12 #include "google/cacheinvalidation/types.pb.h" |
| 13 |
| 14 namespace extensions { |
| 15 |
| 16 namespace { |
| 17 |
| 18 const int kNumberOfSubchannels = 4; |
| 19 |
| 20 // Chrome Components object IDs currently have the following format: |
| 21 // <1 byte format type> |
| 22 // Currently the only format type defined is 'U' but we'll need to create a |
| 23 // new one. |
| 24 // <8 byte GAIA ID> |
| 25 // Handled entirely server-side. The client never see this. |
| 26 // <2 byte project ID> |
| 27 // We use "PM" for Push Messaging. |
| 28 // <4-48 byte project object ID> |
| 29 // We use the extension ID concatenated with the subchannel. |
| 30 // TODO(dcheng): This serialization/deserialization is pretty ugly. Factor out |
| 31 // shared constants and clean it up. |
| 32 syncer::ObjectIdSet ExtensionIdToObjectIds(const std::string& extension_id) { |
| 33 syncer::ObjectIdSet object_ids; |
| 34 for (int i = 0; i < kNumberOfSubchannels; ++i) { |
| 35 std::string name; |
| 36 name += 'U'; |
| 37 name += "PM"; |
| 38 name += extension_id; |
| 39 name += base::IntToString(i); |
| 40 object_ids.insert(invalidation::ObjectId( |
| 41 ipc::invalidation::ObjectSource::CHROME_COMPONENTS, |
| 42 name)); |
| 43 } |
| 44 return object_ids; |
| 45 } |
| 46 |
| 47 // Returns true iff the conversion was successful. |
| 48 bool ObjectIdToExtensionAndSubchannel(const invalidation::ObjectId& object_id, |
| 49 std::string* extension_id, |
| 50 int* subchannel) { |
| 51 if (object_id.source() != |
| 52 ipc::invalidation::ObjectSource::CHROME_COMPONENTS) { |
| 53 DLOG(WARNING) << "Invalid source: " << object_id.source(); |
| 54 return false; |
| 55 } |
| 56 |
| 57 const std::string& name = object_id.name(); |
| 58 const size_t kValidObjectIdLength = |
| 59 1 + // format type |
| 60 2 + // project ID |
| 61 32 + // length of extension ID |
| 62 1; // length of subchannel as a string. |
| 63 if (name.size() != kValidObjectIdLength) { |
| 64 DLOG(WARNING) << "Invalid length for object name " << name; |
| 65 return false; |
| 66 } |
| 67 if (name.compare(0, 1, "U")) { |
| 68 DLOG(WARNING) << "Invalid format type for object name " << name; |
| 69 return false; |
| 70 } |
| 71 if (name.compare(1, 2, "PM")) { |
| 72 DLOG(WARNING) << "Invalid project ID for object name " << name; |
| 73 return false; |
| 74 } |
| 75 // TODO(dcheng): We should check that the extension ID in question is actually |
| 76 // installed. |
| 77 *extension_id = name.substr(3, 32); |
| 78 if (!base::StringToInt(name.substr(35), 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 observer_(observer) { |
| 96 DCHECK(service_); |
| 97 |
| 98 for (std::set<std::string>::const_iterator it = extension_ids.begin(); |
| 99 it != extension_ids.end(); ++it) { |
| 100 const syncer::ObjectIdSet& object_ids = ExtensionIdToObjectIds(*it); |
| 101 registered_ids_.insert(object_ids.begin(), object_ids.end()); |
| 102 } |
| 103 service_->UpdateRegisteredInvalidationIds(this, registered_ids_); |
| 104 } |
| 105 |
| 106 PushMessagingInvalidationHandler::~PushMessagingInvalidationHandler() { |
| 107 DCHECK(thread_checker_.CalledOnValidThread()); |
| 108 service_->UpdateRegisteredInvalidationIds(this, syncer::ObjectIdSet()); |
| 109 } |
| 110 |
| 111 void PushMessagingInvalidationHandler::RegisterExtension( |
| 112 const std::string& extension_id) { |
| 113 DCHECK(thread_checker_.CalledOnValidThread()); |
| 114 DCHECK_EQ(32U, extension_id.size()); |
| 115 const syncer::ObjectIdSet& object_ids = ExtensionIdToObjectIds(extension_id); |
| 116 registered_ids_.insert(object_ids.begin(), object_ids.end()); |
| 117 service_->UpdateRegisteredInvalidationIds(this, registered_ids_); |
| 118 } |
| 119 |
| 120 void PushMessagingInvalidationHandler::UnregisterExtension( |
| 121 const std::string& extension_id) { |
| 122 DCHECK(thread_checker_.CalledOnValidThread()); |
| 123 DCHECK_EQ(32U, extension_id.size()); |
| 124 const syncer::ObjectIdSet& object_ids = ExtensionIdToObjectIds(extension_id); |
| 125 syncer::ObjectIdSet updated_registered_ids; |
| 126 std::set_difference(registered_ids_.begin(), registered_ids_.end(), |
| 127 object_ids.begin(), object_ids.end(), |
| 128 std::inserter(updated_registered_ids, |
| 129 updated_registered_ids.begin()), |
| 130 syncer::ObjectIdLessThan()); |
| 131 registered_ids_.swap(updated_registered_ids); |
| 132 service_->UpdateRegisteredInvalidationIds(this, registered_ids_); |
| 133 } |
| 134 |
| 135 void PushMessagingInvalidationHandler::OnNotificationsEnabled() { |
| 136 DCHECK(thread_checker_.CalledOnValidThread()); |
| 137 // Nothing to do. |
| 138 } |
| 139 |
| 140 void PushMessagingInvalidationHandler::OnNotificationsDisabled( |
| 141 syncer::NotificationsDisabledReason reason) { |
| 142 DCHECK(thread_checker_.CalledOnValidThread()); |
| 143 // Nothing to do. |
| 144 } |
| 145 |
| 146 void PushMessagingInvalidationHandler::OnIncomingNotification( |
| 147 const syncer::ObjectIdPayloadMap& id_payloads, |
| 148 syncer::IncomingNotificationSource source) { |
| 149 DCHECK(thread_checker_.CalledOnValidThread()); |
| 150 for (syncer::ObjectIdPayloadMap::const_iterator it = id_payloads.begin(); |
| 151 it != id_payloads.end(); ++it) { |
| 152 std::string extension_id; |
| 153 int subchannel; |
| 154 if (ObjectIdToExtensionAndSubchannel(it->first, |
| 155 &extension_id, |
| 156 &subchannel)) { |
| 157 observer_->OnMessage(extension_id, subchannel, it->second); |
| 158 } |
| 159 } |
| 160 } |
| 161 |
| 162 } // namespace extensions |
OLD | NEW |