| 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 | 11 |
| 11 #include "base/i18n/case_conversion.h" | 12 #include "base/i18n/case_conversion.h" |
| 12 #include "base/logging.h" | 13 #include "base/logging.h" |
| 13 #include "base/memory/scoped_ptr.h" | |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "components/prefs/pref_registry_simple.h" | 15 #include "components/prefs/pref_registry_simple.h" |
| 16 #include "components/prefs/scoped_user_pref_update.h" | 16 #include "components/prefs/scoped_user_pref_update.h" |
| 17 #include "ios/chrome/browser/browser_state/browser_state_info_cache_observer.h" | 17 #include "ios/chrome/browser/browser_state/browser_state_info_cache_observer.h" |
| 18 #include "ios/chrome/browser/pref_names.h" | 18 #include "ios/chrome/browser/pref_names.h" |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 const char kGAIAIdKey[] = "gaia_id"; | 21 const char kGAIAIdKey[] = "gaia_id"; |
| 22 const char kIsAuthErrorKey[] = "is_auth_error"; | 22 const char kIsAuthErrorKey[] = "is_auth_error"; |
| 23 const char kUserNameKey[] = "user_name"; | 23 const char kUserNameKey[] = "user_name"; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 41 BrowserStateInfoCache::~BrowserStateInfoCache() {} | 41 BrowserStateInfoCache::~BrowserStateInfoCache() {} |
| 42 | 42 |
| 43 void BrowserStateInfoCache::AddBrowserState( | 43 void BrowserStateInfoCache::AddBrowserState( |
| 44 const base::FilePath& browser_state_path, | 44 const base::FilePath& browser_state_path, |
| 45 const std::string& gaia_id, | 45 const std::string& gaia_id, |
| 46 const base::string16& user_name) { | 46 const base::string16& user_name) { |
| 47 std::string key = CacheKeyFromBrowserStatePath(browser_state_path); | 47 std::string key = CacheKeyFromBrowserStatePath(browser_state_path); |
| 48 DictionaryPrefUpdate update(prefs_, prefs::kBrowserStateInfoCache); | 48 DictionaryPrefUpdate update(prefs_, prefs::kBrowserStateInfoCache); |
| 49 base::DictionaryValue* cache = update.Get(); | 49 base::DictionaryValue* cache = update.Get(); |
| 50 | 50 |
| 51 scoped_ptr<base::DictionaryValue> info(new base::DictionaryValue); | 51 std::unique_ptr<base::DictionaryValue> info(new base::DictionaryValue); |
| 52 info->SetString(kGAIAIdKey, gaia_id); | 52 info->SetString(kGAIAIdKey, gaia_id); |
| 53 info->SetString(kUserNameKey, user_name); | 53 info->SetString(kUserNameKey, user_name); |
| 54 cache->SetWithoutPathExpansion(key, info.release()); | 54 cache->SetWithoutPathExpansion(key, info.release()); |
| 55 AddBrowserStateCacheKey(key); | 55 AddBrowserStateCacheKey(key); |
| 56 | 56 |
| 57 FOR_EACH_OBSERVER(BrowserStateInfoCacheObserver, observer_list_, | 57 FOR_EACH_OBSERVER(BrowserStateInfoCacheObserver, observer_list_, |
| 58 OnBrowserStateAdded(browser_state_path)); | 58 OnBrowserStateAdded(browser_state_path)); |
| 59 } | 59 } |
| 60 | 60 |
| 61 void BrowserStateInfoCache::AddObserver( | 61 void BrowserStateInfoCache::AddObserver( |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 void BrowserStateInfoCache::SetAuthInfoOfBrowserStateAtIndex( | 140 void BrowserStateInfoCache::SetAuthInfoOfBrowserStateAtIndex( |
| 141 size_t index, | 141 size_t index, |
| 142 const std::string& gaia_id, | 142 const std::string& gaia_id, |
| 143 const base::string16& user_name) { | 143 const base::string16& user_name) { |
| 144 // If both gaia_id and username are unchanged, abort early. | 144 // If both gaia_id and username are unchanged, abort early. |
| 145 if (gaia_id == GetGAIAIdOfBrowserStateAtIndex(index) && | 145 if (gaia_id == GetGAIAIdOfBrowserStateAtIndex(index) && |
| 146 user_name == GetUserNameOfBrowserStateAtIndex(index)) { | 146 user_name == GetUserNameOfBrowserStateAtIndex(index)) { |
| 147 return; | 147 return; |
| 148 } | 148 } |
| 149 | 149 |
| 150 scoped_ptr<base::DictionaryValue> info( | 150 std::unique_ptr<base::DictionaryValue> info( |
| 151 GetInfoForBrowserStateAtIndex(index)->DeepCopy()); | 151 GetInfoForBrowserStateAtIndex(index)->DeepCopy()); |
| 152 | 152 |
| 153 info->SetString(kGAIAIdKey, gaia_id); | 153 info->SetString(kGAIAIdKey, gaia_id); |
| 154 info->SetString(kUserNameKey, user_name); | 154 info->SetString(kUserNameKey, user_name); |
| 155 | 155 |
| 156 // This takes ownership of |info|. | 156 // This takes ownership of |info|. |
| 157 SetInfoForBrowserStateAtIndex(index, info.release()); | 157 SetInfoForBrowserStateAtIndex(index, info.release()); |
| 158 } | 158 } |
| 159 | 159 |
| 160 void BrowserStateInfoCache::SetBrowserStateIsAuthErrorAtIndex(size_t index, | 160 void BrowserStateInfoCache::SetBrowserStateIsAuthErrorAtIndex(size_t index, |
| 161 bool value) { | 161 bool value) { |
| 162 if (value == BrowserStateIsAuthErrorAtIndex(index)) | 162 if (value == BrowserStateIsAuthErrorAtIndex(index)) |
| 163 return; | 163 return; |
| 164 | 164 |
| 165 scoped_ptr<base::DictionaryValue> info( | 165 std::unique_ptr<base::DictionaryValue> info( |
| 166 GetInfoForBrowserStateAtIndex(index)->DeepCopy()); | 166 GetInfoForBrowserStateAtIndex(index)->DeepCopy()); |
| 167 info->SetBoolean(kIsAuthErrorKey, value); | 167 info->SetBoolean(kIsAuthErrorKey, value); |
| 168 // This takes ownership of |info|. | 168 // This takes ownership of |info|. |
| 169 SetInfoForBrowserStateAtIndex(index, info.release()); | 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 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 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 |