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. 4 5 #include "chrome/browser/sync/sync_error_notifier_ash.h" 6 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 "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" 29 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" 34 #endif 35 36 37 namespace { 38 39 const char kProfileSyncNotificationId[] = "chrome://settings/sync/"; 40 41 // A simple notification delegate for the sync setup button. 42 class SyncNotificationDelegate : public NotificationDelegate { 43 public: 44 SyncNotificationDelegate(const std::string& id, 45 Profile* profile); 46 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; 55 virtual content::WebContents* GetWebContents() const OVERRIDE; 56 57 protected: 58 virtual ~SyncNotificationDelegate(); 59 60 private: 61 void ShowSyncSetup(); 62 63 // Unique id of the notification. 64 const std::string id_; 65 66 Profile* profile_; 67 68 DISALLOW_COPY_AND_ASSIGN(SyncNotificationDelegate); 69 }; 70 71 SyncNotificationDelegate::SyncNotificationDelegate( 72 const std::string& id, 73 Profile* profile) 74 : id_(id), 75 profile_(profile) { 76 } 77 78 SyncNotificationDelegate::~SyncNotificationDelegate() { 79 } 80 81 void SyncNotificationDelegate::Display() { 82 } 83 84 void SyncNotificationDelegate::Error() { 85 } 86 87 void SyncNotificationDelegate::Close(bool by_user) { 88 } 89 90 bool SyncNotificationDelegate::HasClickedListener() { 91 return false; 92 } 93 94 void SyncNotificationDelegate::Click() { 95 ShowSyncSetup(); 96 } 97 98 void SyncNotificationDelegate::ButtonClick(int button_index) { 99 ShowSyncSetup(); 100 } 101 102 std::string SyncNotificationDelegate::id() const { 103 return id_; 104 } 105 106 content::WebContents* SyncNotificationDelegate::GetWebContents() const { 107 return NULL; 108 } 109 110 void SyncNotificationDelegate::ShowSyncSetup() { 111 LoginUIService* login_ui = LoginUIServiceFactory::GetForProfile(profile_); 112 if (login_ui->current_login_ui()) { 113 // TODO(michaelpg): The LoginUI might be on an inactive desktop. 114 // See crbug.com/354280. 115 login_ui->current_login_ui()->FocusUI(); 116 return; 117 } 118 119 chrome::ShowSettingsSubPageForProfile(profile_, chrome::kSyncSetupSubPage); 120 } 121 122 } // namespace 123 124 SyncErrorNotifier::SyncErrorNotifier(SyncErrorController* controller, 125 Profile* profile) 126 : error_controller_(controller), 127 profile_(profile) { 128 // Create a unique notification ID for this profile. 129 notification_id_ = kProfileSyncNotificationId + profile_->GetProfileName(); 130 131 error_controller_->AddObserver(this); 132 OnErrorChanged(); 133 } 134 135 SyncErrorNotifier::~SyncErrorNotifier() { 136 DCHECK(!error_controller_) 137 << "SyncErrorNotifier::Shutdown() was not called"; 138 } 139 140 void SyncErrorNotifier::Shutdown() { 141 error_controller_->RemoveObserver(this); 142 error_controller_ = NULL; 143 } 144 145 void SyncErrorNotifier::OnErrorChanged() { 146 NotificationUIManager* notification_ui_manager = 147 g_browser_process->notification_ui_manager(); 148 149 // notification_ui_manager() may return NULL when shutting down. 150 if (!notification_ui_manager) 151 return; 152 153 if (!error_controller_->HasError()) { 154 g_browser_process->notification_ui_manager()->CancelById(notification_id_); 155 return; 156 } 157 158 #if defined(OS_CHROMEOS) 159 if (user_manager::UserManager::IsInitialized()) { 160 chromeos::UserFlow* user_flow = 161 chromeos::ChromeUserManager::Get()->GetCurrentUserFlow(); 162 163 // Check whether Chrome OS user flow allows launching browser. 164 // Example: Supervised user creation flow which handles token invalidation 165 // itself and notifications should be suppressed. http://crbug.com/359045 166 if (!user_flow->ShouldLaunchBrowser()) 167 return; 168 } 169 #endif 170 171 // Keep the existing notification if there is one. 172 if (notification_ui_manager->FindById(notification_id_)) 173 return; 174 175 // Add an accept button to launch the sync setup settings subpage. 176 message_center::RichNotificationData data; 177 data.buttons.push_back(message_center::ButtonInfo( 178 l10n_util::GetStringUTF16(IDS_SYNC_NOTIFICATION_ACCEPT))); 179 180 // Set the delegate for the notification's sync setup button. 181 SyncNotificationDelegate* delegate = 182 new SyncNotificationDelegate(notification_id_, profile_); 183 184 message_center::NotifierId notifier_id( 185 message_center::NotifierId::SYSTEM_COMPONENT, 186 kProfileSyncNotificationId); 187 188 // Set |profile_id| for multi-user notification blocker. 189 notifier_id.profile_id = multi_user_util::GetUserIDFromProfile(profile_); 190 191 // Add a new notification. 192 Notification notification( 193 message_center::NOTIFICATION_TYPE_SIMPLE, 194 GURL(notification_id_), 195 l10n_util::GetStringUTF16(IDS_SYNC_ERROR_BUBBLE_VIEW_TITLE), 196 l10n_util::GetStringUTF16(IDS_SYNC_PASSPHRASE_ERROR_BUBBLE_VIEW_MESSAGE), 197 ui::ResourceBundle::GetSharedInstance().GetImageNamed( 198 IDR_NOTIFICATION_ALERT), 199 blink::WebTextDirectionDefault, 200 notifier_id, 201 base::string16(), // display_source 202 base::ASCIIToUTF16(notification_id_), 203 data, 204 delegate); 205 notification_ui_manager->Add(notification, profile_); 206 } 207