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 <algorithm> |
| 8 |
7 #include "base/i18n/case_conversion.h" | 9 #include "base/i18n/case_conversion.h" |
8 #include "base/logging.h" | 10 #include "base/logging.h" |
9 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
10 #include "base/prefs/pref_registry_simple.h" | 12 #include "base/prefs/pref_registry_simple.h" |
11 #include "base/prefs/scoped_user_pref_update.h" | 13 #include "base/prefs/scoped_user_pref_update.h" |
12 #include "base/values.h" | 14 #include "base/values.h" |
13 #include "ios/chrome/browser/browser_state/browser_state_info_cache_observer.h" | 15 #include "ios/chrome/browser/browser_state/browser_state_info_cache_observer.h" |
14 #include "ios/chrome/browser/pref_names.h" | 16 #include "ios/chrome/browser/pref_names.h" |
15 | 17 |
16 namespace { | 18 namespace { |
17 const char kGAIAIdKey[] = "gaia_id"; | 19 const char kGAIAIdKey[] = "gaia_id"; |
18 const char kIsAuthErrorKey[] = "is_auth_error"; | 20 const char kIsAuthErrorKey[] = "is_auth_error"; |
19 const char kNameKey[] = "name"; | |
20 const char kUserNameKey[] = "user_name"; | 21 const char kUserNameKey[] = "user_name"; |
21 } | 22 } |
22 | 23 |
23 BrowserStateInfoCache::BrowserStateInfoCache( | 24 BrowserStateInfoCache::BrowserStateInfoCache( |
24 PrefService* prefs, | 25 PrefService* prefs, |
25 const base::FilePath& user_data_dir) | 26 const base::FilePath& user_data_dir) |
26 : prefs_(prefs), user_data_dir_(user_data_dir) { | 27 : prefs_(prefs), user_data_dir_(user_data_dir) { |
27 // Populate the cache | 28 // Populate the cache |
28 DictionaryPrefUpdate update(prefs_, ios::prefs::kBrowserStateInfoCache); | 29 DictionaryPrefUpdate update(prefs_, ios::prefs::kBrowserStateInfoCache); |
29 base::DictionaryValue* cache = update.Get(); | 30 base::DictionaryValue* cache = update.Get(); |
30 for (base::DictionaryValue::Iterator it(*cache); !it.IsAtEnd(); | 31 for (base::DictionaryValue::Iterator it(*cache); !it.IsAtEnd(); |
31 it.Advance()) { | 32 it.Advance()) { |
32 base::DictionaryValue* info = nullptr; | 33 base::DictionaryValue* info = nullptr; |
33 cache->GetDictionaryWithoutPathExpansion(it.key(), &info); | 34 cache->GetDictionaryWithoutPathExpansion(it.key(), &info); |
34 base::string16 name; | 35 AddBrowserStateCacheKey(it.key()); |
35 info->GetString(kNameKey, &name); | |
36 sorted_keys_.insert(FindPositionForBrowserState(it.key(), name), it.key()); | |
37 } | 36 } |
38 } | 37 } |
39 | 38 |
40 BrowserStateInfoCache::~BrowserStateInfoCache() {} | 39 BrowserStateInfoCache::~BrowserStateInfoCache() {} |
41 | 40 |
42 void BrowserStateInfoCache::AddBrowserState( | 41 void BrowserStateInfoCache::AddBrowserState( |
43 const base::FilePath& browser_state_path, | 42 const base::FilePath& browser_state_path, |
44 const base::string16& name, | |
45 const std::string& gaia_id, | 43 const std::string& gaia_id, |
46 const base::string16& user_name) { | 44 const base::string16& user_name) { |
47 std::string key = CacheKeyFromBrowserStatePath(browser_state_path); | 45 std::string key = CacheKeyFromBrowserStatePath(browser_state_path); |
48 DictionaryPrefUpdate update(prefs_, ios::prefs::kBrowserStateInfoCache); | 46 DictionaryPrefUpdate update(prefs_, ios::prefs::kBrowserStateInfoCache); |
49 base::DictionaryValue* cache = update.Get(); | 47 base::DictionaryValue* cache = update.Get(); |
50 | 48 |
51 scoped_ptr<base::DictionaryValue> info(new base::DictionaryValue); | 49 scoped_ptr<base::DictionaryValue> info(new base::DictionaryValue); |
52 info->SetString(kNameKey, name); | |
53 info->SetString(kGAIAIdKey, gaia_id); | 50 info->SetString(kGAIAIdKey, gaia_id); |
54 info->SetString(kUserNameKey, user_name); | 51 info->SetString(kUserNameKey, user_name); |
55 cache->SetWithoutPathExpansion(key, info.release()); | 52 cache->SetWithoutPathExpansion(key, info.release()); |
56 | 53 AddBrowserStateCacheKey(key); |
57 sorted_keys_.insert(FindPositionForBrowserState(key, name), key); | |
58 | 54 |
59 FOR_EACH_OBSERVER(BrowserStateInfoCacheObserver, observer_list_, | 55 FOR_EACH_OBSERVER(BrowserStateInfoCacheObserver, observer_list_, |
60 OnBrowserStateAdded(browser_state_path)); | 56 OnBrowserStateAdded(browser_state_path)); |
61 } | 57 } |
62 | 58 |
63 void BrowserStateInfoCache::AddObserver( | 59 void BrowserStateInfoCache::AddObserver( |
64 BrowserStateInfoCacheObserver* observer) { | 60 BrowserStateInfoCacheObserver* observer) { |
65 observer_list_.AddObserver(observer); | 61 observer_list_.AddObserver(observer); |
66 } | 62 } |
67 | 63 |
68 void BrowserStateInfoCache::RemoveObserver( | 64 void BrowserStateInfoCache::RemoveObserver( |
69 BrowserStateInfoCacheObserver* observer) { | 65 BrowserStateInfoCacheObserver* observer) { |
70 observer_list_.RemoveObserver(observer); | 66 observer_list_.RemoveObserver(observer); |
71 } | 67 } |
72 | 68 |
73 void BrowserStateInfoCache::RemoveBrowserState( | 69 void BrowserStateInfoCache::RemoveBrowserState( |
74 const base::FilePath& browser_state_path) { | 70 const base::FilePath& browser_state_path) { |
75 size_t browser_state_index = | 71 size_t browser_state_index = |
76 GetIndexOfBrowserStateWithPath(browser_state_path); | 72 GetIndexOfBrowserStateWithPath(browser_state_path); |
77 if (browser_state_index == std::string::npos) { | 73 if (browser_state_index == std::string::npos) { |
78 NOTREACHED(); | 74 NOTREACHED(); |
79 return; | 75 return; |
80 } | 76 } |
81 base::string16 name = GetNameOfBrowserStateAtIndex(browser_state_index); | |
82 | |
83 DictionaryPrefUpdate update(prefs_, ios::prefs::kBrowserStateInfoCache); | 77 DictionaryPrefUpdate update(prefs_, ios::prefs::kBrowserStateInfoCache); |
84 base::DictionaryValue* cache = update.Get(); | 78 base::DictionaryValue* cache = update.Get(); |
85 std::string key = CacheKeyFromBrowserStatePath(browser_state_path); | 79 std::string key = CacheKeyFromBrowserStatePath(browser_state_path); |
86 cache->Remove(key, nullptr); | 80 cache->Remove(key, nullptr); |
87 sorted_keys_.erase(std::find(sorted_keys_.begin(), sorted_keys_.end(), key)); | 81 sorted_keys_.erase(std::find(sorted_keys_.begin(), sorted_keys_.end(), key)); |
88 | 82 |
89 FOR_EACH_OBSERVER(BrowserStateInfoCacheObserver, observer_list_, | 83 FOR_EACH_OBSERVER(BrowserStateInfoCacheObserver, observer_list_, |
90 OnBrowserStateWasRemoved(browser_state_path, name)); | 84 OnBrowserStateWasRemoved(browser_state_path)); |
91 } | 85 } |
92 | 86 |
93 size_t BrowserStateInfoCache::GetNumberOfBrowserStates() const { | 87 size_t BrowserStateInfoCache::GetNumberOfBrowserStates() const { |
94 return sorted_keys_.size(); | 88 return sorted_keys_.size(); |
95 } | 89 } |
96 | 90 |
97 size_t BrowserStateInfoCache::GetIndexOfBrowserStateWithPath( | 91 size_t BrowserStateInfoCache::GetIndexOfBrowserStateWithPath( |
98 const base::FilePath& browser_state_path) const { | 92 const base::FilePath& browser_state_path) const { |
99 if (browser_state_path.DirName() != user_data_dir_) | 93 if (browser_state_path.DirName() != user_data_dir_) |
100 return std::string::npos; | 94 return std::string::npos; |
101 std::string search_key = CacheKeyFromBrowserStatePath(browser_state_path); | 95 std::string search_key = CacheKeyFromBrowserStatePath(browser_state_path); |
102 for (size_t i = 0; i < sorted_keys_.size(); ++i) { | 96 for (size_t i = 0; i < sorted_keys_.size(); ++i) { |
103 if (sorted_keys_[i] == search_key) | 97 if (sorted_keys_[i] == search_key) |
104 return i; | 98 return i; |
105 } | 99 } |
106 return std::string::npos; | 100 return std::string::npos; |
107 } | 101 } |
108 | 102 |
109 base::string16 BrowserStateInfoCache::GetNameOfBrowserStateAtIndex( | |
110 size_t index) const { | |
111 base::string16 name; | |
112 GetInfoForBrowserStateAtIndex(index)->GetString(kNameKey, &name); | |
113 return name; | |
114 } | |
115 | |
116 base::string16 BrowserStateInfoCache::GetUserNameOfBrowserStateAtIndex( | 103 base::string16 BrowserStateInfoCache::GetUserNameOfBrowserStateAtIndex( |
117 size_t index) const { | 104 size_t index) const { |
118 base::string16 user_name; | 105 base::string16 user_name; |
119 GetInfoForBrowserStateAtIndex(index)->GetString(kUserNameKey, &user_name); | 106 GetInfoForBrowserStateAtIndex(index)->GetString(kUserNameKey, &user_name); |
120 return user_name; | 107 return user_name; |
121 } | 108 } |
122 | 109 |
123 base::FilePath BrowserStateInfoCache::GetPathOfBrowserStateAtIndex( | 110 base::FilePath BrowserStateInfoCache::GetPathOfBrowserStateAtIndex( |
124 size_t index) const { | 111 size_t index) const { |
125 return user_data_dir_.AppendASCII(sorted_keys_[index]); | 112 return user_data_dir_.AppendASCII(sorted_keys_[index]); |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 cache->SetWithoutPathExpansion(sorted_keys_[index], info); | 194 cache->SetWithoutPathExpansion(sorted_keys_[index], info); |
208 } | 195 } |
209 | 196 |
210 std::string BrowserStateInfoCache::CacheKeyFromBrowserStatePath( | 197 std::string BrowserStateInfoCache::CacheKeyFromBrowserStatePath( |
211 const base::FilePath& browser_state_path) const { | 198 const base::FilePath& browser_state_path) const { |
212 DCHECK(user_data_dir_ == browser_state_path.DirName()); | 199 DCHECK(user_data_dir_ == browser_state_path.DirName()); |
213 base::FilePath base_name = browser_state_path.BaseName(); | 200 base::FilePath base_name = browser_state_path.BaseName(); |
214 return base_name.MaybeAsASCII(); | 201 return base_name.MaybeAsASCII(); |
215 } | 202 } |
216 | 203 |
217 std::vector<std::string>::iterator | 204 void BrowserStateInfoCache::AddBrowserStateCacheKey(const std::string& key) { |
218 BrowserStateInfoCache::FindPositionForBrowserState( | 205 sorted_keys_.insert( |
219 const std::string& search_key, | 206 std::upper_bound(sorted_keys_.begin(), sorted_keys_.end(), key), key); |
220 const base::string16& search_name) { | |
221 base::string16 search_name_l = base::i18n::ToLower(search_name); | |
222 for (size_t i = 0; i < GetNumberOfBrowserStates(); ++i) { | |
223 base::string16 name_l = | |
224 base::i18n::ToLower(GetNameOfBrowserStateAtIndex(i)); | |
225 int name_compare = search_name_l.compare(name_l); | |
226 if (name_compare < 0) | |
227 return sorted_keys_.begin() + i; | |
228 if (name_compare == 0) { | |
229 int key_compare = search_key.compare(sorted_keys_[i]); | |
230 if (key_compare < 0) | |
231 return sorted_keys_.begin() + i; | |
232 } | |
233 } | |
234 return sorted_keys_.end(); | |
235 } | 207 } |
OLD | NEW |