Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / signin / signin_ui_util.cc
bloba5ed1122b1c59a704cf6411bc19d223e9a33527b
1 // Copyright (c) 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/signin/signin_ui_util.h"
7 #include "base/prefs/pref_service.h"
8 #include "base/strings/sys_string_conversions.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/signin/account_tracker_service_factory.h"
12 #include "chrome/browser/signin/signin_error_controller_factory.h"
13 #include "chrome/browser/signin/signin_global_error.h"
14 #include "chrome/browser/signin/signin_global_error_factory.h"
15 #include "chrome/browser/signin/signin_manager_factory.h"
16 #include "chrome/browser/sync/profile_sync_service.h"
17 #include "chrome/browser/sync/profile_sync_service_factory.h"
18 #include "chrome/browser/sync/sync_global_error.h"
19 #include "chrome/browser/sync/sync_global_error_factory.h"
20 #include "chrome/browser/ui/browser_navigator.h"
21 #include "chrome/common/pref_names.h"
22 #include "chrome/grit/chromium_strings.h"
23 #include "chrome/grit/generated_resources.h"
24 #include "components/signin/core/browser/account_tracker_service.h"
25 #include "components/signin/core/browser/signin_manager.h"
26 #include "components/signin/core/common/profile_management_switches.h"
27 #include "ui/base/l10n/l10n_util.h"
28 #include "ui/gfx/font_list.h"
29 #include "ui/gfx/text_elider.h"
31 namespace {
32 // Maximum width of a username - we trim emails that are wider than this so
33 // the wrench menu doesn't get ridiculously wide.
34 const int kUsernameMaxWidth = 200;
35 } // namespace
37 namespace signin_ui_util {
39 GlobalError* GetSignedInServiceError(Profile* profile) {
40 std::vector<GlobalError*> errors = GetSignedInServiceErrors(profile);
41 if (errors.empty())
42 return NULL;
43 return errors[0];
46 std::vector<GlobalError*> GetSignedInServiceErrors(Profile* profile) {
47 std::vector<GlobalError*> errors;
48 // Chrome OS doesn't use SigninGlobalError or SyncGlobalError. Other platforms
49 // may use these services to show auth and sync errors in the toolbar menu.
50 #if !defined(OS_CHROMEOS)
51 // Auth errors have the highest priority - after that, individual service
52 // errors.
53 SigninGlobalError* signin_error =
54 SigninGlobalErrorFactory::GetForProfile(profile);
55 if (signin_error && signin_error->HasError())
56 errors.push_back(signin_error);
58 // No auth error - now try other services. Currently the list is just hard-
59 // coded but in the future if we add more we can create some kind of
60 // registration framework.
61 if (profile->IsSyncAllowed()) {
62 SyncGlobalError* error = SyncGlobalErrorFactory::GetForProfile(profile);
63 if (error && error->HasMenuItem())
64 errors.push_back(error);
66 #endif
68 return errors;
71 base::string16 GetSigninMenuLabel(Profile* profile) {
72 GlobalError* error = signin_ui_util::GetSignedInServiceError(profile);
73 if (error)
74 return error->MenuItemLabel();
76 // No errors, so just display the signed in user, if any.
77 ProfileSyncService* service = profile->IsSyncAllowed() ?
78 ProfileSyncServiceFactory::GetForProfile(profile) : NULL;
80 // Even if the user is signed in, don't display the "signed in as..."
81 // label if we're still setting up sync.
82 if (!service || !service->FirstSetupInProgress()) {
83 std::string username;
84 SigninManagerBase* signin_manager =
85 SigninManagerFactory::GetForProfileIfExists(profile);
86 if (signin_manager)
87 username = signin_manager->GetAuthenticatedUsername();
88 if (!username.empty() && !signin_manager->AuthInProgress()) {
89 const base::string16 elided = gfx::ElideText(base::UTF8ToUTF16(username),
90 gfx::FontList(), kUsernameMaxWidth, gfx::ELIDE_EMAIL);
91 return l10n_util::GetStringFUTF16(IDS_SYNC_MENU_SYNCED_LABEL, elided);
94 return l10n_util::GetStringFUTF16(IDS_SYNC_MENU_PRE_SYNCED_LABEL,
95 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME));
98 // Given an authentication state this helper function returns various labels
99 // that can be used to display information about the state.
100 void GetStatusLabelsForAuthError(Profile* profile,
101 const SigninManagerBase& signin_manager,
102 base::string16* status_label,
103 base::string16* link_label) {
104 base::string16 product_name = l10n_util::GetStringUTF16(IDS_PRODUCT_NAME);
105 if (link_label)
106 link_label->assign(l10n_util::GetStringUTF16(IDS_SYNC_RELOGIN_LINK_LABEL));
108 const GoogleServiceAuthError::State state =
109 SigninErrorControllerFactory::GetForProfile(profile)->
110 auth_error().state();
111 switch (state) {
112 case GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS:
113 case GoogleServiceAuthError::SERVICE_ERROR:
114 case GoogleServiceAuthError::ACCOUNT_DELETED:
115 case GoogleServiceAuthError::ACCOUNT_DISABLED:
116 // If the user name is empty then the first login failed, otherwise the
117 // credentials are out-of-date.
118 if (!signin_manager.IsAuthenticated()) {
119 if (status_label) {
120 status_label->assign(
121 l10n_util::GetStringUTF16(IDS_SYNC_INVALID_USER_CREDENTIALS));
123 } else {
124 if (status_label) {
125 status_label->assign(
126 l10n_util::GetStringUTF16(IDS_SYNC_LOGIN_INFO_OUT_OF_DATE));
129 break;
130 case GoogleServiceAuthError::SERVICE_UNAVAILABLE:
131 if (status_label) {
132 status_label->assign(
133 l10n_util::GetStringUTF16(IDS_SYNC_SERVICE_UNAVAILABLE));
135 if (link_label)
136 link_label->clear();
137 break;
138 case GoogleServiceAuthError::CONNECTION_FAILED:
139 if (status_label) {
140 status_label->assign(
141 l10n_util::GetStringFUTF16(IDS_SYNC_SERVER_IS_UNREACHABLE,
142 product_name));
144 // Note that there is little the user can do if the server is not
145 // reachable. Since attempting to re-connect is done automatically by
146 // the Syncer, we do not show the (re)login link.
147 if (link_label)
148 link_label->clear();
149 break;
150 default:
151 if (status_label) {
152 status_label->assign(l10n_util::GetStringUTF16(
153 IDS_SYNC_ERROR_SIGNING_IN));
155 break;
159 void InitializePrefsForProfile(Profile* profile) {
160 if (profile->IsNewProfile() && switches::IsNewAvatarMenu()) {
161 // Suppresses the upgrade tutorial for a new profile.
162 profile->GetPrefs()->SetInteger(
163 prefs::kProfileAvatarTutorialShown, kUpgradeWelcomeTutorialShowMax + 1);
167 void ShowSigninErrorLearnMorePage(Profile* profile) {
168 static const char kSigninErrorLearnMoreUrl[] =
169 "https://support.google.com/chrome/answer/1181420?";
170 chrome::NavigateParams params(
171 profile, GURL(kSigninErrorLearnMoreUrl), ui::PAGE_TRANSITION_LINK);
172 params.disposition = NEW_FOREGROUND_TAB;
173 chrome::Navigate(&params);
176 std::string GetDisplayEmail(Profile* profile, const std::string& account_id) {
177 AccountTrackerService* account_tracker =
178 AccountTrackerServiceFactory::GetForProfile(profile);
179 std::string email = account_tracker->GetAccountInfo(account_id).email;
180 if (email.empty()) {
181 DCHECK_EQ(AccountTrackerService::MIGRATION_NOT_STARTED,
182 account_tracker->GetMigrationState());
183 return account_id;
185 return email;
188 } // namespace signin_ui_util