Disable accessible touch exploration by default.
[chromium-blink-merge.git] / chrome / browser / signin / signin_error_notifier_ash.cc
blobaae8e7ca5d1d69f4a651a30c812e19203ebc3188
1 // Copyright 2014 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_error_notifier_ash.h"
7 #include "ash/shell.h"
8 #include "ash/shell_delegate.h"
9 #include "ash/system/system_notifier.h"
10 #include "base/logging.h"
11 #include "base/strings/string16.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/lifetime/application_lifetime.h"
15 #include "chrome/browser/notifications/notification.h"
16 #include "chrome/browser/notifications/notification_delegate.h"
17 #include "chrome/browser/notifications/notification_ui_manager.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h"
20 #include "chrome/browser/ui/browser_tabstrip.h"
21 #include "chrome/browser/ui/browser_window.h"
22 #include "chrome/browser/ui/chrome_pages.h"
23 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h"
24 #include "chrome/browser/ui/webui/signin/login_ui_service.h"
25 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
26 #include "chrome/common/url_constants.h"
27 #include "grit/chromium_strings.h"
28 #include "grit/generated_resources.h"
29 #include "grit/theme_resources.h"
30 #include "third_party/WebKit/public/web/WebTextDirection.h"
31 #include "ui/base/l10n/l10n_util.h"
32 #include "ui/base/resource/resource_bundle.h"
33 #include "ui/message_center/notification.h"
34 #include "ui/message_center/notification_delegate.h"
36 #if defined(OS_CHROMEOS)
37 #include "chrome/browser/chromeos/login/user_flow.h"
38 #include "chrome/browser/chromeos/login/users/user_manager.h"
39 #endif
41 namespace {
43 const char kProfileSigninNotificationId[] = "chrome://settings/signin/";
45 // A notification delegate for the sign-out button.
46 class SigninNotificationDelegate : public NotificationDelegate {
47 public:
48 SigninNotificationDelegate(const std::string& id,
49 Profile* profile);
51 // NotificationDelegate:
52 virtual void Display() OVERRIDE;
53 virtual void Error() OVERRIDE;
54 virtual void Close(bool by_user) OVERRIDE;
55 virtual bool HasClickedListener() OVERRIDE;
56 virtual void Click() OVERRIDE;
57 virtual void ButtonClick(int button_index) OVERRIDE;
58 virtual std::string id() const OVERRIDE;
59 virtual content::WebContents* GetWebContents() const OVERRIDE;
61 protected:
62 virtual ~SigninNotificationDelegate();
64 private:
65 void FixSignIn();
67 // Unique id of the notification.
68 const std::string id_;
70 Profile* profile_;
72 DISALLOW_COPY_AND_ASSIGN(SigninNotificationDelegate);
75 SigninNotificationDelegate::SigninNotificationDelegate(
76 const std::string& id,
77 Profile* profile)
78 : id_(id),
79 profile_(profile) {
82 SigninNotificationDelegate::~SigninNotificationDelegate() {
85 void SigninNotificationDelegate::Display() {
88 void SigninNotificationDelegate::Error() {
91 void SigninNotificationDelegate::Close(bool by_user) {
94 bool SigninNotificationDelegate::HasClickedListener() {
95 return false;
98 void SigninNotificationDelegate::Click() {
99 FixSignIn();
102 void SigninNotificationDelegate::ButtonClick(int button_index) {
103 FixSignIn();
106 std::string SigninNotificationDelegate::id() const {
107 return id_;
110 content::WebContents* SigninNotificationDelegate::GetWebContents() const {
111 return NULL;
114 void SigninNotificationDelegate::FixSignIn() {
115 #if defined(OS_CHROMEOS)
116 chrome::AttemptUserExit();
117 #else
118 LoginUIService* login_ui = LoginUIServiceFactory::GetForProfile(profile_);
119 if (login_ui->current_login_ui()) {
120 login_ui->current_login_ui()->FocusUI();
121 return;
124 // Find a browser instance or create one.
125 chrome::ScopedTabbedBrowserDisplayer browser_displayer(
126 profile_, chrome::HOST_DESKTOP_TYPE_ASH);
128 // Navigate to the sync setup subpage, which will launch a login page.
129 chrome::ShowSettingsSubPage(browser_displayer.browser(),
130 chrome::kSyncSetupSubPage);
131 #endif
134 } // namespace
136 SigninErrorNotifier::SigninErrorNotifier(SigninErrorController* controller,
137 Profile* profile)
138 : error_controller_(controller),
139 profile_(profile) {
140 // Create a unique notification ID for this profile.
141 notification_id_ = kProfileSigninNotificationId + profile->GetProfileName();
143 error_controller_->AddObserver(this);
144 OnErrorChanged();
147 SigninErrorNotifier::~SigninErrorNotifier() {
148 DCHECK(!error_controller_)
149 << "SigninErrorNotifier::Shutdown() was not called";
152 void SigninErrorNotifier::Shutdown() {
153 error_controller_->RemoveObserver(this);
154 error_controller_ = NULL;
157 void SigninErrorNotifier::OnErrorChanged() {
158 NotificationUIManager* notification_ui_manager =
159 g_browser_process->notification_ui_manager();
161 // notification_ui_manager() may return NULL when shutting down.
162 if (!notification_ui_manager)
163 return;
165 if (!error_controller_->HasError()) {
166 g_browser_process->notification_ui_manager()->CancelById(notification_id_);
167 return;
170 #if defined(OS_CHROMEOS)
171 if (chromeos::UserManager::IsInitialized()) {
172 chromeos::UserFlow* user_flow =
173 chromeos::UserManager::Get()->GetCurrentUserFlow();
175 // Check whether Chrome OS user flow allows launching browser.
176 // Example: Supervised user creation flow which handles token invalidation
177 // itself and notifications should be suppressed. http://crbug.com/359045
178 if (!user_flow->ShouldLaunchBrowser())
179 return;
181 #endif
183 // Add an accept button to sign the user out.
184 message_center::RichNotificationData data;
185 data.buttons.push_back(message_center::ButtonInfo(
186 l10n_util::GetStringUTF16(IDS_SYNC_RELOGIN_LINK_LABEL)));
188 // Set the delegate for the notification's sign-out button.
189 SigninNotificationDelegate* delegate =
190 new SigninNotificationDelegate(notification_id_, profile_);
192 message_center::NotifierId notifier_id(
193 message_center::NotifierId::SYSTEM_COMPONENT,
194 kProfileSigninNotificationId);
196 // Set |profile_id| for multi-user notification blocker.
197 notifier_id.profile_id = multi_user_util::GetUserIDFromProfile(profile_);
199 Notification notification(
200 message_center::NOTIFICATION_TYPE_SIMPLE,
201 GURL(notification_id_),
202 l10n_util::GetStringUTF16(IDS_SIGNIN_ERROR_BUBBLE_VIEW_TITLE),
203 GetMessageBody(),
204 ui::ResourceBundle::GetSharedInstance().GetImageNamed(
205 IDR_NOTIFICATION_ALERT),
206 blink::WebTextDirectionDefault,
207 notifier_id,
208 base::string16(), // display_source
209 base::ASCIIToUTF16(notification_id_),
210 data,
211 delegate);
213 // Update or add the notification.
214 if (notification_ui_manager->FindById(notification_id_))
215 notification_ui_manager->Update(notification, profile_);
216 else
217 notification_ui_manager->Add(notification, profile_);
220 base::string16 SigninErrorNotifier::GetMessageBody() const {
221 switch (error_controller_->auth_error().state()) {
222 // TODO(rogerta): use account id in error messages.
224 // User credentials are invalid (bad acct, etc).
225 case GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS:
226 case GoogleServiceAuthError::SERVICE_ERROR:
227 case GoogleServiceAuthError::ACCOUNT_DELETED:
228 case GoogleServiceAuthError::ACCOUNT_DISABLED:
229 return l10n_util::GetStringUTF16(
230 IDS_SYNC_SIGN_IN_ERROR_BUBBLE_VIEW_MESSAGE);
231 break;
233 // Sync service is not available for this account's domain.
234 case GoogleServiceAuthError::SERVICE_UNAVAILABLE:
235 return l10n_util::GetStringUTF16(
236 IDS_SYNC_UNAVAILABLE_ERROR_BUBBLE_VIEW_MESSAGE);
237 break;
239 // Generic message for "other" errors.
240 default:
241 return l10n_util::GetStringUTF16(
242 IDS_SYNC_OTHER_SIGN_IN_ERROR_BUBBLE_VIEW_MESSAGE);