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