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 #include <vector> |
| 9 |
| 10 #include "base/string_number_conversions.h" |
| 11 #include "base/string_split.h" |
| 12 #include "chrome/browser/extensions/api/push_messaging/push_messaging_invalidati
on_handler_delegate.h" |
| 13 #include "chrome/browser/sync/invalidation_frontend.h" |
| 14 #include "chrome/common/extensions/extension.h" |
| 15 #include "google/cacheinvalidation/types.pb.h" |
| 16 |
| 17 namespace extensions { |
| 18 |
| 19 namespace { |
| 20 |
| 21 const int kNumberOfSubchannels = 4; |
| 22 |
| 23 // Chrome push messaging object IDs currently have the following format: |
| 24 // <format type>/<GAIA ID>/<extension ID>/<subchannel> |
| 25 // <format type> must be 'U', and <GAIA ID> is handled server-side so the client |
| 26 // never sees it. |
| 27 syncer::ObjectIdSet ExtensionIdToObjectIds(const std::string& extension_id) { |
| 28 syncer::ObjectIdSet object_ids; |
| 29 for (int i = 0; i < kNumberOfSubchannels; ++i) { |
| 30 std::string name("U/"); |
| 31 name += extension_id; |
| 32 name += "/"; |
| 33 name += base::IntToString(i); |
| 34 // TODO(dcheng): CHROME_COMPONENTS is temporary, we need to update this once |
| 35 // we roll cacheinvalidation. |
| 36 object_ids.insert(invalidation::ObjectId( |
| 37 ipc::invalidation::ObjectSource::CHROME_COMPONENTS, |
| 38 name)); |
| 39 } |
| 40 return object_ids; |
| 41 } |
| 42 |
| 43 // Returns true iff the conversion was successful. |
| 44 bool ObjectIdToExtensionAndSubchannel(const invalidation::ObjectId& object_id, |
| 45 std::string* extension_id, |
| 46 int* subchannel) { |
| 47 // TODO(dcheng): CHROME_COMPONENTS is temporary, we need to update this once |
| 48 // we roll cacheinvalidation. |
| 49 if (object_id.source() != |
| 50 ipc::invalidation::ObjectSource::CHROME_COMPONENTS) { |
| 51 DLOG(WARNING) << "Invalid source: " << object_id.source(); |
| 52 return false; |
| 53 } |
| 54 |
| 55 const std::string& name = object_id.name(); |
| 56 std::vector<std::string> components; |
| 57 base::SplitStringDontTrim(name, '/', &components); |
| 58 if (components.size() < 3) { |
| 59 DLOG(WARNING) << "Invalid format type from object name " << name; |
| 60 return false; |
| 61 } |
| 62 if (components[0] != "U") { |
| 63 DLOG(WARNING) << "Invalid format type from object name " << name; |
| 64 return false; |
| 65 } |
| 66 if (!Extension::IdIsValid(components[1])) { |
| 67 DLOG(WARNING) << "Invalid extension ID from object name " << name; |
| 68 return false; |
| 69 } |
| 70 *extension_id = components[1]; |
| 71 if (!base::StringToInt(components[2], subchannel)) { |
| 72 DLOG(WARNING) << "Subchannel not a number from object name " << name; |
| 73 return false; |
| 74 } |
| 75 if (*subchannel < 0 || *subchannel >= kNumberOfSubchannels) { |
| 76 DLOG(WARNING) << "Subchannel out of range from object name " << name; |
| 77 return false; |
| 78 } |
| 79 return true; |
| 80 } |
| 81 |
| 82 } // namespace |
| 83 |
| 84 PushMessagingInvalidationHandler::PushMessagingInvalidationHandler( |
| 85 InvalidationFrontend* service, |
| 86 PushMessagingInvalidationHandlerDelegate* delegate, |
| 87 const std::set<std::string>& extension_ids) |
| 88 : service_(service), |
| 89 registered_extensions_(extension_ids), |
| 90 delegate_(delegate) { |
| 91 DCHECK(service_); |
| 92 service_->RegisterInvalidationHandler(this); |
| 93 UpdateRegistrations(); |
| 94 } |
| 95 |
| 96 PushMessagingInvalidationHandler::~PushMessagingInvalidationHandler() { |
| 97 DCHECK(thread_checker_.CalledOnValidThread()); |
| 98 service_->UnregisterInvalidationHandler(this); |
| 99 } |
| 100 |
| 101 void PushMessagingInvalidationHandler::RegisterExtension( |
| 102 const std::string& extension_id) { |
| 103 DCHECK(thread_checker_.CalledOnValidThread()); |
| 104 DCHECK(Extension::IdIsValid(extension_id)); |
| 105 registered_extensions_.insert(extension_id); |
| 106 UpdateRegistrations(); |
| 107 } |
| 108 |
| 109 void PushMessagingInvalidationHandler::UnregisterExtension( |
| 110 const std::string& extension_id) { |
| 111 DCHECK(thread_checker_.CalledOnValidThread()); |
| 112 DCHECK(Extension::IdIsValid(extension_id)); |
| 113 registered_extensions_.erase(extension_id); |
| 114 UpdateRegistrations(); |
| 115 } |
| 116 |
| 117 void PushMessagingInvalidationHandler::OnNotificationsEnabled() { |
| 118 DCHECK(thread_checker_.CalledOnValidThread()); |
| 119 // Nothing to do. |
| 120 } |
| 121 |
| 122 void PushMessagingInvalidationHandler::OnNotificationsDisabled( |
| 123 syncer::NotificationsDisabledReason reason) { |
| 124 DCHECK(thread_checker_.CalledOnValidThread()); |
| 125 // Nothing to do. |
| 126 } |
| 127 |
| 128 void PushMessagingInvalidationHandler::OnIncomingNotification( |
| 129 const syncer::ObjectIdPayloadMap& id_payloads, |
| 130 syncer::IncomingNotificationSource source) { |
| 131 DCHECK(thread_checker_.CalledOnValidThread()); |
| 132 for (syncer::ObjectIdPayloadMap::const_iterator it = id_payloads.begin(); |
| 133 it != id_payloads.end(); ++it) { |
| 134 std::string extension_id; |
| 135 int subchannel; |
| 136 if (ObjectIdToExtensionAndSubchannel(it->first, |
| 137 &extension_id, |
| 138 &subchannel)) { |
| 139 delegate_->OnMessage(extension_id, subchannel, it->second); |
| 140 } |
| 141 } |
| 142 } |
| 143 |
| 144 void PushMessagingInvalidationHandler::UpdateRegistrations() { |
| 145 syncer::ObjectIdSet ids; |
| 146 for (std::set<std::string>::const_iterator it = |
| 147 registered_extensions_.begin(); it != registered_extensions_.end(); |
| 148 ++it) { |
| 149 const syncer::ObjectIdSet& object_ids = ExtensionIdToObjectIds(*it); |
| 150 ids.insert(object_ids.begin(), object_ids.end()); |
| 151 } |
| 152 service_->UpdateRegisteredInvalidationIds(this, ids); |
| 153 } |
| 154 |
| 155 } // namespace extensions |
OLD | NEW |