| 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 |
| 19 const char kInstanceIDUseSubtypePrefix[] = "use_subtype,"; |
| 20 const int kInstanceIDUseSubtypePrefixLength = |
| 21 sizeof(kInstanceIDUseSubtypePrefix) / sizeof(char) - 1; |
| 18 } // namespace | 22 } // namespace |
| 19 | 23 |
| 20 // static | 24 // static |
| 21 std::unique_ptr<RegistrationInfo> RegistrationInfo::BuildFromString( | 25 std::unique_ptr<RegistrationInfo> RegistrationInfo::BuildFromString( |
| 22 const std::string& serialized_key, | 26 const std::string& serialized_key, |
| 23 const std::string& serialized_value, | 27 const std::string& serialized_value, |
| 24 std::string* registration_id) { | 28 std::string* registration_id) { |
| 25 std::unique_ptr<RegistrationInfo> registration; | 29 std::unique_ptr<RegistrationInfo> registration; |
| 26 | 30 |
| 27 if (base::StartsWith(serialized_key, kInsanceIDSerializationPrefix, | 31 if (base::StartsWith(serialized_key, kInstanceIDSerializationPrefix, |
| 28 base::CompareCase::SENSITIVE)) | 32 base::CompareCase::SENSITIVE)) |
| 29 registration.reset(new InstanceIDTokenInfo); | 33 registration.reset(new InstanceIDTokenInfo); |
| 30 else | 34 else |
| 31 registration.reset(new GCMRegistrationInfo); | 35 registration.reset(new GCMRegistrationInfo); |
| 32 | 36 |
| 33 if (!registration->Deserialize(serialized_key, | 37 if (!registration->Deserialize(serialized_key, |
| 34 serialized_value, | 38 serialized_value, |
| 35 registration_id)) { | 39 registration_id)) { |
| 36 registration.reset(); | 40 registration.reset(); |
| 37 } | 41 } |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 } | 155 } |
| 152 | 156 |
| 153 InstanceIDTokenInfo::~InstanceIDTokenInfo() { | 157 InstanceIDTokenInfo::~InstanceIDTokenInfo() { |
| 154 } | 158 } |
| 155 | 159 |
| 156 RegistrationInfo::RegistrationType InstanceIDTokenInfo::GetType() const { | 160 RegistrationInfo::RegistrationType InstanceIDTokenInfo::GetType() const { |
| 157 return INSTANCE_ID_TOKEN; | 161 return INSTANCE_ID_TOKEN; |
| 158 } | 162 } |
| 159 | 163 |
| 160 std::string InstanceIDTokenInfo::GetSerializedKey() const { | 164 std::string InstanceIDTokenInfo::GetSerializedKey() const { |
| 161 DCHECK(authorized_entity.find(',') == std::string::npos && | 165 DCHECK(app_id.find(',') == std::string::npos && |
| 166 authorized_entity.find(',') == std::string::npos && |
| 162 scope.find(',') == std::string::npos); | 167 scope.find(',') == std::string::npos); |
| 163 | 168 |
| 164 // Multiple registrations are supported for Instance ID. So the key is based | 169 // Multiple registrations are supported for Instance ID. So the key is based |
| 165 // on the combination of (app_id, authorized_entity, scope). | 170 // on the combination of (app_id, authorized_entity, scope). |
| 166 | 171 |
| 167 // Adds a prefix to differentiate easily with GCM registration key. | 172 // Adds a prefix to differentiate easily with GCM registration key. |
| 168 std::string key(kInsanceIDSerializationPrefix); | 173 std::string key(kInstanceIDSerializationPrefix); |
| 169 key += app_id; | 174 key += app_id; |
| 170 key += ","; | 175 key += ","; |
| 171 key += authorized_entity; | 176 key += authorized_entity; |
| 172 key += ","; | 177 key += ","; |
| 173 key += scope; | 178 key += scope; |
| 174 return key; | 179 return key; |
| 175 } | 180 } |
| 176 | 181 |
| 177 std::string InstanceIDTokenInfo::GetSerializedValue( | 182 std::string InstanceIDTokenInfo::GetSerializedValue( |
| 178 const std::string& registration_id) const { | 183 const std::string& registration_id) const { |
| 179 return registration_id; | 184 DCHECK(!base::StartsWith(registration_id, kInstanceIDUseSubtypePrefix, |
| 185 base::CompareCase::SENSITIVE)); |
| 186 return (use_subtype ? kInstanceIDUseSubtypePrefix : std::string()) + |
| 187 registration_id; |
| 180 } | 188 } |
| 181 | 189 |
| 182 bool InstanceIDTokenInfo::Deserialize( | 190 bool InstanceIDTokenInfo::Deserialize( |
| 183 const std::string& serialized_key, | 191 const std::string& serialized_key, |
| 184 const std::string& serialized_value, | 192 const std::string& serialized_value, |
| 185 std::string* registration_id) { | 193 std::string* registration_id) { |
| 186 if (serialized_key.empty() || serialized_value.empty()) | 194 if (serialized_key.empty() || serialized_value.empty()) |
| 187 return false; | 195 return false; |
| 188 | 196 |
| 189 if (!base::StartsWith(serialized_key, kInsanceIDSerializationPrefix, | 197 if (!base::StartsWith(serialized_key, kInstanceIDSerializationPrefix, |
| 190 base::CompareCase::SENSITIVE)) | 198 base::CompareCase::SENSITIVE)) |
| 191 return false; | 199 return false; |
| 192 | 200 |
| 193 std::vector<std::string> fields = base::SplitString( | 201 std::vector<std::string> fields = base::SplitString( |
| 194 serialized_key.substr(kInsanceIDSerializationPrefixLength), | 202 serialized_key.substr(kInstanceIDSerializationPrefixLength), ",", |
| 195 ",", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); | 203 base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); |
| 196 if (fields.size() != 3 || fields[0].empty() || | 204 if (fields.size() != 3 || fields[0].empty() || |
| 197 fields[1].empty() || fields[2].empty()) { | 205 fields[1].empty() || fields[2].empty()) { |
| 198 return false; | 206 return false; |
| 199 } | 207 } |
| 200 app_id = fields[0]; | 208 app_id = fields[0]; |
| 201 authorized_entity = fields[1]; | 209 authorized_entity = fields[1]; |
| 202 scope = fields[2]; | 210 scope = fields[2]; |
| 203 | 211 |
| 204 // Registration ID is same as the serialized value; | 212 // Serialized value is registration ID, with a prefix if |use_subtype|. |
| 205 if (registration_id) | 213 use_subtype = base::StartsWith(serialized_value, kInstanceIDUseSubtypePrefix, |
| 206 *registration_id = serialized_value; | 214 base::CompareCase::SENSITIVE); |
| 215 if (registration_id) { |
| 216 *registration_id = |
| 217 use_subtype ? serialized_value.substr(kInstanceIDUseSubtypePrefixLength) |
| 218 : serialized_value; |
| 219 } |
| 207 | 220 |
| 208 return true; | 221 return true; |
| 209 } | 222 } |
| 210 | 223 |
| 211 bool RegistrationInfoComparer::operator()( | 224 bool RegistrationInfoComparer::operator()( |
| 212 const linked_ptr<RegistrationInfo>& a, | 225 const linked_ptr<RegistrationInfo>& a, |
| 213 const linked_ptr<RegistrationInfo>& b) const { | 226 const linked_ptr<RegistrationInfo>& b) const { |
| 214 DCHECK(a.get() && b.get()); | 227 DCHECK(a.get() && b.get()); |
| 215 | 228 |
| 216 // For GCMRegistrationInfo, the comparison is based on app_id only. | 229 // For GCMRegistrationInfo, the comparison is based on app_id only. |
| 217 // For InstanceIDTokenInfo, the comparison is bsaed on | 230 // For InstanceIDTokenInfo, the comparison is based on |
| 218 // <app_id, authorized_entity, scope>. | 231 // <app_id, authorized_entity, scope>. |
| 219 if (a->app_id < b->app_id) | 232 if (a->app_id < b->app_id) |
| 220 return true; | 233 return true; |
| 221 if (a->app_id > b->app_id) | 234 if (a->app_id > b->app_id) |
| 222 return false; | 235 return false; |
| 223 | 236 |
| 224 InstanceIDTokenInfo* iid_a = | 237 InstanceIDTokenInfo* iid_a = |
| 225 InstanceIDTokenInfo::FromRegistrationInfo(a.get()); | 238 InstanceIDTokenInfo::FromRegistrationInfo(a.get()); |
| 226 InstanceIDTokenInfo* iid_b = | 239 InstanceIDTokenInfo* iid_b = |
| 227 InstanceIDTokenInfo::FromRegistrationInfo(b.get()); | 240 InstanceIDTokenInfo::FromRegistrationInfo(b.get()); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 247 bool ExistsGCMRegistrationInMap(const RegistrationInfoMap& map, | 260 bool ExistsGCMRegistrationInMap(const RegistrationInfoMap& map, |
| 248 const std::string& app_id) { | 261 const std::string& app_id) { |
| 249 std::unique_ptr<GCMRegistrationInfo> gcm_registration( | 262 std::unique_ptr<GCMRegistrationInfo> gcm_registration( |
| 250 new GCMRegistrationInfo); | 263 new GCMRegistrationInfo); |
| 251 gcm_registration->app_id = app_id; | 264 gcm_registration->app_id = app_id; |
| 252 return map.count( | 265 return map.count( |
| 253 make_linked_ptr<RegistrationInfo>(gcm_registration.release())) > 0; | 266 make_linked_ptr<RegistrationInfo>(gcm_registration.release())) > 0; |
| 254 } | 267 } |
| 255 | 268 |
| 256 } // namespace gcm | 269 } // namespace gcm |
| OLD | NEW |