OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "ios/chrome/browser/browser_state/browser_state_info_cache.h" | 5 #include "ios/chrome/browser/browser_state/browser_state_info_cache.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <memory> | 10 #include <memory> |
| 11 #include <utility> |
11 | 12 |
12 #include "base/i18n/case_conversion.h" | 13 #include "base/i18n/case_conversion.h" |
13 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/memory/ptr_util.h" |
14 #include "base/values.h" | 16 #include "base/values.h" |
15 #include "components/prefs/pref_registry_simple.h" | 17 #include "components/prefs/pref_registry_simple.h" |
16 #include "components/prefs/scoped_user_pref_update.h" | 18 #include "components/prefs/scoped_user_pref_update.h" |
17 #include "ios/chrome/browser/browser_state/browser_state_info_cache_observer.h" | 19 #include "ios/chrome/browser/browser_state/browser_state_info_cache_observer.h" |
18 #include "ios/chrome/browser/pref_names.h" | 20 #include "ios/chrome/browser/pref_names.h" |
19 | 21 |
20 namespace { | 22 namespace { |
21 const char kGAIAIdKey[] = "gaia_id"; | 23 const char kGAIAIdKey[] = "gaia_id"; |
22 const char kIsAuthErrorKey[] = "is_auth_error"; | 24 const char kIsAuthErrorKey[] = "is_auth_error"; |
23 const char kUserNameKey[] = "user_name"; | 25 const char kUserNameKey[] = "user_name"; |
(...skipping 20 matching lines...) Expand all Loading... |
44 const base::FilePath& browser_state_path, | 46 const base::FilePath& browser_state_path, |
45 const std::string& gaia_id, | 47 const std::string& gaia_id, |
46 const base::string16& user_name) { | 48 const base::string16& user_name) { |
47 std::string key = CacheKeyFromBrowserStatePath(browser_state_path); | 49 std::string key = CacheKeyFromBrowserStatePath(browser_state_path); |
48 DictionaryPrefUpdate update(prefs_, prefs::kBrowserStateInfoCache); | 50 DictionaryPrefUpdate update(prefs_, prefs::kBrowserStateInfoCache); |
49 base::DictionaryValue* cache = update.Get(); | 51 base::DictionaryValue* cache = update.Get(); |
50 | 52 |
51 std::unique_ptr<base::DictionaryValue> info(new base::DictionaryValue); | 53 std::unique_ptr<base::DictionaryValue> info(new base::DictionaryValue); |
52 info->SetString(kGAIAIdKey, gaia_id); | 54 info->SetString(kGAIAIdKey, gaia_id); |
53 info->SetString(kUserNameKey, user_name); | 55 info->SetString(kUserNameKey, user_name); |
54 cache->SetWithoutPathExpansion(key, info.release()); | 56 cache->SetWithoutPathExpansion(key, std::move(info)); |
55 AddBrowserStateCacheKey(key); | 57 AddBrowserStateCacheKey(key); |
56 | 58 |
57 for (auto& observer : observer_list_) | 59 for (auto& observer : observer_list_) |
58 observer.OnBrowserStateAdded(browser_state_path); | 60 observer.OnBrowserStateAdded(browser_state_path); |
59 } | 61 } |
60 | 62 |
61 void BrowserStateInfoCache::AddObserver( | 63 void BrowserStateInfoCache::AddObserver( |
62 BrowserStateInfoCacheObserver* observer) { | 64 BrowserStateInfoCacheObserver* observer) { |
63 observer_list_.AddObserver(observer); | 65 observer_list_.AddObserver(observer); |
64 } | 66 } |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 void BrowserStateInfoCache::SetAuthInfoOfBrowserStateAtIndex( | 142 void BrowserStateInfoCache::SetAuthInfoOfBrowserStateAtIndex( |
141 size_t index, | 143 size_t index, |
142 const std::string& gaia_id, | 144 const std::string& gaia_id, |
143 const base::string16& user_name) { | 145 const base::string16& user_name) { |
144 // If both gaia_id and username are unchanged, abort early. | 146 // If both gaia_id and username are unchanged, abort early. |
145 if (gaia_id == GetGAIAIdOfBrowserStateAtIndex(index) && | 147 if (gaia_id == GetGAIAIdOfBrowserStateAtIndex(index) && |
146 user_name == GetUserNameOfBrowserStateAtIndex(index)) { | 148 user_name == GetUserNameOfBrowserStateAtIndex(index)) { |
147 return; | 149 return; |
148 } | 150 } |
149 | 151 |
150 std::unique_ptr<base::DictionaryValue> info( | 152 auto info = base::MakeUnique<base::DictionaryValue>( |
151 GetInfoForBrowserStateAtIndex(index)->DeepCopy()); | 153 *GetInfoForBrowserStateAtIndex(index)); |
152 | 154 |
153 info->SetString(kGAIAIdKey, gaia_id); | 155 info->SetString(kGAIAIdKey, gaia_id); |
154 info->SetString(kUserNameKey, user_name); | 156 info->SetString(kUserNameKey, user_name); |
155 | 157 |
156 // This takes ownership of |info|. | 158 SetInfoForBrowserStateAtIndex(index, std::move(info)); |
157 SetInfoForBrowserStateAtIndex(index, info.release()); | |
158 } | 159 } |
159 | 160 |
160 void BrowserStateInfoCache::SetBrowserStateIsAuthErrorAtIndex(size_t index, | 161 void BrowserStateInfoCache::SetBrowserStateIsAuthErrorAtIndex(size_t index, |
161 bool value) { | 162 bool value) { |
162 if (value == BrowserStateIsAuthErrorAtIndex(index)) | 163 if (value == BrowserStateIsAuthErrorAtIndex(index)) |
163 return; | 164 return; |
164 | 165 |
165 std::unique_ptr<base::DictionaryValue> info( | 166 auto info = base::MakeUnique<base::DictionaryValue>( |
166 GetInfoForBrowserStateAtIndex(index)->DeepCopy()); | 167 *GetInfoForBrowserStateAtIndex(index)); |
167 info->SetBoolean(kIsAuthErrorKey, value); | 168 info->SetBoolean(kIsAuthErrorKey, value); |
168 // This takes ownership of |info|. | 169 SetInfoForBrowserStateAtIndex(index, std::move(info)); |
169 SetInfoForBrowserStateAtIndex(index, info.release()); | |
170 } | 170 } |
171 | 171 |
172 const base::FilePath& BrowserStateInfoCache::GetUserDataDir() const { | 172 const base::FilePath& BrowserStateInfoCache::GetUserDataDir() const { |
173 return user_data_dir_; | 173 return user_data_dir_; |
174 } | 174 } |
175 | 175 |
176 // static | 176 // static |
177 void BrowserStateInfoCache::RegisterPrefs(PrefRegistrySimple* registry) { | 177 void BrowserStateInfoCache::RegisterPrefs(PrefRegistrySimple* registry) { |
178 registry->RegisterDictionaryPref(prefs::kBrowserStateInfoCache); | 178 registry->RegisterDictionaryPref(prefs::kBrowserStateInfoCache); |
179 } | 179 } |
180 | 180 |
181 const base::DictionaryValue* | 181 const base::DictionaryValue* |
182 BrowserStateInfoCache::GetInfoForBrowserStateAtIndex(size_t index) const { | 182 BrowserStateInfoCache::GetInfoForBrowserStateAtIndex(size_t index) const { |
183 DCHECK_LT(index, GetNumberOfBrowserStates()); | 183 DCHECK_LT(index, GetNumberOfBrowserStates()); |
184 const base::DictionaryValue* cache = | 184 const base::DictionaryValue* cache = |
185 prefs_->GetDictionary(prefs::kBrowserStateInfoCache); | 185 prefs_->GetDictionary(prefs::kBrowserStateInfoCache); |
186 const base::DictionaryValue* info = nullptr; | 186 const base::DictionaryValue* info = nullptr; |
187 cache->GetDictionaryWithoutPathExpansion(sorted_keys_[index], &info); | 187 cache->GetDictionaryWithoutPathExpansion(sorted_keys_[index], &info); |
188 return info; | 188 return info; |
189 } | 189 } |
190 | 190 |
191 void BrowserStateInfoCache::SetInfoForBrowserStateAtIndex( | 191 void BrowserStateInfoCache::SetInfoForBrowserStateAtIndex( |
192 size_t index, | 192 size_t index, |
193 base::DictionaryValue* info) { | 193 std::unique_ptr<base::DictionaryValue> info) { |
194 DictionaryPrefUpdate update(prefs_, prefs::kBrowserStateInfoCache); | 194 DictionaryPrefUpdate update(prefs_, prefs::kBrowserStateInfoCache); |
195 base::DictionaryValue* cache = update.Get(); | 195 base::DictionaryValue* cache = update.Get(); |
196 cache->SetWithoutPathExpansion(sorted_keys_[index], info); | 196 cache->SetWithoutPathExpansion(sorted_keys_[index], std::move(info)); |
197 } | 197 } |
198 | 198 |
199 std::string BrowserStateInfoCache::CacheKeyFromBrowserStatePath( | 199 std::string BrowserStateInfoCache::CacheKeyFromBrowserStatePath( |
200 const base::FilePath& browser_state_path) const { | 200 const base::FilePath& browser_state_path) const { |
201 DCHECK(user_data_dir_ == browser_state_path.DirName()); | 201 DCHECK(user_data_dir_ == browser_state_path.DirName()); |
202 base::FilePath base_name = browser_state_path.BaseName(); | 202 base::FilePath base_name = browser_state_path.BaseName(); |
203 return base_name.MaybeAsASCII(); | 203 return base_name.MaybeAsASCII(); |
204 } | 204 } |
205 | 205 |
206 void BrowserStateInfoCache::AddBrowserStateCacheKey(const std::string& key) { | 206 void BrowserStateInfoCache::AddBrowserStateCacheKey(const std::string& key) { |
207 sorted_keys_.insert( | 207 sorted_keys_.insert( |
208 std::upper_bound(sorted_keys_.begin(), sorted_keys_.end(), key), key); | 208 std::upper_bound(sorted_keys_.begin(), sorted_keys_.end(), key), key); |
209 } | 209 } |
OLD | NEW |