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_global_error.h" 6 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_header_helper.h" 11#include "chrome/browser/signin/signin_manager_factory.h" 12#include "chrome/browser/signin/signin_promo.h" 13#include "chrome/browser/ui/browser_commands.h" 14#include "chrome/browser/ui/browser_window.h" 15#include "chrome/browser/ui/chrome_pages.h" 16#include "chrome/browser/ui/global_error/global_error_service.h" 17#include "chrome/browser/ui/global_error/global_error_service_factory.h" 18#include "chrome/browser/ui/singleton_tabs.h" 19#include "chrome/browser/ui/webui/signin/login_ui_service.h" 20#include "chrome/browser/ui/webui/signin/login_ui_service_factory.h" 21#include "chrome/common/url_constants.h" 22#include "chrome/grit/chromium_strings.h" 23#include "chrome/grit/generated_resources.h" 24#include "components/signin/core/browser/signin_manager.h" 25#include "components/signin/core/common/profile_management_switches.h" 26#include "net/base/url_util.h" 27#include "ui/base/l10n/l10n_util.h" 28 29SigninGlobalError::SigninGlobalError( 30 SigninErrorController* error_controller, 31 Profile* profile) 32 : profile_(profile), 33 error_controller_(error_controller), 34 is_added_to_global_error_service_(false) { 35 error_controller_->AddObserver(this); 36 is_added_to_global_error_service_ = !switches::IsNewAvatarMenu(); 37 if (is_added_to_global_error_service_) 38 GlobalErrorServiceFactory::GetForProfile(profile_)->AddGlobalError(this); 39} 40 41SigninGlobalError::~SigninGlobalError() { 42 DCHECK(!error_controller_) 43 << "SigninGlobalError::Shutdown() was not called"; 44} 45 46bool SigninGlobalError::HasError() { 47 return HasMenuItem(); 48} 49 50void SigninGlobalError::AttemptToFixError(Browser* browser) { 51 if (!HasError()) 52 return; 53 54 ExecuteMenuItem(browser); 55} 56 57void SigninGlobalError::Shutdown() { 58 if (is_added_to_global_error_service_) { 59 GlobalErrorServiceFactory::GetForProfile(profile_)->RemoveGlobalError(this); 60 is_added_to_global_error_service_ = false; 61 } 62 63 error_controller_->RemoveObserver(this); 64 error_controller_ = NULL; 65} 66 67bool SigninGlobalError::HasMenuItem() { 68 return error_controller_->HasError(); 69} 70 71int SigninGlobalError::MenuItemCommandID() { 72 return IDC_SHOW_SIGNIN_ERROR; 73} 74 75base::string16 SigninGlobalError::MenuItemLabel() { 76 // Notify the user if there's an auth error the user should know about. 77 if (error_controller_->HasError()) 78 return l10n_util::GetStringUTF16(IDS_SYNC_SIGN_IN_ERROR_WRENCH_MENU_ITEM); 79 return base::string16(); 80} 81 82void SigninGlobalError::ExecuteMenuItem(Browser* browser) { 83#if defined(OS_CHROMEOS) 84 if (error_controller_->auth_error().state() != 85 GoogleServiceAuthError::NONE) { 86 DVLOG(1) << "Signing out the user to fix a sync error."; 87 // TODO(beng): seems like this could just call chrome::AttemptUserExit(). 88 chrome::ExecuteCommand(browser, IDC_EXIT); 89 return; 90 } 91#endif 92 93 // Global errors don't show up in the wrench menu on android. 94#if !defined(OS_ANDROID) 95 LoginUIService* login_ui = LoginUIServiceFactory::GetForProfile(profile_); 96 if (login_ui->current_login_ui()) { 97 login_ui->current_login_ui()->FocusUI(); 98 return; 99 } 100 101 if (switches::IsNewAvatarMenu()) { 102 browser->window()->ShowAvatarBubbleFromAvatarButton( 103 BrowserWindow::AVATAR_BUBBLE_MODE_REAUTH, 104 signin::ManageAccountsParams()); 105 } else { 106 chrome::ShowSingletonTab( 107 browser, 108 signin::GetReauthURL(profile_, error_controller_->error_account_id())); 109 } 110#endif 111} 112 113bool SigninGlobalError::HasBubbleView() { 114 return !GetBubbleViewMessages().empty(); 115} 116 117base::string16 SigninGlobalError::GetBubbleViewTitle() { 118 return l10n_util::GetStringUTF16(IDS_SIGNIN_ERROR_BUBBLE_VIEW_TITLE); 119} 120 121std::vector<base::string16> SigninGlobalError::GetBubbleViewMessages() { 122 std::vector<base::string16> messages; 123 124 // If the user isn't signed in, no need to display an error bubble. 125 SigninManagerBase* signin_manager = 126 SigninManagerFactory::GetForProfileIfExists(profile_); 127 if (signin_manager && !signin_manager->IsAuthenticated()) 128 return messages; 129 130 if (!error_controller_->HasError()) 131 return messages; 132 133 switch (error_controller_->auth_error().state()) { 134 // TODO(rogerta): use account id in error messages. 135 136 // User credentials are invalid (bad acct, etc). 137 case GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS: 138 case GoogleServiceAuthError::SERVICE_ERROR: 139 case GoogleServiceAuthError::ACCOUNT_DELETED: 140 case GoogleServiceAuthError::ACCOUNT_DISABLED: 141 messages.push_back(l10n_util::GetStringUTF16( 142 IDS_SYNC_SIGN_IN_ERROR_BUBBLE_VIEW_MESSAGE)); 143 break; 144 145 // Sync service is not available for this account's domain. 146 case GoogleServiceAuthError::SERVICE_UNAVAILABLE: 147 messages.push_back(l10n_util::GetStringUTF16( 148 IDS_SYNC_UNAVAILABLE_ERROR_BUBBLE_VIEW_MESSAGE)); 149 break; 150 151 // Generic message for "other" errors. 152 default: 153 messages.push_back(l10n_util::GetStringUTF16( 154 IDS_SYNC_OTHER_SIGN_IN_ERROR_BUBBLE_VIEW_MESSAGE)); 155 } 156 return messages; 157} 158 159base::string16 SigninGlobalError::GetBubbleViewAcceptButtonLabel() { 160 // If the auth service is unavailable, don't give the user the option to try 161 // signing in again. 162 if (error_controller_->auth_error().state() == 163 GoogleServiceAuthError::SERVICE_UNAVAILABLE) { 164 return l10n_util::GetStringUTF16( 165 IDS_SYNC_UNAVAILABLE_ERROR_BUBBLE_VIEW_ACCEPT); 166 } else { 167 return l10n_util::GetStringUTF16(IDS_SYNC_SIGN_IN_ERROR_BUBBLE_VIEW_ACCEPT); 168 } 169} 170 171base::string16 SigninGlobalError::GetBubbleViewCancelButtonLabel() { 172 return base::string16(); 173} 174 175void SigninGlobalError::OnBubbleViewDidClose(Browser* browser) { 176} 177 178void SigninGlobalError::BubbleViewAcceptButtonPressed(Browser* browser) { 179 ExecuteMenuItem(browser); 180} 181 182void SigninGlobalError::BubbleViewCancelButtonPressed(Browser* browser) { 183 NOTREACHED(); 184} 185 186void SigninGlobalError::OnErrorChanged() { 187 GlobalErrorServiceFactory::GetForProfile(profile_)->NotifyErrorsChanged(this); 188} 189