Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / signin / signin_global_error.cc
blob87d7e29ef30cd0ed0d26a0fb2e8e7f22d4c9ed71
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/ui/browser_commands.h"
16 #include "chrome/browser/ui/chrome_pages.h"
17 #include "chrome/browser/ui/global_error/global_error_service.h"
18 #include "chrome/browser/ui/global_error/global_error_service_factory.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/pref_names.h"
22 #include "chrome/common/url_constants.h"
23 #include "grit/chromium_strings.h"
24 #include "grit/generated_resources.h"
25 #include "ui/base/l10n/l10n_util.h"
27 SigninGlobalError::SigninGlobalError(Profile* profile)
28 : auth_error_(GoogleServiceAuthError::AuthErrorNone()), profile_(profile) {
31 SigninGlobalError::~SigninGlobalError() {
32 DCHECK(provider_set_.empty())
33 << "All AuthStatusProviders should be unregistered before"
34 << " SigninManager::Shutdown() is called";
37 void SigninGlobalError::AddProvider(const AuthStatusProvider* provider) {
38 DCHECK(provider_set_.find(provider) == provider_set_.end())
39 << "Adding same AuthStatusProvider multiple times";
40 provider_set_.insert(provider);
41 AuthStatusChanged();
44 void SigninGlobalError::RemoveProvider(const AuthStatusProvider* provider) {
45 std::set<const AuthStatusProvider*>::iterator iter =
46 provider_set_.find(provider);
47 DCHECK(iter != provider_set_.end())
48 << "Removing provider that was never added";
49 provider_set_.erase(iter);
50 AuthStatusChanged();
53 SigninGlobalError::AuthStatusProvider::AuthStatusProvider() {
56 SigninGlobalError::AuthStatusProvider::~AuthStatusProvider() {
59 void SigninGlobalError::AuthStatusChanged() {
60 // Walk all of the status providers and collect any error.
61 GoogleServiceAuthError current_error(GoogleServiceAuthError::AuthErrorNone());
62 std::string current_account_id;
63 for (std::set<const AuthStatusProvider*>::const_iterator it =
64 provider_set_.begin(); it != provider_set_.end(); ++it) {
65 current_account_id = (*it)->GetAccountId();
66 current_error = (*it)->GetAuthStatus();
68 // Break out if any provider reports an error (ignoring ordinary network
69 // errors, which are not surfaced to the user). This logic may eventually
70 // need to be extended to prioritize different auth errors, but for now
71 // all auth errors are treated the same.
72 if (current_error.state() != GoogleServiceAuthError::NONE &&
73 current_error.state() != GoogleServiceAuthError::CONNECTION_FAILED) {
74 break;
77 if (current_error.state() != auth_error_.state() ||
78 account_id_ != current_account_id) {
79 auth_error_ = current_error;
80 if (auth_error_.state() == GoogleServiceAuthError::NONE) {
81 account_id_.clear();
82 } else {
83 account_id_ = current_account_id;
86 GlobalErrorServiceFactory::GetForProfile(profile_)->NotifyErrorsChanged(
87 this);
91 bool SigninGlobalError::HasMenuItem() {
92 return !MenuItemLabel().empty();
95 int SigninGlobalError::MenuItemCommandID() {
96 return IDC_SHOW_SIGNIN_ERROR;
99 base::string16 SigninGlobalError::MenuItemLabel() {
100 if (account_id_.empty() ||
101 auth_error_.state() == GoogleServiceAuthError::NONE ||
102 auth_error_.state() == GoogleServiceAuthError::CONNECTION_FAILED) {
103 // If the user isn't signed in, or there's no auth error worth elevating to
104 // the user, don't display any menu item.
105 return base::string16();
106 } else {
107 // There's an auth error the user should know about - notify the user.
108 return l10n_util::GetStringUTF16(IDS_SYNC_SIGN_IN_ERROR_WRENCH_MENU_ITEM);
112 void SigninGlobalError::ExecuteMenuItem(Browser* browser) {
113 #if defined(OS_CHROMEOS)
114 if (auth_error_.state() != GoogleServiceAuthError::NONE) {
115 DLOG(INFO) << "Signing out the user to fix a sync error.";
116 // TODO(beng): seems like this could just call chrome::AttemptUserExit().
117 chrome::ExecuteCommand(browser, IDC_EXIT);
118 return;
120 #endif
122 // TODO(rogerta): what we do depends on which account is reporting an error.
123 // This will be needed once the account reconcilor is implemented. The
124 // LoginUIService will support multi-login as well.
126 // Global errors don't show up in the wrench menu on android.
127 #if !defined(OS_ANDROID)
128 LoginUIService* login_ui = LoginUIServiceFactory::GetForProfile(profile_);
129 if (login_ui->current_login_ui()) {
130 login_ui->current_login_ui()->FocusUI();
131 return;
133 // Need to navigate to the settings page and display the UI.
134 chrome::ShowSettingsSubPage(browser, chrome::kSyncSetupSubPage);
135 #endif
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())
155 return messages;
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:
163 return messages;
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::GetStringFUTF16(
173 IDS_SYNC_SIGN_IN_ERROR_BUBBLE_VIEW_MESSAGE,
174 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME)));
175 break;
177 // Sync service is not available for this account's domain.
178 case GoogleServiceAuthError::SERVICE_UNAVAILABLE:
179 messages.push_back(l10n_util::GetStringFUTF16(
180 IDS_SYNC_UNAVAILABLE_ERROR_BUBBLE_VIEW_MESSAGE,
181 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME)));
182 break;
184 // Generic message for "other" errors.
185 default:
186 messages.push_back(l10n_util::GetStringFUTF16(
187 IDS_SYNC_OTHER_SIGN_IN_ERROR_BUBBLE_VIEW_MESSAGE,
188 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME)));
190 return messages;
193 base::string16 SigninGlobalError::GetBubbleViewAcceptButtonLabel() {
194 // If the service is unavailable, don't give the user the option to try
195 // signing in again.
196 if (auth_error_.state() == GoogleServiceAuthError::SERVICE_UNAVAILABLE) {
197 return l10n_util::GetStringUTF16(
198 IDS_SYNC_UNAVAILABLE_ERROR_BUBBLE_VIEW_ACCEPT);
199 } else {
200 return l10n_util::GetStringUTF16(IDS_SYNC_SIGN_IN_ERROR_BUBBLE_VIEW_ACCEPT);
204 base::string16 SigninGlobalError::GetBubbleViewCancelButtonLabel() {
205 return base::string16();
208 void SigninGlobalError::OnBubbleViewDidClose(Browser* browser) {
211 void SigninGlobalError::BubbleViewAcceptButtonPressed(Browser* browser) {
212 ExecuteMenuItem(browser);
215 void SigninGlobalError::BubbleViewCancelButtonPressed(Browser* browser) {
216 NOTREACHED();
219 // static
220 SigninGlobalError* SigninGlobalError::GetForProfile(Profile* profile) {
221 return ProfileOAuth2TokenServiceFactory::GetForProfile(profile)->
222 signin_global_error();