OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/profiles/profile_info_cache.h" | 5 #include "chrome/browser/profiles/profile_info_cache.h" |
6 | 6 |
7 #include <algorithm> | |
7 #include <utility> | 8 #include <utility> |
8 | 9 |
9 #include "base/bind.h" | 10 #include "base/bind.h" |
10 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
11 #include "base/i18n/case_conversion.h" | 12 #include "base/i18n/case_conversion.h" |
13 #include "base/i18n/string_compare.h" | |
12 #include "base/logging.h" | 14 #include "base/logging.h" |
13 #include "base/macros.h" | 15 #include "base/macros.h" |
14 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
15 #include "base/profiler/scoped_tracker.h" | 17 #include "base/profiler/scoped_tracker.h" |
16 #include "base/rand_util.h" | 18 #include "base/rand_util.h" |
17 #include "base/stl_util.h" | 19 #include "base/stl_util.h" |
18 #include "base/strings/string_number_conversions.h" | 20 #include "base/strings/string_number_conversions.h" |
19 #include "base/strings/string_piece.h" | 21 #include "base/strings/string_piece.h" |
20 #include "base/strings/utf_string_conversions.h" | 22 #include "base/strings/utf_string_conversions.h" |
21 #include "base/values.h" | 23 #include "base/values.h" |
22 #include "build/build_config.h" | 24 #include "build/build_config.h" |
23 #include "chrome/browser/browser_process.h" | 25 #include "chrome/browser/browser_process.h" |
24 #include "chrome/browser/profiles/profile_avatar_downloader.h" | 26 #include "chrome/browser/profiles/profile_avatar_downloader.h" |
25 #include "chrome/browser/profiles/profile_avatar_icon_util.h" | 27 #include "chrome/browser/profiles/profile_avatar_icon_util.h" |
26 #include "chrome/browser/profiles/profiles_state.h" | 28 #include "chrome/browser/profiles/profiles_state.h" |
27 #include "chrome/common/pref_names.h" | 29 #include "chrome/common/pref_names.h" |
28 #include "chrome/grit/generated_resources.h" | 30 #include "chrome/grit/generated_resources.h" |
29 #include "components/prefs/pref_registry_simple.h" | 31 #include "components/prefs/pref_registry_simple.h" |
30 #include "components/prefs/pref_service.h" | 32 #include "components/prefs/pref_service.h" |
31 #include "components/prefs/scoped_user_pref_update.h" | 33 #include "components/prefs/scoped_user_pref_update.h" |
32 #include "components/signin/core/common/profile_management_switches.h" | 34 #include "components/signin/core/common/profile_management_switches.h" |
33 #include "content/public/browser/browser_thread.h" | 35 #include "content/public/browser/browser_thread.h" |
36 #include "third_party/icu/source/i18n/unicode/coll.h" | |
34 #include "ui/base/l10n/l10n_util.h" | 37 #include "ui/base/l10n/l10n_util.h" |
35 #include "ui/base/resource/resource_bundle.h" | 38 #include "ui/base/resource/resource_bundle.h" |
36 #include "ui/gfx/image/image.h" | 39 #include "ui/gfx/image/image.h" |
37 #include "ui/gfx/image/image_util.h" | 40 #include "ui/gfx/image/image_util.h" |
38 | 41 |
39 #if defined(ENABLE_SUPERVISED_USERS) | 42 #if defined(ENABLE_SUPERVISED_USERS) |
40 #include "chrome/browser/supervised_user/supervised_user_constants.h" | 43 #include "chrome/browser/supervised_user/supervised_user_constants.h" |
41 #endif | 44 #endif |
42 | 45 |
43 using content::BrowserThread; | 46 using content::BrowserThread; |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
150 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 153 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
151 if (!base::PathExists(file_path)) | 154 if (!base::PathExists(file_path)) |
152 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, callback); | 155 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, callback); |
153 } | 156 } |
154 | 157 |
155 void DeleteBitmap(const base::FilePath& image_path) { | 158 void DeleteBitmap(const base::FilePath& image_path) { |
156 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 159 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
157 base::DeleteFile(image_path, false); | 160 base::DeleteFile(image_path, false); |
158 } | 161 } |
159 | 162 |
163 // Compares two ProfileAttributesEntry using locale-sensitive comparison of | |
164 // their names. For ties, the profile path is compared next. | |
165 class ProfileAttributesSortComparator { | |
166 public: | |
167 explicit ProfileAttributesSortComparator(icu::Collator* collator); | |
168 bool operator()(const ProfileAttributesEntry* const a, | |
169 const ProfileAttributesEntry* const b) const; | |
170 private: | |
171 icu::Collator* collator_; | |
172 }; | |
173 | |
174 ProfileAttributesSortComparator::ProfileAttributesSortComparator( | |
175 icu::Collator* collator) : collator_(collator) {} | |
176 | |
177 bool ProfileAttributesSortComparator::operator()( | |
178 const ProfileAttributesEntry* const a, | |
179 const ProfileAttributesEntry* const b) const { | |
180 UCollationResult result = base::i18n::CompareString16WithCollator( | |
181 *collator_, a->GetName(), b->GetName()); | |
182 if (result != UCOL_EQUAL) | |
183 return result == UCOL_LESS; | |
184 | |
185 // Compare the profile paths. Since the paths are "[userdir]\Profile #", so | |
186 // comparisons take care of the numbers represented by # only. | |
187 base::FilePath::StringType a_path = a->GetPath().value(); | |
188 base::FilePath::StringType b_path = b->GetPath().value(); | |
189 if (a_path.length() != b_path.length()) | |
anthonyvd
2016/02/08 21:58:42
I think this check isn't needed. base::FilePath::S
tapted
2016/02/08 22:33:36
I read this as solving the issue that for profiles
lwchkg
2016/02/10 19:44:20
You're right. If the profile names are the same, t
| |
190 return a_path.length() < b_path.length(); | |
191 return a_path < b_path; | |
192 } | |
193 | |
160 } // namespace | 194 } // namespace |
161 | 195 |
162 ProfileInfoCache::ProfileInfoCache(PrefService* prefs, | 196 ProfileInfoCache::ProfileInfoCache(PrefService* prefs, |
163 const base::FilePath& user_data_dir) | 197 const base::FilePath& user_data_dir) |
164 : prefs_(prefs), | 198 : prefs_(prefs), |
165 user_data_dir_(user_data_dir), | 199 user_data_dir_(user_data_dir), |
166 disable_avatar_download_for_testing_(false) { | 200 disable_avatar_download_for_testing_(false) { |
167 // Populate the cache | 201 // Populate the cache |
168 DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache); | 202 DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache); |
169 base::DictionaryValue* cache = update.Get(); | 203 base::DictionaryValue* cache = update.Get(); |
(...skipping 1184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1354 std::vector<ProfileAttributesEntry*> ret; | 1388 std::vector<ProfileAttributesEntry*> ret; |
1355 for (size_t i = 0; i < GetNumberOfProfiles(); ++i) { | 1389 for (size_t i = 0; i < GetNumberOfProfiles(); ++i) { |
1356 ProfileAttributesEntry* entry; | 1390 ProfileAttributesEntry* entry; |
1357 if (GetProfileAttributesWithPath(GetPathOfProfileAtIndex(i), &entry)) { | 1391 if (GetProfileAttributesWithPath(GetPathOfProfileAtIndex(i), &entry)) { |
1358 ret.push_back(entry); | 1392 ret.push_back(entry); |
1359 } | 1393 } |
1360 } | 1394 } |
1361 return ret; | 1395 return ret; |
1362 } | 1396 } |
1363 | 1397 |
1398 std::vector<ProfileAttributesEntry*> | |
1399 ProfileInfoCache::GetAllProfilesAttributesSortedByName() { | |
1400 UErrorCode error_code = U_ZERO_ERROR; | |
1401 // Use the default collator. The default locale should have been properly | |
1402 // set by the time this constructor is called. | |
1403 scoped_ptr<icu::Collator> collator(icu::Collator::createInstance(error_code)); | |
1404 DCHECK(U_SUCCESS(error_code)); | |
1405 | |
1406 std::vector<ProfileAttributesEntry*> ret = GetAllProfilesAttributes(); | |
1407 std::sort(ret.begin(), ret.end(), | |
1408 ProfileAttributesSortComparator(collator.get())); | |
1409 return ret; | |
1410 } | |
1411 | |
1364 bool ProfileInfoCache::GetProfileAttributesWithPath( | 1412 bool ProfileInfoCache::GetProfileAttributesWithPath( |
1365 const base::FilePath& path, ProfileAttributesEntry** entry) { | 1413 const base::FilePath& path, ProfileAttributesEntry** entry) { |
1366 if (GetNumberOfProfiles() == 0) | 1414 if (GetNumberOfProfiles() == 0) |
1367 return false; | 1415 return false; |
1368 | 1416 |
1369 if (GetIndexOfProfileWithPath(path) == std::string::npos) | 1417 if (GetIndexOfProfileWithPath(path) == std::string::npos) |
1370 return false; | 1418 return false; |
1371 | 1419 |
1372 if (profile_attributes_entries_.find(path) == | 1420 if (profile_attributes_entries_.find(path) == |
1373 profile_attributes_entries_.end()) { | 1421 profile_attributes_entries_.end()) { |
1374 // The profile info is in the cache but its entry isn't created yet, insert | 1422 // The profile info is in the cache but its entry isn't created yet, insert |
1375 // it in the map. | 1423 // it in the map. |
1376 scoped_ptr<ProfileAttributesEntry> new_entry(new ProfileAttributesEntry()); | 1424 scoped_ptr<ProfileAttributesEntry> new_entry(new ProfileAttributesEntry()); |
1377 profile_attributes_entries_.add(path, std::move(new_entry)); | 1425 profile_attributes_entries_.add(path, std::move(new_entry)); |
1378 profile_attributes_entries_.get(path)->Initialize(this, path); | 1426 profile_attributes_entries_.get(path)->Initialize(this, path); |
1379 } | 1427 } |
1380 | 1428 |
1381 *entry = profile_attributes_entries_.get(path); | 1429 *entry = profile_attributes_entries_.get(path); |
1382 return true; | 1430 return true; |
1383 } | 1431 } |
OLD | NEW |