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

Side by Side Diff: components/gcm_driver/registration_info.cc

Issue 1137463003: Support getting and deleting token for Instance ID. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Patch to land Created 5 years, 6 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
« no previous file with comments | « components/gcm_driver/registration_info.h ('k') | google_apis/gcm/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2015 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 "components/gcm_driver/registration_info.h"
6
7 #include "base/strings/string_util.h"
8
9 namespace gcm {
10
11 namespace {
12 const char kInsanceIDSerializationPrefix[] = "iid-";
13 const int kInsanceIDSerializationPrefixLength =
14 sizeof(kInsanceIDSerializationPrefix) / sizeof(char) - 1;
15 } // namespace
16
17 // static
18 scoped_ptr<RegistrationInfo> RegistrationInfo::BuildFromString(
19 const std::string& serialzied_key,
20 const std::string& serialzied_value,
21 std::string* registration_id) {
22 scoped_ptr<RegistrationInfo> registration;
23
24 if (StartsWithASCII(serialzied_key, kInsanceIDSerializationPrefix, true))
25 registration.reset(new InstanceIDTokenInfo);
26 else
27 registration.reset(new GCMRegistrationInfo);
28
29 if (!registration->Deserialize(serialzied_key,
30 serialzied_value,
31 registration_id)) {
32 registration.reset();
33 }
34 return registration.Pass();
35 }
36
37 RegistrationInfo::RegistrationInfo() {
38 }
39
40 RegistrationInfo::~RegistrationInfo() {
41 }
42
43 // static
44 const GCMRegistrationInfo* GCMRegistrationInfo::FromRegistrationInfo(
45 const RegistrationInfo* registration_info) {
46 if (!registration_info || registration_info->GetType() != GCM_REGISTRATION)
47 return NULL;
48 return static_cast<const GCMRegistrationInfo*>(registration_info);
49 }
50
51 // static
52 GCMRegistrationInfo* GCMRegistrationInfo::FromRegistrationInfo(
53 RegistrationInfo* registration_info) {
54 if (!registration_info || registration_info->GetType() != GCM_REGISTRATION)
55 return NULL;
56 return static_cast<GCMRegistrationInfo*>(registration_info);
57 }
58
59 GCMRegistrationInfo::GCMRegistrationInfo() {
60 }
61
62 GCMRegistrationInfo::~GCMRegistrationInfo() {
63 }
64
65 RegistrationInfo::RegistrationType GCMRegistrationInfo::GetType() const {
66 return GCM_REGISTRATION;
67 }
68
69 std::string GCMRegistrationInfo::GetSerializedKey() const {
70 // Multiple registrations are not supported for legacy GCM. So the key is
71 // purely based on the application id.
72 return app_id;
73 }
74
75 std::string GCMRegistrationInfo::GetSerializedValue(
76 const std::string& registration_id) const {
77 if (sender_ids.empty() || registration_id.empty())
78 return std::string();
79
80 // Serialize as:
81 // sender1,sender2,...=reg_id
82 std::string value;
83 for (std::vector<std::string>::const_iterator iter = sender_ids.begin();
84 iter != sender_ids.end(); ++iter) {
85 DCHECK(!iter->empty() &&
86 iter->find(',') == std::string::npos &&
87 iter->find('=') == std::string::npos);
88 if (!value.empty())
89 value += ",";
90 value += *iter;
91 }
92
93 value += '=';
94 value += registration_id;
95 return value;
96 }
97
98 bool GCMRegistrationInfo::Deserialize(
99 const std::string& serialzied_key,
100 const std::string& serialzied_value,
101 std::string* registration_id) {
102 if (serialzied_key.empty() || serialzied_value.empty())
103 return false;
104
105 // Application ID is same as the serialized key.
106 app_id = serialzied_key;
107
108 // Sender IDs and registration ID are constructed from the serialized value.
109 size_t pos = serialzied_value.find('=');
110 if (pos == std::string::npos)
111 return false;
112
113 std::string senders = serialzied_value.substr(0, pos);
114 std::string registration_id_str = serialzied_value.substr(pos + 1);
115
116 Tokenize(senders, ",", &sender_ids);
117
118 if (sender_ids.empty() || registration_id_str.empty()) {
119 sender_ids.clear();
120 registration_id_str.clear();
121 return false;
122 }
123
124 if (registration_id)
125 *registration_id = registration_id_str;
126
127 return true;
128 }
129
130 // static
131 const InstanceIDTokenInfo* InstanceIDTokenInfo::FromRegistrationInfo(
132 const RegistrationInfo* registration_info) {
133 if (!registration_info || registration_info->GetType() != INSTANCE_ID_TOKEN)
134 return NULL;
135 return static_cast<const InstanceIDTokenInfo*>(registration_info);
136 }
137
138 // static
139 InstanceIDTokenInfo* InstanceIDTokenInfo::FromRegistrationInfo(
140 RegistrationInfo* registration_info) {
141 if (!registration_info || registration_info->GetType() != INSTANCE_ID_TOKEN)
142 return NULL;
143 return static_cast<InstanceIDTokenInfo*>(registration_info);
144 }
145
146 InstanceIDTokenInfo::InstanceIDTokenInfo() {
147 }
148
149 InstanceIDTokenInfo::~InstanceIDTokenInfo() {
150 }
151
152 RegistrationInfo::RegistrationType InstanceIDTokenInfo::GetType() const {
153 return INSTANCE_ID_TOKEN;
154 }
155
156 std::string InstanceIDTokenInfo::GetSerializedKey() const {
157 DCHECK(authorized_entity.find(',') == std::string::npos &&
158 scope.find(',') == std::string::npos);
159
160 // Multiple registrations are supported for Instance ID. So the key is based
161 // on the combination of (app_id, authorized_entity, scope).
162
163 // Adds a prefix to differentiate easily with GCM registration key.
164 std::string key(kInsanceIDSerializationPrefix);
165 key += app_id;
166 key += ",";
167 key += authorized_entity;
168 key += ",";
169 key += scope;
170 return key;
171 }
172
173 std::string InstanceIDTokenInfo::GetSerializedValue(
174 const std::string& registration_id) const {
175 return registration_id;
176 }
177
178 bool InstanceIDTokenInfo::Deserialize(
179 const std::string& serialized_key,
180 const std::string& serialized_value,
181 std::string* registration_id) {
182 if (serialized_key.empty() || serialized_value.empty())
183 return false;
184
185 if (!StartsWithASCII(serialized_key, kInsanceIDSerializationPrefix, true))
186 return false;
187
188 std::vector<std::string> fields;
189 Tokenize(serialized_key.substr(kInsanceIDSerializationPrefixLength),
190 ",",
191 &fields);
192 if (fields.size() != 3 || fields[0].empty() ||
193 fields[1].empty() || fields[2].empty()) {
194 return false;
195 }
196 app_id = fields[0];
197 authorized_entity = fields[1];
198 scope = fields[2];
199
200 // Registration ID is same as the serialized value;
201 if (registration_id)
202 *registration_id = serialized_value;
203
204 return true;
205 }
206
207 bool RegistrationInfoComparer::operator()(
208 const linked_ptr<RegistrationInfo>& a,
209 const linked_ptr<RegistrationInfo>& b) const {
210 DCHECK(a.get() && b.get());
211
212 // For GCMRegistrationInfo, the comparison is based on app_id only.
213 // For InstanceIDTokenInfo, the comparison is bsaed on
214 // <app_id, authorized_entity, scope>.
215 if (a->app_id < b->app_id)
216 return true;
217 if (a->app_id > b->app_id)
218 return false;
219
220 InstanceIDTokenInfo* iid_a =
221 InstanceIDTokenInfo::FromRegistrationInfo(a.get());
222 InstanceIDTokenInfo* iid_b =
223 InstanceIDTokenInfo::FromRegistrationInfo(b.get());
224
225 // !iid_a && !iid_b => false.
226 // !iid_a && iid_b => true.
227 // This makes GCM record is sorted before InstanceID record.
228 if (!iid_a)
229 return iid_b != NULL;
230
231 // iid_a && !iid_b => false.
232 if (!iid_b)
233 return false;
234
235 // Otherwise, compare with authorized_entity and scope.
236 if (iid_a->authorized_entity < iid_b->authorized_entity)
237 return true;
238 if (iid_a->authorized_entity > iid_b->authorized_entity)
239 return false;
240 return iid_a->scope < iid_b->scope;
241 }
242
243 bool ExistsGCMRegistrationInMap(const RegistrationInfoMap& map,
244 const std::string& app_id) {
245 scoped_ptr<GCMRegistrationInfo> gcm_registration(new GCMRegistrationInfo);
246 gcm_registration->app_id = app_id;
247 return map.count(
248 make_linked_ptr<RegistrationInfo>(gcm_registration.release())) > 0;
249 }
250
251 } // namespace gcm
OLDNEW
« no previous file with comments | « components/gcm_driver/registration_info.h ('k') | google_apis/gcm/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698