Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / signin / signin_ui_util.cc
blobbab391d20493a3834f06a46b143b7080b1b3cf4b
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 signin_ui_util {
33 namespace {
35 // Maximum width of a username - we trim emails that are wider than this so
36 // the wrench menu doesn't get ridiculously wide.
37 const int kUsernameMaxWidth = 200;
39 // Returns all errors reported by signed in services.
40 std::vector<GlobalError*> GetSignedInServiceErrors(Profile* profile) {
41 std::vector<GlobalError*> errors;
42 // Chrome OS doesn't use SigninGlobalError or SyncGlobalError. Other platforms
43 // may use these services to show auth and sync errors in the toolbar menu.
44 #if !defined(OS_CHROMEOS)
45 // Auth errors have the highest priority - after that, individual service
46 // errors.
47 SigninGlobalError* signin_error =
48 SigninGlobalErrorFactory::GetForProfile(profile);
49 if (signin_error && signin_error->HasError())
50 errors.push_back(signin_error);
52 // No auth error - now try other services. Currently the list is just hard-
53 // coded but in the future if we add more we can create some kind of
54 // registration framework.
55 if (profile->IsSyncAllowed()) {
56 SyncGlobalError* error = SyncGlobalErrorFactory::GetForProfile(profile);
57 if (error && error->HasMenuItem())
58 errors.push_back(error);
60 #endif
62 return errors;
65 // If a signed in service is reporting an error, returns the GlobalError
66 // object associated with that service, or NULL if no errors are reported.
67 GlobalError* GetSignedInServiceError(Profile* profile) {
68 std::vector<GlobalError*> errors = GetSignedInServiceErrors(profile);
69 if (errors.empty())
70 return NULL;
71 return errors[0];
74 } // namespace
76 base::string16 GetSigninMenuLabel(Profile* profile) {
77 GlobalError* error = signin_ui_util::GetSignedInServiceError(profile);
78 if (error)
79 return error->MenuItemLabel();
81 // No errors, so just display the signed in user, if any.
82 ProfileSyncService* service = profile->IsSyncAllowed() ?
83 ProfileSyncServiceFactory::GetForProfile(profile) : NULL;
85 // Even if the user is signed in, don't display the "signed in as..."
86 // label if we're still setting up sync.
87 if (!service || !service->FirstSetupInProgress()) {
88 std::string username;
89 SigninManagerBase* signin_manager =
90 SigninManagerFactory::GetForProfileIfExists(profile);
91 if (signin_manager)
92 username = signin_manager->GetAuthenticatedAccountInfo().email;
93 if (!username.empty() && !signin_manager->AuthInProgress()) {
94 const base::string16 elided = gfx::ElideText(base::UTF8ToUTF16(username),
95 gfx::FontList(), kUsernameMaxWidth, gfx::ELIDE_EMAIL);
96 return l10n_util::GetStringFUTF16(IDS_SYNC_MENU_SYNCED_LABEL, elided);
99 return l10n_util::GetStringFUTF16(IDS_SYNC_MENU_PRE_SYNCED_LABEL,
100 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME));
103 // Given an authentication state this helper function returns various labels
104 // that can be used to display information about the state.
105 void GetStatusLabelsForAuthError(Profile* profile,
106 const SigninManagerBase& signin_manager,
107 base::string16* status_label,
108 base::string16* link_label) {
109 base::string16 product_name = l10n_util::GetStringUTF16(IDS_PRODUCT_NAME);
110 if (link_label)
111 link_label->assign(l10n_util::GetStringUTF16(IDS_SYNC_RELOGIN_LINK_LABEL));
113 const GoogleServiceAuthError::State state =
114 SigninErrorControllerFactory::GetForProfile(profile)->
115 auth_error().state();
116 switch (state) {
117 case GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS:
118 case GoogleServiceAuthError::SERVICE_ERROR:
119 case GoogleServiceAuthError::ACCOUNT_DELETED:
120 case GoogleServiceAuthError::ACCOUNT_DISABLED:
121 // If the user name is empty then the first login failed, otherwise the
122 // credentials are out-of-date.
123 if (!signin_manager.IsAuthenticated()) {
124 if (status_label) {
125 status_label->assign(
126 l10n_util::GetStringUTF16(IDS_SYNC_INVALID_USER_CREDENTIALS));
128 } else {
129 if (status_label) {
130 status_label->assign(
131 l10n_util::GetStringUTF16(IDS_SYNC_LOGIN_INFO_OUT_OF_DATE));
134 break;
135 case GoogleServiceAuthError::SERVICE_UNAVAILABLE:
136 if (status_label) {
137 status_label->assign(
138 l10n_util::GetStringUTF16(IDS_SYNC_SERVICE_UNAVAILABLE));
140 if (link_label)
141 link_label->clear();
142 break;
143 case GoogleServiceAuthError::CONNECTION_FAILED:
144 if (status_label) {
145 status_label->assign(
146 l10n_util::GetStringFUTF16(IDS_SYNC_SERVER_IS_UNREACHABLE,
147 product_name));
149 // Note that there is little the user can do if the server is not
150 // reachable. Since attempting to re-connect is done automatically by
151 // the Syncer, we do not show the (re)login link.
152 if (link_label)
153 link_label->clear();
154 break;
155 default:
156 if (status_label) {
157 status_label->assign(l10n_util::GetStringUTF16(
158 IDS_SYNC_ERROR_SIGNING_IN));
160 break;
164 void InitializePrefsForProfile(Profile* profile) {
165 if (profile->IsNewProfile() && switches::IsNewAvatarMenu()) {
166 // Suppresses the upgrade tutorial for a new profile.
167 profile->GetPrefs()->SetInteger(
168 prefs::kProfileAvatarTutorialShown, kUpgradeWelcomeTutorialShowMax + 1);
172 void ShowSigninErrorLearnMorePage(Profile* profile) {
173 static const char kSigninErrorLearnMoreUrl[] =
174 "https://support.google.com/chrome/answer/1181420?";
175 chrome::NavigateParams params(
176 profile, GURL(kSigninErrorLearnMoreUrl), ui::PAGE_TRANSITION_LINK);
177 params.disposition = NEW_FOREGROUND_TAB;
178 chrome::Navigate(&params);
181 std::string GetDisplayEmail(Profile* profile, const std::string& account_id) {
182 AccountTrackerService* account_tracker =
183 AccountTrackerServiceFactory::GetForProfile(profile);
184 std::string email = account_tracker->GetAccountInfo(account_id).email;
185 if (email.empty()) {
186 DCHECK_EQ(AccountTrackerService::MIGRATION_NOT_STARTED,
187 account_tracker->GetMigrationState());
188 return account_id;
190 return email;
193 } // namespace signin_ui_util