profiles_state.cc revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
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" 2103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include "chrome/grit/generated_resources.h" 22effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "components/signin/core/browser/profile_oauth2_token_service.h" 23cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/signin/core/common/profile_management_switches.h" 248bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "ui/base/l10n/l10n_util.h" 255f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "ui/gfx/text_elider.h" 269ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch 279ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdochnamespace profiles { 289ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch 299ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdochbool IsMultipleProfilesEnabled() { 309ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#if defined(OS_ANDROID) 319ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch return false; 329ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#endif 339ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch return true; 349ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch} 359ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch 368bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)base::FilePath GetDefaultProfileDir(const base::FilePath& user_data_dir) { 379ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch base::FilePath default_profile_dir(user_data_dir); 389ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch default_profile_dir = 399ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch default_profile_dir.AppendASCII(chrome::kInitialProfile); 409ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch return default_profile_dir; 419ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch} 429ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch 439ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdochvoid RegisterPrefs(PrefRegistrySimple* registry) { 441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // Preferences about global profile information. 459ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch registry->RegisterStringPref(prefs::kProfileLastUsed, std::string()); 469ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch registry->RegisterIntegerPref(prefs::kProfilesNumCreated, 1); 479ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch registry->RegisterListPref(prefs::kProfilesLastActive); 481320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 491320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // Preferences about the user manager. 501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci registry->RegisterBooleanPref(prefs::kBrowserGuestModeEnabled, true); 511320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci registry->RegisterBooleanPref(prefs::kBrowserAddPersonEnabled, true); 529ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch} 539ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch 54116680a4aac90f2aa7413d9095a592090648e557Ben Murdochbase::string16 GetAvatarNameForProfile(const base::FilePath& profile_path) { 55a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) base::string16 display_name; 568bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 57116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch if (profile_path == ProfileManager::GetGuestProfilePath()) { 58a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) display_name = l10n_util::GetStringUTF16(IDS_GUEST_PROFILE_NAME); 598bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) } else { 601320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci const ProfileInfoCache& cache = 618bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) g_browser_process->profile_manager()->GetProfileInfoCache(); 62116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch size_t index = cache.GetIndexOfProfileWithPath(profile_path); 63a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 64a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) if (index == std::string::npos) 65a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME); 66a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 6703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) // Using the --new-avatar-menu flag, there's a couple of rules about what 6803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) // the avatar button displays. If there's a single profile, with a default 6903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) // name (i.e. of the form Person %d) not manually set, it should display 701320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // IDS_SINGLE_PROFILE_DISPLAY_NAME. If the profile is signed in but is using 711320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // a default name, use the profiles's email address. Otherwise, it 721320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // will return the actual name of the profile. 731320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci const base::string16 profile_name = cache.GetNameOfProfileAtIndex(index); 741320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci const base::string16 email = cache.GetUserNameOfProfileAtIndex(index); 751320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci bool is_default_name = cache.ProfileIsUsingDefaultNameAtIndex(index) && 7603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) cache.IsDefaultProfileName(profile_name); 77a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 781320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci if (cache.GetNumberOfProfiles() == 1 && is_default_name) 79a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) display_name = l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME); 8003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) else 811320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci display_name = (is_default_name && !email.empty()) ? email : profile_name; 828bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) } 83a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return display_name; 848bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)} 858bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 865f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)base::string16 GetAvatarButtonTextForProfile(Profile* profile) { 875f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) const int kMaxCharactersToDisplay = 15; 885f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) base::string16 name = GetAvatarNameForProfile(profile->GetPath()); 895f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) name = gfx::TruncateString(name, 905f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) kMaxCharactersToDisplay, 915f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) gfx::CHARACTER_BREAK); 925f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) if (profile->IsSupervised()) { 935f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) name = l10n_util::GetStringFUTF16(IDS_SUPERVISED_USER_NEW_AVATAR_LABEL, 945f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) name); 955f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) } 965f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) return name; 975f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)} 985f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 99f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void UpdateProfileName(Profile* profile, 100f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) const base::string16& new_profile_name) { 1011320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci const ProfileInfoCache& cache = 10203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) g_browser_process->profile_manager()->GetProfileInfoCache(); 10303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) size_t profile_index = cache.GetIndexOfProfileWithPath(profile->GetPath()); 10403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) if (profile_index == std::string::npos) 10503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) return; 10603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) 10703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) if (new_profile_name == cache.GetNameOfProfileAtIndex(profile_index)) 10803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) return; 10903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) 11003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) // This is only called when updating the profile name through the UI, 11103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) // so we can assume the user has done this on purpose. 112effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch PrefService* pref_service = profile->GetPrefs(); 11303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) pref_service->SetBoolean(prefs::kProfileUsingDefaultName, false); 11403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) 115effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch // Updating the profile preference will cause the cache to be updated for 116effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch // this preference. 117effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch pref_service->SetString(prefs::kProfileName, 118effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch base::UTF16ToUTF8(new_profile_name)); 119f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)} 120f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 1215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)std::vector<std::string> GetSecondaryAccountsForProfile( 1225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) Profile* profile, 1235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const std::string& primary_account) { 1245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::vector<std::string> accounts = 1255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ProfileOAuth2TokenServiceFactory::GetForProfile(profile)->GetAccounts(); 1265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // The vector returned by ProfileOAuth2TokenService::GetAccounts() contains 1285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // the primary account too, so we need to remove it from the list. 1295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::vector<std::string>::iterator primary_index = 1305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::find_if(accounts.begin(), accounts.end(), 1315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::bind1st(std::equal_to<std::string>(), primary_account)); 1325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) DCHECK(primary_index != accounts.end()); 1335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) accounts.erase(primary_index); 1345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return accounts; 1365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 1375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool IsRegularOrGuestSession(Browser* browser) { 1395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) Profile* profile = browser->profile(); 1405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return profile->IsGuestSession() || !profile->IsOffTheRecord(); 1415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 1425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 143cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void UpdateGaiaProfilePhotoIfNeeded(Profile* profile) { 144cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // If the --google-profile-info flag isn't used, then the 145cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // GAIAInfoUpdateService isn't initialized, and we can't download the picture. 146cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) if (!switches::IsGoogleProfileInfo()) 147cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) return; 148cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 149cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) DCHECK(profile); 150cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) GAIAInfoUpdateServiceFactory::GetInstance()->GetForProfile(profile)->Update(); 151cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 152cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 153cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)SigninErrorController* GetSigninErrorController(Profile* profile) { 154cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) ProfileOAuth2TokenService* token_service = 155cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) ProfileOAuth2TokenServiceFactory::GetForProfile(profile); 156cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) return token_service ? token_service->signin_error_controller() : NULL; 157cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 158cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 1591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciProfile* SetActiveProfileToGuestIfLocked() { 1601320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci Profile* active_profile = ProfileManager::GetLastUsedProfile(); 1611320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci DCHECK(active_profile); 1621320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 1631320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci if (active_profile->IsGuestSession()) 1641320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci return active_profile; 1651320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 1661320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci ProfileManager* profile_manager = g_browser_process->profile_manager(); 1671320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci const ProfileInfoCache& cache = profile_manager->GetProfileInfoCache(); 1681320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci size_t index = cache.GetIndexOfProfileWithPath(active_profile->GetPath()); 1691320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci if (!cache.ProfileIsSigninRequiredAtIndex(index)) 1701320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci return NULL; 1711320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 1721320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // The guest profile must have been loaded already. 1731320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci Profile* guest_profile = profile_manager->GetProfile( 1741320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci ProfileManager::GetGuestProfilePath()); 1751320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci DCHECK(guest_profile); 1761320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 1771320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci PrefService* local_state = g_browser_process->local_state(); 1781320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci DCHECK(local_state); 1791320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci local_state->SetString(prefs::kProfileLastUsed, 1801320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci guest_profile->GetPath().BaseName().MaybeAsASCII()); 1811320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci return guest_profile; 1821320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci} 1831320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 1849ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch} // namespace profiles 185