Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / profiles / profiles_state.cc
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/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"
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 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;
45 }
46
47 void RegisterPrefs(PrefRegistrySimple* registry) {
48   registry->RegisterStringPref(prefs::kProfileLastUsed, std::string());
49   registry->RegisterIntegerPref(prefs::kProfilesNumCreated, 1);
50   registry->RegisterListPref(prefs::kProfilesLastActive);
51 }
52
53 base::string16 GetActiveProfileDisplayName(Browser* browser) {
54   base::string16 profile_name;
55   Profile* profile = browser->profile();
56
57   if (profile->IsGuestSession()) {
58     profile_name = l10n_util::GetStringUTF16(IDS_GUEST_PROFILE_NAME);
59   } else {
60     ProfileInfoCache& cache =
61         g_browser_process->profile_manager()->GetProfileInfoCache();
62     size_t index = cache.GetIndexOfProfileWithPath(profile->GetPath());
63     if (index != std::string::npos)
64       profile_name = cache.GetNameOfProfileAtIndex(index);
65   }
66   return profile_name;
67 }
68
69 void UpdateProfileName(Profile* profile,
70                        const base::string16& new_profile_name) {
71   ProfileInfoCache& cache =
72         g_browser_process->profile_manager()->GetProfileInfoCache();
73   base::FilePath profile_file_path = profile->GetPath();
74   size_t profile_index = cache.GetIndexOfProfileWithPath(profile_file_path);
75
76   if ((new_profile_name ==
77           cache.GetGAIAGivenNameOfProfileAtIndex(profile_index)) ||
78       (new_profile_name == cache.GetGAIANameOfProfileAtIndex(profile_index))) {
79     // Set the profile to use the GAIA name as the profile name. Note, this
80     // is a little weird if the user typed their GAIA name manually but
81     // it's not a big deal.
82     cache.SetIsUsingGAIANameOfProfileAtIndex(profile_index, true);
83   } else {
84     PrefService* pref_service = profile->GetPrefs();
85     // Updating the profile preference will cause the cache to be updated for
86     // this preference.
87     pref_service->SetString(prefs::kProfileName,
88                             base::UTF16ToUTF8(new_profile_name));
89
90     // Changing the profile name can invalidate the profile index.
91     profile_index = cache.GetIndexOfProfileWithPath(profile_file_path);
92     if (profile_index == std::string::npos)
93       return;
94
95     cache.SetIsUsingGAIANameOfProfileAtIndex(profile_index, false);
96   }
97 }
98
99 std::vector<std::string> GetSecondaryAccountsForProfile(
100     Profile* profile,
101     const std::string& primary_account) {
102   std::vector<std::string> accounts =
103       ProfileOAuth2TokenServiceFactory::GetForProfile(profile)->GetAccounts();
104
105   // The vector returned by ProfileOAuth2TokenService::GetAccounts() contains
106   // the primary account too, so we need to remove it from the list.
107   std::vector<std::string>::iterator primary_index =
108       std::find_if(accounts.begin(), accounts.end(),
109                    std::bind1st(std::equal_to<std::string>(), primary_account));
110   DCHECK(primary_index != accounts.end());
111   accounts.erase(primary_index);
112
113   return accounts;
114 }
115
116 bool IsRegularOrGuestSession(Browser* browser) {
117   Profile* profile = browser->profile();
118   return profile->IsGuestSession() || !profile->IsOffTheRecord();
119 }
120
121 }  // namespace profiles