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 "chrome/app/chrome_command_ids.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/signin/signin_manager_factory.h"
11 #include "chrome/browser/signin/signin_promo.h"
12 #include "chrome/browser/ui/browser_commands.h"
13 #include "chrome/browser/ui/chrome_pages.h"
14 #include "chrome/browser/ui/global_error/global_error_service.h"
15 #include "chrome/browser/ui/global_error/global_error_service_factory.h"
16 #include "chrome/browser/ui/singleton_tabs.h"
17 #include "chrome/browser/ui/webui/signin/login_ui_service.h"
18 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
19 #include "chrome/common/url_constants.h"
20 #include "components/signin/core/browser/signin_manager.h"
21 #include "grit/chromium_strings.h"
22 #include "grit/generated_resources.h"
23 #include "net/base/url_util.h"
24 #include "ui/base/l10n/l10n_util.h"
26 SigninGlobalError::SigninGlobalError(
27 SigninErrorController* error_controller,
30 error_controller_(error_controller) {
31 error_controller_->AddObserver(this);
32 GlobalErrorServiceFactory::GetForProfile(profile_)->AddGlobalError(this);
35 SigninGlobalError::~SigninGlobalError() {
36 DCHECK(!error_controller_)
37 << "SigninGlobalError::Shutdown() was not called";
40 void SigninGlobalError::Shutdown() {
41 GlobalErrorServiceFactory::GetForProfile(profile_)->RemoveGlobalError(this);
42 error_controller_->RemoveObserver(this);
43 error_controller_ = NULL;
46 bool SigninGlobalError::HasMenuItem() {
47 return error_controller_->HasError();
50 int SigninGlobalError::MenuItemCommandID() {
51 return IDC_SHOW_SIGNIN_ERROR;
54 base::string16 SigninGlobalError::MenuItemLabel() {
55 // Notify the user if there's an auth error the user should know about.
56 if (error_controller_->HasError())
57 return l10n_util::GetStringUTF16(IDS_SYNC_SIGN_IN_ERROR_WRENCH_MENU_ITEM);
58 return base::string16();
61 void SigninGlobalError::ExecuteMenuItem(Browser* browser) {
62 #if defined(OS_CHROMEOS)
63 if (error_controller_->auth_error().state() !=
64 GoogleServiceAuthError::NONE) {
65 DVLOG(1) << "Signing out the user to fix a sync error.";
66 // TODO(beng): seems like this could just call chrome::AttemptUserExit().
67 chrome::ExecuteCommand(browser, IDC_EXIT);
72 // Global errors don't show up in the wrench menu on android.
73 #if !defined(OS_ANDROID)
74 LoginUIService* login_ui = LoginUIServiceFactory::GetForProfile(profile_);
75 if (login_ui->current_login_ui()) {
76 login_ui->current_login_ui()->FocusUI();
80 chrome::ShowSingletonTab(
82 signin::GetReauthURL(profile_, error_controller_->error_account_id()));
86 bool SigninGlobalError::HasBubbleView() {
87 return !GetBubbleViewMessages().empty();
90 base::string16 SigninGlobalError::GetBubbleViewTitle() {
91 return l10n_util::GetStringUTF16(IDS_SIGNIN_ERROR_BUBBLE_VIEW_TITLE);
94 std::vector<base::string16> SigninGlobalError::GetBubbleViewMessages() {
95 std::vector<base::string16> messages;
97 // If the user isn't signed in, no need to display an error bubble.
98 SigninManagerBase* signin_manager =
99 SigninManagerFactory::GetForProfileIfExists(profile_);
100 if (signin_manager) {
101 std::string username = signin_manager->GetAuthenticatedUsername();
102 if (username.empty())
106 if (!error_controller_->HasError())
109 switch (error_controller_->auth_error().state()) {
110 // TODO(rogerta): use account id in error messages.
112 // User credentials are invalid (bad acct, etc).
113 case GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS:
114 case GoogleServiceAuthError::SERVICE_ERROR:
115 case GoogleServiceAuthError::ACCOUNT_DELETED:
116 case GoogleServiceAuthError::ACCOUNT_DISABLED:
117 messages.push_back(l10n_util::GetStringUTF16(
118 IDS_SYNC_SIGN_IN_ERROR_BUBBLE_VIEW_MESSAGE));
121 // Sync service is not available for this account's domain.
122 case GoogleServiceAuthError::SERVICE_UNAVAILABLE:
123 messages.push_back(l10n_util::GetStringUTF16(
124 IDS_SYNC_UNAVAILABLE_ERROR_BUBBLE_VIEW_MESSAGE));
127 // Generic message for "other" errors.
129 messages.push_back(l10n_util::GetStringUTF16(
130 IDS_SYNC_OTHER_SIGN_IN_ERROR_BUBBLE_VIEW_MESSAGE));
135 base::string16 SigninGlobalError::GetBubbleViewAcceptButtonLabel() {
136 // If the auth service is unavailable, don't give the user the option to try
138 if (error_controller_->auth_error().state() ==
139 GoogleServiceAuthError::SERVICE_UNAVAILABLE) {
140 return l10n_util::GetStringUTF16(
141 IDS_SYNC_UNAVAILABLE_ERROR_BUBBLE_VIEW_ACCEPT);
143 return l10n_util::GetStringUTF16(IDS_SYNC_SIGN_IN_ERROR_BUBBLE_VIEW_ACCEPT);
147 base::string16 SigninGlobalError::GetBubbleViewCancelButtonLabel() {
148 return base::string16();
151 void SigninGlobalError::OnBubbleViewDidClose(Browser* browser) {
154 void SigninGlobalError::BubbleViewAcceptButtonPressed(Browser* browser) {
155 ExecuteMenuItem(browser);
158 void SigninGlobalError::BubbleViewCancelButtonPressed(Browser* browser) {
162 void SigninGlobalError::OnErrorChanged() {
163 GlobalErrorServiceFactory::GetForProfile(profile_)->NotifyErrorsChanged(this);