profiles_state.cc revision 5f1c94371a64b3196d4be9466099bb892df9b88e
19ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// Copyright 2013 The Chromium Authors. All rights reserved. 29ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// Use of this source code is governed by a BSD-style license that can be 39ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// found in the LICENSE file. 49ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch 59ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "chrome/browser/profiles/profiles_state.h" 69ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch 79ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "base/files/file_path.h" 89ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "base/prefs/pref_registry_simple.h" 9f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "base/prefs/pref_service.h" 10f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "base/strings/utf_string_conversions.h" 118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/browser_process.h" 12cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/browser/profiles/gaia_info_update_service.h" 13cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/browser/profiles/gaia_info_update_service_factory.h" 148bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/profiles/profile.h" 158bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/profiles/profile_info_cache.h" 168bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/profiles/profile_manager.h" 175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/signin/profile_oauth2_token_service_factory.h" 188bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/ui/browser.h" 199ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "chrome/common/chrome_constants.h" 209ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "chrome/common/pref_names.h" 21effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "components/signin/core/browser/profile_oauth2_token_service.h" 22cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/signin/core/common/profile_management_switches.h" 238bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "grit/generated_resources.h" 248bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "ui/base/l10n/l10n_util.h" 255f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "ui/gfx/text_elider.h" 269ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch 2768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#if defined(OS_CHROMEOS) 28cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/browser/chromeos/login/users/user_manager.h" 2968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#endif 3068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) 319ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdochnamespace profiles { 329ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch 339ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdochbool IsMultipleProfilesEnabled() { 349ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#if defined(OS_ANDROID) 359ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch return false; 369ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#endif 379ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch return true; 389ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch} 399ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch 408bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)base::FilePath GetDefaultProfileDir(const base::FilePath& user_data_dir) { 419ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch base::FilePath default_profile_dir(user_data_dir); 429ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch default_profile_dir = 439ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch default_profile_dir.AppendASCII(chrome::kInitialProfile); 449ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch return default_profile_dir; 459ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch} 469ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch 479ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdochvoid RegisterPrefs(PrefRegistrySimple* registry) { 489ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch registry->RegisterStringPref(prefs::kProfileLastUsed, std::string()); 499ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch registry->RegisterIntegerPref(prefs::kProfilesNumCreated, 1); 509ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch registry->RegisterListPref(prefs::kProfilesLastActive); 519ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch} 529ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch 53116680a4aac90f2aa7413d9095a592090648e557Ben Murdochbase::string16 GetAvatarNameForProfile(const base::FilePath& profile_path) { 54a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) base::string16 display_name; 558bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 56116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch if (profile_path == ProfileManager::GetGuestProfilePath()) { 57a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) display_name = l10n_util::GetStringUTF16(IDS_GUEST_PROFILE_NAME); 588bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) } else { 598bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) ProfileInfoCache& cache = 608bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) g_browser_process->profile_manager()->GetProfileInfoCache(); 61116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch size_t index = cache.GetIndexOfProfileWithPath(profile_path); 62a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 63a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) if (index == std::string::npos) 64a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME); 65a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 66a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Using the --new-profile-management flag, there's a couple of rules 67a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // about what the avatar button displays. If there's a single, local 68a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // profile, with a default name (i.e. of the form Person %d), it should 69a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // display IDS_SINGLE_PROFILE_DISPLAY_NAME. If this is a signed in profile, 70a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // or the user has edited the profile name, or there are multiple profiles, 71a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // it will return the actual name of the profile. 72a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) base::string16 profile_name = cache.GetNameOfProfileAtIndex(index); 73a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) bool has_default_name = cache.ProfileIsUsingDefaultNameAtIndex(index); 74a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 75a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) if (cache.GetNumberOfProfiles() == 1 && has_default_name && 76a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) cache.GetUserNameOfProfileAtIndex(index).empty()) { 77a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) display_name = l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME); 78a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) } else { 79a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) display_name = profile_name; 80a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) } 818bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) } 82a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return display_name; 838bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)} 848bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 855f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)base::string16 GetAvatarButtonTextForProfile(Profile* profile) { 865f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) const int kMaxCharactersToDisplay = 15; 875f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) base::string16 name = GetAvatarNameForProfile(profile->GetPath()); 885f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) name = gfx::TruncateString(name, 895f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) kMaxCharactersToDisplay, 905f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) gfx::CHARACTER_BREAK); 915f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) if (profile->IsSupervised()) { 925f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) name = l10n_util::GetStringFUTF16(IDS_SUPERVISED_USER_NEW_AVATAR_LABEL, 935f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) name); 945f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) } 955f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) return name; 965f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)} 975f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 98f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void UpdateProfileName(Profile* profile, 99f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) const base::string16& new_profile_name) { 100effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch PrefService* pref_service = profile->GetPrefs(); 101effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch // Updating the profile preference will cause the cache to be updated for 102effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch // this preference. 103effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch pref_service->SetString(prefs::kProfileName, 104effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch base::UTF16ToUTF8(new_profile_name)); 105f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)} 106f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)std::vector<std::string> GetSecondaryAccountsForProfile( 1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) Profile* profile, 1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const std::string& primary_account) { 1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::vector<std::string> accounts = 1115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ProfileOAuth2TokenServiceFactory::GetForProfile(profile)->GetAccounts(); 1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // The vector returned by ProfileOAuth2TokenService::GetAccounts() contains 1145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // the primary account too, so we need to remove it from the list. 1155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::vector<std::string>::iterator primary_index = 1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::find_if(accounts.begin(), accounts.end(), 1175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::bind1st(std::equal_to<std::string>(), primary_account)); 1185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) DCHECK(primary_index != accounts.end()); 1195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) accounts.erase(primary_index); 1205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return accounts; 1225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 1235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool IsRegularOrGuestSession(Browser* browser) { 1255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) Profile* profile = browser->profile(); 1265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return profile->IsGuestSession() || !profile->IsOffTheRecord(); 1275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 1285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 129cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void UpdateGaiaProfilePhotoIfNeeded(Profile* profile) { 130cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // If the --google-profile-info flag isn't used, then the 131cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // GAIAInfoUpdateService isn't initialized, and we can't download the picture. 132cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) if (!switches::IsGoogleProfileInfo()) 133cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) return; 134cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 135cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) DCHECK(profile); 136cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) GAIAInfoUpdateServiceFactory::GetInstance()->GetForProfile(profile)->Update(); 137cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 138cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 139cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)SigninErrorController* GetSigninErrorController(Profile* profile) { 140cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) ProfileOAuth2TokenService* token_service = 141cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) ProfileOAuth2TokenServiceFactory::GetForProfile(profile); 142cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) return token_service ? token_service->signin_error_controller() : NULL; 143cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 144cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 1459ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch} // namespace profiles 146