profiles_state.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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 535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)base::string16 GetActiveProfileDisplayName(Browser* browser) { 54a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) base::string16 profile_name; 558bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) Profile* profile = browser->profile(); 568bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 578bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) if (profile->IsGuestSession()) { 588bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) profile_name = l10n_util::GetStringUTF16(IDS_GUEST_PROFILE_NAME); 598bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) } else { 608bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) ProfileInfoCache& cache = 618bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) g_browser_process->profile_manager()->GetProfileInfoCache(); 628bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) size_t index = cache.GetIndexOfProfileWithPath(profile->GetPath()); 638bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) if (index != std::string::npos) 648bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) profile_name = cache.GetNameOfProfileAtIndex(index); 658bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) } 668bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) return profile_name; 678bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)} 688bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 69f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void UpdateProfileName(Profile* profile, 70f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) const base::string16& new_profile_name) { 71f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) ProfileInfoCache& cache = 72f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) g_browser_process->profile_manager()->GetProfileInfoCache(); 73f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) base::FilePath profile_file_path = profile->GetPath(); 74f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) size_t profile_index = cache.GetIndexOfProfileWithPath(profile_file_path); 75f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 76f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) if ((new_profile_name == 77f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) cache.GetGAIAGivenNameOfProfileAtIndex(profile_index)) || 78f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) (new_profile_name == cache.GetGAIANameOfProfileAtIndex(profile_index))) { 79f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // Set the profile to use the GAIA name as the profile name. Note, this 80f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // is a little weird if the user typed their GAIA name manually but 81f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // it's not a big deal. 82f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) cache.SetIsUsingGAIANameOfProfileAtIndex(profile_index, true); 83f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) } else { 84f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) PrefService* pref_service = profile->GetPrefs(); 85f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // Updating the profile preference will cause the cache to be updated for 86f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // this preference. 875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) pref_service->SetString(prefs::kProfileName, 885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) base::UTF16ToUTF8(new_profile_name)); 89f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 90f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // Changing the profile name can invalidate the profile index. 91f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) profile_index = cache.GetIndexOfProfileWithPath(profile_file_path); 92f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) if (profile_index == std::string::npos) 93f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) return; 94f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 95f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) cache.SetIsUsingGAIANameOfProfileAtIndex(profile_index, false); 96f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) } 97f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)} 98f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)std::vector<std::string> GetSecondaryAccountsForProfile( 1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) Profile* profile, 1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const std::string& primary_account) { 1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::vector<std::string> accounts = 1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ProfileOAuth2TokenServiceFactory::GetForProfile(profile)->GetAccounts(); 1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // The vector returned by ProfileOAuth2TokenService::GetAccounts() contains 1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // the primary account too, so we need to remove it from the list. 1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::vector<std::string>::iterator primary_index = 1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::find_if(accounts.begin(), accounts.end(), 1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::bind1st(std::equal_to<std::string>(), primary_account)); 1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) DCHECK(primary_index != accounts.end()); 1115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) accounts.erase(primary_index); 1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return accounts; 1145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 1155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool IsRegularOrGuestSession(Browser* browser) { 1175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) Profile* profile = browser->profile(); 1185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return profile->IsGuestSession() || !profile->IsOffTheRecord(); 1195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 1205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1219ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch} // namespace profiles 122