Upstream version 9.38.198.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/gaia_info_update_service.h"
13 #include "chrome/browser/profiles/gaia_info_update_service_factory.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/profiles/profile_info_cache.h"
16 #include "chrome/browser/profiles/profile_manager.h"
17 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
18 #include "chrome/browser/ui/browser.h"
19 #include "chrome/common/chrome_constants.h"
20 #include "chrome/common/pref_names.h"
21 #include "components/signin/core/browser/profile_oauth2_token_service.h"
22 #include "components/signin/core/common/profile_management_switches.h"
23 #include "grit/generated_resources.h"
24 #include "ui/base/l10n/l10n_util.h"
25 #include "ui/gfx/text_elider.h"
26
27 namespace profiles {
28
29 bool IsMultipleProfilesEnabled() {
30 #if defined(OS_ANDROID)
31   return false;
32 #endif
33   return true;
34 }
35
36 base::FilePath GetDefaultProfileDir(const base::FilePath& user_data_dir) {
37   base::FilePath default_profile_dir(user_data_dir);
38   default_profile_dir =
39       default_profile_dir.AppendASCII(chrome::kInitialProfile);
40   return default_profile_dir;
41 }
42
43 void RegisterPrefs(PrefRegistrySimple* registry) {
44   registry->RegisterStringPref(prefs::kProfileLastUsed, std::string());
45   registry->RegisterIntegerPref(prefs::kProfilesNumCreated, 1);
46   registry->RegisterListPref(prefs::kProfilesLastActive);
47 }
48
49 base::string16 GetAvatarNameForProfile(const base::FilePath& profile_path) {
50   base::string16 display_name;
51
52   if (profile_path == ProfileManager::GetGuestProfilePath()) {
53     display_name = l10n_util::GetStringUTF16(IDS_GUEST_PROFILE_NAME);
54   } else {
55     ProfileInfoCache& cache =
56         g_browser_process->profile_manager()->GetProfileInfoCache();
57     size_t index = cache.GetIndexOfProfileWithPath(profile_path);
58
59     if (index == std::string::npos)
60       return l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME);
61
62     // Using the --new-profile-management flag, there's a couple of rules
63     // about what the avatar button displays. If there's a single, local
64     // profile, with a default name (i.e. of the form Person %d), it should
65     // display IDS_SINGLE_PROFILE_DISPLAY_NAME. If this is a signed in profile,
66     // or the user has edited the profile name, or there are multiple profiles,
67     // it will return the actual name  of the profile.
68     base::string16 profile_name = cache.GetNameOfProfileAtIndex(index);
69     bool has_default_name = cache.ProfileIsUsingDefaultNameAtIndex(index);
70
71     if (cache.GetNumberOfProfiles() == 1 && has_default_name &&
72         cache.GetUserNameOfProfileAtIndex(index).empty()) {
73       display_name = l10n_util::GetStringUTF16(IDS_SINGLE_PROFILE_DISPLAY_NAME);
74     } else {
75       display_name = profile_name;
76     }
77   }
78   return display_name;
79 }
80
81 base::string16 GetAvatarButtonTextForProfile(Profile* profile) {
82   const int kMaxCharactersToDisplay = 15;
83   base::string16 name = GetAvatarNameForProfile(profile->GetPath());
84   name = gfx::TruncateString(name,
85                              kMaxCharactersToDisplay,
86                              gfx::CHARACTER_BREAK);
87   if (profile->IsSupervised()) {
88     name = l10n_util::GetStringFUTF16(IDS_SUPERVISED_USER_NEW_AVATAR_LABEL,
89                                       name);
90   }
91   return name;
92 }
93
94 void UpdateProfileName(Profile* profile,
95                        const base::string16& new_profile_name) {
96   PrefService* pref_service = profile->GetPrefs();
97   ProfileInfoCache& cache =
98       g_browser_process->profile_manager()->GetProfileInfoCache();
99
100   // This is only called when updating the profile name through the UI,
101   // so we can assume the user has done this on purpose.
102   size_t profile_index = cache.GetIndexOfProfileWithPath(profile->GetPath());
103   if (profile_index != std::string::npos)
104     pref_service->SetBoolean(prefs::kProfileUsingDefaultName, false);
105
106   // Updating the profile preference will cause the cache to be updated for
107   // this preference.
108   pref_service->SetString(prefs::kProfileName,
109                           base::UTF16ToUTF8(new_profile_name));
110 }
111
112 std::vector<std::string> GetSecondaryAccountsForProfile(
113     Profile* profile,
114     const std::string& primary_account) {
115   std::vector<std::string> accounts =
116       ProfileOAuth2TokenServiceFactory::GetForProfile(profile)->GetAccounts();
117
118   // The vector returned by ProfileOAuth2TokenService::GetAccounts() contains
119   // the primary account too, so we need to remove it from the list.
120   std::vector<std::string>::iterator primary_index =
121       std::find_if(accounts.begin(), accounts.end(),
122                    std::bind1st(std::equal_to<std::string>(), primary_account));
123   DCHECK(primary_index != accounts.end());
124   accounts.erase(primary_index);
125
126   return accounts;
127 }
128
129 bool IsRegularOrGuestSession(Browser* browser) {
130   Profile* profile = browser->profile();
131   return profile->IsGuestSession() || !profile->IsOffTheRecord();
132 }
133
134 void UpdateGaiaProfilePhotoIfNeeded(Profile* profile) {
135   // If the --google-profile-info flag isn't used, then the
136   // GAIAInfoUpdateService isn't initialized, and we can't download the picture.
137   if (!switches::IsGoogleProfileInfo())
138     return;
139
140   DCHECK(profile);
141   GAIAInfoUpdateServiceFactory::GetInstance()->GetForProfile(profile)->Update();
142 }
143
144 SigninErrorController* GetSigninErrorController(Profile* profile) {
145   ProfileOAuth2TokenService* token_service =
146       ProfileOAuth2TokenServiceFactory::GetForProfile(profile);
147   return token_service ? token_service->signin_error_controller() : NULL;
148 }
149
150 }  // namespace profiles