Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6319)

Unified Diff: chrome/browser/chromeos/printing/cups_print_job_notification.cc

Issue 2435303003: [CUPS] Implement the Print Job notification. (Closed)
Patch Set: Bug fix. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/chromeos/printing/cups_print_job_notification.cc
diff --git a/chrome/browser/chromeos/printing/cups_print_job_notification.cc b/chrome/browser/chromeos/printing/cups_print_job_notification.cc
new file mode 100644
index 0000000000000000000000000000000000000000..853808e40c46aa5a3e06516c3c1033ff64fdce5c
--- /dev/null
+++ b/chrome/browser/chromeos/printing/cups_print_job_notification.cc
@@ -0,0 +1,320 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/chromeos/printing/cups_print_job_notification.h"
+
+#include "base/strings/string_number_conversions.h"
+#include "base/strings/utf_string_conversions.h"
+#include "chrome/browser/browser_process.h"
+#include "chrome/browser/chromeos/printing/cups_print_job.h"
+#include "chrome/browser/chromeos/printing/cups_print_job_manager.h"
+#include "chrome/browser/chromeos/printing/cups_print_job_manager_factory.h"
+#include "chrome/browser/notifications/notification_ui_manager.h"
+#include "chrome/browser/profiles/profile.h"
+#include "chrome/grit/generated_resources.h"
+#include "ui/base/l10n/l10n_util.h"
+#include "ui/base/resource/resource_bundle.h"
+#include "ui/chromeos/resources/grit/ui_chromeos_resources.h"
+#include "ui/message_center/message_center.h"
+#include "ui/message_center/notification_types.h"
+
+namespace chromeos {
+
+namespace {
+
+const char kCUPSPrintJobNotificationId[] =
+ "chrome://settings/printing/cups-print-job-notification";
+
+class CUPSPrintJobNotificationDelegate : public NotificationDelegate {
+ public:
+ explicit CUPSPrintJobNotificationDelegate(CUPSPrintJobNotification* item)
+ : item_(item) {}
+
+ // NotificationDelegate overrides:
+ void Close(bool by_user) override {
+ if (by_user)
+ item_->CloseNotificationByUser();
+ }
+
+ bool HasClickedListener() override { return true; }
+
+ void ButtonClick(int button_index) override {
+ item_->ClickOnNotificationButton(button_index);
+ }
+
+ std::string id() const override { return item_->GetNotificationId(); }
+
+ private:
+ ~CUPSPrintJobNotificationDelegate() override {}
+
+ CUPSPrintJobNotification* item_;
+
+ DISALLOW_COPY_AND_ASSIGN(CUPSPrintJobNotificationDelegate);
+};
+
+} // namespace
+
+CUPSPrintJobNotification::CUPSPrintJobNotification(CUPSPrintJob* print_job,
+ Profile* profile)
+ : notification_id_(print_job->GetId()),
+ print_job_(print_job),
+ delegate_(new CUPSPrintJobNotificationDelegate(this)),
+ profile_(profile) {
+ // Create a notification for the print job. The title, body, icon and buttons
+ // of the notification will be updated in UpdateNotification().
+ notification_.reset(new Notification(
+ message_center::NOTIFICATION_TYPE_SIMPLE,
+ base::string16(), // title
+ base::string16(), // body
+ gfx::Image(), // icon
+ message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT,
+ kCUPSPrintJobNotificationId),
+ base::string16(), // display_source
+ GURL(kCUPSPrintJobNotificationId),
+ notification_id_, // tag
+ message_center::RichNotificationData(), delegate_.get()));
+ UpdateNotification();
+}
+
+CUPSPrintJobNotification::~CUPSPrintJobNotification() {}
+
+void CUPSPrintJobNotification::OnPrintJobStatusUpdated() {
+ UpdateNotification();
+}
+
+void CUPSPrintJobNotification::CloseNotificationByUser() {
+ closed_in_middle_ = true;
+ g_browser_process->message_center()->RemoveNotification(GetNotificationId(),
+ true /* by_user */);
+}
+
+void CUPSPrintJobNotification::ClickOnNotificationButton(int button_index) {
+ DCHECK(button_index >= 0 &&
+ static_cast<size_t>(button_index) < button_commands_->size());
+
+ CUPSPrintJobNotification::ButtonCommand button_command =
+ button_commands_->at(button_index);
+ CUPSPrintJobManager* print_job_manager =
+ CUPSPrintJobManagerFactory::GetForBrowserContext(profile_);
+ const ProfileID profile_id = NotificationUIManager::GetProfileID(profile_);
+
+ switch (button_command) {
+ case CUPSPrintJobNotification::CANCEL_PRINTING:
+ print_job_manager->CancelPrintJob(print_job_);
+ g_browser_process->notification_ui_manager()->CancelById(
+ GetNotificationId(), profile_id);
+ // |print_job_| is deleted by CUPSPrintManager when the print job is
+ // cancelled, thus set it to nullptr.
+ print_job_ = nullptr;
+ break;
+ case CUPSPrintJobNotification::PAUSE_PRINTING:
+ print_job_manager->SuspendPrintJob(print_job_);
+ break;
+ case CUPSPrintJobNotification::RESUME_PRINTING:
+ print_job_manager->ResumePrintJob(print_job_);
+ break;
+ case CUPSPrintJobNotification::GET_HELP:
+ break;
+ }
+}
+
+const std::string& CUPSPrintJobNotification::GetNotificationId() {
+ return notification_id_;
+}
+
+void CUPSPrintJobNotification::UpdateNotification() {
+ DCHECK(print_job_->state() != CUPSPrintJob::STATE_CANCELLED);
+
+ UpdateNotificationTitle();
+ UpdateNotificationIcon();
+ UpdateNotificationBodyMessage();
+ UpdateNotificationType();
+ UpdateNotificationButtons();
+
+ if (closed_in_middle_ &&
+ print_job_->state() == CUPSPrintJob::STATE_PAGE_DONE) {
+ // If the notification was closed during the printing, prevent showing the
+ // following printing progress.
+ g_browser_process->notification_ui_manager()->Update(*notification_,
+ profile_);
+ } else if (print_job_->state() == CUPSPrintJob::STATE_PAGE_DONE) {
+ // If it was not closed, update the notification message directly.
+ g_browser_process->notification_ui_manager()->Add(*notification_, profile_);
+ } else {
+ closed_in_middle_ = false;
+ // In order to make sure it pop up, we should delete it before readding it.
+ const ProfileID profile_id = NotificationUIManager::GetProfileID(profile_);
+ g_browser_process->notification_ui_manager()->CancelById(
+ GetNotificationId(), profile_id);
+ g_browser_process->notification_ui_manager()->Add(*notification_, profile_);
+ }
+}
+
+void CUPSPrintJobNotification::UpdateNotificationTitle() {
+ notification_->set_title(base::UTF8ToUTF16(print_job_->document_title()));
+}
+
+void CUPSPrintJobNotification::UpdateNotificationIcon() {
+ ResourceBundle& bundle = ResourceBundle::GetSharedInstance();
+ switch (print_job_->state()) {
+ case CUPSPrintJob::STATE_WAITING:
+ notification_->set_icon(
+ bundle.GetImageNamed(IDR_PRINT_NOTIFICATION_WAITING));
+ break;
+ case CUPSPrintJob::STATE_STARTED:
+ case CUPSPrintJob::STATE_PAGE_DONE:
+ case CUPSPrintJob::STATE_SUSPENDED:
+ case CUPSPrintJob::STATE_RESUMED:
+ notification_->set_icon(
+ bundle.GetImageNamed(IDR_PRINT_NOTIFICATION_PRINTING));
+ break;
+ case CUPSPrintJob::STATE_DOCUMENT_DONE:
+ notification_->set_icon(
+ bundle.GetImageNamed(IDR_PRINT_NOTIFICATION_DONE));
+ break;
+ case CUPSPrintJob::STATE_ERROR:
+ notification_->set_icon(
+ bundle.GetImageNamed(IDR_PRINT_NOTIFICATION_ERROR));
+ break;
+ default:
+ break;
+ }
+}
+
+void CUPSPrintJobNotification::UpdateNotificationBodyMessage() {
+ base::string16 message;
+ switch (print_job_->state()) {
+ case CUPSPrintJob::STATE_NONE:
+ break;
+ case CUPSPrintJob::STATE_WAITING:
+ message = l10n_util::GetStringFUTF16(
+ IDS_PRINT_JOB_WAITING_NOTIFICATION_MESSAGE,
+ base::IntToString16(print_job_->total_page_number()),
+ base::UTF8ToUTF16(print_job_->printer().display_name()));
+ break;
+ case CUPSPrintJob::STATE_STARTED:
+ case CUPSPrintJob::STATE_PAGE_DONE:
+ case CUPSPrintJob::STATE_SUSPENDED:
+ case CUPSPrintJob::STATE_RESUMED:
+ message = l10n_util::GetStringFUTF16(
+ IDS_PRINT_JOB_PRINTING_NOTIFICATION_MESSAGE,
+ base::IntToString16(print_job_->total_page_number()),
+ base::UTF8ToUTF16(print_job_->printer().display_name()));
+
+ break;
+ case CUPSPrintJob::STATE_DOCUMENT_DONE:
+ message = l10n_util::GetStringFUTF16(
+ IDS_PRINT_JOB_DONE_NOTIFICATION_MESSAGE,
+ base::IntToString16(print_job_->total_page_number()),
+ base::UTF8ToUTF16(print_job_->printer().display_name()));
+ break;
+ case CUPSPrintJob::STATE_ERROR:
+ message = l10n_util::GetStringFUTF16(
+ IDS_PRINT_JOB_ERROR_NOTIFICATION_MESSAGE,
+ base::IntToString16(print_job_->total_page_number()),
+ base::UTF8ToUTF16(print_job_->printer().display_name()));
+ break;
+ default:
+ break;
+ }
+ notification_->set_message(message);
+}
+
+void CUPSPrintJobNotification::UpdateNotificationType() {
+ switch (print_job_->state()) {
+ case CUPSPrintJob::STATE_STARTED:
+ case CUPSPrintJob::STATE_PAGE_DONE:
+ case CUPSPrintJob::STATE_SUSPENDED:
+ case CUPSPrintJob::STATE_RESUMED:
+ notification_->set_type(message_center::NOTIFICATION_TYPE_PROGRESS);
+ notification_->set_progress(print_job_->printed_page_number() * 100 /
+ print_job_->total_page_number());
+ break;
+ case CUPSPrintJob::STATE_NONE:
+ case CUPSPrintJob::STATE_WAITING:
+ case CUPSPrintJob::STATE_DOCUMENT_DONE:
+ case CUPSPrintJob::STATE_ERROR:
+ default:
+ notification_->set_type(message_center::NOTIFICATION_TYPE_SIMPLE);
+ break;
+ }
+}
+
+void CUPSPrintJobNotification::UpdateNotificationButtons() {
+ std::vector<message_center::ButtonInfo> buttons;
+ button_commands_ = GetButtonCommands();
+ for (auto& iter : *button_commands_.get()) {
+ message_center::ButtonInfo button_info =
+ message_center::ButtonInfo(GetButtonLabel(iter));
+ button_info.icon = GetButtonIcon(iter);
+ buttons.push_back(button_info);
+ }
+ notification_->set_buttons(buttons);
+}
+
+std::unique_ptr<std::vector<CUPSPrintJobNotification::ButtonCommand>>
+CUPSPrintJobNotification::GetButtonCommands() const {
+ std::unique_ptr<std::vector<CUPSPrintJobNotification::ButtonCommand>>
+ commands(new std::vector<CUPSPrintJobNotification::ButtonCommand>());
+ switch (print_job_->state()) {
+ case CUPSPrintJob::STATE_WAITING:
+ commands->push_back(CANCEL_PRINTING);
+ break;
+ case CUPSPrintJob::STATE_STARTED:
+ case CUPSPrintJob::STATE_PAGE_DONE:
+ case CUPSPrintJob::STATE_RESUMED:
+ commands->push_back(PAUSE_PRINTING);
+ commands->push_back(CANCEL_PRINTING);
+ break;
+ case CUPSPrintJob::STATE_SUSPENDED:
+ commands->push_back(RESUME_PRINTING);
+ commands->push_back(CANCEL_PRINTING);
+ break;
+ case CUPSPrintJob::STATE_ERROR:
+ commands->push_back(GET_HELP);
+ break;
+ default:
+ break;
+ }
+ return commands;
+}
+
+base::string16 CUPSPrintJobNotification::GetButtonLabel(
+ ButtonCommand button) const {
+ switch (button) {
+ case CANCEL_PRINTING:
+ return l10n_util::GetStringUTF16(
+ IDS_PRINT_JOB_NOTIFICATION_CANCEL_BUTTON);
+ case PAUSE_PRINTING:
+ return l10n_util::GetStringUTF16(IDS_PRINT_JOB_NOTIFICATION_PAUSE_BUTTON);
+ case RESUME_PRINTING:
+ return l10n_util::GetStringUTF16(
+ IDS_PRINT_JOB_NOTIFICATION_RESUME_BUTTON);
+ case GET_HELP:
+ return l10n_util::GetStringUTF16(
+ IDS_PRINT_JOB_NOTIFICATION_GET_HELP_BUTTON);
+ }
+}
+
+gfx::Image CUPSPrintJobNotification::GetButtonIcon(ButtonCommand button) const {
+ ResourceBundle& bundle = ResourceBundle::GetSharedInstance();
+ gfx::Image icon;
+ switch (button) {
+ case CANCEL_PRINTING:
+ icon = bundle.GetImageNamed(IDR_PRINT_NOTIFICATION_CANCEL);
+ break;
+ case PAUSE_PRINTING:
+ icon = bundle.GetImageNamed(IDR_PRINT_NOTIFICATION_PAUSE);
+ break;
+ case RESUME_PRINTING:
+ // TODO(xdai): Add "Resume" icon once it's available.
+ break;
+ case GET_HELP:
+ // TODO(xdai): Add "Get help" icon once it's available.
+ break;
+ }
+ return icon;
+}
+
+} // namespace chromeos

Powered by Google App Engine
This is Rietveld 408576698