| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 
 | // Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/gcm_driver/registration_info.h"
#include <stddef.h>
#include "base/format_macros.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
namespace gcm {
namespace {
constexpr char kInstanceIDSerializationPrefix[] = "iid-";
constexpr char kSerializedValidationTimeSeparator = '#';
constexpr char kSerializedKeySeparator = ',';
constexpr int kInstanceIDSerializationPrefixLength =
    sizeof(kInstanceIDSerializationPrefix) / sizeof(char) - 1;
}  // namespace
// static
scoped_refptr<RegistrationInfo> RegistrationInfo::BuildFromString(
    const std::string& serialized_key,
    const std::string& serialized_value,
    std::string* registration_id) {
  scoped_refptr<RegistrationInfo> registration;
  if (base::StartsWith(serialized_key, kInstanceIDSerializationPrefix,
                       base::CompareCase::SENSITIVE)) {
    registration = base::MakeRefCounted<InstanceIDTokenInfo>();
  } else {
    registration = base::MakeRefCounted<GCMRegistrationInfo>();
  }
  if (!registration->Deserialize(serialized_key, serialized_value,
                                 registration_id)) {
    registration.reset();
  }
  return registration;
}
RegistrationInfo::RegistrationInfo() = default;
RegistrationInfo::~RegistrationInfo() = default;
// static
const GCMRegistrationInfo* GCMRegistrationInfo::FromRegistrationInfo(
    const RegistrationInfo* registration_info) {
  if (!registration_info || registration_info->GetType() != GCM_REGISTRATION)
    return nullptr;
  return static_cast<const GCMRegistrationInfo*>(registration_info);
}
// static
GCMRegistrationInfo* GCMRegistrationInfo::FromRegistrationInfo(
    RegistrationInfo* registration_info) {
  if (!registration_info || registration_info->GetType() != GCM_REGISTRATION)
    return nullptr;
  return static_cast<GCMRegistrationInfo*>(registration_info);
}
GCMRegistrationInfo::GCMRegistrationInfo() = default;
GCMRegistrationInfo::~GCMRegistrationInfo() = default;
RegistrationInfo::RegistrationType GCMRegistrationInfo::GetType() const {
  return GCM_REGISTRATION;
}
std::string GCMRegistrationInfo::GetSerializedKey() const {
  // Multiple registrations are not supported for legacy GCM. So the key is
  // purely based on the application id.
  return app_id;
}
std::string GCMRegistrationInfo::GetSerializedValue(
    const std::string& registration_id) const {
  if (sender_ids.empty() || registration_id.empty())
    return std::string();
  // Serialize as:
  // sender1,sender2,...=reg_id#time_of_last_validation
  std::string value;
  for (auto iter = sender_ids.begin(); iter != sender_ids.end(); ++iter) {
    DCHECK(!iter->empty() &&
           iter->find(',') == std::string::npos &&
           iter->find('=') == std::string::npos);
    if (!value.empty())
      value += ",";
    value += *iter;
  }
  return base::StringPrintf("%s=%s%c%" PRId64, value.c_str(),
                            registration_id.c_str(),
                            kSerializedValidationTimeSeparator,
                            last_validated.since_origin().InMicroseconds());
}
bool GCMRegistrationInfo::Deserialize(const std::string& serialized_key,
                                      const std::string& serialized_value,
                                      std::string* registration_id) {
  if (serialized_key.empty() || serialized_value.empty())
    return false;
  // Application ID is same as the serialized key.
  app_id = serialized_key;
  // Sender IDs and registration ID are constructed from the serialized value.
  size_t pos_equals = serialized_value.find('=');
  if (pos_equals == std::string::npos)
    return false;
  // Note that it's valid for pos_hash to be std::string::npos.
  size_t pos_hash = serialized_value.find(kSerializedValidationTimeSeparator);
  bool has_timestamp = pos_hash != std::string::npos;
  std::string senders = serialized_value.substr(0, pos_equals);
  std::string registration_id_str, last_validated_str;
  if (has_timestamp) {
    registration_id_str =
        serialized_value.substr(pos_equals + 1, pos_hash - pos_equals - 1);
    last_validated_str = serialized_value.substr(pos_hash + 1);
  } else {
    registration_id_str = serialized_value.substr(pos_equals + 1);
  }
  sender_ids = base::SplitString(
      senders, ",", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
  if (sender_ids.empty() || registration_id_str.empty()) {
    sender_ids.clear();
    registration_id_str.clear();
    return false;
  }
  if (registration_id)
    *registration_id = registration_id_str;
  int64_t last_validated_ms = 0;
  if (base::StringToInt64(last_validated_str, &last_validated_ms)) {
    // It's okay for |last_validated| to be the default base::Time() value
    // when there is no serialized timestamp value available.
    last_validated = base::Time() + base::Microseconds(last_validated_ms);
  }
  return true;
}
// static
const InstanceIDTokenInfo* InstanceIDTokenInfo::FromRegistrationInfo(
    const RegistrationInfo* registration_info) {
  if (!registration_info || registration_info->GetType() != INSTANCE_ID_TOKEN)
    return nullptr;
  return static_cast<const InstanceIDTokenInfo*>(registration_info);
}
// static
InstanceIDTokenInfo* InstanceIDTokenInfo::FromRegistrationInfo(
    RegistrationInfo* registration_info) {
  if (!registration_info || registration_info->GetType() != INSTANCE_ID_TOKEN)
    return nullptr;
  return static_cast<InstanceIDTokenInfo*>(registration_info);
}
InstanceIDTokenInfo::InstanceIDTokenInfo() = default;
InstanceIDTokenInfo::~InstanceIDTokenInfo() = default;
RegistrationInfo::RegistrationType InstanceIDTokenInfo::GetType() const {
  return INSTANCE_ID_TOKEN;
}
std::string InstanceIDTokenInfo::GetSerializedKey() const {
  DCHECK(app_id.find(',') == std::string::npos &&
         authorized_entity.find(',') == std::string::npos &&
         scope.find(',') == std::string::npos);
  // Multiple registrations are supported for Instance ID. So the key is based
  // on the combination of (app_id, authorized_entity, scope).
  // Adds a prefix to differentiate easily with GCM registration key.
  return base::StringPrintf("%s%s%c%s%c%s", kInstanceIDSerializationPrefix,
                            app_id.c_str(), kSerializedKeySeparator,
                            authorized_entity.c_str(), kSerializedKeySeparator,
                            scope.c_str());
}
std::string InstanceIDTokenInfo::GetSerializedValue(
    const std::string& registration_id) const {
  int64_t last_validated_ms = last_validated.since_origin().InMicroseconds();
  return registration_id + kSerializedValidationTimeSeparator +
         base::NumberToString(last_validated_ms);
}
bool InstanceIDTokenInfo::Deserialize(const std::string& serialized_key,
                                      const std::string& serialized_value,
                                      std::string* registration_id) {
  if (serialized_key.empty() || serialized_value.empty())
    return false;
  if (!base::StartsWith(serialized_key, kInstanceIDSerializationPrefix,
                        base::CompareCase::SENSITIVE))
    return false;
  std::vector<std::string> fields = base::SplitString(
      serialized_key.substr(kInstanceIDSerializationPrefixLength), ",",
      base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
  if (fields.size() != 3 || fields[0].empty() ||
      fields[1].empty() || fields[2].empty()) {
    return false;
  }
  app_id = fields[0];
  authorized_entity = fields[1];
  scope = fields[2];
  // Get Registration ID and last_validated from serialized value
  size_t pos_hash = serialized_value.find(kSerializedValidationTimeSeparator);
  bool has_timestamp = (pos_hash != std::string::npos);
  std::string registration_id_str, last_validated_str;
  if (has_timestamp) {
    registration_id_str = serialized_value.substr(0, pos_hash);
    last_validated_str = serialized_value.substr(pos_hash + 1);
  } else {
    registration_id_str = serialized_value;
  }
  if (registration_id)
    *registration_id = registration_id_str;
  int64_t last_validated_ms = 0;
  if (base::StringToInt64(last_validated_str, &last_validated_ms)) {
    // It's okay for last_validated to be the default base::Time() value
    // when there is no serialized timestamp available.
    last_validated += base::Microseconds(last_validated_ms);
  }
  return true;
}
bool RegistrationInfoComparer::operator()(
    const scoped_refptr<RegistrationInfo>& a,
    const scoped_refptr<RegistrationInfo>& b) const {
  DCHECK(a.get() && b.get());
  // For GCMRegistrationInfo, the comparison is based on app_id only.
  // For InstanceIDTokenInfo, the comparison is based on
  // <app_id, authorized_entity, scope>.
  if (a->app_id < b->app_id)
    return true;
  if (a->app_id > b->app_id)
    return false;
  InstanceIDTokenInfo* iid_a =
      InstanceIDTokenInfo::FromRegistrationInfo(a.get());
  InstanceIDTokenInfo* iid_b =
    InstanceIDTokenInfo::FromRegistrationInfo(b.get());
  // !iid_a && !iid_b => false.
  // !iid_a && iid_b => true.
  // This makes GCM record is sorted before InstanceID record.
  if (!iid_a)
    return iid_b != nullptr;
  // iid_a && !iid_b => false.
  if (!iid_b)
    return false;
  // Otherwise, compare with authorized_entity and scope.
  if (iid_a->authorized_entity < iid_b->authorized_entity)
    return true;
  if (iid_a->authorized_entity > iid_b->authorized_entity)
    return false;
  return iid_a->scope < iid_b->scope;
}
bool ExistsGCMRegistrationInMap(const RegistrationInfoMap& map,
                                const std::string& app_id) {
  scoped_refptr<RegistrationInfo> gcm_registration =
      base::MakeRefCounted<GCMRegistrationInfo>();
  gcm_registration->app_id = app_id;
  return map.find(gcm_registration) != map.end();
}
}  // namespace gcm
 |