Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / profiles / profiles_state.cc
blob961fe6d941a988d8b5cf0fe47a7a78f5b55aff0f
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.
5 #include "chrome/browser/profiles/profiles_state.h"
7 #include "base/files/file_path.h"
8 #include "base/prefs/pref_registry_simple.h"
9 #include "base/prefs/pref_service.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/profiles/profile_info_cache.h"
14 #include "chrome/browser/profiles/profile_manager.h"
15 #include "chrome/browser/signin/profile_oauth2_token_service.h"
16 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
17 #include "chrome/browser/ui/browser.h"
18 #include "chrome/common/chrome_constants.h"
19 #include "chrome/common/pref_names.h"
20 #include "grit/generated_resources.h"
21 #include "ui/base/l10n/l10n_util.h"
23 #if defined(OS_CHROMEOS)
24 #include "chrome/browser/chromeos/login/user_manager.h"
25 #endif
27 namespace profiles {
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
37 return true;
40 base::FilePath GetDefaultProfileDir(const base::FilePath& user_data_dir) {
41 base::FilePath default_profile_dir(user_data_dir);
42 default_profile_dir =
43 default_profile_dir.AppendASCII(chrome::kInitialProfile);
44 return default_profile_dir;
47 base::FilePath GetProfilePrefsPath(
48 const base::FilePath &profile_dir) {
49 base::FilePath default_prefs_path(profile_dir);
50 default_prefs_path = default_prefs_path.Append(chrome::kPreferencesFilename);
51 return default_prefs_path;
54 void RegisterPrefs(PrefRegistrySimple* registry) {
55 registry->RegisterStringPref(prefs::kProfileLastUsed, std::string());
56 registry->RegisterIntegerPref(prefs::kProfilesNumCreated, 1);
57 registry->RegisterListPref(prefs::kProfilesLastActive);
60 base::string16 GetActiveProfileDisplayName(Browser* browser) {
61 base::string16 profile_name;
62 Profile* profile = browser->profile();
64 if (profile->IsGuestSession()) {
65 profile_name = l10n_util::GetStringUTF16(IDS_GUEST_PROFILE_NAME);
66 } else {
67 ProfileInfoCache& cache =
68 g_browser_process->profile_manager()->GetProfileInfoCache();
69 size_t index = cache.GetIndexOfProfileWithPath(profile->GetPath());
70 if (index != std::string::npos)
71 profile_name = cache.GetNameOfProfileAtIndex(index);
73 return profile_name;
76 void UpdateProfileName(Profile* profile,
77 const base::string16& new_profile_name) {
78 ProfileInfoCache& cache =
79 g_browser_process->profile_manager()->GetProfileInfoCache();
80 base::FilePath profile_file_path = profile->GetPath();
81 size_t profile_index = cache.GetIndexOfProfileWithPath(profile_file_path);
83 if ((new_profile_name ==
84 cache.GetGAIAGivenNameOfProfileAtIndex(profile_index)) ||
85 (new_profile_name == cache.GetGAIANameOfProfileAtIndex(profile_index))) {
86 // Set the profile to use the GAIA name as the profile name. Note, this
87 // is a little weird if the user typed their GAIA name manually but
88 // it's not a big deal.
89 cache.SetIsUsingGAIANameOfProfileAtIndex(profile_index, true);
90 } else {
91 PrefService* pref_service = profile->GetPrefs();
92 // Updating the profile preference will cause the cache to be updated for
93 // this preference.
94 pref_service->SetString(prefs::kProfileName,
95 base::UTF16ToUTF8(new_profile_name));
97 // Changing the profile name can invalidate the profile index.
98 profile_index = cache.GetIndexOfProfileWithPath(profile_file_path);
99 if (profile_index == std::string::npos)
100 return;
102 cache.SetIsUsingGAIANameOfProfileAtIndex(profile_index, false);
106 std::vector<std::string> GetSecondaryAccountsForProfile(
107 Profile* profile,
108 const std::string& primary_account) {
109 std::vector<std::string> accounts =
110 ProfileOAuth2TokenServiceFactory::GetForProfile(profile)->GetAccounts();
112 // The vector returned by ProfileOAuth2TokenService::GetAccounts() contains
113 // the primary account too, so we need to remove it from the list.
114 std::vector<std::string>::iterator primary_index =
115 std::find_if(accounts.begin(), accounts.end(),
116 std::bind1st(std::equal_to<std::string>(), primary_account));
117 DCHECK(primary_index != accounts.end());
118 accounts.erase(primary_index);
120 return accounts;
123 } // namespace profiles