Commit 9b85bf74 authored by Sylvain Defresne's avatar Sylvain Defresne Committed by Commit Bot

Cleanup AccountTrackerService

Minor cleanups of AccountTrackerService:
- fix style issues (automatically using clang-format)
- use standard algorithms, C++11 auto and for loops
- use FILE_PATH_LITERAL to declare path component constants

Bug: none
Change-Id: I89d0fdb08a974b2542183beaf030d68d952b6af6
Reviewed-on: https://chromium-review.googlesource.com/1196366
Commit-Queue: Sylvain Defresne <sdefresne@chromium.org>
Reviewed-by: default avatarDavid Roger <droger@chromium.org>
Cr-Commit-Position: refs/heads/master@{#589167}
parent 46cef98b
...@@ -39,6 +39,12 @@ const char kAccountChildAccountStatusPath[] = "is_child_account"; ...@@ -39,6 +39,12 @@ const char kAccountChildAccountStatusPath[] = "is_child_account";
const char kAdvancedProtectionAccountStatusPath[] = const char kAdvancedProtectionAccountStatusPath[] =
"is_under_advanced_protection"; "is_under_advanced_protection";
// Account folders used for storing account related data at disk.
const base::FilePath::CharType kAccountsFolder[] =
FILE_PATH_LITERAL("Accounts");
const base::FilePath::CharType kAvatarImagesFolder[] =
FILE_PATH_LITERAL("Avatar Images");
// TODO(M48): Remove deprecated preference migration. // TODO(M48): Remove deprecated preference migration.
const char kAccountServiceFlagsPath[] = "service_flags"; const char kAccountServiceFlagsPath[] = "service_flags";
...@@ -101,9 +107,6 @@ const char AccountTrackerService::kNoHostedDomainFound[] = "NO_HOSTED_DOMAIN"; ...@@ -101,9 +107,6 @@ const char AccountTrackerService::kNoHostedDomainFound[] = "NO_HOSTED_DOMAIN";
// This must be a string which can never be a valid picture URL. // This must be a string which can never be a valid picture URL.
const char AccountTrackerService::kNoPictureURLFound[] = "NO_PICTURE_URL"; const char AccountTrackerService::kNoPictureURLFound[] = "NO_PICTURE_URL";
const char AccountTrackerService::kAccountsFolder[] = "Accounts";
const char AccountTrackerService::kAvatarImagesFolder[] = "Avatar Images";
AccountTrackerService::AccountTrackerService() : weak_factory_(this) {} AccountTrackerService::AccountTrackerService() : weak_factory_(this) {}
AccountTrackerService::~AccountTrackerService() { AccountTrackerService::~AccountTrackerService() {
...@@ -149,23 +152,17 @@ void AccountTrackerService::RemoveObserver(Observer* observer) { ...@@ -149,23 +152,17 @@ void AccountTrackerService::RemoveObserver(Observer* observer) {
std::vector<AccountInfo> AccountTrackerService::GetAccounts() const { std::vector<AccountInfo> AccountTrackerService::GetAccounts() const {
std::vector<AccountInfo> accounts; std::vector<AccountInfo> accounts;
for (const auto& pair : accounts_) {
for (std::map<std::string, AccountState>::const_iterator it = accounts.push_back(pair.second.info);
accounts_.begin();
it != accounts_.end();
++it) {
const AccountState& state = it->second;
accounts.push_back(state.info);
} }
return accounts; return accounts;
} }
AccountInfo AccountTrackerService::GetAccountInfo( AccountInfo AccountTrackerService::GetAccountInfo(
const std::string& account_id) const { const std::string& account_id) const {
std::map<std::string, AccountState>::const_iterator it = const auto iterator = accounts_.find(account_id);
accounts_.find(account_id); if (iterator != accounts_.end())
if (it != accounts_.end()) return iterator->second.info;
return it->second.info;
return AccountInfo(); return AccountInfo();
} }
...@@ -173,14 +170,12 @@ AccountInfo AccountTrackerService::GetAccountInfo( ...@@ -173,14 +170,12 @@ AccountInfo AccountTrackerService::GetAccountInfo(
AccountInfo AccountTrackerService::FindAccountInfoByGaiaId( AccountInfo AccountTrackerService::FindAccountInfoByGaiaId(
const std::string& gaia_id) const { const std::string& gaia_id) const {
if (!gaia_id.empty()) { if (!gaia_id.empty()) {
for (std::map<std::string, AccountState>::const_iterator it = const auto iterator = std::find_if(
accounts_.begin(); accounts_.begin(), accounts_.end(), [&gaia_id](const auto& pair) {
it != accounts_.end(); return pair.second.info.gaia == gaia_id;
++it) { });
const AccountState& state = it->second; if (iterator != accounts_.end())
if (state.info.gaia == gaia_id) return iterator->second.info;
return state.info;
}
} }
return AccountInfo(); return AccountInfo();
...@@ -189,14 +184,12 @@ AccountInfo AccountTrackerService::FindAccountInfoByGaiaId( ...@@ -189,14 +184,12 @@ AccountInfo AccountTrackerService::FindAccountInfoByGaiaId(
AccountInfo AccountTrackerService::FindAccountInfoByEmail( AccountInfo AccountTrackerService::FindAccountInfoByEmail(
const std::string& email) const { const std::string& email) const {
if (!email.empty()) { if (!email.empty()) {
for (std::map<std::string, AccountState>::const_iterator it = const auto iterator = std::find_if(
accounts_.begin(); accounts_.begin(), accounts_.end(), [&email](const auto& pair) {
it != accounts_.end(); return gaia::AreEmailsSame(pair.second.info.email, email);
++it) { });
const AccountState& state = it->second; if (iterator != accounts_.end())
if (gaia::AreEmailsSame(state.info.email, email)) return iterator->second.info;
return state.info;
}
} }
return AccountInfo(); return AccountInfo();
...@@ -204,8 +197,11 @@ AccountInfo AccountTrackerService::FindAccountInfoByEmail( ...@@ -204,8 +197,11 @@ AccountInfo AccountTrackerService::FindAccountInfoByEmail(
gfx::Image AccountTrackerService::GetAccountImage( gfx::Image AccountTrackerService::GetAccountImage(
const std::string& account_id) { const std::string& account_id) {
return base::ContainsKey(accounts_, account_id) ? accounts_[account_id].image const auto iterator = accounts_.find(account_id);
: gfx::Image(); if (iterator != accounts_.end())
return iterator->second.image;
return gfx::Image();
} }
// static // static
...@@ -300,7 +296,7 @@ void AccountTrackerService::SetAccountStateFromUserInfo( ...@@ -300,7 +296,7 @@ void AccountTrackerService::SetAccountStateFromUserInfo(
user_info->GetString("locale", &state.info.locale); user_info->GetString("locale", &state.info.locale);
std::string picture_url; std::string picture_url;
if(user_info->GetString("picture", &picture_url)) { if (user_info->GetString("picture", &picture_url)) {
state.info.picture_url = picture_url; state.info.picture_url = picture_url;
} else { } else {
state.info.picture_url = kNoPictureURLFound; state.info.picture_url = kNoPictureURLFound;
...@@ -435,8 +431,8 @@ AccountTrackerService::GetMigrationState(const PrefService* pref_service) { ...@@ -435,8 +431,8 @@ AccountTrackerService::GetMigrationState(const PrefService* pref_service) {
base::FilePath AccountTrackerService::GetImagePathFor( base::FilePath AccountTrackerService::GetImagePathFor(
const std::string& account_id) { const std::string& account_id) {
return user_data_dir_.AppendASCII(kAccountsFolder) return user_data_dir_.Append(kAccountsFolder)
.AppendASCII(kAvatarImagesFolder) .Append(kAvatarImagesFolder)
.AppendASCII(account_id); .AppendASCII(account_id);
} }
...@@ -452,8 +448,8 @@ void AccountTrackerService::OnAccountImageLoaded(const std::string& account_id, ...@@ -452,8 +448,8 @@ void AccountTrackerService::OnAccountImageLoaded(const std::string& account_id,
void AccountTrackerService::LoadAccountImagesFromDisk() { void AccountTrackerService::LoadAccountImagesFromDisk() {
if (!image_storage_task_runner_) if (!image_storage_task_runner_)
return; return;
for (const std::pair<std::string, AccountState>& account : accounts_) { for (const auto& pair : accounts_) {
const std::string& account_id = account.second.info.account_id; const std::string& account_id = pair.second.info.account_id;
PostTaskAndReplyWithResult( PostTaskAndReplyWithResult(
image_storage_task_runner_.get(), FROM_HERE, image_storage_task_runner_.get(), FROM_HERE,
base::BindOnce(&ReadImage, GetImagePathFor(account_id)), base::BindOnce(&ReadImage, GetImagePathFor(account_id)),
...@@ -624,7 +620,7 @@ void AccountTrackerService::RemoveFromPrefs(const AccountState& state) { ...@@ -624,7 +620,7 @@ void AccountTrackerService::RemoveFromPrefs(const AccountState& state) {
base::string16 account_id_16 = base::UTF8ToUTF16(state.info.account_id); base::string16 account_id_16 = base::UTF8ToUTF16(state.info.account_id);
ListPrefUpdate update(pref_service_, kAccountInfoPref); ListPrefUpdate update(pref_service_, kAccountInfoPref);
for(size_t i = 0; i < update->GetSize(); ++i) { for (size_t i = 0; i < update->GetSize(); ++i) {
base::DictionaryValue* dict = nullptr; base::DictionaryValue* dict = nullptr;
if (update->GetDictionary(i, &dict)) { if (update->GetDictionary(i, &dict)) {
base::string16 value; base::string16 value;
...@@ -650,12 +646,13 @@ std::string AccountTrackerService::PickAccountIdForAccount( ...@@ -650,12 +646,13 @@ std::string AccountTrackerService::PickAccountIdForAccount(
DCHECK(!gaia.empty() || DCHECK(!gaia.empty() ||
GetMigrationState(pref_service) == MIGRATION_NOT_STARTED); GetMigrationState(pref_service) == MIGRATION_NOT_STARTED);
DCHECK(!email.empty()); DCHECK(!email.empty());
switch(GetMigrationState(pref_service)) { switch (GetMigrationState(pref_service)) {
case MIGRATION_NOT_STARTED: case MIGRATION_NOT_STARTED:
// Some tests don't use a real email address. To support these cases, // Some tests don't use a real email address. To support these cases,
// don't try to canonicalize these strings. // don't try to canonicalize these strings.
return (email.find('@') == std::string::npos) ? email : return (email.find('@') == std::string::npos)
gaia::CanonicalizeEmail(email); ? email
: gaia::CanonicalizeEmail(email);
case MIGRATION_IN_PROGRESS: case MIGRATION_IN_PROGRESS:
case MIGRATION_DONE: case MIGRATION_DONE:
return gaia; return gaia;
...@@ -677,8 +674,7 @@ std::string AccountTrackerService::SeedAccountInfo(const std::string& gaia, ...@@ -677,8 +674,7 @@ std::string AccountTrackerService::SeedAccountInfo(const std::string& gaia,
SaveToPrefs(state); SaveToPrefs(state);
DVLOG(1) << "AccountTrackerService::SeedAccountInfo" DVLOG(1) << "AccountTrackerService::SeedAccountInfo"
<< " account_id=" << account_id << " account_id=" << account_id << " gaia_id=" << gaia
<< " gaia_id=" << gaia
<< " email=" << email; << " email=" << email;
return account_id; return account_id;
......
...@@ -48,10 +48,6 @@ class AccountTrackerService : public KeyedService { ...@@ -48,10 +48,6 @@ class AccountTrackerService : public KeyedService {
// Child account service flag name. // Child account service flag name.
static const char kChildAccountServiceFlag[]; static const char kChildAccountServiceFlag[];
// Account folders used for storing account related data at disk.
static const char kAccountsFolder[];
static const char kAvatarImagesFolder[];
// Clients of AccountTrackerService can implement this interface and register // Clients of AccountTrackerService can implement this interface and register
// with AddObserver() to learn about account information changes. // with AddObserver() to learn about account information changes.
class Observer { class Observer {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment