Home | History | Annotate | Download | only in profiles
      1 // Copyright 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/profiles/profiles_state.h"
      6 
      7 #include "base/command_line.h"
      8 #include "base/files/file_path.h"
      9 #include "base/prefs/pref_registry_simple.h"
     10 #include "base/prefs/pref_service.h"
     11 #include "base/strings/utf_string_conversions.h"
     12 #include "chrome/browser/browser_process.h"
     13 #include "chrome/browser/profiles/profile.h"
     14 #include "chrome/browser/profiles/profile_info_cache.h"
     15 #include "chrome/browser/profiles/profile_manager.h"
     16 #include "chrome/browser/ui/browser.h"
     17 #include "chrome/common/chrome_constants.h"
     18 #include "chrome/common/chrome_switches.h"
     19 #include "chrome/common/pref_names.h"
     20 #include "grit/generated_resources.h"
     21 #include "ui/base/l10n/l10n_util.h"
     22 
     23 #if defined(OS_CHROMEOS)
     24 #include "chrome/browser/chromeos/login/user_manager.h"
     25 #endif
     26 
     27 namespace profiles {
     28 
     29 bool IsMultipleProfilesEnabled() {
     30 #if defined(OS_ANDROID)
     31   return false;
     32 #endif
     33 #if defined(OS_CHROMEOS)
     34   return chromeos::UserManager::IsMultipleProfilesAllowed();
     35 #endif
     36 
     37   return true;
     38 }
     39 
     40 bool IsNewProfileManagementEnabled() {
     41   return CommandLine::ForCurrentProcess()->HasSwitch(
     42       switches::kNewProfileManagement);
     43 }
     44 
     45 base::FilePath GetDefaultProfileDir(const base::FilePath& user_data_dir) {
     46   base::FilePath default_profile_dir(user_data_dir);
     47   default_profile_dir =
     48       default_profile_dir.AppendASCII(chrome::kInitialProfile);
     49   return default_profile_dir;
     50 }
     51 
     52 base::FilePath GetProfilePrefsPath(
     53     const base::FilePath &profile_dir) {
     54   base::FilePath default_prefs_path(profile_dir);
     55   default_prefs_path = default_prefs_path.Append(chrome::kPreferencesFilename);
     56   return default_prefs_path;
     57 }
     58 
     59 void RegisterPrefs(PrefRegistrySimple* registry) {
     60   registry->RegisterStringPref(prefs::kProfileLastUsed, std::string());
     61   registry->RegisterIntegerPref(prefs::kProfilesNumCreated, 1);
     62   registry->RegisterListPref(prefs::kProfilesLastActive);
     63 }
     64 
     65 base::string16 GetActiveProfileDisplayName(Browser* browser) {
     66   base::string16 profile_name;
     67   Profile* profile = browser->profile();
     68 
     69   if (profile->IsGuestSession()) {
     70     profile_name = l10n_util::GetStringUTF16(IDS_GUEST_PROFILE_NAME);
     71   } else {
     72     ProfileInfoCache& cache =
     73         g_browser_process->profile_manager()->GetProfileInfoCache();
     74     size_t index = cache.GetIndexOfProfileWithPath(profile->GetPath());
     75     if (index != std::string::npos)
     76       profile_name = cache.GetNameOfProfileAtIndex(index);
     77   }
     78   return profile_name;
     79 }
     80 
     81 void UpdateProfileName(Profile* profile,
     82                        const base::string16& new_profile_name) {
     83   ProfileInfoCache& cache =
     84         g_browser_process->profile_manager()->GetProfileInfoCache();
     85   base::FilePath profile_file_path = profile->GetPath();
     86   size_t profile_index = cache.GetIndexOfProfileWithPath(profile_file_path);
     87 
     88   if ((new_profile_name ==
     89           cache.GetGAIAGivenNameOfProfileAtIndex(profile_index)) ||
     90       (new_profile_name == cache.GetGAIANameOfProfileAtIndex(profile_index))) {
     91     // Set the profile to use the GAIA name as the profile name. Note, this
     92     // is a little weird if the user typed their GAIA name manually but
     93     // it's not a big deal.
     94     cache.SetIsUsingGAIANameOfProfileAtIndex(profile_index, true);
     95   } else {
     96     PrefService* pref_service = profile->GetPrefs();
     97     // Updating the profile preference will cause the cache to be updated for
     98     // this preference.
     99     pref_service->SetString(prefs::kProfileName, UTF16ToUTF8(new_profile_name));
    100 
    101     // Changing the profile name can invalidate the profile index.
    102     profile_index = cache.GetIndexOfProfileWithPath(profile_file_path);
    103     if (profile_index == std::string::npos)
    104       return;
    105 
    106     cache.SetIsUsingGAIANameOfProfileAtIndex(profile_index, false);
    107   }
    108 }
    109 
    110 }  // namespace profiles
    111