| 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 // If the names are the same, then compare the paths, which must be unique. |
| 186 return a->GetPath().value() < b->GetPath().value(); |
| 187 } |
| 188 |
| 160 } // namespace | 189 } // namespace |
| 161 | 190 |
| 162 ProfileInfoCache::ProfileInfoCache(PrefService* prefs, | 191 ProfileInfoCache::ProfileInfoCache(PrefService* prefs, |
| 163 const base::FilePath& user_data_dir) | 192 const base::FilePath& user_data_dir) |
| 164 : prefs_(prefs), | 193 : prefs_(prefs), |
| 165 user_data_dir_(user_data_dir), | 194 user_data_dir_(user_data_dir), |
| 166 disable_avatar_download_for_testing_(false) { | 195 disable_avatar_download_for_testing_(false) { |
| 167 // Populate the cache | 196 // Populate the cache |
| 168 DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache); | 197 DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache); |
| 169 base::DictionaryValue* cache = update.Get(); | 198 base::DictionaryValue* cache = update.Get(); |
| (...skipping 1184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1354 std::vector<ProfileAttributesEntry*> ret; | 1383 std::vector<ProfileAttributesEntry*> ret; |
| 1355 for (size_t i = 0; i < GetNumberOfProfiles(); ++i) { | 1384 for (size_t i = 0; i < GetNumberOfProfiles(); ++i) { |
| 1356 ProfileAttributesEntry* entry; | 1385 ProfileAttributesEntry* entry; |
| 1357 if (GetProfileAttributesWithPath(GetPathOfProfileAtIndex(i), &entry)) { | 1386 if (GetProfileAttributesWithPath(GetPathOfProfileAtIndex(i), &entry)) { |
| 1358 ret.push_back(entry); | 1387 ret.push_back(entry); |
| 1359 } | 1388 } |
| 1360 } | 1389 } |
| 1361 return ret; | 1390 return ret; |
| 1362 } | 1391 } |
| 1363 | 1392 |
| 1393 std::vector<ProfileAttributesEntry*> |
| 1394 ProfileInfoCache::GetAllProfilesAttributesSortedByName() { |
| 1395 UErrorCode error_code = U_ZERO_ERROR; |
| 1396 // Use the default collator. The default locale should have been properly |
| 1397 // set by the time this constructor is called. |
| 1398 scoped_ptr<icu::Collator> collator(icu::Collator::createInstance(error_code)); |
| 1399 DCHECK(U_SUCCESS(error_code)); |
| 1400 |
| 1401 std::vector<ProfileAttributesEntry*> ret = GetAllProfilesAttributes(); |
| 1402 std::sort(ret.begin(), ret.end(), |
| 1403 ProfileAttributesSortComparator(collator.get())); |
| 1404 return ret; |
| 1405 } |
| 1406 |
| 1364 bool ProfileInfoCache::GetProfileAttributesWithPath( | 1407 bool ProfileInfoCache::GetProfileAttributesWithPath( |
| 1365 const base::FilePath& path, ProfileAttributesEntry** entry) { | 1408 const base::FilePath& path, ProfileAttributesEntry** entry) { |
| 1366 if (GetNumberOfProfiles() == 0) | 1409 if (GetNumberOfProfiles() == 0) |
| 1367 return false; | 1410 return false; |
| 1368 | 1411 |
| 1369 if (GetIndexOfProfileWithPath(path) == std::string::npos) | 1412 if (GetIndexOfProfileWithPath(path) == std::string::npos) |
| 1370 return false; | 1413 return false; |
| 1371 | 1414 |
| 1372 if (profile_attributes_entries_.find(path) == | 1415 if (profile_attributes_entries_.find(path) == |
| 1373 profile_attributes_entries_.end()) { | 1416 profile_attributes_entries_.end()) { |
| 1374 // The profile info is in the cache but its entry isn't created yet, insert | 1417 // The profile info is in the cache but its entry isn't created yet, insert |
| 1375 // it in the map. | 1418 // it in the map. |
| 1376 scoped_ptr<ProfileAttributesEntry> new_entry(new ProfileAttributesEntry()); | 1419 scoped_ptr<ProfileAttributesEntry> new_entry(new ProfileAttributesEntry()); |
| 1377 profile_attributes_entries_.add(path, std::move(new_entry)); | 1420 profile_attributes_entries_.add(path, std::move(new_entry)); |
| 1378 profile_attributes_entries_.get(path)->Initialize(this, path); | 1421 profile_attributes_entries_.get(path)->Initialize(this, path); |
| 1379 } | 1422 } |
| 1380 | 1423 |
| 1381 *entry = profile_attributes_entries_.get(path); | 1424 *entry = profile_attributes_entries_.get(path); |
| 1382 return true; | 1425 return true; |
| 1383 } | 1426 } |
| OLD | NEW |