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

Side by Side Diff: chrome/browser/push_messaging/push_messaging_app_identifier.cc

Issue 1141613003: Push API: Include origin in generated app_ids (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@ident_test
Patch Set: Created 5 years, 7 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "chrome/browser/push_messaging/push_messaging_app_identifier.h" 5 #include "chrome/browser/push_messaging/push_messaging_app_identifier.h"
6 6
7 #include <string.h>
8
7 #include "base/guid.h" 9 #include "base/guid.h"
8 #include "base/logging.h" 10 #include "base/logging.h"
9 #include "base/prefs/pref_service.h" 11 #include "base/prefs/pref_service.h"
10 #include "base/prefs/scoped_user_pref_update.h" 12 #include "base/prefs/scoped_user_pref_update.h"
11 #include "base/strings/string_number_conversions.h" 13 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_split.h" 14 #include "base/strings/string_split.h"
13 #include "base/strings/string_util.h" 15 #include "base/strings/string_util.h"
14 #include "base/values.h" 16 #include "base/values.h"
15 #include "chrome/browser/profiles/profile.h" 17 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/common/pref_names.h" 18 #include "chrome/common/pref_names.h"
17 #include "components/pref_registry/pref_registry_syncable.h" 19 #include "components/pref_registry/pref_registry_syncable.h"
18 20
19 namespace { 21 namespace {
22
20 const char kSeparator = '#'; // Ok as only the origin of the url is used. 23 const char kSeparator = '#'; // Ok as only the origin of the url is used.
24 const int kGuidLength = 36; // "%08X-%04X-%04X-%04X-%012llX"
25
26 bool IsLegacyAppId(const std::string& app_id) {
27 return app_id.size() == strlen(kPushMessagingAppIdentifierPrefix)
Peter Beverloo 2015/05/13 13:04:14 I'd slightly prefer to use IsValidGUID for this.
johnme 2015/05/13 16:18:09 How about: if (app_id.size() == strlen(kPushMes
28 + kGuidLength;
29 }
30
31 GURL GetOriginFromModernAppId(const std::string& app_id) {
Peter Beverloo 2015/05/13 13:04:14 s/Modern//. The omission from "Legacy" in the name
johnme 2015/05/13 16:18:09 Done.
32 const size_t prefix_len = strlen(kPushMessagingAppIdentifierPrefix);
33 const size_t suffix_len = 1 /* kSeparator */ + kGuidLength;
34 CHECK(app_id.size() > prefix_len + suffix_len);
35 return GURL(app_id.substr(prefix_len,
36 app_id.size() - prefix_len - suffix_len));
37 }
38
39 bool GetOriginAndSWRFromLegacyMapValue(
Peter Beverloo 2015/05/13 13:04:14 nit: While the variable in PushMessagingAppIdentif
johnme 2015/05/13 16:18:09 Done.
40 const std::string& map_value, GURL* origin,
41 int64_t* service_worker_registration_id) {
42 // map_value is origin + kSeparator + service_worker_registration_id
43 std::vector<std::string> parts;
44 base::SplitString(map_value, kSeparator, &parts);
45 if (parts.size() != 2)
46 return false;
47
48 if (!base::StringToInt64(parts[1], service_worker_registration_id))
49 return false;
50
51 *origin = GURL(parts[0]);
52
53 return true;
54 }
55
21 } // namespace 56 } // namespace
22 57
23 const char kPushMessagingAppIdentifierPrefix[] = "wp:"; 58 const char kPushMessagingAppIdentifierPrefix[] = "wp:";
24 59
25 // static 60 // static
26 void PushMessagingAppIdentifier::RegisterProfilePrefs( 61 void PushMessagingAppIdentifier::RegisterProfilePrefs(
27 user_prefs::PrefRegistrySyncable* registry) { 62 user_prefs::PrefRegistrySyncable* registry) {
28 registry->RegisterDictionaryPref(prefs::kPushMessagingAppIdentifierMap); 63 registry->RegisterDictionaryPref(prefs::kPushMessagingAppIdentifierMap);
29 } 64 }
30 65
31 // static 66 // static
32 PushMessagingAppIdentifier PushMessagingAppIdentifier::Generate( 67 PushMessagingAppIdentifier PushMessagingAppIdentifier::Generate(
33 const GURL& origin, int64_t service_worker_registration_id) 68 const GURL& origin, int64_t service_worker_registration_id)
34 { 69 {
35 std::string guid = base::GenerateGUID(); 70 std::string guid = base::GenerateGUID();
36 CHECK(!guid.empty()); 71 CHECK(!guid.empty());
37 std::string app_id = kPushMessagingAppIdentifierPrefix + guid; 72 std::string app_id = kPushMessagingAppIdentifierPrefix + origin.spec()
Peter Beverloo 2015/05/13 13:04:14 How certain are we that |origin|==|origin.GetOrigi
Peter Beverloo 2015/05/13 13:04:14 You could consider using base::StringPrintF here,
johnme 2015/05/13 16:18:09 Seems more legible to use + (even if minorly less
johnme 2015/05/13 16:18:09 DCheckValid enforces this if DCHECKs are on. Happy
73 + kSeparator + guid;
38 74
39 PushMessagingAppIdentifier app_identifier(app_id, origin, 75 PushMessagingAppIdentifier app_identifier(app_id, origin,
40 service_worker_registration_id); 76 service_worker_registration_id);
41 app_identifier.DCheckValid(); 77 app_identifier.DCheckValid();
42 return app_identifier; 78 return app_identifier;
43 } 79 }
44 80
45 // static 81 // static
46 PushMessagingAppIdentifier PushMessagingAppIdentifier::Get( 82 PushMessagingAppIdentifier PushMessagingAppIdentifier::Get(
47 Profile* profile, const std::string& app_id) { 83 Profile* profile, const std::string& app_id) {
48 // Workaround crbug.com/461867 in GCM where it converts subtypes to lowercase.
49 // TODO(johnme): Remove this when obsolete
50 const size_t prefix_len = strlen(kPushMessagingAppIdentifierPrefix);
51 if (app_id.size() < prefix_len)
52 return PushMessagingAppIdentifier();
53 std::string uppercase_app_id =
54 app_id.substr(0, prefix_len) +
55 StringToUpperASCII(app_id.substr(prefix_len, std::string::npos));
56
57 const base::DictionaryValue* map = 84 const base::DictionaryValue* map =
58 profile->GetPrefs()->GetDictionary(prefs::kPushMessagingAppIdentifierMap); 85 profile->GetPrefs()->GetDictionary(prefs::kPushMessagingAppIdentifierMap);
59 86
60 std::string origin_and_sw_id; 87 std::string map_value;
61 if (!map->GetStringWithoutPathExpansion(uppercase_app_id, &origin_and_sw_id)) 88 if (!map->GetStringWithoutPathExpansion(app_id, &map_value))
62 return PushMessagingAppIdentifier(); 89 return PushMessagingAppIdentifier();
63 90
64 std::vector<std::string> parts; 91 GURL origin;
65 base::SplitString(origin_and_sw_id, kSeparator, &parts); 92 int64_t service_worker_registration_id;
66 if (parts.size() != 2) 93 if (IsLegacyAppId(app_id)) {
67 return PushMessagingAppIdentifier(); 94 if (!GetOriginAndSWRFromLegacyMapValue(map_value, &origin,
95 &service_worker_registration_id)) {
96 NOTREACHED();
97 return PushMessagingAppIdentifier();
98 }
99 } else {
100 origin = GetOriginFromModernAppId(app_id);
68 101
69 GURL origin = GURL(parts[0]); 102 if (!base::StringToInt64(map_value, &service_worker_registration_id)) {
103 NOTREACHED();
104 return PushMessagingAppIdentifier();
105 }
106 }
70 107
71 int64_t service_worker_registration_id; 108 PushMessagingAppIdentifier app_identifier(app_id, origin,
72 if (!base::StringToInt64(parts[1], &service_worker_registration_id))
73 return PushMessagingAppIdentifier();
74
75 PushMessagingAppIdentifier app_identifier(uppercase_app_id, origin,
76 service_worker_registration_id); 109 service_worker_registration_id);
77 app_identifier.DCheckValid(); 110 app_identifier.DCheckValid();
78 return app_identifier; 111 return app_identifier;
79 } 112 }
80 113
81 // static 114 // static
82 PushMessagingAppIdentifier PushMessagingAppIdentifier::Get( 115 PushMessagingAppIdentifier PushMessagingAppIdentifier::Get(
83 Profile* profile, const GURL& origin, 116 Profile* profile, const GURL& origin,
84 int64_t service_worker_registration_id) 117 int64_t service_worker_registration_id)
85 { 118 {
86 base::StringValue origin_and_sw_id = base::StringValue(origin.spec() + 119 const base::StringValue modern_swr_id_only = base::StringValue(
87 kSeparator + base::Int64ToString(service_worker_registration_id)); 120 base::Int64ToString(service_worker_registration_id));
121 const base::StringValue legacy_origin_and_swr_id = base::StringValue(
122 origin.spec() + kSeparator + modern_swr_id_only.GetString());
88 123
89 const base::DictionaryValue* map = 124 const base::DictionaryValue* map =
90 profile->GetPrefs()->GetDictionary(prefs::kPushMessagingAppIdentifierMap); 125 profile->GetPrefs()->GetDictionary(prefs::kPushMessagingAppIdentifierMap);
91 for (auto it = base::DictionaryValue::Iterator(*map); !it.IsAtEnd(); 126 for (auto it = base::DictionaryValue::Iterator(*map); !it.IsAtEnd();
92 it.Advance()) { 127 it.Advance()) {
93 if (it.value().Equals(&origin_and_sw_id)) 128 if (IsLegacyAppId(it.key())) {
129 if (it.value().Equals(&legacy_origin_and_swr_id))
130 return Get(profile, it.key());
131 } else if (GetOriginFromModernAppId(it.key()) == origin &&
132 it.value().Equals(&modern_swr_id_only)) {
94 return Get(profile, it.key()); 133 return Get(profile, it.key());
134 }
95 } 135 }
96 return PushMessagingAppIdentifier(); 136 return PushMessagingAppIdentifier();
97 } 137 }
98 138
99 // static 139 // static
100 std::vector<PushMessagingAppIdentifier> PushMessagingAppIdentifier::GetAll( 140 std::vector<PushMessagingAppIdentifier> PushMessagingAppIdentifier::GetAll(
101 Profile* profile) { 141 Profile* profile) {
102 std::vector<PushMessagingAppIdentifier> result; 142 std::vector<PushMessagingAppIdentifier> result;
103 143
104 const base::DictionaryValue* map = 144 const base::DictionaryValue* map =
(...skipping 28 matching lines...) Expand all
133 173
134 DictionaryPrefUpdate update(profile->GetPrefs(), 174 DictionaryPrefUpdate update(profile->GetPrefs(),
135 prefs::kPushMessagingAppIdentifierMap); 175 prefs::kPushMessagingAppIdentifierMap);
136 base::DictionaryValue* map = update.Get(); 176 base::DictionaryValue* map = update.Get();
137 177
138 // Delete any stale entry with the same origin and Service Worker 178 // Delete any stale entry with the same origin and Service Worker
139 // registration id (hence we ensure there is a 1:1 not 1:many mapping). 179 // registration id (hence we ensure there is a 1:1 not 1:many mapping).
140 PushMessagingAppIdentifier old = Get(profile, origin_, 180 PushMessagingAppIdentifier old = Get(profile, origin_,
141 service_worker_registration_id_); 181 service_worker_registration_id_);
142 if (!old.is_null()) 182 if (!old.is_null())
143 map->RemoveWithoutPathExpansion(old.app_id_, nullptr); 183 map->RemoveWithoutPathExpansion(old.app_id_, nullptr /* out_value */);
144 184
145 std::string origin_and_sw_id = origin_.spec() + kSeparator + 185 map->SetStringWithoutPathExpansion(
146 base::Int64ToString(service_worker_registration_id_); 186 app_id_, base::Int64ToString(service_worker_registration_id_));
147 map->SetStringWithoutPathExpansion(app_id_, origin_and_sw_id);
148 } 187 }
149 188
150 void PushMessagingAppIdentifier::DeleteFromPrefs(Profile* profile) const { 189 void PushMessagingAppIdentifier::DeleteFromPrefs(Profile* profile) const {
151 DCheckValid(); 190 DCheckValid();
152 191
153 DictionaryPrefUpdate update(profile->GetPrefs(), 192 DictionaryPrefUpdate update(profile->GetPrefs(),
154 prefs::kPushMessagingAppIdentifierMap); 193 prefs::kPushMessagingAppIdentifierMap);
155 base::DictionaryValue* map = update.Get(); 194 base::DictionaryValue* map = update.Get();
156 map->RemoveWithoutPathExpansion(app_id_, nullptr); 195 map->RemoveWithoutPathExpansion(app_id_, nullptr /* out_value */);
157 } 196 }
158 197
159 void PushMessagingAppIdentifier::DCheckValid() const { 198 void PushMessagingAppIdentifier::DCheckValid() const {
160 const size_t prefix_len = strlen(kPushMessagingAppIdentifierPrefix); 199 const size_t prefix_len = strlen(kPushMessagingAppIdentifierPrefix);
161 DCHECK_GE(service_worker_registration_id_, 0); 200 DCHECK_GE(service_worker_registration_id_, 0);
162 DCHECK(origin_.is_valid()); 201 DCHECK(origin_.is_valid());
163 DCHECK_EQ(origin_.GetOrigin(), origin_); 202 DCHECK_EQ(origin_.GetOrigin(), origin_);
164 DCHECK_EQ(app_id_.substr(0, prefix_len), kPushMessagingAppIdentifierPrefix); 203 DCHECK_EQ(app_id_.substr(0, prefix_len), kPushMessagingAppIdentifierPrefix);
165 DCHECK(base::IsValidGUID(app_id_.substr(prefix_len, std::string::npos))); 204 if (!IsLegacyAppId(app_id_)) {
205 DCHECK_EQ(origin_, GetOriginFromModernAppId(app_id_));
206 DCHECK_EQ(std::string(1, kSeparator),
207 app_id_.substr(app_id_.size() - kGuidLength - 1, 1));
208 }
209 DCHECK(base::IsValidGUID(app_id_.substr(app_id_.size() - kGuidLength,
210 std::string::npos)));
Peter Beverloo 2015/05/13 13:04:14 std::string::npos is the default argument value, y
johnme 2015/05/13 16:18:09 Done.
166 } 211 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698