Chromium Code Reviews| 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 "base/stringprintf.h" | |
| 13 #include "chrome/browser/extensions/api/push_messaging/push_messaging_invalidati on_handler_observer.h" | |
| 14 #include "chrome/browser/sync/invalidation_service.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) { | |
|
Munjal (Google)
2012/08/20 19:26:00
Nit: Should we pass in ObjectIdSet* instead of ret
akalin
2012/08/20 20:35:01
not really -- it's a microoptimization that's usua
dcheng
2012/08/20 20:58:21
Code that deals with ObjectIdSets (e.g. in sync) g
| |
| 28 syncer::ObjectIdSet object_ids; | |
| 29 for (int i = 0; i < kNumberOfSubchannels; ++i) { | |
| 30 std::string name = base::StringPrintf("U/%s/%d", extension_id.c_str(), i); | |
|
akalin
2012/08/20 20:35:01
Consider just doing:
const std::string& name = "U
dcheng
2012/08/20 20:58:21
Done.
| |
| 31 // TODO(dcheng): CHROME_COMPONENTS is temporary, we need to update this once | |
| 32 // we roll cacheinvalidation. | |
| 33 object_ids.insert(invalidation::ObjectId( | |
| 34 ipc::invalidation::ObjectSource::CHROME_COMPONENTS, | |
| 35 name)); | |
| 36 } | |
| 37 return object_ids; | |
| 38 } | |
| 39 | |
| 40 // Returns true iff the conversion was successful. | |
| 41 bool ObjectIdToExtensionAndSubchannel(const invalidation::ObjectId& object_id, | |
| 42 std::string* extension_id, | |
| 43 int* subchannel) { | |
| 44 // TODO(dcheng): CHROME_COMPONENTS is temporary, we need to update this once | |
| 45 // we roll cacheinvalidation. | |
| 46 if (object_id.source() != | |
| 47 ipc::invalidation::ObjectSource::CHROME_COMPONENTS) { | |
| 48 DLOG(WARNING) << "Invalid source: " << object_id.source(); | |
| 49 return false; | |
| 50 } | |
| 51 | |
| 52 const std::string& name = object_id.name(); | |
| 53 std::vector<std::string> components; | |
| 54 base::SplitStringDontTrim(name, '/', &components); | |
| 55 if (components[0] != "U") { | |
|
akalin
2012/08/20 20:35:01
check components size?
dcheng
2012/08/20 20:58:21
Oops. Done.
| |
| 56 DLOG(WARNING) << "Invalid format type from object name " << name; | |
| 57 return false; | |
| 58 } | |
| 59 if (components[1].size() != 32) { | |
|
akalin
2012/08/20 20:35:01
I think there's an IsExtensionIdValid() function s
dcheng
2012/08/20 20:58:21
Done.
| |
| 60 DLOG(WARNING) << "Invalid extension ID from object name " << name; | |
| 61 return false; | |
| 62 } | |
| 63 *extension_id = components[1]; | |
| 64 if (!base::StringToInt(components[2], subchannel)) { | |
| 65 DLOG(WARNING) << "Subchannel not a number from object name " << name; | |
| 66 return false; | |
| 67 } | |
| 68 if (*subchannel < 0 || *subchannel >= kNumberOfSubchannels) { | |
| 69 DLOG(WARNING) << "Subchannel out of range from object name " << name; | |
| 70 return false; | |
| 71 } | |
| 72 return true; | |
| 73 } | |
| 74 | |
| 75 } // namespace | |
| 76 | |
| 77 PushMessagingInvalidationHandler::PushMessagingInvalidationHandler( | |
| 78 InvalidationService* service, | |
| 79 PushMessagingInvalidationHandlerObserver* observer, | |
|
Munjal (Google)
2012/08/20 19:26:00
I just realized that "observer" is a more appropri
dcheng
2012/08/20 20:58:21
Done.
| |
| 80 const std::set<std::string>& extension_ids) | |
| 81 : service_(service), | |
| 82 registered_extensions_(extension_ids), | |
| 83 observer_(observer) { | |
| 84 DCHECK(service_); | |
| 85 service_->RegisterInvalidationHandler(this); | |
| 86 UpdateRegistrations(); | |
| 87 } | |
| 88 | |
| 89 PushMessagingInvalidationHandler::~PushMessagingInvalidationHandler() { | |
| 90 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 91 service_->UnregisterInvalidationHandler(this); | |
| 92 } | |
| 93 | |
| 94 void PushMessagingInvalidationHandler::RegisterExtension( | |
| 95 const std::string& extension_id) { | |
| 96 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 97 DCHECK_EQ(32U, extension_id.size()); | |
|
Munjal (Google)
2012/08/20 19:26:00
There are few places in this file which use 32 as
akalin
2012/08/20 20:35:01
probably should just use IsExtensionIdValid()
dcheng
2012/08/20 20:58:21
Using Extension::IdIsValid() here and several othe
| |
| 98 registered_extensions_.insert(extension_id); | |
| 99 UpdateRegistrations(); | |
| 100 } | |
| 101 | |
| 102 void PushMessagingInvalidationHandler::UnregisterExtension( | |
| 103 const std::string& extension_id) { | |
| 104 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 105 DCHECK_EQ(32U, extension_id.size()); | |
|
akalin
2012/08/20 20:35:01
here too
dcheng
2012/08/20 20:58:21
Done.
| |
| 106 registered_extensions_.erase(extension_id); | |
| 107 UpdateRegistrations(); | |
| 108 } | |
| 109 | |
| 110 void PushMessagingInvalidationHandler::OnNotificationsEnabled() { | |
| 111 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 112 // Nothing to do. | |
| 113 } | |
| 114 | |
| 115 void PushMessagingInvalidationHandler::OnNotificationsDisabled( | |
| 116 syncer::NotificationsDisabledReason reason) { | |
| 117 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 118 // Nothing to do. | |
| 119 } | |
| 120 | |
| 121 void PushMessagingInvalidationHandler::OnIncomingNotification( | |
| 122 const syncer::ObjectIdPayloadMap& id_payloads, | |
| 123 syncer::IncomingNotificationSource source) { | |
| 124 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 125 for (syncer::ObjectIdPayloadMap::const_iterator it = id_payloads.begin(); | |
| 126 it != id_payloads.end(); ++it) { | |
| 127 std::string extension_id; | |
| 128 int subchannel; | |
| 129 if (ObjectIdToExtensionAndSubchannel(it->first, | |
| 130 &extension_id, | |
| 131 &subchannel)) { | |
| 132 observer_->OnMessage(extension_id, subchannel, it->second); | |
| 133 } | |
| 134 } | |
| 135 } | |
| 136 | |
| 137 void PushMessagingInvalidationHandler::UpdateRegistrations() { | |
| 138 syncer::ObjectIdSet ids; | |
| 139 for (std::set<std::string>::const_iterator it = | |
| 140 registered_extensions_.begin(); it != registered_extensions_.end(); | |
|
akalin
2012/08/20 20:35:01
i usually try to break after semicolons
dcheng
2012/08/20 20:58:21
It doesn't fit on one line.
<embarrassed look>
| |
| 141 ++it) { | |
| 142 const syncer::ObjectIdSet& object_ids = ExtensionIdToObjectIds(*it); | |
| 143 ids.insert(object_ids.begin(), object_ids.end()); | |
| 144 } | |
| 145 service_->UpdateRegisteredInvalidationIds(this, ids); | |
| 146 } | |
| 147 | |
| 148 } // namespace extensions | |
| OLD | NEW |