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/sync/sync_error_notifier_ash.h"
8 #include "ash/shell_delegate.h"
9 #include "ash/system/system_notifier.h"
10 #include "base/strings/string16.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/notifications/notification.h"
14 #include "chrome/browser/notifications/notification_ui_manager.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h"
17 #include "chrome/browser/ui/chrome_pages.h"
18 #include "chrome/browser/ui/webui/signin/login_ui_service.h"
19 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
20 #include "chrome/common/url_constants.h"
21 #include "chrome/grit/chromium_strings.h"
22 #include "chrome/grit/generated_resources.h"
23 #include "grit/theme_resources.h"
24 #include "third_party/WebKit/public/web/WebTextDirection.h"
25 #include "ui/base/l10n/l10n_util.h"
26 #include "ui/base/resource/resource_bundle.h"
27 #include "ui/message_center/notification.h"
28 #include "ui/message_center/notification_delegate.h"
30 #if defined(OS_CHROMEOS)
31 #include "chrome/browser/chromeos/login/user_flow.h"
32 #include "chrome/browser/chromeos/login/users/chrome_user_manager.h"
33 #include "components/user_manager/user_manager.h"
39 const char kProfileSyncNotificationId
[] = "chrome://settings/sync/";
41 // A simple notification delegate for the sync setup button.
42 class SyncNotificationDelegate
: public NotificationDelegate
{
44 SyncNotificationDelegate(const std::string
& id
,
47 // NotificationDelegate:
48 virtual void Display() override
;
49 virtual void Error() override
;
50 virtual void Close(bool by_user
) override
;
51 virtual bool HasClickedListener() override
;
52 virtual void Click() override
;
53 virtual void ButtonClick(int button_index
) override
;
54 virtual std::string
id() const override
;
57 virtual ~SyncNotificationDelegate();
62 // Unique id of the notification.
63 const std::string id_
;
67 DISALLOW_COPY_AND_ASSIGN(SyncNotificationDelegate
);
70 SyncNotificationDelegate::SyncNotificationDelegate(
71 const std::string
& id
,
77 SyncNotificationDelegate::~SyncNotificationDelegate() {
80 void SyncNotificationDelegate::Display() {
83 void SyncNotificationDelegate::Error() {
86 void SyncNotificationDelegate::Close(bool by_user
) {
89 bool SyncNotificationDelegate::HasClickedListener() {
93 void SyncNotificationDelegate::Click() {
97 void SyncNotificationDelegate::ButtonClick(int button_index
) {
101 std::string
SyncNotificationDelegate::id() const {
105 void SyncNotificationDelegate::ShowSyncSetup() {
106 LoginUIService
* login_ui
= LoginUIServiceFactory::GetForProfile(profile_
);
107 if (login_ui
->current_login_ui()) {
108 // TODO(michaelpg): The LoginUI might be on an inactive desktop.
109 // See crbug.com/354280.
110 login_ui
->current_login_ui()->FocusUI();
114 chrome::ShowSettingsSubPageForProfile(profile_
, chrome::kSyncSetupSubPage
);
119 SyncErrorNotifier::SyncErrorNotifier(SyncErrorController
* controller
,
121 : error_controller_(controller
),
123 // Create a unique notification ID for this profile.
124 notification_id_
= kProfileSyncNotificationId
+ profile_
->GetProfileName();
126 error_controller_
->AddObserver(this);
130 SyncErrorNotifier::~SyncErrorNotifier() {
131 DCHECK(!error_controller_
)
132 << "SyncErrorNotifier::Shutdown() was not called";
135 void SyncErrorNotifier::Shutdown() {
136 error_controller_
->RemoveObserver(this);
137 error_controller_
= NULL
;
140 void SyncErrorNotifier::OnErrorChanged() {
141 NotificationUIManager
* notification_ui_manager
=
142 g_browser_process
->notification_ui_manager();
144 // notification_ui_manager() may return NULL when shutting down.
145 if (!notification_ui_manager
)
148 if (!error_controller_
->HasError()) {
149 g_browser_process
->notification_ui_manager()->CancelById(
150 notification_id_
, NotificationUIManager::GetProfileID(profile_
));
154 #if defined(OS_CHROMEOS)
155 if (user_manager::UserManager::IsInitialized()) {
156 chromeos::UserFlow
* user_flow
=
157 chromeos::ChromeUserManager::Get()->GetCurrentUserFlow();
159 // Check whether Chrome OS user flow allows launching browser.
160 // Example: Supervised user creation flow which handles token invalidation
161 // itself and notifications should be suppressed. http://crbug.com/359045
162 if (!user_flow
->ShouldLaunchBrowser())
167 // Keep the existing notification if there is one.
168 if (notification_ui_manager
->FindById(
169 notification_id_
, NotificationUIManager::GetProfileID(profile_
)))
172 // Add an accept button to launch the sync setup settings subpage.
173 message_center::RichNotificationData data
;
174 data
.buttons
.push_back(message_center::ButtonInfo(
175 l10n_util::GetStringUTF16(IDS_SYNC_NOTIFICATION_ACCEPT
)));
177 // Set the delegate for the notification's sync setup button.
178 SyncNotificationDelegate
* delegate
=
179 new SyncNotificationDelegate(notification_id_
, profile_
);
181 message_center::NotifierId
notifier_id(
182 message_center::NotifierId::SYSTEM_COMPONENT
,
183 kProfileSyncNotificationId
);
185 // Set |profile_id| for multi-user notification blocker.
186 notifier_id
.profile_id
= multi_user_util::GetUserIDFromProfile(profile_
);
188 // Add a new notification.
189 Notification
notification(
190 message_center::NOTIFICATION_TYPE_SIMPLE
,
191 GURL(notification_id_
),
192 l10n_util::GetStringUTF16(IDS_SYNC_ERROR_BUBBLE_VIEW_TITLE
),
193 l10n_util::GetStringUTF16(IDS_SYNC_PASSPHRASE_ERROR_BUBBLE_VIEW_MESSAGE
),
194 ui::ResourceBundle::GetSharedInstance().GetImageNamed(
195 IDR_NOTIFICATION_ALERT
),
196 blink::WebTextDirectionDefault
,
198 base::string16(), // display_source
199 base::ASCIIToUTF16(notification_id_
),
202 notification_ui_manager
->Add(notification
, profile_
);