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/prefs/pref_service.h"
9 #include "chrome/app/chrome_command_ids.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/signin/profile_oauth2_token_service.h"
12 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
13 #include "chrome/browser/signin/signin_manager.h"
14 #include "chrome/browser/signin/signin_manager_factory.h"
15 #include "chrome/browser/signin/signin_promo.h"
16 #include "chrome/browser/ui/browser_commands.h"
17 #include "chrome/browser/ui/chrome_pages.h"
18 #include "chrome/browser/ui/global_error/global_error_service.h"
19 #include "chrome/browser/ui/global_error/global_error_service_factory.h"
20 #include "chrome/browser/ui/singleton_tabs.h"
21 #include "chrome/browser/ui/webui/signin/login_ui_service.h"
22 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
23 #include "chrome/common/pref_names.h"
24 #include "chrome/common/url_constants.h"
25 #include "grit/chromium_strings.h"
26 #include "grit/generated_resources.h"
27 #include "net/base/url_util.h"
28 #include "ui/base/l10n/l10n_util.h"
30 SigninGlobalError::SigninGlobalError(Profile
* profile
)
31 : auth_error_(GoogleServiceAuthError::AuthErrorNone()), profile_(profile
) {
34 SigninGlobalError::~SigninGlobalError() {
35 DCHECK(provider_set_
.empty())
36 << "All AuthStatusProviders should be unregistered before"
37 << " SigninManager::Shutdown() is called";
40 void SigninGlobalError::AddProvider(const AuthStatusProvider
* provider
) {
41 DCHECK(provider_set_
.find(provider
) == provider_set_
.end())
42 << "Adding same AuthStatusProvider multiple times";
43 provider_set_
.insert(provider
);
47 void SigninGlobalError::RemoveProvider(const AuthStatusProvider
* provider
) {
48 std::set
<const AuthStatusProvider
*>::iterator iter
=
49 provider_set_
.find(provider
);
50 DCHECK(iter
!= provider_set_
.end())
51 << "Removing provider that was never added";
52 provider_set_
.erase(iter
);
56 SigninGlobalError::AuthStatusProvider::AuthStatusProvider() {
59 SigninGlobalError::AuthStatusProvider::~AuthStatusProvider() {
62 void SigninGlobalError::AuthStatusChanged() {
63 // Walk all of the status providers and collect any error.
64 GoogleServiceAuthError
current_error(GoogleServiceAuthError::AuthErrorNone());
65 std::string current_account_id
;
66 for (std::set
<const AuthStatusProvider
*>::const_iterator it
=
67 provider_set_
.begin(); it
!= provider_set_
.end(); ++it
) {
68 current_account_id
= (*it
)->GetAccountId();
69 current_error
= (*it
)->GetAuthStatus();
71 // Break out if any provider reports an error (ignoring ordinary network
72 // errors, which are not surfaced to the user). This logic may eventually
73 // need to be extended to prioritize different auth errors, but for now
74 // all auth errors are treated the same.
75 if (current_error
.state() != GoogleServiceAuthError::NONE
&&
76 current_error
.state() != GoogleServiceAuthError::CONNECTION_FAILED
) {
80 if (current_error
.state() != auth_error_
.state() ||
81 account_id_
!= current_account_id
) {
82 auth_error_
= current_error
;
83 if (auth_error_
.state() == GoogleServiceAuthError::NONE
) {
86 account_id_
= current_account_id
;
89 GlobalErrorServiceFactory::GetForProfile(profile_
)->NotifyErrorsChanged(
94 bool SigninGlobalError::HasMenuItem() {
95 return !MenuItemLabel().empty();
98 int SigninGlobalError::MenuItemCommandID() {
99 return IDC_SHOW_SIGNIN_ERROR
;
102 base::string16
SigninGlobalError::MenuItemLabel() {
103 if (account_id_
.empty() ||
104 auth_error_
.state() == GoogleServiceAuthError::NONE
||
105 auth_error_
.state() == GoogleServiceAuthError::CONNECTION_FAILED
) {
106 // If the user isn't signed in, or there's no auth error worth elevating to
107 // the user, don't display any menu item.
108 return base::string16();
110 // There's an auth error the user should know about - notify the user.
111 return l10n_util::GetStringUTF16(IDS_SYNC_SIGN_IN_ERROR_WRENCH_MENU_ITEM
);
115 void SigninGlobalError::ExecuteMenuItem(Browser
* browser
) {
116 #if defined(OS_CHROMEOS)
117 if (auth_error_
.state() != GoogleServiceAuthError::NONE
) {
118 DVLOG(1) << "Signing out the user to fix a sync error.";
119 // TODO(beng): seems like this could just call chrome::AttemptUserExit().
120 chrome::ExecuteCommand(browser
, IDC_EXIT
);
125 // Global errors don't show up in the wrench menu on android.
126 #if !defined(OS_ANDROID)
127 LoginUIService
* login_ui
= LoginUIServiceFactory::GetForProfile(profile_
);
128 if (login_ui
->current_login_ui()) {
129 login_ui
->current_login_ui()->FocusUI();
133 chrome::ShowSingletonTab(browser
, signin::GetReauthURL(profile_
,
138 bool SigninGlobalError::HasBubbleView() {
139 return !GetBubbleViewMessages().empty();
142 base::string16
SigninGlobalError::GetBubbleViewTitle() {
143 return l10n_util::GetStringUTF16(IDS_SIGNIN_ERROR_BUBBLE_VIEW_TITLE
);
146 std::vector
<base::string16
> SigninGlobalError::GetBubbleViewMessages() {
147 std::vector
<base::string16
> messages
;
149 // If the user isn't signed in, no need to display an error bubble.
150 SigninManagerBase
* signin_manager
=
151 SigninManagerFactory::GetForProfileIfExists(profile_
);
152 if (signin_manager
) {
153 std::string username
= signin_manager
->GetAuthenticatedUsername();
154 if (username
.empty())
158 switch (auth_error_
.state()) {
159 // In the case of no error, or a simple network error, don't bother
160 // displaying a popup bubble.
161 case GoogleServiceAuthError::CONNECTION_FAILED
:
162 case GoogleServiceAuthError::NONE
:
165 // TODO(rogerta): use account id in error messages.
167 // User credentials are invalid (bad acct, etc).
168 case GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS
:
169 case GoogleServiceAuthError::SERVICE_ERROR
:
170 case GoogleServiceAuthError::ACCOUNT_DELETED
:
171 case GoogleServiceAuthError::ACCOUNT_DISABLED
:
172 messages
.push_back(l10n_util::GetStringUTF16(
173 IDS_SYNC_SIGN_IN_ERROR_BUBBLE_VIEW_MESSAGE
));
176 // Sync service is not available for this account's domain.
177 case GoogleServiceAuthError::SERVICE_UNAVAILABLE
:
178 messages
.push_back(l10n_util::GetStringUTF16(
179 IDS_SYNC_UNAVAILABLE_ERROR_BUBBLE_VIEW_MESSAGE
));
182 // Generic message for "other" errors.
184 messages
.push_back(l10n_util::GetStringUTF16(
185 IDS_SYNC_OTHER_SIGN_IN_ERROR_BUBBLE_VIEW_MESSAGE
));
190 base::string16
SigninGlobalError::GetBubbleViewAcceptButtonLabel() {
191 // If the service is unavailable, don't give the user the option to try
193 if (auth_error_
.state() == GoogleServiceAuthError::SERVICE_UNAVAILABLE
) {
194 return l10n_util::GetStringUTF16(
195 IDS_SYNC_UNAVAILABLE_ERROR_BUBBLE_VIEW_ACCEPT
);
197 return l10n_util::GetStringUTF16(IDS_SYNC_SIGN_IN_ERROR_BUBBLE_VIEW_ACCEPT
);
201 base::string16
SigninGlobalError::GetBubbleViewCancelButtonLabel() {
202 return base::string16();
205 void SigninGlobalError::OnBubbleViewDidClose(Browser
* browser
) {
208 void SigninGlobalError::BubbleViewAcceptButtonPressed(Browser
* browser
) {
209 ExecuteMenuItem(browser
);
212 void SigninGlobalError::BubbleViewCancelButtonPressed(Browser
* browser
) {
217 SigninGlobalError
* SigninGlobalError::GetForProfile(Profile
* profile
) {
218 return ProfileOAuth2TokenServiceFactory::GetForProfile(profile
)->
219 signin_global_error();