profiles_state.cc revision effb81e5f8246d0db0270817048dc992db66e9fb
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_factory.h"
168bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/ui/browser.h"
179ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "chrome/common/chrome_constants.h"
189ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "chrome/common/pref_names.h"
19effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "components/signin/core/browser/profile_oauth2_token_service.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) {
87effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  PrefService* pref_service = profile->GetPrefs();
88effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Updating the profile preference will cause the cache to be updated for
89effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // this preference.
90effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  pref_service->SetString(prefs::kProfileName,
91effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                          base::UTF16ToUTF8(new_profile_name));
92f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
93f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)std::vector<std::string> GetSecondaryAccountsForProfile(
955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    Profile* profile,
965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const std::string& primary_account) {
975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::vector<std::string> accounts =
985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      ProfileOAuth2TokenServiceFactory::GetForProfile(profile)->GetAccounts();
995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // The vector returned by ProfileOAuth2TokenService::GetAccounts() contains
1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // the primary account too, so we need to remove it from the list.
1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::vector<std::string>::iterator primary_index =
1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      std::find_if(accounts.begin(), accounts.end(),
1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                   std::bind1st(std::equal_to<std::string>(), primary_account));
1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DCHECK(primary_index != accounts.end());
1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  accounts.erase(primary_index);
1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return accounts;
1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool IsRegularOrGuestSession(Browser* browser) {
1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  Profile* profile = browser->profile();
1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return profile->IsGuestSession() || !profile->IsOffTheRecord();
1145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1169ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch}  // namespace profiles
117