MD Downloads: UI review feedback
[chromium-blink-merge.git] / chrome / browser / signin / signin_error_notifier_ash.cc
blob60e5f49741a20f3cbe2b21c55eae096dc7c79b4b
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 "chrome/grit/chromium_strings.h"
28 #include "chrome/grit/generated_resources.h"
29 #include "grit/theme_resources.h"
30 #include "ui/base/l10n/l10n_util.h"
31 #include "ui/base/resource/resource_bundle.h"
32 #include "ui/message_center/notification.h"
33 #include "ui/message_center/notification_delegate.h"
35 #if defined(OS_CHROMEOS)
36 #include "chrome/browser/chromeos/login/user_flow.h"
37 #include "chrome/browser/chromeos/login/users/chrome_user_manager.h"
38 #include "components/user_manager/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 void Click() override;
53 void ButtonClick(int button_index) override;
54 std::string id() const override;
56 protected:
57 ~SigninNotificationDelegate() override;
59 private:
60 void FixSignIn();
62 // Unique id of the notification.
63 const std::string id_;
65 Profile* profile_;
67 DISALLOW_COPY_AND_ASSIGN(SigninNotificationDelegate);
70 SigninNotificationDelegate::SigninNotificationDelegate(
71 const std::string& id,
72 Profile* profile)
73 : id_(id),
74 profile_(profile) {
77 SigninNotificationDelegate::~SigninNotificationDelegate() {
80 void SigninNotificationDelegate::Click() {
81 FixSignIn();
84 void SigninNotificationDelegate::ButtonClick(int button_index) {
85 FixSignIn();
88 std::string SigninNotificationDelegate::id() const {
89 return id_;
92 void SigninNotificationDelegate::FixSignIn() {
93 #if defined(OS_CHROMEOS)
94 chrome::AttemptUserExit();
95 #else
96 LoginUIService* login_ui = LoginUIServiceFactory::GetForProfile(profile_);
97 if (login_ui->current_login_ui()) {
98 login_ui->current_login_ui()->FocusUI();
99 return;
102 // Find a browser instance or create one.
103 chrome::ScopedTabbedBrowserDisplayer browser_displayer(
104 profile_, chrome::HOST_DESKTOP_TYPE_ASH);
106 // Navigate to the sync setup subpage, which will launch a login page.
107 chrome::ShowSettingsSubPage(browser_displayer.browser(),
108 chrome::kSyncSetupSubPage);
109 #endif
112 } // namespace
114 SigninErrorNotifier::SigninErrorNotifier(SigninErrorController* controller,
115 Profile* profile)
116 : error_controller_(controller),
117 profile_(profile) {
118 // Create a unique notification ID for this profile.
119 notification_id_ =
120 kProfileSigninNotificationId + profile->GetProfileUserName();
122 error_controller_->AddObserver(this);
123 OnErrorChanged();
126 SigninErrorNotifier::~SigninErrorNotifier() {
127 DCHECK(!error_controller_)
128 << "SigninErrorNotifier::Shutdown() was not called";
131 void SigninErrorNotifier::Shutdown() {
132 error_controller_->RemoveObserver(this);
133 error_controller_ = NULL;
136 void SigninErrorNotifier::OnErrorChanged() {
137 NotificationUIManager* notification_ui_manager =
138 g_browser_process->notification_ui_manager();
140 // notification_ui_manager() may return NULL when shutting down.
141 if (!notification_ui_manager)
142 return;
144 if (!error_controller_->HasError()) {
145 g_browser_process->notification_ui_manager()->CancelById(
146 notification_id_, NotificationUIManager::GetProfileID(profile_));
147 return;
150 #if defined(OS_CHROMEOS)
151 if (user_manager::UserManager::IsInitialized()) {
152 chromeos::UserFlow* user_flow =
153 chromeos::ChromeUserManager::Get()->GetCurrentUserFlow();
155 // Check whether Chrome OS user flow allows launching browser.
156 // Example: Supervised user creation flow which handles token invalidation
157 // itself and notifications should be suppressed. http://crbug.com/359045
158 if (!user_flow->ShouldLaunchBrowser())
159 return;
161 #endif
163 // Add an accept button to sign the user out.
164 message_center::RichNotificationData data;
165 data.buttons.push_back(message_center::ButtonInfo(
166 l10n_util::GetStringUTF16(IDS_SYNC_RELOGIN_LINK_LABEL)));
168 // Set the delegate for the notification's sign-out button.
169 SigninNotificationDelegate* delegate =
170 new SigninNotificationDelegate(notification_id_, profile_);
172 message_center::NotifierId notifier_id(
173 message_center::NotifierId::SYSTEM_COMPONENT,
174 kProfileSigninNotificationId);
176 // Set |profile_id| for multi-user notification blocker.
177 notifier_id.profile_id = multi_user_util::GetUserIDFromProfile(profile_);
179 Notification notification(
180 message_center::NOTIFICATION_TYPE_SIMPLE,
181 l10n_util::GetStringUTF16(IDS_SIGNIN_ERROR_BUBBLE_VIEW_TITLE),
182 GetMessageBody(), ui::ResourceBundle::GetSharedInstance().GetImageNamed(
183 IDR_NOTIFICATION_ALERT),
184 notifier_id,
185 base::string16(), // display_source
186 GURL(notification_id_), notification_id_, data, delegate);
188 // Update or add the notification.
189 if (notification_ui_manager->FindById(
190 notification_id_, NotificationUIManager::GetProfileID(profile_)))
191 notification_ui_manager->Update(notification, profile_);
192 else
193 notification_ui_manager->Add(notification, profile_);
196 base::string16 SigninErrorNotifier::GetMessageBody() const {
197 switch (error_controller_->auth_error().state()) {
198 // TODO(rogerta): use account id in error messages.
200 // User credentials are invalid (bad acct, etc).
201 case GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS:
202 case GoogleServiceAuthError::SERVICE_ERROR:
203 case GoogleServiceAuthError::ACCOUNT_DELETED:
204 case GoogleServiceAuthError::ACCOUNT_DISABLED:
205 return l10n_util::GetStringUTF16(
206 IDS_SYNC_SIGN_IN_ERROR_BUBBLE_VIEW_MESSAGE);
207 break;
209 // Sync service is not available for this account's domain.
210 case GoogleServiceAuthError::SERVICE_UNAVAILABLE:
211 return l10n_util::GetStringUTF16(
212 IDS_SYNC_UNAVAILABLE_ERROR_BUBBLE_VIEW_MESSAGE);
213 break;
215 // Generic message for "other" errors.
216 default:
217 return l10n_util::GetStringUTF16(
218 IDS_SYNC_OTHER_SIGN_IN_ERROR_BUBBLE_VIEW_MESSAGE);