Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_CHROMEOS_PRINTING_CUPS_PRINT_JOB_MANAGER_IMPL_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_PRINTING_CUPS_PRINT_JOB_MANAGER_IMPL_H_ |
| 6 #define CHROME_BROWSER_CHROMEOS_PRINTING_CUPS_PRINT_JOB_MANAGER_IMPL_H_ | 6 #define CHROME_BROWSER_CHROMEOS_PRINTING_CUPS_PRINT_JOB_MANAGER_IMPL_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <string> | 10 #include <string> |
| 11 #include <unordered_map> | 11 #include <unordered_map> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/memory/ref_counted.h" | |
| 14 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 16 #include "base/sequence_checker.h" | |
| 17 #include "base/sequenced_task_runner.h" | |
| 18 #include "base/single_thread_task_runner.h" | |
| 15 #include "chrome/browser/chromeos/printing/cups_print_job.h" | 19 #include "chrome/browser/chromeos/printing/cups_print_job.h" |
| 16 #include "chrome/browser/chromeos/printing/cups_print_job_manager.h" | 20 #include "chrome/browser/chromeos/printing/cups_print_job_manager.h" |
| 17 #include "chrome/browser/chromeos/printing/printers_manager.h" | 21 #include "chrome/browser/chromeos/printing/printers_manager.h" |
| 18 #include "content/public/browser/notification_observer.h" | 22 #include "content/public/browser/notification_observer.h" |
| 19 #include "content/public/browser/notification_registrar.h" | 23 #include "content/public/browser/notification_registrar.h" |
| 20 #include "printing/backend/cups_connection.h" | 24 #include "printing/backend/cups_connection.h" |
| 21 | 25 |
| 22 class Profile; | 26 class Profile; |
| 23 | 27 |
| 24 namespace chromeos { | 28 namespace chromeos { |
| 25 | 29 |
| 26 struct QueryResult { | 30 struct QueryResult { |
| 27 QueryResult(); | 31 QueryResult(); |
| 28 QueryResult(const QueryResult& other); | 32 QueryResult(const QueryResult& other); |
| 29 ~QueryResult(); | 33 ~QueryResult(); |
| 30 | 34 |
| 31 bool success; | 35 bool success; |
| 32 std::vector<::printing::QueueStatus> queues; | 36 std::vector<::printing::QueueStatus> queues; |
| 33 }; | 37 }; |
| 34 | 38 |
| 39 // A wrapper around the CUPS connection to ensure that it's always accessed on | |
| 40 // the same sequence. | |
| 41 class CupsWrapper : public base::SupportsWeakPtr<CupsWrapper> { | |
| 42 public: | |
| 43 CupsWrapper(); | |
| 44 ~CupsWrapper(); | |
| 45 | |
| 46 // Query CUPS for the current jobs for the given |printer_ids|. Writes result | |
| 47 // to |result|. | |
| 48 void QueryCups(const std::vector<std::string>& printer_ids, | |
| 49 QueryResult* result); | |
| 50 | |
| 51 // Cancel the print job on the blocking thread. | |
| 52 void CancelJobImpl(const std::string& printer_id, const int job_id); | |
| 53 | |
| 54 private: | |
| 55 ::printing::CupsConnection cups_connection_; | |
| 56 SEQUENCE_CHECKER(sequence_checker_); | |
| 57 }; | |
|
gab
2017/06/27 17:44:18
DISALLOW_COPY_AND_ASSIGN
skau
2017/06/27 20:25:41
Done.
| |
| 58 | |
| 35 class CupsPrintJobManagerImpl : public CupsPrintJobManager, | 59 class CupsPrintJobManagerImpl : public CupsPrintJobManager, |
| 36 public content::NotificationObserver { | 60 public content::NotificationObserver { |
| 37 public: | 61 public: |
| 38 explicit CupsPrintJobManagerImpl(Profile* profile); | 62 explicit CupsPrintJobManagerImpl(Profile* profile); |
| 39 ~CupsPrintJobManagerImpl() override; | 63 ~CupsPrintJobManagerImpl() override; |
| 40 | 64 |
| 41 // CupsPrintJobManager overrides: | 65 // CupsPrintJobManager overrides: |
| 42 void CancelPrintJob(CupsPrintJob* job) override; | 66 void CancelPrintJob(CupsPrintJob* job) override; |
| 43 bool SuspendPrintJob(CupsPrintJob* job) override; | 67 bool SuspendPrintJob(CupsPrintJob* job) override; |
| 44 bool ResumePrintJob(CupsPrintJob* job) override; | 68 bool ResumePrintJob(CupsPrintJob* job) override; |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 58 | 82 |
| 59 // Schedule a query of CUPS for print job status with the default delay. | 83 // Schedule a query of CUPS for print job status with the default delay. |
| 60 void ScheduleQuery(); | 84 void ScheduleQuery(); |
| 61 // Schedule a query of CUPS for print job status with a delay of |delay|. | 85 // Schedule a query of CUPS for print job status with a delay of |delay|. |
| 62 void ScheduleQuery(const base::TimeDelta& delay); | 86 void ScheduleQuery(const base::TimeDelta& delay); |
| 63 | 87 |
| 64 // Schedule the CUPS query off the UI thread. Posts results back to UI thread | 88 // Schedule the CUPS query off the UI thread. Posts results back to UI thread |
| 65 // to UpdateJobs. | 89 // to UpdateJobs. |
| 66 void PostQuery(); | 90 void PostQuery(); |
| 67 | 91 |
| 68 // Updates the state of a print job based on |printer_status| and |job|. | |
| 69 // Returns true if observers need to be notified of an update. | |
| 70 bool UpdatePrintJob(const ::printing::PrinterStatus& printer_status, | |
| 71 const ::printing::CupsJob& job, | |
| 72 CupsPrintJob* print_job); | |
| 73 | |
| 74 // Process jobs from CUPS and perform notifications. | 92 // Process jobs from CUPS and perform notifications. |
| 75 void UpdateJobs(const QueryResult& results); | 93 void UpdateJobs(std::unique_ptr<QueryResult> results); |
| 76 | 94 |
| 77 // Mark remaining jobs as errors and remove active jobs. | 95 // Mark remaining jobs as errors and remove active jobs. |
| 78 void PurgeJobs(); | 96 void PurgeJobs(); |
| 79 | 97 |
| 80 // Cancel the print job on the blocking thread. | |
| 81 void CancelJobImpl(const std::string& printer_id, const int job_id); | |
| 82 | |
| 83 // Notify observers that a state update has occured for |job|. | 98 // Notify observers that a state update has occured for |job|. |
| 84 void NotifyJobStateUpdate(CupsPrintJob* job); | 99 void NotifyJobStateUpdate(CupsPrintJob* job); |
| 85 | 100 |
| 86 // Ongoing print jobs. | 101 // Ongoing print jobs. |
| 87 std::map<std::string, std::unique_ptr<CupsPrintJob>> jobs_; | 102 std::map<std::string, std::unique_ptr<CupsPrintJob>> jobs_; |
| 88 | 103 |
| 89 // Prevents multiple queries from being scheduled simultaneously. | 104 // Prevents multiple queries from being scheduled simultaneously. |
| 90 bool in_query_ = false; | 105 bool in_query_ = false; |
| 91 | 106 |
| 92 // Records the number of consecutive times the GetJobs query has failed. | 107 // Records the number of consecutive times the GetJobs query has failed. |
| 93 int retry_count_ = 0; | 108 int retry_count_ = 0; |
| 94 | 109 |
| 95 ::printing::CupsConnection cups_connection_; | 110 CupsWrapper cups_wrapper_; |
|
gab
2017/06/27 17:44:18
To ensure this is deleted on its sequence you'll n
skau
2017/06/27 20:25:41
Thanks! I was looking for that.
| |
| 96 content::NotificationRegistrar registrar_; | 111 content::NotificationRegistrar registrar_; |
| 112 // Task runner for queries to CUPS. | |
| 113 scoped_refptr<base::SequencedTaskRunner> query_runner_; | |
| 114 // Task runner to for the Browser UI thread. | |
| 115 scoped_refptr<base::SingleThreadTaskRunner> browser_thread_runner_; | |
| 97 base::WeakPtrFactory<CupsPrintJobManagerImpl> weak_ptr_factory_; | 116 base::WeakPtrFactory<CupsPrintJobManagerImpl> weak_ptr_factory_; |
| 98 | 117 |
| 99 DISALLOW_COPY_AND_ASSIGN(CupsPrintJobManagerImpl); | 118 DISALLOW_COPY_AND_ASSIGN(CupsPrintJobManagerImpl); |
| 100 }; | 119 }; |
| 101 | 120 |
| 102 } // namespace chromeos | 121 } // namespace chromeos |
| 103 | 122 |
| 104 #endif // CHROME_BROWSER_CHROMEOS_PRINTING_CUPS_PRINT_JOB_MANAGER_IMPL_H_ | 123 #endif // CHROME_BROWSER_CHROMEOS_PRINTING_CUPS_PRINT_JOB_MANAGER_IMPL_H_ |
| OLD | NEW |