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/strings/sys_string_conversions.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/signin/signin_global_error.h"
11 #include "chrome/browser/signin/signin_manager.h"
12 #include "chrome/browser/signin/signin_manager_factory.h"
13 #include "chrome/browser/sync/profile_sync_service.h"
14 #include "chrome/browser/sync/profile_sync_service_factory.h"
15 #include "chrome/browser/sync/sync_global_error.h"
16 #include "grit/chromium_strings.h"
17 #include "grit/generated_resources.h"
18 #include "ui/base/l10n/l10n_util.h"
19 #include "ui/gfx/font_list.h"
20 #include "ui/gfx/text_elider.h"
23 // Maximum width of a username - we trim emails that are wider than this so
24 // the wrench menu doesn't get ridiculously wide.
25 const int kUsernameMaxWidth
= 200;
28 namespace signin_ui_util
{
30 GlobalError
* GetSignedInServiceError(Profile
* profile
) {
31 std::vector
<GlobalError
*> errors
= GetSignedInServiceErrors(profile
);
37 std::vector
<GlobalError
*> GetSignedInServiceErrors(Profile
* profile
) {
38 std::vector
<GlobalError
*> errors
;
40 // Auth errors have the highest priority - after that, individual service
42 SigninGlobalError
* signin_error
= SigninGlobalError::GetForProfile(profile
);
43 if (signin_error
&& signin_error
->HasMenuItem())
44 errors
.push_back(signin_error
);
46 // No auth error - now try other services. Currently the list is just hard-
47 // coded but in the future if we add more we can create some kind of
48 // registration framework.
49 if (profile
->IsSyncAccessible()) {
50 ProfileSyncService
* service
=
51 ProfileSyncServiceFactory::GetForProfile(profile
);
52 SyncGlobalError
* error
= service
->sync_global_error();
53 if (error
&& error
->HasMenuItem())
54 errors
.push_back(error
);
60 base::string16
GetSigninMenuLabel(Profile
* profile
) {
61 GlobalError
* error
= signin_ui_util::GetSignedInServiceError(profile
);
63 return error
->MenuItemLabel();
65 // No errors, so just display the signed in user, if any.
66 ProfileSyncService
* service
= profile
->IsSyncAccessible() ?
67 ProfileSyncServiceFactory::GetForProfile(profile
) : NULL
;
69 // Even if the user is signed in, don't display the "signed in as..."
70 // label if we're still setting up sync.
71 if (!service
|| !service
->FirstSetupInProgress()) {
73 SigninManagerBase
* signin_manager
=
74 SigninManagerFactory::GetForProfileIfExists(profile
);
76 username
= signin_manager
->GetAuthenticatedUsername();
77 if (!username
.empty() && !signin_manager
->AuthInProgress()) {
78 base::string16 elided_username
= gfx::ElideEmail(
79 base::UTF8ToUTF16(username
), gfx::FontList(), kUsernameMaxWidth
);
80 return l10n_util::GetStringFUTF16(IDS_SYNC_MENU_SYNCED_LABEL
,
84 return l10n_util::GetStringFUTF16(IDS_SYNC_MENU_PRE_SYNCED_LABEL
,
85 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME
));
88 // Given an authentication state this helper function returns various labels
89 // that can be used to display information about the state.
90 void GetStatusLabelsForAuthError(Profile
* profile
,
91 const SigninManagerBase
& signin_manager
,
92 base::string16
* status_label
,
93 base::string16
* link_label
) {
94 base::string16 username
=
95 base::UTF8ToUTF16(signin_manager
.GetAuthenticatedUsername());
96 base::string16 product_name
= l10n_util::GetStringUTF16(IDS_PRODUCT_NAME
);
98 link_label
->assign(l10n_util::GetStringUTF16(IDS_SYNC_RELOGIN_LINK_LABEL
));
100 const GoogleServiceAuthError::State state
=
101 SigninGlobalError::GetForProfile(profile
)->GetLastAuthError().state();
103 case GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS
:
104 case GoogleServiceAuthError::SERVICE_ERROR
:
105 case GoogleServiceAuthError::ACCOUNT_DELETED
:
106 case GoogleServiceAuthError::ACCOUNT_DISABLED
:
107 // If the user name is empty then the first login failed, otherwise the
108 // credentials are out-of-date.
109 if (username
.empty()) {
111 status_label
->assign(
112 l10n_util::GetStringUTF16(IDS_SYNC_INVALID_USER_CREDENTIALS
));
116 status_label
->assign(
117 l10n_util::GetStringUTF16(IDS_SYNC_LOGIN_INFO_OUT_OF_DATE
));
121 case GoogleServiceAuthError::SERVICE_UNAVAILABLE
:
123 status_label
->assign(
124 l10n_util::GetStringUTF16(IDS_SYNC_SERVICE_UNAVAILABLE
));
129 case GoogleServiceAuthError::CONNECTION_FAILED
:
131 status_label
->assign(
132 l10n_util::GetStringFUTF16(IDS_SYNC_SERVER_IS_UNREACHABLE
,
135 // Note that there is little the user can do if the server is not
136 // reachable. Since attempting to re-connect is done automatically by
137 // the Syncer, we do not show the (re)login link.
143 status_label
->assign(l10n_util::GetStringUTF16(
144 IDS_SYNC_ERROR_SIGNING_IN
));
150 } // namespace signin_ui_util