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> { | |
gab
2017/06/28 15:55:58
As mentioned in previous paradigm comment: I don't
skau
2017/06/28 23:59:48
I misunderstood slightly. It's just unretained no
| |
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 | |
58 DISALLOW_COPY_AND_ASSIGN(CupsWrapper); | |
59 }; | |
60 | |
35 class CupsPrintJobManagerImpl : public CupsPrintJobManager, | 61 class CupsPrintJobManagerImpl : public CupsPrintJobManager, |
36 public content::NotificationObserver { | 62 public content::NotificationObserver { |
37 public: | 63 public: |
38 explicit CupsPrintJobManagerImpl(Profile* profile); | 64 explicit CupsPrintJobManagerImpl(Profile* profile); |
39 ~CupsPrintJobManagerImpl() override; | 65 ~CupsPrintJobManagerImpl() override; |
40 | 66 |
41 // CupsPrintJobManager overrides: | 67 // CupsPrintJobManager overrides: |
42 void CancelPrintJob(CupsPrintJob* job) override; | 68 void CancelPrintJob(CupsPrintJob* job) override; |
43 bool SuspendPrintJob(CupsPrintJob* job) override; | 69 bool SuspendPrintJob(CupsPrintJob* job) override; |
44 bool ResumePrintJob(CupsPrintJob* job) override; | 70 bool ResumePrintJob(CupsPrintJob* job) override; |
(...skipping 13 matching lines...) Expand all Loading... | |
58 | 84 |
59 // Schedule a query of CUPS for print job status with the default delay. | 85 // Schedule a query of CUPS for print job status with the default delay. |
60 void ScheduleQuery(); | 86 void ScheduleQuery(); |
61 // Schedule a query of CUPS for print job status with a delay of |delay|. | 87 // Schedule a query of CUPS for print job status with a delay of |delay|. |
62 void ScheduleQuery(const base::TimeDelta& delay); | 88 void ScheduleQuery(const base::TimeDelta& delay); |
63 | 89 |
64 // Schedule the CUPS query off the UI thread. Posts results back to UI thread | 90 // Schedule the CUPS query off the UI thread. Posts results back to UI thread |
65 // to UpdateJobs. | 91 // to UpdateJobs. |
66 void PostQuery(); | 92 void PostQuery(); |
67 | 93 |
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. | 94 // Process jobs from CUPS and perform notifications. |
75 void UpdateJobs(const QueryResult& results); | 95 void UpdateJobs(std::unique_ptr<QueryResult> results); |
76 | 96 |
77 // Mark remaining jobs as errors and remove active jobs. | 97 // Mark remaining jobs as errors and remove active jobs. |
78 void PurgeJobs(); | 98 void PurgeJobs(); |
79 | 99 |
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|. | 100 // Notify observers that a state update has occured for |job|. |
84 void NotifyJobStateUpdate(CupsPrintJob* job); | 101 void NotifyJobStateUpdate(CupsPrintJob* job); |
85 | 102 |
86 // Ongoing print jobs. | 103 // Ongoing print jobs. |
87 std::map<std::string, std::unique_ptr<CupsPrintJob>> jobs_; | 104 std::map<std::string, std::unique_ptr<CupsPrintJob>> jobs_; |
88 | 105 |
89 // Prevents multiple queries from being scheduled simultaneously. | 106 // Prevents multiple queries from being scheduled simultaneously. |
90 bool in_query_ = false; | 107 bool in_query_ = false; |
91 | 108 |
92 // Records the number of consecutive times the GetJobs query has failed. | 109 // Records the number of consecutive times the GetJobs query has failed. |
93 int retry_count_ = 0; | 110 int retry_count_ = 0; |
94 | 111 |
95 ::printing::CupsConnection cups_connection_; | |
96 content::NotificationRegistrar registrar_; | 112 content::NotificationRegistrar registrar_; |
113 // Task runner for queries to CUPS. | |
114 scoped_refptr<base::SequencedTaskRunner> query_runner_; | |
115 // Task runner to for the Browser UI thread. | |
gab
2017/06/28 15:55:57
rm "to"
skau
2017/06/28 23:59:48
Done.
| |
116 scoped_refptr<base::SingleThreadTaskRunner> browser_thread_runner_; | |
gab
2017/06/28 15:55:57
I'd say keep using DCHECK_CURRENTLY_ON(content::Br
skau
2017/06/28 23:59:48
Done.
| |
117 std::unique_ptr<CupsWrapper, base::OnTaskRunnerDeleter> cups_wrapper_; | |
97 base::WeakPtrFactory<CupsPrintJobManagerImpl> weak_ptr_factory_; | 118 base::WeakPtrFactory<CupsPrintJobManagerImpl> weak_ptr_factory_; |
98 | 119 |
99 DISALLOW_COPY_AND_ASSIGN(CupsPrintJobManagerImpl); | 120 DISALLOW_COPY_AND_ASSIGN(CupsPrintJobManagerImpl); |
100 }; | 121 }; |
101 | 122 |
102 } // namespace chromeos | 123 } // namespace chromeos |
103 | 124 |
104 #endif // CHROME_BROWSER_CHROMEOS_PRINTING_CUPS_PRINT_JOB_MANAGER_IMPL_H_ | 125 #endif // CHROME_BROWSER_CHROMEOS_PRINTING_CUPS_PRINT_JOB_MANAGER_IMPL_H_ |
OLD | NEW |