| 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 |
| 109 PushMessagingApplicationId::PushMessagingApplicationId( |
| 110 const PushMessagingApplicationId& other) |
| 111 : app_id_guid(other.app_id_guid), |
| 112 origin(other.origin), |
| 113 service_worker_registration_id(other.service_worker_registration_id) { |
| 114 } |
| 115 |
| 94 void PushMessagingApplicationId::PersistToDisk(Profile* profile) const { | 116 void PushMessagingApplicationId::PersistToDisk(Profile* profile) const { |
| 95 DCHECK(IsValid()); | 117 DCHECK(IsValid()); |
| 96 | 118 |
| 97 DictionaryPrefUpdate update(profile->GetPrefs(), | 119 DictionaryPrefUpdate update(profile->GetPrefs(), |
| 98 prefs::kPushMessagingApplicationIdMap); | 120 prefs::kPushMessagingApplicationIdMap); |
| 99 base::DictionaryValue* map = update.Get(); | 121 base::DictionaryValue* map = update.Get(); |
| 100 | 122 |
| 101 // Delete any stale entry with the same origin and Service Worker | 123 // 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). | 124 // registration id (hence we ensure there is a 1:1 not 1:many mapping). |
| 103 PushMessagingApplicationId old = Get(profile, origin, | 125 PushMessagingApplicationId old = Get(profile, origin, |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 | 160 |
| 139 bool PushMessagingApplicationId::IsValid() const { | 161 bool PushMessagingApplicationId::IsValid() const { |
| 140 const size_t prefix_len = strlen(kPushMessagingApplicationIdPrefix); | 162 const size_t prefix_len = strlen(kPushMessagingApplicationIdPrefix); |
| 141 return origin.is_valid() && origin.GetOrigin() == origin | 163 return origin.is_valid() && origin.GetOrigin() == origin |
| 142 && service_worker_registration_id >= 0 | 164 && service_worker_registration_id >= 0 |
| 143 && !app_id_guid.compare(0, prefix_len, kPushMessagingApplicationIdPrefix) | 165 && !app_id_guid.compare(0, prefix_len, kPushMessagingApplicationIdPrefix) |
| 144 && base::IsValidGUID(app_id_guid.substr(prefix_len, std::string::npos)); | 166 && base::IsValidGUID(app_id_guid.substr(prefix_len, std::string::npos)); |
| 145 } | 167 } |
| 146 | 168 |
| 147 } // namespace gcm | 169 } // namespace gcm |
| OLD | NEW |