Adding instrumentation to locate the source of jankiness
[chromium-blink-merge.git] / chrome / browser / signin / signin_ui_util.cc
blob831674775a5b24683d29c4d3501f30edee33780e
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/profile_oauth2_token_service_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/profile_oauth2_token_service.h"
26 #include "components/signin/core/browser/signin_manager.h"
27 #include "components/signin/core/common/profile_management_switches.h"
28 #include "ui/base/l10n/l10n_util.h"
29 #include "ui/gfx/font_list.h"
30 #include "ui/gfx/text_elider.h"
32 namespace {
33 // Maximum width of a username - we trim emails that are wider than this so
34 // the wrench menu doesn't get ridiculously wide.
35 const int kUsernameMaxWidth = 200;
36 } // namespace
38 namespace signin_ui_util {
40 GlobalError* GetSignedInServiceError(Profile* profile) {
41 std::vector<GlobalError*> errors = GetSignedInServiceErrors(profile);
42 if (errors.empty())
43 return NULL;
44 return errors[0];
47 std::vector<GlobalError*> GetSignedInServiceErrors(Profile* profile) {
48 std::vector<GlobalError*> errors;
49 // Chrome OS doesn't use SigninGlobalError or SyncGlobalError. Other platforms
50 // may use these services to show auth and sync errors in the toolbar menu.
51 #if !defined(OS_CHROMEOS)
52 // Auth errors have the highest priority - after that, individual service
53 // errors.
54 SigninGlobalError* signin_error =
55 SigninGlobalErrorFactory::GetForProfile(profile);
56 if (signin_error && signin_error->HasError())
57 errors.push_back(signin_error);
59 // No auth error - now try other services. Currently the list is just hard-
60 // coded but in the future if we add more we can create some kind of
61 // registration framework.
62 if (profile->IsSyncAccessible()) {
63 SyncGlobalError* error = SyncGlobalErrorFactory::GetForProfile(profile);
64 if (error && error->HasMenuItem())
65 errors.push_back(error);
67 #endif
69 return errors;
72 base::string16 GetSigninMenuLabel(Profile* profile) {
73 GlobalError* error = signin_ui_util::GetSignedInServiceError(profile);
74 if (error)
75 return error->MenuItemLabel();
77 // No errors, so just display the signed in user, if any.
78 ProfileSyncService* service = profile->IsSyncAccessible() ?
79 ProfileSyncServiceFactory::GetForProfile(profile) : NULL;
81 // Even if the user is signed in, don't display the "signed in as..."
82 // label if we're still setting up sync.
83 if (!service || !service->FirstSetupInProgress()) {
84 std::string username;
85 SigninManagerBase* signin_manager =
86 SigninManagerFactory::GetForProfileIfExists(profile);
87 if (signin_manager)
88 username = signin_manager->GetAuthenticatedUsername();
89 if (!username.empty() && !signin_manager->AuthInProgress()) {
90 const base::string16 elided = gfx::ElideText(base::UTF8ToUTF16(username),
91 gfx::FontList(), kUsernameMaxWidth, gfx::ELIDE_EMAIL);
92 return l10n_util::GetStringFUTF16(IDS_SYNC_MENU_SYNCED_LABEL, elided);
95 return l10n_util::GetStringFUTF16(IDS_SYNC_MENU_PRE_SYNCED_LABEL,
96 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME));
99 // Given an authentication state this helper function returns various labels
100 // that can be used to display information about the state.
101 void GetStatusLabelsForAuthError(Profile* profile,
102 const SigninManagerBase& signin_manager,
103 base::string16* status_label,
104 base::string16* link_label) {
105 base::string16 product_name = l10n_util::GetStringUTF16(IDS_PRODUCT_NAME);
106 if (link_label)
107 link_label->assign(l10n_util::GetStringUTF16(IDS_SYNC_RELOGIN_LINK_LABEL));
109 const GoogleServiceAuthError::State state =
110 ProfileOAuth2TokenServiceFactory::GetForProfile(profile)->
111 signin_error_controller()->auth_error().state();
112 switch (state) {
113 case GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS:
114 case GoogleServiceAuthError::SERVICE_ERROR:
115 case GoogleServiceAuthError::ACCOUNT_DELETED:
116 case GoogleServiceAuthError::ACCOUNT_DISABLED:
117 // If the user name is empty then the first login failed, otherwise the
118 // credentials are out-of-date.
119 if (!signin_manager.IsAuthenticated()) {
120 if (status_label) {
121 status_label->assign(
122 l10n_util::GetStringUTF16(IDS_SYNC_INVALID_USER_CREDENTIALS));
124 } else {
125 if (status_label) {
126 status_label->assign(
127 l10n_util::GetStringUTF16(IDS_SYNC_LOGIN_INFO_OUT_OF_DATE));
130 break;
131 case GoogleServiceAuthError::SERVICE_UNAVAILABLE:
132 if (status_label) {
133 status_label->assign(
134 l10n_util::GetStringUTF16(IDS_SYNC_SERVICE_UNAVAILABLE));
136 if (link_label)
137 link_label->clear();
138 break;
139 case GoogleServiceAuthError::CONNECTION_FAILED:
140 if (status_label) {
141 status_label->assign(
142 l10n_util::GetStringFUTF16(IDS_SYNC_SERVER_IS_UNREACHABLE,
143 product_name));
145 // Note that there is little the user can do if the server is not
146 // reachable. Since attempting to re-connect is done automatically by
147 // the Syncer, we do not show the (re)login link.
148 if (link_label)
149 link_label->clear();
150 break;
151 default:
152 if (status_label) {
153 status_label->assign(l10n_util::GetStringUTF16(
154 IDS_SYNC_ERROR_SIGNING_IN));
156 break;
160 void InitializePrefsForProfile(Profile* profile) {
161 // Suppresses the upgrade tutorial for a new profile.
162 if (profile->IsNewProfile() && switches::IsNewAvatarMenu()) {
163 profile->GetPrefs()->SetInteger(
164 prefs::kProfileAvatarTutorialShown, kUpgradeWelcomeTutorialShowMax + 1);
168 void ShowSigninErrorLearnMorePage(Profile* profile) {
169 static const char kSigninErrorLearnMoreUrl[] =
170 "https://support.google.com/chrome/answer/1181420?";
171 chrome::NavigateParams params(
172 profile, GURL(kSigninErrorLearnMoreUrl), ui::PAGE_TRANSITION_LINK);
173 params.disposition = NEW_FOREGROUND_TAB;
174 chrome::Navigate(&params);
177 std::string GetDisplayEmail(Profile* profile, const std::string& account_id) {
178 AccountTrackerService* account_tracker =
179 AccountTrackerServiceFactory::GetForProfile(profile);
180 std::string email = account_tracker->GetAccountInfo(account_id).email;
181 if (email.empty()) {
182 DCHECK_EQ(AccountTrackerService::MIGRATION_NOT_STARTED,
183 account_tracker->GetMigrationState());
184 return account_id;
186 return email;
189 } // namespace signin_ui_util