1
2
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
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/push_messaging/push_messaging_app_identifier.h"
#include <string.h>
#include "base/guid.h"
#include "base/logging.h"
#include "base/prefs/pref_service.h"
#include "base/prefs/scoped_user_pref_update.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/values.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/pref_names.h"
#include "components/pref_registry/pref_registry_syncable.h"
const char kPushMessagingAppIdentifierPrefix[] = "wp:";
namespace {
// sizeof is strlen + 1 since it's null-terminated.
const size_t kPrefixLength = sizeof(kPushMessagingAppIdentifierPrefix) - 1;
const char kSeparator = '#'; // Ok as only the origin of the url is used.
const size_t kGuidLength = 36; // "%08X-%04X-%04X-%04X-%012llX"
std::string MakePrefValue(const GURL& origin,
int64_t service_worker_registration_id) {
return origin.spec() + kSeparator
+ base::Int64ToString(service_worker_registration_id);
}
bool GetOriginAndSWRFromPrefValue(
const std::string& pref_value, GURL* origin,
int64_t* service_worker_registration_id) {
std::vector<std::string> parts;
base::SplitString(pref_value, kSeparator, &parts);
if (parts.size() != 2)
return false;
if (!base::StringToInt64(parts[1], service_worker_registration_id))
return false;
*origin = GURL(parts[0]);
return origin->is_valid();
}
} // namespace
// static
void PushMessagingAppIdentifier::RegisterProfilePrefs(
user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterDictionaryPref(prefs::kPushMessagingAppIdentifierMap);
}
// static
PushMessagingAppIdentifier PushMessagingAppIdentifier::Generate(
const GURL& origin, int64_t service_worker_registration_id)
{
std::string guid = base::GenerateGUID();
CHECK(!guid.empty());
std::string app_id = kPushMessagingAppIdentifierPrefix + origin.spec()
+ kSeparator + guid;
PushMessagingAppIdentifier app_identifier(app_id, origin,
service_worker_registration_id);
app_identifier.DCheckValid();
return app_identifier;
}
// static
PushMessagingAppIdentifier PushMessagingAppIdentifier::FindByAppId(
Profile* profile, const std::string& app_id) {
if (!base::StartsWith(app_id, kPushMessagingAppIdentifierPrefix,
base::CompareCase::INSENSITIVE_ASCII)) {
return PushMessagingAppIdentifier();
}
// Since we now know this is a Push Messaging app_id, check the case hasn't
// been mangled (crbug.com/461867).
DCHECK_EQ(kPushMessagingAppIdentifierPrefix, app_id.substr(0, kPrefixLength));
DCHECK_GE(app_id.size(), kPrefixLength + kGuidLength);
DCHECK_EQ(app_id.substr(app_id.size() - kGuidLength),
base::StringToUpperASCII(
app_id.substr(app_id.size() - kGuidLength)));
const base::DictionaryValue* map =
profile->GetPrefs()->GetDictionary(prefs::kPushMessagingAppIdentifierMap);
std::string map_value;
if (!map->GetStringWithoutPathExpansion(app_id, &map_value))
return PushMessagingAppIdentifier();
GURL origin;
int64_t service_worker_registration_id;
if (!GetOriginAndSWRFromPrefValue(map_value, &origin,
&service_worker_registration_id)) {
NOTREACHED();
return PushMessagingAppIdentifier();
}
PushMessagingAppIdentifier app_identifier(app_id, origin,
service_worker_registration_id);
app_identifier.DCheckValid();
return app_identifier;
}
// static
PushMessagingAppIdentifier PushMessagingAppIdentifier::FindByServiceWorker(
Profile* profile, const GURL& origin,
int64_t service_worker_registration_id)
{
const base::StringValue pref_value =
base::StringValue(MakePrefValue(origin, service_worker_registration_id));
const base::DictionaryValue* map =
profile->GetPrefs()->GetDictionary(prefs::kPushMessagingAppIdentifierMap);
for (auto it = base::DictionaryValue::Iterator(*map); !it.IsAtEnd();
it.Advance()) {
if (it.value().Equals(&pref_value))
return FindByAppId(profile, it.key());
}
return PushMessagingAppIdentifier();
}
// static
std::vector<PushMessagingAppIdentifier> PushMessagingAppIdentifier::GetAll(
Profile* profile) {
std::vector<PushMessagingAppIdentifier> result;
const base::DictionaryValue* map =
profile->GetPrefs()->GetDictionary(prefs::kPushMessagingAppIdentifierMap);
for (auto it = base::DictionaryValue::Iterator(*map); !it.IsAtEnd();
it.Advance()) {
result.push_back(FindByAppId(profile, it.key()));
}
return result;
}
PushMessagingAppIdentifier::PushMessagingAppIdentifier()
: origin_(GURL::EmptyGURL()),
service_worker_registration_id_(-1) {
}
PushMessagingAppIdentifier::PushMessagingAppIdentifier(
const std::string& app_id,
const GURL& origin,
int64_t service_worker_registration_id)
: app_id_(app_id),
origin_(origin),
service_worker_registration_id_(service_worker_registration_id) {
}
PushMessagingAppIdentifier::~PushMessagingAppIdentifier() {
}
void PushMessagingAppIdentifier::PersistToPrefs(Profile* profile) const {
DCheckValid();
DictionaryPrefUpdate update(profile->GetPrefs(),
prefs::kPushMessagingAppIdentifierMap);
base::DictionaryValue* map = update.Get();
// Delete any stale entry with the same origin and Service Worker
// registration id (hence we ensure there is a 1:1 not 1:many mapping).
PushMessagingAppIdentifier old = FindByServiceWorker(
profile, origin_, service_worker_registration_id_);
if (!old.is_null())
map->RemoveWithoutPathExpansion(old.app_id_, nullptr /* out_value */);
map->SetStringWithoutPathExpansion(
app_id_, MakePrefValue(origin_, service_worker_registration_id_));
}
void PushMessagingAppIdentifier::DeleteFromPrefs(Profile* profile) const {
DCheckValid();
DictionaryPrefUpdate update(profile->GetPrefs(),
prefs::kPushMessagingAppIdentifierMap);
base::DictionaryValue* map = update.Get();
map->RemoveWithoutPathExpansion(app_id_, nullptr /* out_value */);
}
void PushMessagingAppIdentifier::DCheckValid() const {
DCHECK_GE(service_worker_registration_id_, 0);
DCHECK(origin_.is_valid());
DCHECK_EQ(origin_.GetOrigin(), origin_);
// "wp:"
DCHECK_EQ(kPushMessagingAppIdentifierPrefix,
app_id_.substr(0, kPrefixLength));
// Optional (origin.spec() + '#')
if (app_id_.size() != kPrefixLength + kGuidLength) {
const size_t suffix_length = 1 /* kSeparator */ + kGuidLength;
DCHECK(app_id_.size() > kPrefixLength + suffix_length);
DCHECK_EQ(origin_, GURL(app_id_.substr(
kPrefixLength, app_id_.size() - kPrefixLength - suffix_length)));
DCHECK_EQ(std::string(1, kSeparator),
app_id_.substr(app_id_.size() - suffix_length, 1));
}
// GUID
DCHECK(base::IsValidGUID(app_id_.substr(app_id_.size() - kGuidLength)));
}
|