| 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/services/gcm/push_messaging_application_id.h" | 5 #include "chrome/browser/services/gcm/push_messaging_application_id.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 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 const base::DictionaryValue* map = | 84 const base::DictionaryValue* map = |
| 85 profile->GetPrefs()->GetDictionary(prefs::kPushMessagingApplicationIdMap); | 85 profile->GetPrefs()->GetDictionary(prefs::kPushMessagingApplicationIdMap); |
| 86 for (auto it = base::DictionaryValue::Iterator(*map); !it.IsAtEnd(); | 86 for (auto it = base::DictionaryValue::Iterator(*map); !it.IsAtEnd(); |
| 87 it.Advance()) { | 87 it.Advance()) { |
| 88 if (it.value().Equals(&origin_and_sw_id)) | 88 if (it.value().Equals(&origin_and_sw_id)) |
| 89 return Get(profile, it.key()); | 89 return Get(profile, it.key()); |
| 90 } | 90 } |
| 91 return PushMessagingApplicationId(); | 91 return PushMessagingApplicationId(); |
| 92 } | 92 } |
| 93 | 93 |
| 94 // static |
| 95 std::vector<PushMessagingApplicationId> PushMessagingApplicationId::GetAll( |
| 96 Profile* profile) { |
| 97 std::vector<PushMessagingApplicationId> result; |
| 98 |
| 99 const base::DictionaryValue* map = |
| 100 profile->GetPrefs()->GetDictionary(prefs::kPushMessagingApplicationIdMap); |
| 101 for (auto it = base::DictionaryValue::Iterator(*map); !it.IsAtEnd(); |
| 102 it.Advance()) { |
| 103 result.push_back(Get(profile, it.key())); |
| 104 } |
| 105 |
| 106 return result; |
| 107 } |
| 108 |
| 94 void PushMessagingApplicationId::PersistToDisk(Profile* profile) const { | 109 void PushMessagingApplicationId::PersistToDisk(Profile* profile) const { |
| 95 DCHECK(IsValid()); | 110 DCHECK(IsValid()); |
| 96 | 111 |
| 97 DictionaryPrefUpdate update(profile->GetPrefs(), | 112 DictionaryPrefUpdate update(profile->GetPrefs(), |
| 98 prefs::kPushMessagingApplicationIdMap); | 113 prefs::kPushMessagingApplicationIdMap); |
| 99 base::DictionaryValue* map = update.Get(); | 114 base::DictionaryValue* map = update.Get(); |
| 100 | 115 |
| 101 // Delete any stale entry with the same origin and Service Worker | 116 // Delete any stale entry with the same origin and Service Worker |
| 102 // registration id (hence we ensure there is a 1:1 not 1:many mapping). | 117 // registration id (hence we ensure there is a 1:1 not 1:many mapping). |
| 103 PushMessagingApplicationId old = Get(profile, origin, | 118 PushMessagingApplicationId old = Get(profile, origin, |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 | 153 |
| 139 bool PushMessagingApplicationId::IsValid() const { | 154 bool PushMessagingApplicationId::IsValid() const { |
| 140 const size_t prefix_len = strlen(kPushMessagingApplicationIdPrefix); | 155 const size_t prefix_len = strlen(kPushMessagingApplicationIdPrefix); |
| 141 return origin.is_valid() && origin.GetOrigin() == origin | 156 return origin.is_valid() && origin.GetOrigin() == origin |
| 142 && service_worker_registration_id >= 0 | 157 && service_worker_registration_id >= 0 |
| 143 && !app_id_guid.compare(0, prefix_len, kPushMessagingApplicationIdPrefix) | 158 && !app_id_guid.compare(0, prefix_len, kPushMessagingApplicationIdPrefix) |
| 144 && base::IsValidGUID(app_id_guid.substr(prefix_len, std::string::npos)); | 159 && base::IsValidGUID(app_id_guid.substr(prefix_len, std::string::npos)); |
| 145 } | 160 } |
| 146 | 161 |
| 147 } // namespace gcm | 162 } // namespace gcm |
| OLD | NEW |