OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "chrome/browser/signin/signin_ui_util.h" |
| 6 |
| 7 #include "base/sys_string_conversions.h" |
| 8 #include "base/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/base/text/text_elider.h" |
| 20 #include "ui/gfx/font.h" |
| 21 |
| 22 namespace { |
| 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; |
| 26 } // namespace |
| 27 |
| 28 namespace signin_ui_util { |
| 29 |
| 30 GlobalError* GetSignedInServiceError(Profile* profile) { |
| 31 // Auth errors have the highest priority - after that, individual service |
| 32 // errors. |
| 33 SigninManager* signin_manager = SigninManagerFactory::GetForProfile(profile); |
| 34 SigninGlobalError* signin_error = signin_manager->signin_global_error(); |
| 35 if (signin_error && signin_error->HasBadge()) |
| 36 return signin_error; |
| 37 |
| 38 // No auth error - now try other services. Currently the list is just hard- |
| 39 // coded but in the future if we add more we can create some kind of |
| 40 // registration framework. |
| 41 if (profile->IsSyncAccessible()) { |
| 42 ProfileSyncService* service = |
| 43 ProfileSyncServiceFactory::GetForProfile(profile); |
| 44 SyncGlobalError* error = service->sync_global_error(); |
| 45 if (error && error->HasBadge()) |
| 46 return error; |
| 47 } |
| 48 return NULL; |
| 49 } |
| 50 |
| 51 string16 GetSigninMenuLabel(Profile* profile) { |
| 52 GlobalError* error = signin_ui_util::GetSignedInServiceError(profile); |
| 53 if (error) |
| 54 return error->MenuItemLabel(); |
| 55 |
| 56 // No errors, so just display the signed in user, if any. |
| 57 ProfileSyncService* service = profile->IsSyncAccessible() ? |
| 58 ProfileSyncServiceFactory::GetForProfile(profile) : NULL; |
| 59 |
| 60 // Even if the user is signed in, don't display the "signed in as..." |
| 61 // label if we're still setting up sync. |
| 62 if (!service || !service->FirstSetupInProgress()) { |
| 63 std::string username; |
| 64 SigninManager* signin_manager = |
| 65 SigninManagerFactory::GetForProfileIfExists(profile); |
| 66 if (signin_manager) |
| 67 username = signin_manager->GetAuthenticatedUsername(); |
| 68 if (!username.empty() && !signin_manager->AuthInProgress()) { |
| 69 string16 elided_username = ui::ElideEmail(UTF8ToUTF16(username), |
| 70 gfx::Font(), |
| 71 kUsernameMaxWidth); |
| 72 return l10n_util::GetStringFUTF16(IDS_SYNC_MENU_SYNCED_LABEL, |
| 73 elided_username); |
| 74 } |
| 75 } |
| 76 return l10n_util::GetStringFUTF16(IDS_SYNC_MENU_PRE_SYNCED_LABEL, |
| 77 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME)); |
| 78 } |
| 79 |
| 80 // Given an authentication state this helper function returns various labels |
| 81 // that can be used to display information about the state. |
| 82 void GetStatusLabelsForAuthError(const SigninManager& signin_manager, |
| 83 string16* status_label, |
| 84 string16* link_label) { |
| 85 string16 username = UTF8ToUTF16(signin_manager.GetAuthenticatedUsername()); |
| 86 string16 product_name = l10n_util::GetStringUTF16(IDS_PRODUCT_NAME); |
| 87 if (link_label) |
| 88 link_label->assign(l10n_util::GetStringUTF16(IDS_SYNC_RELOGIN_LINK_LABEL)); |
| 89 |
| 90 switch (signin_manager.signin_global_error()->GetLastAuthError().state()) { |
| 91 case GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS: |
| 92 case GoogleServiceAuthError::ACCOUNT_DELETED: |
| 93 case GoogleServiceAuthError::ACCOUNT_DISABLED: |
| 94 // If the user name is empty then the first login failed, otherwise the |
| 95 // credentials are out-of-date. |
| 96 if (username.empty()) { |
| 97 if (status_label) { |
| 98 status_label->assign( |
| 99 l10n_util::GetStringUTF16(IDS_SYNC_INVALID_USER_CREDENTIALS)); |
| 100 } |
| 101 } else { |
| 102 if (status_label) { |
| 103 status_label->assign( |
| 104 l10n_util::GetStringUTF16(IDS_SYNC_LOGIN_INFO_OUT_OF_DATE)); |
| 105 } |
| 106 } |
| 107 break; |
| 108 case GoogleServiceAuthError::SERVICE_UNAVAILABLE: |
| 109 if (status_label) { |
| 110 status_label->assign( |
| 111 l10n_util::GetStringUTF16(IDS_SYNC_SERVICE_UNAVAILABLE)); |
| 112 } |
| 113 if (link_label) |
| 114 link_label->clear(); |
| 115 break; |
| 116 case GoogleServiceAuthError::CONNECTION_FAILED: |
| 117 // Note that there is little the user can do if the server is not |
| 118 // reachable. Since attempting to re-connect is done automatically by |
| 119 // the Syncer, we do not show the (re)login link. |
| 120 if (status_label) { |
| 121 status_label->assign( |
| 122 l10n_util::GetStringFUTF16(IDS_SYNC_SERVER_IS_UNREACHABLE, |
| 123 product_name)); |
| 124 } |
| 125 break; |
| 126 default: |
| 127 if (status_label) { |
| 128 status_label->assign(l10n_util::GetStringUTF16( |
| 129 IDS_SYNC_ERROR_SIGNING_IN)); |
| 130 } |
| 131 break; |
| 132 } |
| 133 } |
| 134 |
| 135 |
| 136 } // namespace signin_ui_util |
OLD | NEW |