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_global_error.h"
7 #include "base/logging.h"
8 #include "base/metrics/histogram_macros.h"
9 #include "chrome/app/chrome_command_ids.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/signin/signin_manager_factory.h"
12 #include "chrome/browser/ui/browser_commands.h"
13 #include "chrome/browser/ui/browser_window.h"
14 #include "chrome/browser/ui/chrome_pages.h"
15 #include "chrome/browser/ui/global_error/global_error_service.h"
16 #include "chrome/browser/ui/global_error/global_error_service_factory.h"
17 #include "chrome/browser/ui/singleton_tabs.h"
18 #include "chrome/browser/ui/webui/signin/login_ui_service.h"
19 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
20 #include "chrome/common/url_constants.h"
21 #include "chrome/grit/chromium_strings.h"
22 #include "chrome/grit/generated_resources.h"
23 #include "components/signin/core/browser/signin_header_helper.h"
24 #include "components/signin/core/browser/signin_manager.h"
25 #include "components/signin/core/browser/signin_metrics.h"
26 #include "components/signin/core/common/profile_management_switches.h"
27 #include "net/base/url_util.h"
28 #include "ui/base/l10n/l10n_util.h"
30 #if !defined(OS_ANDROID) && !defined(OS_IOS)
31 #include "chrome/browser/signin/signin_promo.h"
34 SigninGlobalError::SigninGlobalError(
35 SigninErrorController
* error_controller
,
38 error_controller_(error_controller
),
39 is_added_to_global_error_service_(false) {
40 error_controller_
->AddObserver(this);
41 is_added_to_global_error_service_
= !switches::IsNewAvatarMenu();
42 if (is_added_to_global_error_service_
)
43 GlobalErrorServiceFactory::GetForProfile(profile_
)->AddGlobalError(this);
46 SigninGlobalError::~SigninGlobalError() {
47 DCHECK(!error_controller_
)
48 << "SigninGlobalError::Shutdown() was not called";
51 bool SigninGlobalError::HasError() {
55 void SigninGlobalError::AttemptToFixError(Browser
* browser
) {
59 ExecuteMenuItem(browser
);
62 void SigninGlobalError::Shutdown() {
63 if (is_added_to_global_error_service_
) {
64 GlobalErrorServiceFactory::GetForProfile(profile_
)->RemoveGlobalError(this);
65 is_added_to_global_error_service_
= false;
68 error_controller_
->RemoveObserver(this);
69 error_controller_
= NULL
;
72 bool SigninGlobalError::HasMenuItem() {
73 return error_controller_
->HasError();
76 int SigninGlobalError::MenuItemCommandID() {
77 return IDC_SHOW_SIGNIN_ERROR
;
80 base::string16
SigninGlobalError::MenuItemLabel() {
81 // Notify the user if there's an auth error the user should know about.
82 if (error_controller_
->HasError())
83 return l10n_util::GetStringUTF16(IDS_SYNC_SIGN_IN_ERROR_WRENCH_MENU_ITEM
);
84 return base::string16();
87 void SigninGlobalError::ExecuteMenuItem(Browser
* browser
) {
88 #if defined(OS_CHROMEOS)
89 if (error_controller_
->auth_error().state() !=
90 GoogleServiceAuthError::NONE
) {
91 DVLOG(1) << "Signing out the user to fix a sync error.";
92 // TODO(beng): seems like this could just call chrome::AttemptUserExit().
93 chrome::ExecuteCommand(browser
, IDC_EXIT
);
98 // Global errors don't show up in the wrench menu on mobile.
99 #if !defined(OS_ANDROID) && !defined(OS_IOS)
100 LoginUIService
* login_ui
= LoginUIServiceFactory::GetForProfile(profile_
);
101 if (login_ui
->current_login_ui()) {
102 login_ui
->current_login_ui()->FocusUI();
106 UMA_HISTOGRAM_ENUMERATION("Signin.Reauth",
107 signin_metrics::HISTOGRAM_REAUTH_SHOWN
,
108 signin_metrics::HISTOGRAM_REAUTH_MAX
);
109 if (switches::IsNewAvatarMenu()) {
110 browser
->window()->ShowAvatarBubbleFromAvatarButton(
111 BrowserWindow::AVATAR_BUBBLE_MODE_REAUTH
,
112 signin::ManageAccountsParams());
114 chrome::ShowSingletonTab(
116 signin::GetReauthURL(profile_
, error_controller_
->error_account_id()));
121 bool SigninGlobalError::HasBubbleView() {
122 return !GetBubbleViewMessages().empty();
125 base::string16
SigninGlobalError::GetBubbleViewTitle() {
126 return l10n_util::GetStringUTF16(IDS_SIGNIN_ERROR_BUBBLE_VIEW_TITLE
);
129 std::vector
<base::string16
> SigninGlobalError::GetBubbleViewMessages() {
130 std::vector
<base::string16
> messages
;
132 // If the user isn't signed in, no need to display an error bubble.
133 SigninManagerBase
* signin_manager
=
134 SigninManagerFactory::GetForProfileIfExists(profile_
);
135 if (signin_manager
&& !signin_manager
->IsAuthenticated())
138 if (!error_controller_
->HasError())
141 switch (error_controller_
->auth_error().state()) {
142 // TODO(rogerta): use account id in error messages.
144 // User credentials are invalid (bad acct, etc).
145 case GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS
:
146 case GoogleServiceAuthError::SERVICE_ERROR
:
147 case GoogleServiceAuthError::ACCOUNT_DELETED
:
148 case GoogleServiceAuthError::ACCOUNT_DISABLED
:
149 messages
.push_back(l10n_util::GetStringUTF16(
150 IDS_SYNC_SIGN_IN_ERROR_BUBBLE_VIEW_MESSAGE
));
153 // Sync service is not available for this account's domain.
154 case GoogleServiceAuthError::SERVICE_UNAVAILABLE
:
155 messages
.push_back(l10n_util::GetStringUTF16(
156 IDS_SYNC_UNAVAILABLE_ERROR_BUBBLE_VIEW_MESSAGE
));
159 // Generic message for "other" errors.
161 messages
.push_back(l10n_util::GetStringUTF16(
162 IDS_SYNC_OTHER_SIGN_IN_ERROR_BUBBLE_VIEW_MESSAGE
));
167 base::string16
SigninGlobalError::GetBubbleViewAcceptButtonLabel() {
168 // If the auth service is unavailable, don't give the user the option to try
170 if (error_controller_
->auth_error().state() ==
171 GoogleServiceAuthError::SERVICE_UNAVAILABLE
) {
172 return l10n_util::GetStringUTF16(
173 IDS_SYNC_UNAVAILABLE_ERROR_BUBBLE_VIEW_ACCEPT
);
175 return l10n_util::GetStringUTF16(IDS_SYNC_SIGN_IN_ERROR_BUBBLE_VIEW_ACCEPT
);
179 base::string16
SigninGlobalError::GetBubbleViewCancelButtonLabel() {
180 return base::string16();
183 void SigninGlobalError::OnBubbleViewDidClose(Browser
* browser
) {
186 void SigninGlobalError::BubbleViewAcceptButtonPressed(Browser
* browser
) {
187 ExecuteMenuItem(browser
);
190 void SigninGlobalError::BubbleViewCancelButtonPressed(Browser
* browser
) {
194 void SigninGlobalError::OnErrorChanged() {
195 GlobalErrorServiceFactory::GetForProfile(profile_
)->NotifyErrorsChanged(this);