profiles_state.cc revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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" 128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/profiles/profile.h" 138bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/profiles/profile_info_cache.h" 148bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/profiles/profile_manager.h" 155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/signin/profile_oauth2_token_service.h" 165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/signin/profile_oauth2_token_service_factory.h" 178bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/ui/browser.h" 189ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "chrome/common/chrome_constants.h" 199ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "chrome/common/pref_names.h" 208bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "grit/generated_resources.h" 218bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "ui/base/l10n/l10n_util.h" 229ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch 2368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#if defined(OS_CHROMEOS) 2468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#include "chrome/browser/chromeos/login/user_manager.h" 2568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#endif 2668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) 279ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdochnamespace profiles { 289ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch 299ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdochbool IsMultipleProfilesEnabled() { 309ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#if defined(OS_ANDROID) 319ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch return false; 329ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#endif 339ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#if defined(OS_CHROMEOS) 3468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) return chromeos::UserManager::IsMultipleProfilesAllowed(); 359ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#endif 369ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch 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 53a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)base::string16 GetAvatarNameForProfile(Profile* profile) { 54a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) base::string16 display_name; 558bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 568bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) if (profile->IsGuestSession()) { 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(); 618bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) size_t index = cache.GetIndexOfProfileWithPath(profile->GetPath()); 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) 85f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void UpdateProfileName(Profile* profile, 86f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) const base::string16& new_profile_name) { 87f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) ProfileInfoCache& cache = 88f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) g_browser_process->profile_manager()->GetProfileInfoCache(); 89f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) base::FilePath profile_file_path = profile->GetPath(); 90f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) size_t profile_index = cache.GetIndexOfProfileWithPath(profile_file_path); 91f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 92f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) if ((new_profile_name == 93f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) cache.GetGAIAGivenNameOfProfileAtIndex(profile_index)) || 94f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) (new_profile_name == cache.GetGAIANameOfProfileAtIndex(profile_index))) { 95f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // Set the profile to use the GAIA name as the profile name. Note, this 96f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // is a little weird if the user typed their GAIA name manually but 97f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // it's not a big deal. 98f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) cache.SetIsUsingGAIANameOfProfileAtIndex(profile_index, true); 99f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) } else { 100f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) PrefService* pref_service = profile->GetPrefs(); 101f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // Updating the profile preference will cause the cache to be updated for 102f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // this preference. 1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) pref_service->SetString(prefs::kProfileName, 1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) base::UTF16ToUTF8(new_profile_name)); 105f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 106f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // Changing the profile name can invalidate the profile index. 107f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) profile_index = cache.GetIndexOfProfileWithPath(profile_file_path); 108f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) if (profile_index == std::string::npos) 109f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) return; 110f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 111f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) cache.SetIsUsingGAIANameOfProfileAtIndex(profile_index, false); 112f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) } 113f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)} 114f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 1155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)std::vector<std::string> GetSecondaryAccountsForProfile( 1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) Profile* profile, 1175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const std::string& primary_account) { 1185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::vector<std::string> accounts = 1195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ProfileOAuth2TokenServiceFactory::GetForProfile(profile)->GetAccounts(); 1205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // The vector returned by ProfileOAuth2TokenService::GetAccounts() contains 1225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // the primary account too, so we need to remove it from the list. 1235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::vector<std::string>::iterator primary_index = 1245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::find_if(accounts.begin(), accounts.end(), 1255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::bind1st(std::equal_to<std::string>(), primary_account)); 1265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) DCHECK(primary_index != accounts.end()); 1275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) accounts.erase(primary_index); 1285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return accounts; 1305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 1315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool IsRegularOrGuestSession(Browser* browser) { 1335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) Profile* profile = browser->profile(); 1345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return profile->IsGuestSession() || !profile->IsOffTheRecord(); 1355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 1365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1379ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch} // namespace profiles 138