| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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/chromeos/printing/cups_print_job_notification.h" |
| 6 |
| 7 #include "base/strings/string_number_conversions.h" |
| 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chrome/browser/browser_process.h" |
| 10 #include "chrome/browser/chromeos/printing/cups_print_job.h" |
| 11 #include "chrome/browser/chromeos/printing/cups_print_job_manager.h" |
| 12 #include "chrome/browser/chromeos/printing/cups_print_job_manager_factory.h" |
| 13 #include "chrome/browser/notifications/notification_ui_manager.h" |
| 14 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/grit/generated_resources.h" |
| 16 #include "ui/base/l10n/l10n_util.h" |
| 17 #include "ui/base/resource/resource_bundle.h" |
| 18 #include "ui/chromeos/resources/grit/ui_chromeos_resources.h" |
| 19 #include "ui/message_center/message_center.h" |
| 20 #include "ui/message_center/notification_types.h" |
| 21 |
| 22 namespace chromeos { |
| 23 |
| 24 namespace { |
| 25 |
| 26 const char kCupsPrintJobNotificationId[] = |
| 27 "chrome://settings/printing/cups-print-job-notification"; |
| 28 |
| 29 class CupsPrintJobNotificationDelegate : public NotificationDelegate { |
| 30 public: |
| 31 explicit CupsPrintJobNotificationDelegate(CupsPrintJobNotification* item) |
| 32 : item_(item) {} |
| 33 |
| 34 // NotificationDelegate overrides: |
| 35 void Close(bool by_user) override { |
| 36 if (by_user) |
| 37 item_->CloseNotificationByUser(); |
| 38 } |
| 39 |
| 40 bool HasClickedListener() override { return true; } |
| 41 |
| 42 void ButtonClick(int button_index) override { |
| 43 item_->ClickOnNotificationButton(button_index); |
| 44 } |
| 45 |
| 46 std::string id() const override { return item_->GetNotificationId(); } |
| 47 |
| 48 private: |
| 49 ~CupsPrintJobNotificationDelegate() override {} |
| 50 |
| 51 CupsPrintJobNotification* item_; |
| 52 |
| 53 DISALLOW_COPY_AND_ASSIGN(CupsPrintJobNotificationDelegate); |
| 54 }; |
| 55 |
| 56 } // namespace |
| 57 |
| 58 CupsPrintJobNotification::CupsPrintJobNotification(CupsPrintJob* print_job, |
| 59 Profile* profile) |
| 60 : notification_id_(print_job->GetUniqueId()), |
| 61 print_job_(print_job), |
| 62 delegate_(new CupsPrintJobNotificationDelegate(this)), |
| 63 profile_(profile) { |
| 64 // Create a notification for the print job. The title, body, icon and buttons |
| 65 // of the notification will be updated in UpdateNotification(). |
| 66 notification_.reset(new Notification( |
| 67 message_center::NOTIFICATION_TYPE_SIMPLE, |
| 68 base::string16(), // title |
| 69 base::string16(), // body |
| 70 gfx::Image(), // icon |
| 71 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT, |
| 72 kCupsPrintJobNotificationId), |
| 73 base::string16(), // display_source |
| 74 GURL(kCupsPrintJobNotificationId), |
| 75 notification_id_, // tag |
| 76 message_center::RichNotificationData(), delegate_.get())); |
| 77 UpdateNotification(); |
| 78 } |
| 79 |
| 80 CupsPrintJobNotification::~CupsPrintJobNotification() {} |
| 81 |
| 82 void CupsPrintJobNotification::OnPrintJobStatusUpdated() { |
| 83 UpdateNotification(); |
| 84 } |
| 85 |
| 86 void CupsPrintJobNotification::CloseNotificationByUser() { |
| 87 closed_in_middle_ = true; |
| 88 g_browser_process->message_center()->RemoveNotification(GetNotificationId(), |
| 89 true /* by_user */); |
| 90 } |
| 91 |
| 92 void CupsPrintJobNotification::ClickOnNotificationButton(int button_index) { |
| 93 DCHECK(button_index >= 0 && |
| 94 static_cast<size_t>(button_index) < button_commands_->size()); |
| 95 |
| 96 CupsPrintJobNotification::ButtonCommand button_command = |
| 97 button_commands_->at(button_index); |
| 98 CupsPrintJobManager* print_job_manager = |
| 99 CupsPrintJobManagerFactory::GetForBrowserContext(profile_); |
| 100 const ProfileID profile_id = NotificationUIManager::GetProfileID(profile_); |
| 101 |
| 102 switch (button_command) { |
| 103 case ButtonCommand::CANCEL_PRINTING: |
| 104 print_job_manager->CancelPrintJob(print_job_); |
| 105 g_browser_process->notification_ui_manager()->CancelById( |
| 106 GetNotificationId(), profile_id); |
| 107 // |print_job_| is deleted by CupsPrintManager when the print job is |
| 108 // cancelled, thus set it to nullptr. |
| 109 print_job_ = nullptr; |
| 110 break; |
| 111 case ButtonCommand::PAUSE_PRINTING: |
| 112 print_job_manager->SuspendPrintJob(print_job_); |
| 113 break; |
| 114 case ButtonCommand::RESUME_PRINTING: |
| 115 print_job_manager->ResumePrintJob(print_job_); |
| 116 break; |
| 117 case ButtonCommand::GET_HELP: |
| 118 break; |
| 119 } |
| 120 } |
| 121 |
| 122 const std::string& CupsPrintJobNotification::GetNotificationId() { |
| 123 return notification_id_; |
| 124 } |
| 125 |
| 126 void CupsPrintJobNotification::UpdateNotification() { |
| 127 DCHECK(print_job_->state() != CupsPrintJob::State::STATE_CANCELLED); |
| 128 |
| 129 UpdateNotificationTitle(); |
| 130 UpdateNotificationIcon(); |
| 131 UpdateNotificationBodyMessage(); |
| 132 UpdateNotificationType(); |
| 133 UpdateNotificationButtons(); |
| 134 |
| 135 // |STATE_PAGE_DONE| is special since if the user closes the notificaiton in |
| 136 // the middle, which means he's not interested in the printing progress, we |
| 137 // should prevent showing the following printing progress. |
| 138 if (print_job_->state() == CupsPrintJob::State::STATE_PAGE_DONE) { |
| 139 if (closed_in_middle_) { |
| 140 // If the notification was closed during the printing, prevent showing the |
| 141 // following printing progress. |
| 142 g_browser_process->notification_ui_manager()->Update(*notification_, |
| 143 profile_); |
| 144 } else { |
| 145 // If it was not closed, update the notification message directly. |
| 146 g_browser_process->notification_ui_manager()->Add(*notification_, |
| 147 profile_); |
| 148 } |
| 149 } else { |
| 150 closed_in_middle_ = false; |
| 151 // In order to make sure it pop up, we should delete it before readding it. |
| 152 const ProfileID profile_id = NotificationUIManager::GetProfileID(profile_); |
| 153 g_browser_process->notification_ui_manager()->CancelById( |
| 154 GetNotificationId(), profile_id); |
| 155 g_browser_process->notification_ui_manager()->Add(*notification_, profile_); |
| 156 } |
| 157 } |
| 158 |
| 159 void CupsPrintJobNotification::UpdateNotificationTitle() { |
| 160 notification_->set_title(base::UTF8ToUTF16(print_job_->document_title())); |
| 161 } |
| 162 |
| 163 void CupsPrintJobNotification::UpdateNotificationIcon() { |
| 164 ResourceBundle& bundle = ResourceBundle::GetSharedInstance(); |
| 165 switch (print_job_->state()) { |
| 166 case CupsPrintJob::State::STATE_WAITING: |
| 167 notification_->set_icon( |
| 168 bundle.GetImageNamed(IDR_PRINT_NOTIFICATION_WAITING)); |
| 169 break; |
| 170 case CupsPrintJob::State::STATE_STARTED: |
| 171 case CupsPrintJob::State::STATE_PAGE_DONE: |
| 172 case CupsPrintJob::State::STATE_SUSPENDED: |
| 173 case CupsPrintJob::State::STATE_RESUMED: |
| 174 notification_->set_icon( |
| 175 bundle.GetImageNamed(IDR_PRINT_NOTIFICATION_PRINTING)); |
| 176 break; |
| 177 case CupsPrintJob::State::STATE_DOCUMENT_DONE: |
| 178 notification_->set_icon( |
| 179 bundle.GetImageNamed(IDR_PRINT_NOTIFICATION_DONE)); |
| 180 break; |
| 181 case CupsPrintJob::State::STATE_ERROR: |
| 182 notification_->set_icon( |
| 183 bundle.GetImageNamed(IDR_PRINT_NOTIFICATION_ERROR)); |
| 184 break; |
| 185 default: |
| 186 break; |
| 187 } |
| 188 } |
| 189 |
| 190 void CupsPrintJobNotification::UpdateNotificationBodyMessage() { |
| 191 base::string16 message; |
| 192 switch (print_job_->state()) { |
| 193 case CupsPrintJob::State::STATE_NONE: |
| 194 break; |
| 195 case CupsPrintJob::State::STATE_WAITING: |
| 196 message = l10n_util::GetStringFUTF16( |
| 197 IDS_PRINT_JOB_WAITING_NOTIFICATION_MESSAGE, |
| 198 base::IntToString16(print_job_->total_page_number()), |
| 199 base::UTF8ToUTF16(print_job_->printer().display_name())); |
| 200 break; |
| 201 case CupsPrintJob::State::STATE_STARTED: |
| 202 case CupsPrintJob::State::STATE_PAGE_DONE: |
| 203 case CupsPrintJob::State::STATE_SUSPENDED: |
| 204 case CupsPrintJob::State::STATE_RESUMED: |
| 205 message = l10n_util::GetStringFUTF16( |
| 206 IDS_PRINT_JOB_PRINTING_NOTIFICATION_MESSAGE, |
| 207 base::IntToString16(print_job_->total_page_number()), |
| 208 base::UTF8ToUTF16(print_job_->printer().display_name())); |
| 209 |
| 210 break; |
| 211 case CupsPrintJob::State::STATE_DOCUMENT_DONE: |
| 212 message = l10n_util::GetStringFUTF16( |
| 213 IDS_PRINT_JOB_DONE_NOTIFICATION_MESSAGE, |
| 214 base::IntToString16(print_job_->total_page_number()), |
| 215 base::UTF8ToUTF16(print_job_->printer().display_name())); |
| 216 break; |
| 217 case CupsPrintJob::State::STATE_ERROR: |
| 218 message = l10n_util::GetStringFUTF16( |
| 219 IDS_PRINT_JOB_ERROR_NOTIFICATION_MESSAGE, |
| 220 base::IntToString16(print_job_->total_page_number()), |
| 221 base::UTF8ToUTF16(print_job_->printer().display_name())); |
| 222 break; |
| 223 default: |
| 224 break; |
| 225 } |
| 226 notification_->set_message(message); |
| 227 } |
| 228 |
| 229 void CupsPrintJobNotification::UpdateNotificationType() { |
| 230 switch (print_job_->state()) { |
| 231 case CupsPrintJob::State::STATE_STARTED: |
| 232 case CupsPrintJob::State::STATE_PAGE_DONE: |
| 233 case CupsPrintJob::State::STATE_SUSPENDED: |
| 234 case CupsPrintJob::State::STATE_RESUMED: |
| 235 notification_->set_type(message_center::NOTIFICATION_TYPE_PROGRESS); |
| 236 notification_->set_progress(print_job_->printed_page_number() * 100 / |
| 237 print_job_->total_page_number()); |
| 238 break; |
| 239 case CupsPrintJob::State::STATE_NONE: |
| 240 case CupsPrintJob::State::STATE_WAITING: |
| 241 case CupsPrintJob::State::STATE_DOCUMENT_DONE: |
| 242 case CupsPrintJob::State::STATE_ERROR: |
| 243 default: |
| 244 notification_->set_type(message_center::NOTIFICATION_TYPE_SIMPLE); |
| 245 break; |
| 246 } |
| 247 } |
| 248 |
| 249 void CupsPrintJobNotification::UpdateNotificationButtons() { |
| 250 std::vector<message_center::ButtonInfo> buttons; |
| 251 button_commands_ = GetButtonCommands(); |
| 252 for (auto& it : *button_commands_) { |
| 253 message_center::ButtonInfo button_info = |
| 254 message_center::ButtonInfo(GetButtonLabel(it)); |
| 255 button_info.icon = GetButtonIcon(it); |
| 256 buttons.push_back(button_info); |
| 257 } |
| 258 notification_->set_buttons(buttons); |
| 259 } |
| 260 |
| 261 std::unique_ptr<std::vector<CupsPrintJobNotification::ButtonCommand>> |
| 262 CupsPrintJobNotification::GetButtonCommands() const { |
| 263 std::unique_ptr<std::vector<CupsPrintJobNotification::ButtonCommand>> |
| 264 commands(new std::vector<CupsPrintJobNotification::ButtonCommand>()); |
| 265 switch (print_job_->state()) { |
| 266 case CupsPrintJob::State::STATE_WAITING: |
| 267 commands->push_back(ButtonCommand::CANCEL_PRINTING); |
| 268 break; |
| 269 case CupsPrintJob::State::STATE_STARTED: |
| 270 case CupsPrintJob::State::STATE_PAGE_DONE: |
| 271 case CupsPrintJob::State::STATE_RESUMED: |
| 272 commands->push_back(ButtonCommand::PAUSE_PRINTING); |
| 273 commands->push_back(ButtonCommand::CANCEL_PRINTING); |
| 274 break; |
| 275 case CupsPrintJob::State::STATE_SUSPENDED: |
| 276 commands->push_back(ButtonCommand::RESUME_PRINTING); |
| 277 commands->push_back(ButtonCommand::CANCEL_PRINTING); |
| 278 break; |
| 279 case CupsPrintJob::State::STATE_ERROR: |
| 280 commands->push_back(ButtonCommand::GET_HELP); |
| 281 break; |
| 282 default: |
| 283 break; |
| 284 } |
| 285 return commands; |
| 286 } |
| 287 |
| 288 base::string16 CupsPrintJobNotification::GetButtonLabel( |
| 289 ButtonCommand button) const { |
| 290 switch (button) { |
| 291 case ButtonCommand::CANCEL_PRINTING: |
| 292 return l10n_util::GetStringUTF16( |
| 293 IDS_PRINT_JOB_NOTIFICATION_CANCEL_BUTTON); |
| 294 case ButtonCommand::PAUSE_PRINTING: |
| 295 return l10n_util::GetStringUTF16(IDS_PRINT_JOB_NOTIFICATION_PAUSE_BUTTON); |
| 296 case ButtonCommand::RESUME_PRINTING: |
| 297 return l10n_util::GetStringUTF16( |
| 298 IDS_PRINT_JOB_NOTIFICATION_RESUME_BUTTON); |
| 299 case ButtonCommand::GET_HELP: |
| 300 return l10n_util::GetStringUTF16( |
| 301 IDS_PRINT_JOB_NOTIFICATION_GET_HELP_BUTTON); |
| 302 } |
| 303 return base::string16(); |
| 304 } |
| 305 |
| 306 gfx::Image CupsPrintJobNotification::GetButtonIcon(ButtonCommand button) const { |
| 307 ResourceBundle& bundle = ResourceBundle::GetSharedInstance(); |
| 308 gfx::Image icon; |
| 309 switch (button) { |
| 310 case ButtonCommand::CANCEL_PRINTING: |
| 311 icon = bundle.GetImageNamed(IDR_PRINT_NOTIFICATION_CANCEL); |
| 312 break; |
| 313 case ButtonCommand::PAUSE_PRINTING: |
| 314 icon = bundle.GetImageNamed(IDR_PRINT_NOTIFICATION_PAUSE); |
| 315 break; |
| 316 case ButtonCommand::RESUME_PRINTING: |
| 317 icon = bundle.GetImageNamed(IDR_PRINT_NOTIFICATION_PLAY); |
| 318 break; |
| 319 case ButtonCommand::GET_HELP: |
| 320 icon = bundle.GetImageNamed(IDR_PRINT_NOTIFICATION_HELP); |
| 321 break; |
| 322 } |
| 323 return icon; |
| 324 } |
| 325 |
| 326 } // namespace chromeos |
| OLD | NEW |