Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(671)

Side by Side Diff: chrome/browser/extensions/api/push_messaging/push_messaging_invalidation_handler.cc

Issue 10826156: Plumb invalidations from Tango to the extensions code for the Push Messaging API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix gmock and maybe Android build Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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
Pete Williamson 2012/08/13 18:24:09 Nit - the "kXxxIndex" constants below might be a b
dcheng 2012/08/13 20:44:14 It's also the index into a string. I named it "Ind
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 }
Pete Williamson 2012/08/13 18:24:09 Might be good to add a comment here: // TODO: Modi
dcheng 2012/08/13 20:44:14 I avoided making that comment since that would req
68 if (name.compare(kFormatTypeIndex, kFormatTypeLength, "U")) {
69 DLOG(WARNING) << "Invalid format type for object name " << name;
70 return false;
71 }
Pete Williamson 2012/08/13 18:24:09 Does this "PM" need to be registered somewhere so
dcheng 2012/08/13 20:44:14 This is an ad-hoc registration. We rely on good ci
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
Pete Williamson 2012/08/13 18:24:09 Coding standards question: I personally think it
dcheng 2012/08/13 20:44:14 I've updated the header with comments about the li
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
Pete Williamson 2012/08/13 18:24:09 Is this removing the registration when the DTOR fi
dcheng 2012/08/13 20:44:14 I've rebased to akalin's latest set of changes. It
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.
Pete Williamson 2012/08/13 18:24:09 Should we DCHECK(false); here since this shouldn't
dcheng 2012/08/13 20:44:14 That just means that sync has been disabled. In th
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698