[safe-browsing] Database full hash matches like prefix match.
[chromium-blink-merge.git] / chrome / browser / sync / sync_error_notifier_ash.cc
blob3e4c79641035b4e7b135ca1a4eff9785e6972b75
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 "grit/chromium_strings.h"
22 #include "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/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 virtual void Display() OVERRIDE;
48 virtual void Error() OVERRIDE;
49 virtual void Close(bool by_user) OVERRIDE;
50 virtual bool HasClickedListener() OVERRIDE;
51 virtual void Click() OVERRIDE;
52 virtual void ButtonClick(int button_index) OVERRIDE;
53 virtual std::string id() const OVERRIDE;
54 virtual content::WebContents* GetWebContents() const OVERRIDE;
56 protected:
57 virtual ~SyncNotificationDelegate();
59 private:
60 // Unique id of the notification.
61 const std::string id_;
63 Profile* profile_;
65 DISALLOW_COPY_AND_ASSIGN(SyncNotificationDelegate);
68 SyncNotificationDelegate::SyncNotificationDelegate(
69 const std::string& id,
70 Profile* profile)
71 : id_(id),
72 profile_(profile) {
75 SyncNotificationDelegate::~SyncNotificationDelegate() {
78 void SyncNotificationDelegate::Display() {
81 void SyncNotificationDelegate::Error() {
84 void SyncNotificationDelegate::Close(bool by_user) {
87 bool SyncNotificationDelegate::HasClickedListener() {
88 return false;
91 void SyncNotificationDelegate::Click() {
94 std::string SyncNotificationDelegate::id() const {
95 return id_;
98 content::WebContents* SyncNotificationDelegate::GetWebContents() const {
99 return NULL;
102 void SyncNotificationDelegate::ButtonClick(int button_index) {
103 LoginUIService* login_ui = LoginUIServiceFactory::GetForProfile(profile_);
104 if (login_ui->current_login_ui()) {
105 // TODO(michaelpg): The LoginUI might be on an inactive desktop.
106 // See crbug.com/354280.
107 login_ui->current_login_ui()->FocusUI();
108 return;
111 chrome::ShowSettingsSubPageForProfile(profile_, chrome::kSyncSetupSubPage);
114 } // namespace
116 SyncErrorNotifier::SyncErrorNotifier(SyncErrorController* controller,
117 Profile* profile)
118 : error_controller_(controller),
119 profile_(profile) {
120 // Create a unique notification ID for this profile.
121 notification_id_ = kProfileSyncNotificationId + profile_->GetProfileName();
123 error_controller_->AddObserver(this);
124 OnErrorChanged();
127 SyncErrorNotifier::~SyncErrorNotifier() {
128 DCHECK(!error_controller_)
129 << "SyncErrorNotifier::Shutdown() was not called";
132 void SyncErrorNotifier::Shutdown() {
133 error_controller_->RemoveObserver(this);
134 error_controller_ = NULL;
137 void SyncErrorNotifier::OnErrorChanged() {
138 NotificationUIManager* notification_ui_manager =
139 g_browser_process->notification_ui_manager();
141 // notification_ui_manager() may return NULL when shutting down.
142 if (!notification_ui_manager)
143 return;
145 if (!error_controller_->HasError()) {
146 g_browser_process->notification_ui_manager()->CancelById(notification_id_);
147 return;
150 #if defined(OS_CHROMEOS)
151 if (chromeos::UserManager::IsInitialized()) {
152 chromeos::UserFlow* user_flow =
153 chromeos::UserManager::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 // Keep the existing notification if there is one.
164 if (notification_ui_manager->FindById(notification_id_))
165 return;
167 // Add an accept button to launch the sync setup settings subpage.
168 message_center::RichNotificationData data;
169 data.buttons.push_back(message_center::ButtonInfo(
170 l10n_util::GetStringUTF16(IDS_SYNC_NOTIFICATION_ACCEPT)));
172 // Set the delegate for the notification's sync setup button.
173 SyncNotificationDelegate* delegate =
174 new SyncNotificationDelegate(notification_id_, profile_);
176 message_center::NotifierId notifier_id(
177 message_center::NotifierId::SYSTEM_COMPONENT,
178 kProfileSyncNotificationId);
180 // Set |profile_id| for multi-user notification blocker.
181 notifier_id.profile_id = multi_user_util::GetUserIDFromProfile(profile_);
183 // Add a new notification.
184 Notification notification(
185 message_center::NOTIFICATION_TYPE_SIMPLE,
186 GURL(notification_id_),
187 l10n_util::GetStringUTF16(IDS_SYNC_ERROR_BUBBLE_VIEW_TITLE),
188 l10n_util::GetStringUTF16(IDS_SYNC_PASSPHRASE_ERROR_BUBBLE_VIEW_MESSAGE),
189 ui::ResourceBundle::GetSharedInstance().GetImageNamed(
190 IDR_NOTIFICATION_ALERT),
191 blink::WebTextDirectionDefault,
192 notifier_id,
193 base::string16(), // display_source
194 base::ASCIIToUTF16(notification_id_),
195 data,
196 delegate);
197 notification_ui_manager->Add(notification, profile_);