profiles_state.cc revision f2477e01787aa58f445919b809d89e252beef54f
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/command_line.h"
89ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "base/files/file_path.h"
99ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "base/prefs/pref_registry_simple.h"
10f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "base/prefs/pref_service.h"
11f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "base/strings/utf_string_conversions.h"
128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/browser_process.h"
138bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/profiles/profile.h"
148bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/profiles/profile_info_cache.h"
158bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/profiles/profile_manager.h"
168bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/ui/browser.h"
179ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "chrome/common/chrome_constants.h"
189ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "chrome/common/chrome_switches.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
409ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdochbool IsNewProfileManagementEnabled() {
419ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  return CommandLine::ForCurrentProcess()->HasSwitch(
429ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch      switches::kNewProfileManagement);
439ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch}
449ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
458bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)base::FilePath GetDefaultProfileDir(const base::FilePath& user_data_dir) {
469ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  base::FilePath default_profile_dir(user_data_dir);
479ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  default_profile_dir =
489ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch      default_profile_dir.AppendASCII(chrome::kInitialProfile);
499ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  return default_profile_dir;
509ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch}
519ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
529ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdochbase::FilePath GetProfilePrefsPath(
539ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch    const base::FilePath &profile_dir) {
549ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  base::FilePath default_prefs_path(profile_dir);
559ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  default_prefs_path = default_prefs_path.Append(chrome::kPreferencesFilename);
569ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  return default_prefs_path;
579ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch}
589ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
599ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdochvoid RegisterPrefs(PrefRegistrySimple* registry) {
609ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  registry->RegisterStringPref(prefs::kProfileLastUsed, std::string());
619ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  registry->RegisterIntegerPref(prefs::kProfilesNumCreated, 1);
629ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  registry->RegisterListPref(prefs::kProfilesLastActive);
639ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch}
649ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
658bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)string16 GetActiveProfileDisplayName(Browser* browser) {
668bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  string16 profile_name;
678bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  Profile* profile = browser->profile();
688bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
698bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (profile->IsGuestSession()) {
708bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    profile_name = l10n_util::GetStringUTF16(IDS_GUEST_PROFILE_NAME);
718bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  } else {
728bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    ProfileInfoCache& cache =
738bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        g_browser_process->profile_manager()->GetProfileInfoCache();
748bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    size_t index = cache.GetIndexOfProfileWithPath(profile->GetPath());
758bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    if (index != std::string::npos)
768bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      profile_name = cache.GetNameOfProfileAtIndex(index);
778bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
788bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  return profile_name;
798bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
808bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
81f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void UpdateProfileName(Profile* profile,
82f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                       const base::string16& new_profile_name) {
83f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  ProfileInfoCache& cache =
84f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        g_browser_process->profile_manager()->GetProfileInfoCache();
85f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  base::FilePath profile_file_path = profile->GetPath();
86f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  size_t profile_index = cache.GetIndexOfProfileWithPath(profile_file_path);
87f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
88f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if ((new_profile_name ==
89f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          cache.GetGAIAGivenNameOfProfileAtIndex(profile_index)) ||
90f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      (new_profile_name == cache.GetGAIANameOfProfileAtIndex(profile_index))) {
91f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // Set the profile to use the GAIA name as the profile name. Note, this
92f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // is a little weird if the user typed their GAIA name manually but
93f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // it's not a big deal.
94f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    cache.SetIsUsingGAIANameOfProfileAtIndex(profile_index, true);
95f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  } else {
96f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    PrefService* pref_service = profile->GetPrefs();
97f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // Updating the profile preference will cause the cache to be updated for
98f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // this preference.
99f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    pref_service->SetString(prefs::kProfileName, UTF16ToUTF8(new_profile_name));
100f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
101f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // Changing the profile name can invalidate the profile index.
102f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    profile_index = cache.GetIndexOfProfileWithPath(profile_file_path);
103f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    if (profile_index == std::string::npos)
104f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      return;
105f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
106f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    cache.SetIsUsingGAIANameOfProfileAtIndex(profile_index, false);
107f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
108f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
109f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
1109ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch}  // namespace profiles
111