OLD | NEW |
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 "base/guid.h" | 7 #include "base/guid.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
10 #include "base/prefs/scoped_user_pref_update.h" | 10 #include "base/prefs/scoped_user_pref_update.h" |
(...skipping 12 matching lines...) Expand all Loading... |
23 const char kPushMessagingAppIdentifierPrefix[] = "wp:"; | 23 const char kPushMessagingAppIdentifierPrefix[] = "wp:"; |
24 | 24 |
25 // static | 25 // static |
26 void PushMessagingAppIdentifier::RegisterProfilePrefs( | 26 void PushMessagingAppIdentifier::RegisterProfilePrefs( |
27 user_prefs::PrefRegistrySyncable* registry) { | 27 user_prefs::PrefRegistrySyncable* registry) { |
28 registry->RegisterDictionaryPref(prefs::kPushMessagingAppIdentifierMap); | 28 registry->RegisterDictionaryPref(prefs::kPushMessagingAppIdentifierMap); |
29 } | 29 } |
30 | 30 |
31 // static | 31 // static |
32 PushMessagingAppIdentifier PushMessagingAppIdentifier::Generate( | 32 PushMessagingAppIdentifier PushMessagingAppIdentifier::Generate( |
33 const GURL& origin, int64 service_worker_registration_id) | 33 const GURL& origin, int64_t service_worker_registration_id) |
34 { | 34 { |
35 std::string guid = base::GenerateGUID(); | 35 std::string guid = base::GenerateGUID(); |
36 CHECK(!guid.empty()); | 36 CHECK(!guid.empty()); |
37 std::string app_id = kPushMessagingAppIdentifierPrefix + guid; | 37 std::string app_id = kPushMessagingAppIdentifierPrefix + guid; |
38 | 38 |
39 PushMessagingAppIdentifier app_identifier(app_id, origin, | 39 PushMessagingAppIdentifier app_identifier(app_id, origin, |
40 service_worker_registration_id); | 40 service_worker_registration_id); |
41 DCHECK(app_identifier.IsValid()); | 41 app_identifier.DCheckValid(); |
42 return app_identifier; | 42 return app_identifier; |
43 } | 43 } |
44 | 44 |
45 // static | 45 // static |
46 PushMessagingAppIdentifier PushMessagingAppIdentifier::Get( | 46 PushMessagingAppIdentifier PushMessagingAppIdentifier::Get( |
47 Profile* profile, const std::string& app_id) { | 47 Profile* profile, const std::string& app_id) { |
48 // Workaround crbug.com/461867 in GCM where it converts subtypes to lowercase. | 48 // Workaround crbug.com/461867 in GCM where it converts subtypes to lowercase. |
49 // TODO(johnme): Remove this when obsolete | 49 // TODO(johnme): Remove this when obsolete |
50 const size_t prefix_len = strlen(kPushMessagingAppIdentifierPrefix); | 50 const size_t prefix_len = strlen(kPushMessagingAppIdentifierPrefix); |
51 if (app_id.size() < prefix_len) | 51 if (app_id.size() < prefix_len) |
52 return PushMessagingAppIdentifier(); | 52 return PushMessagingAppIdentifier(); |
53 std::string uppercase_app_id = | 53 std::string uppercase_app_id = |
54 app_id.substr(0, prefix_len) + | 54 app_id.substr(0, prefix_len) + |
55 StringToUpperASCII(app_id.substr(prefix_len, std::string::npos)); | 55 StringToUpperASCII(app_id.substr(prefix_len, std::string::npos)); |
56 | 56 |
57 const base::DictionaryValue* map = | 57 const base::DictionaryValue* map = |
58 profile->GetPrefs()->GetDictionary(prefs::kPushMessagingAppIdentifierMap); | 58 profile->GetPrefs()->GetDictionary(prefs::kPushMessagingAppIdentifierMap); |
59 | 59 |
60 std::string origin_and_sw_id; | 60 std::string origin_and_sw_id; |
61 if (!map->GetStringWithoutPathExpansion(uppercase_app_id, &origin_and_sw_id)) | 61 if (!map->GetStringWithoutPathExpansion(uppercase_app_id, &origin_and_sw_id)) |
62 return PushMessagingAppIdentifier(); | 62 return PushMessagingAppIdentifier(); |
63 | 63 |
64 std::vector<std::string> parts; | 64 std::vector<std::string> parts; |
65 base::SplitString(origin_and_sw_id, kSeparator, &parts); | 65 base::SplitString(origin_and_sw_id, kSeparator, &parts); |
66 if (parts.size() != 2) | 66 if (parts.size() != 2) |
67 return PushMessagingAppIdentifier(); | 67 return PushMessagingAppIdentifier(); |
68 | 68 |
69 GURL origin = GURL(parts[0]); | 69 GURL origin = GURL(parts[0]); |
70 | 70 |
71 int64 service_worker_registration_id; | 71 int64_t service_worker_registration_id; |
72 if (!base::StringToInt64(parts[1], &service_worker_registration_id)) | 72 if (!base::StringToInt64(parts[1], &service_worker_registration_id)) |
73 return PushMessagingAppIdentifier(); | 73 return PushMessagingAppIdentifier(); |
74 | 74 |
75 PushMessagingAppIdentifier app_identifier(uppercase_app_id, origin, | 75 PushMessagingAppIdentifier app_identifier(uppercase_app_id, origin, |
76 service_worker_registration_id); | 76 service_worker_registration_id); |
77 DCHECK(app_identifier.IsValid()); | 77 app_identifier.DCheckValid(); |
78 return app_identifier; | 78 return app_identifier; |
79 } | 79 } |
80 | 80 |
81 // static | 81 // static |
82 PushMessagingAppIdentifier PushMessagingAppIdentifier::Get( | 82 PushMessagingAppIdentifier PushMessagingAppIdentifier::Get( |
83 Profile* profile, const GURL& origin, int64 service_worker_registration_id) | 83 Profile* profile, const GURL& origin, |
| 84 int64_t service_worker_registration_id) |
84 { | 85 { |
85 base::StringValue origin_and_sw_id = base::StringValue(origin.spec() + | 86 base::StringValue origin_and_sw_id = base::StringValue(origin.spec() + |
86 kSeparator + base::Int64ToString(service_worker_registration_id)); | 87 kSeparator + base::Int64ToString(service_worker_registration_id)); |
87 | 88 |
88 const base::DictionaryValue* map = | 89 const base::DictionaryValue* map = |
89 profile->GetPrefs()->GetDictionary(prefs::kPushMessagingAppIdentifierMap); | 90 profile->GetPrefs()->GetDictionary(prefs::kPushMessagingAppIdentifierMap); |
90 for (auto it = base::DictionaryValue::Iterator(*map); !it.IsAtEnd(); | 91 for (auto it = base::DictionaryValue::Iterator(*map); !it.IsAtEnd(); |
91 it.Advance()) { | 92 it.Advance()) { |
92 if (it.value().Equals(&origin_and_sw_id)) | 93 if (it.value().Equals(&origin_and_sw_id)) |
93 return Get(profile, it.key()); | 94 return Get(profile, it.key()); |
94 } | 95 } |
95 return PushMessagingAppIdentifier(); | 96 return PushMessagingAppIdentifier(); |
96 } | 97 } |
97 | 98 |
98 // static | 99 // static |
99 std::vector<PushMessagingAppIdentifier> PushMessagingAppIdentifier::GetAll( | 100 std::vector<PushMessagingAppIdentifier> PushMessagingAppIdentifier::GetAll( |
100 Profile* profile) { | 101 Profile* profile) { |
101 std::vector<PushMessagingAppIdentifier> result; | 102 std::vector<PushMessagingAppIdentifier> result; |
102 | 103 |
103 const base::DictionaryValue* map = | 104 const base::DictionaryValue* map = |
104 profile->GetPrefs()->GetDictionary(prefs::kPushMessagingAppIdentifierMap); | 105 profile->GetPrefs()->GetDictionary(prefs::kPushMessagingAppIdentifierMap); |
105 for (auto it = base::DictionaryValue::Iterator(*map); !it.IsAtEnd(); | 106 for (auto it = base::DictionaryValue::Iterator(*map); !it.IsAtEnd(); |
106 it.Advance()) { | 107 it.Advance()) { |
107 result.push_back(Get(profile, it.key())); | 108 result.push_back(Get(profile, it.key())); |
108 } | 109 } |
109 | 110 |
110 return result; | 111 return result; |
111 } | 112 } |
112 | 113 |
113 void PushMessagingAppIdentifier::PersistToDisk(Profile* profile) const { | 114 PushMessagingAppIdentifier::PushMessagingAppIdentifier() |
114 DCHECK(IsValid()); | 115 : origin_(GURL::EmptyGURL()), |
| 116 service_worker_registration_id_(-1) { |
| 117 } |
| 118 |
| 119 PushMessagingAppIdentifier::PushMessagingAppIdentifier( |
| 120 const std::string& app_id, |
| 121 const GURL& origin, |
| 122 int64_t service_worker_registration_id) |
| 123 : app_id_(app_id), |
| 124 origin_(origin), |
| 125 service_worker_registration_id_(service_worker_registration_id) { |
| 126 } |
| 127 |
| 128 PushMessagingAppIdentifier::~PushMessagingAppIdentifier() { |
| 129 } |
| 130 |
| 131 void PushMessagingAppIdentifier::PersistToPrefs(Profile* profile) const { |
| 132 DCheckValid(); |
115 | 133 |
116 DictionaryPrefUpdate update(profile->GetPrefs(), | 134 DictionaryPrefUpdate update(profile->GetPrefs(), |
117 prefs::kPushMessagingAppIdentifierMap); | 135 prefs::kPushMessagingAppIdentifierMap); |
118 base::DictionaryValue* map = update.Get(); | 136 base::DictionaryValue* map = update.Get(); |
119 | 137 |
120 // Delete any stale entry with the same origin and Service Worker | 138 // Delete any stale entry with the same origin and Service Worker |
121 // registration id (hence we ensure there is a 1:1 not 1:many mapping). | 139 // registration id (hence we ensure there is a 1:1 not 1:many mapping). |
122 PushMessagingAppIdentifier old = Get(profile, origin_, | 140 PushMessagingAppIdentifier old = Get(profile, origin_, |
123 service_worker_registration_id_); | 141 service_worker_registration_id_); |
124 if (old.IsValid()) | 142 if (!old.is_null()) |
125 map->RemoveWithoutPathExpansion(old.app_id_, nullptr); | 143 map->RemoveWithoutPathExpansion(old.app_id_, nullptr); |
126 | 144 |
127 std::string origin_and_sw_id = origin_.spec() + kSeparator + | 145 std::string origin_and_sw_id = origin_.spec() + kSeparator + |
128 base::Int64ToString(service_worker_registration_id_); | 146 base::Int64ToString(service_worker_registration_id_); |
129 map->SetStringWithoutPathExpansion(app_id_, origin_and_sw_id); | 147 map->SetStringWithoutPathExpansion(app_id_, origin_and_sw_id); |
130 } | 148 } |
131 | 149 |
132 void PushMessagingAppIdentifier::DeleteFromDisk(Profile* profile) const { | 150 void PushMessagingAppIdentifier::DeleteFromPrefs(Profile* profile) const { |
133 DCHECK(IsValid()); | 151 DCheckValid(); |
134 | 152 |
135 DictionaryPrefUpdate update(profile->GetPrefs(), | 153 DictionaryPrefUpdate update(profile->GetPrefs(), |
136 prefs::kPushMessagingAppIdentifierMap); | 154 prefs::kPushMessagingAppIdentifierMap); |
137 base::DictionaryValue* map = update.Get(); | 155 base::DictionaryValue* map = update.Get(); |
138 map->RemoveWithoutPathExpansion(app_id_, nullptr); | 156 map->RemoveWithoutPathExpansion(app_id_, nullptr); |
139 } | 157 } |
140 | 158 |
141 PushMessagingAppIdentifier::PushMessagingAppIdentifier() | 159 void PushMessagingAppIdentifier::DCheckValid() const { |
142 : origin_(GURL::EmptyGURL()), | 160 const size_t prefix_len = strlen(kPushMessagingAppIdentifierPrefix); |
143 service_worker_registration_id_(-1) { | 161 DCHECK_GE(service_worker_registration_id_, 0); |
| 162 DCHECK(origin_.is_valid()); |
| 163 DCHECK_EQ(origin_.GetOrigin(), origin_); |
| 164 DCHECK_EQ(app_id_.substr(0, prefix_len), kPushMessagingAppIdentifierPrefix); |
| 165 DCHECK(base::IsValidGUID(app_id_.substr(prefix_len, std::string::npos))); |
144 } | 166 } |
145 | |
146 PushMessagingAppIdentifier::PushMessagingAppIdentifier( | |
147 const std::string& app_id, | |
148 const GURL& origin, | |
149 int64 service_worker_registration_id) | |
150 : app_id_(app_id), | |
151 origin_(origin), | |
152 service_worker_registration_id_(service_worker_registration_id) { | |
153 } | |
154 | |
155 PushMessagingAppIdentifier::~PushMessagingAppIdentifier() { | |
156 } | |
157 | |
158 bool PushMessagingAppIdentifier::IsValid() const { | |
159 const size_t prefix_len = strlen(kPushMessagingAppIdentifierPrefix); | |
160 return origin_.is_valid() && origin_.GetOrigin() == origin_ | |
161 && service_worker_registration_id_ >= 0 | |
162 && !app_id_.compare(0, prefix_len, kPushMessagingAppIdentifierPrefix) | |
163 && base::IsValidGUID(app_id_.substr(prefix_len, std::string::npos)); | |
164 } | |
OLD | NEW |