OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/gcm_driver/registration_info.h" | 5 #include "components/gcm_driver/registration_info.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/strings/string_split.h" | 9 #include "base/strings/string_split.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
11 | 11 |
12 namespace gcm { | 12 namespace gcm { |
13 | 13 |
14 namespace { | 14 namespace { |
15 const char kInsanceIDSerializationPrefix[] = "iid-"; | 15 const char kInstanceIDSerializationPrefix[] = "iid-"; |
16 const int kInsanceIDSerializationPrefixLength = | 16 const int kInstanceIDSerializationPrefixLength = |
17 sizeof(kInsanceIDSerializationPrefix) / sizeof(char) - 1; | 17 sizeof(kInstanceIDSerializationPrefix) / sizeof(char) - 1; |
18 } // namespace | 18 } // namespace |
19 | 19 |
20 // static | 20 // static |
21 std::unique_ptr<RegistrationInfo> RegistrationInfo::BuildFromString( | 21 std::unique_ptr<RegistrationInfo> RegistrationInfo::BuildFromString( |
22 const std::string& serialized_key, | 22 const std::string& serialized_key, |
23 const std::string& serialized_value, | 23 const std::string& serialized_value, |
24 std::string* registration_id) { | 24 std::string* registration_id) { |
25 std::unique_ptr<RegistrationInfo> registration; | 25 std::unique_ptr<RegistrationInfo> registration; |
26 | 26 |
27 if (base::StartsWith(serialized_key, kInsanceIDSerializationPrefix, | 27 if (base::StartsWith(serialized_key, kInstanceIDSerializationPrefix, |
28 base::CompareCase::SENSITIVE)) | 28 base::CompareCase::SENSITIVE)) |
29 registration.reset(new InstanceIDTokenInfo); | 29 registration.reset(new InstanceIDTokenInfo); |
30 else | 30 else |
31 registration.reset(new GCMRegistrationInfo); | 31 registration.reset(new GCMRegistrationInfo); |
32 | 32 |
33 if (!registration->Deserialize(serialized_key, | 33 if (!registration->Deserialize(serialized_key, |
34 serialized_value, | 34 serialized_value, |
35 registration_id)) { | 35 registration_id)) { |
36 registration.reset(); | 36 registration.reset(); |
37 } | 37 } |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 } | 151 } |
152 | 152 |
153 InstanceIDTokenInfo::~InstanceIDTokenInfo() { | 153 InstanceIDTokenInfo::~InstanceIDTokenInfo() { |
154 } | 154 } |
155 | 155 |
156 RegistrationInfo::RegistrationType InstanceIDTokenInfo::GetType() const { | 156 RegistrationInfo::RegistrationType InstanceIDTokenInfo::GetType() const { |
157 return INSTANCE_ID_TOKEN; | 157 return INSTANCE_ID_TOKEN; |
158 } | 158 } |
159 | 159 |
160 std::string InstanceIDTokenInfo::GetSerializedKey() const { | 160 std::string InstanceIDTokenInfo::GetSerializedKey() const { |
161 DCHECK(authorized_entity.find(',') == std::string::npos && | 161 DCHECK(app_id.find(',') == std::string::npos && |
| 162 authorized_entity.find(',') == std::string::npos && |
162 scope.find(',') == std::string::npos); | 163 scope.find(',') == std::string::npos); |
163 | 164 |
164 // Multiple registrations are supported for Instance ID. So the key is based | 165 // Multiple registrations are supported for Instance ID. So the key is based |
165 // on the combination of (app_id, authorized_entity, scope). | 166 // on the combination of (app_id, authorized_entity, scope). |
166 | 167 |
167 // Adds a prefix to differentiate easily with GCM registration key. | 168 // Adds a prefix to differentiate easily with GCM registration key. |
168 std::string key(kInsanceIDSerializationPrefix); | 169 std::string key(kInstanceIDSerializationPrefix); |
169 key += app_id; | 170 key += app_id; |
170 key += ","; | 171 key += ","; |
171 key += authorized_entity; | 172 key += authorized_entity; |
172 key += ","; | 173 key += ","; |
173 key += scope; | 174 key += scope; |
174 return key; | 175 return key; |
175 } | 176 } |
176 | 177 |
177 std::string InstanceIDTokenInfo::GetSerializedValue( | 178 std::string InstanceIDTokenInfo::GetSerializedValue( |
178 const std::string& registration_id) const { | 179 const std::string& registration_id) const { |
179 return registration_id; | 180 return registration_id; |
180 } | 181 } |
181 | 182 |
182 bool InstanceIDTokenInfo::Deserialize( | 183 bool InstanceIDTokenInfo::Deserialize( |
183 const std::string& serialized_key, | 184 const std::string& serialized_key, |
184 const std::string& serialized_value, | 185 const std::string& serialized_value, |
185 std::string* registration_id) { | 186 std::string* registration_id) { |
186 if (serialized_key.empty() || serialized_value.empty()) | 187 if (serialized_key.empty() || serialized_value.empty()) |
187 return false; | 188 return false; |
188 | 189 |
189 if (!base::StartsWith(serialized_key, kInsanceIDSerializationPrefix, | 190 if (!base::StartsWith(serialized_key, kInstanceIDSerializationPrefix, |
190 base::CompareCase::SENSITIVE)) | 191 base::CompareCase::SENSITIVE)) |
191 return false; | 192 return false; |
192 | 193 |
193 std::vector<std::string> fields = base::SplitString( | 194 std::vector<std::string> fields = base::SplitString( |
194 serialized_key.substr(kInsanceIDSerializationPrefixLength), | 195 serialized_key.substr(kInstanceIDSerializationPrefixLength), ",", |
195 ",", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); | 196 base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); |
196 if (fields.size() != 3 || fields[0].empty() || | 197 if (fields.size() != 3 || fields[0].empty() || |
197 fields[1].empty() || fields[2].empty()) { | 198 fields[1].empty() || fields[2].empty()) { |
198 return false; | 199 return false; |
199 } | 200 } |
200 app_id = fields[0]; | 201 app_id = fields[0]; |
201 authorized_entity = fields[1]; | 202 authorized_entity = fields[1]; |
202 scope = fields[2]; | 203 scope = fields[2]; |
203 | 204 |
204 // Registration ID is same as the serialized value; | 205 // Registration ID is same as the serialized value; |
205 if (registration_id) | 206 if (registration_id) |
206 *registration_id = serialized_value; | 207 *registration_id = serialized_value; |
207 | 208 |
208 return true; | 209 return true; |
209 } | 210 } |
210 | 211 |
211 bool RegistrationInfoComparer::operator()( | 212 bool RegistrationInfoComparer::operator()( |
212 const linked_ptr<RegistrationInfo>& a, | 213 const linked_ptr<RegistrationInfo>& a, |
213 const linked_ptr<RegistrationInfo>& b) const { | 214 const linked_ptr<RegistrationInfo>& b) const { |
214 DCHECK(a.get() && b.get()); | 215 DCHECK(a.get() && b.get()); |
215 | 216 |
216 // For GCMRegistrationInfo, the comparison is based on app_id only. | 217 // For GCMRegistrationInfo, the comparison is based on app_id only. |
217 // For InstanceIDTokenInfo, the comparison is bsaed on | 218 // For InstanceIDTokenInfo, the comparison is based on |
218 // <app_id, authorized_entity, scope>. | 219 // <app_id, authorized_entity, scope>. |
219 if (a->app_id < b->app_id) | 220 if (a->app_id < b->app_id) |
220 return true; | 221 return true; |
221 if (a->app_id > b->app_id) | 222 if (a->app_id > b->app_id) |
222 return false; | 223 return false; |
223 | 224 |
224 InstanceIDTokenInfo* iid_a = | 225 InstanceIDTokenInfo* iid_a = |
225 InstanceIDTokenInfo::FromRegistrationInfo(a.get()); | 226 InstanceIDTokenInfo::FromRegistrationInfo(a.get()); |
226 InstanceIDTokenInfo* iid_b = | 227 InstanceIDTokenInfo* iid_b = |
227 InstanceIDTokenInfo::FromRegistrationInfo(b.get()); | 228 InstanceIDTokenInfo::FromRegistrationInfo(b.get()); |
(...skipping 19 matching lines...) Expand all Loading... |
247 bool ExistsGCMRegistrationInMap(const RegistrationInfoMap& map, | 248 bool ExistsGCMRegistrationInMap(const RegistrationInfoMap& map, |
248 const std::string& app_id) { | 249 const std::string& app_id) { |
249 std::unique_ptr<GCMRegistrationInfo> gcm_registration( | 250 std::unique_ptr<GCMRegistrationInfo> gcm_registration( |
250 new GCMRegistrationInfo); | 251 new GCMRegistrationInfo); |
251 gcm_registration->app_id = app_id; | 252 gcm_registration->app_id = app_id; |
252 return map.count( | 253 return map.count( |
253 make_linked_ptr<RegistrationInfo>(gcm_registration.release())) > 0; | 254 make_linked_ptr<RegistrationInfo>(gcm_registration.release())) > 0; |
254 } | 255 } |
255 | 256 |
256 } // namespace gcm | 257 } // namespace gcm |
OLD | NEW |