Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / sync / sync_error_notifier_ash.cc
blob420513857da19aa1af4b7d1df2ee199952217ec8
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"
7 #include "ash/shell.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 "ui/base/l10n/l10n_util.h"
25 #include "ui/base/resource/resource_bundle.h"
26 #include "ui/message_center/notification.h"
27 #include "ui/message_center/notification_delegate.h"
29 #if defined(OS_CHROMEOS)
30 #include "chrome/browser/chromeos/login/user_flow.h"
31 #include "chrome/browser/chromeos/login/users/chrome_user_manager.h"
32 #include "components/user_manager/user_manager.h"
33 #endif
36 namespace {
38 const char kProfileSyncNotificationId[] = "chrome://settings/sync/";
40 // A simple notification delegate for the sync setup button.
41 class SyncNotificationDelegate : public NotificationDelegate {
42 public:
43 SyncNotificationDelegate(const std::string& id,
44 Profile* profile);
46 // NotificationDelegate:
47 void Click() override;
48 void ButtonClick(int button_index) override;
49 std::string id() const override;
51 protected:
52 ~SyncNotificationDelegate() override;
54 private:
55 void ShowSyncSetup();
57 // Unique id of the notification.
58 const std::string id_;
60 Profile* profile_;
62 DISALLOW_COPY_AND_ASSIGN(SyncNotificationDelegate);
65 SyncNotificationDelegate::SyncNotificationDelegate(
66 const std::string& id,
67 Profile* profile)
68 : id_(id),
69 profile_(profile) {
72 SyncNotificationDelegate::~SyncNotificationDelegate() {
74 void SyncNotificationDelegate::Click() {
75 ShowSyncSetup();
78 void SyncNotificationDelegate::ButtonClick(int button_index) {
79 ShowSyncSetup();
82 std::string SyncNotificationDelegate::id() const {
83 return id_;
86 void SyncNotificationDelegate::ShowSyncSetup() {
87 LoginUIService* login_ui = LoginUIServiceFactory::GetForProfile(profile_);
88 if (login_ui->current_login_ui()) {
89 // TODO(michaelpg): The LoginUI might be on an inactive desktop.
90 // See crbug.com/354280.
91 login_ui->current_login_ui()->FocusUI();
92 return;
95 chrome::ShowSettingsSubPageForProfile(profile_, chrome::kSyncSetupSubPage);
98 } // namespace
100 SyncErrorNotifier::SyncErrorNotifier(SyncErrorController* controller,
101 Profile* profile)
102 : error_controller_(controller),
103 profile_(profile) {
104 // Create a unique notification ID for this profile.
105 notification_id_ =
106 kProfileSyncNotificationId + profile_->GetProfileUserName();
108 error_controller_->AddObserver(this);
109 OnErrorChanged();
112 SyncErrorNotifier::~SyncErrorNotifier() {
113 DCHECK(!error_controller_)
114 << "SyncErrorNotifier::Shutdown() was not called";
117 void SyncErrorNotifier::Shutdown() {
118 error_controller_->RemoveObserver(this);
119 error_controller_ = NULL;
122 void SyncErrorNotifier::OnErrorChanged() {
123 NotificationUIManager* notification_ui_manager =
124 g_browser_process->notification_ui_manager();
126 // notification_ui_manager() may return NULL when shutting down.
127 if (!notification_ui_manager)
128 return;
130 if (!error_controller_->HasError()) {
131 g_browser_process->notification_ui_manager()->CancelById(
132 notification_id_, NotificationUIManager::GetProfileID(profile_));
133 return;
136 #if defined(OS_CHROMEOS)
137 if (user_manager::UserManager::IsInitialized()) {
138 chromeos::UserFlow* user_flow =
139 chromeos::ChromeUserManager::Get()->GetCurrentUserFlow();
141 // Check whether Chrome OS user flow allows launching browser.
142 // Example: Supervised user creation flow which handles token invalidation
143 // itself and notifications should be suppressed. http://crbug.com/359045
144 if (!user_flow->ShouldLaunchBrowser())
145 return;
147 #endif
149 // Keep the existing notification if there is one.
150 if (notification_ui_manager->FindById(
151 notification_id_, NotificationUIManager::GetProfileID(profile_)))
152 return;
154 // Add an accept button to launch the sync setup settings subpage.
155 message_center::RichNotificationData data;
156 data.buttons.push_back(message_center::ButtonInfo(
157 l10n_util::GetStringUTF16(IDS_SYNC_NOTIFICATION_ACCEPT)));
159 // Set the delegate for the notification's sync setup button.
160 SyncNotificationDelegate* delegate =
161 new SyncNotificationDelegate(notification_id_, profile_);
163 message_center::NotifierId notifier_id(
164 message_center::NotifierId::SYSTEM_COMPONENT,
165 kProfileSyncNotificationId);
167 // Set |profile_id| for multi-user notification blocker.
168 notifier_id.profile_id = multi_user_util::GetUserIDFromProfile(profile_);
170 // Add a new notification.
171 Notification notification(
172 message_center::NOTIFICATION_TYPE_SIMPLE,
173 l10n_util::GetStringUTF16(IDS_SYNC_ERROR_BUBBLE_VIEW_TITLE),
174 l10n_util::GetStringUTF16(IDS_SYNC_PASSPHRASE_ERROR_BUBBLE_VIEW_MESSAGE),
175 ui::ResourceBundle::GetSharedInstance().GetImageNamed(
176 IDR_NOTIFICATION_ALERT),
177 notifier_id,
178 base::string16(), // display_source
179 GURL(notification_id_), notification_id_, data, delegate);
180 notification_ui_manager->Add(notification, profile_);