OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CHROME_BROWSER_CHROMEOS_PRINTING_CUPS_PRINT_JOB_MANAGER_IMPL_H_ | |
6 #define CHROME_BROWSER_CHROMEOS_PRINTING_CUPS_PRINT_JOB_MANAGER_IMPL_H_ | |
7 | |
8 #include <map> | |
9 #include <memory> | |
10 #include <string> | |
11 #include <vector> | |
12 | |
13 #include "base/memory/ref_counted.h" | |
14 #include "base/memory/weak_ptr.h" | |
15 #include "base/sequence_checker.h" | |
16 #include "base/sequenced_task_runner.h" | |
17 #include "chrome/browser/chromeos/printing/cups_print_job.h" | |
18 #include "chrome/browser/chromeos/printing/cups_print_job_manager.h" | |
19 #include "chrome/browser/chromeos/printing/printers_manager.h" | |
20 #include "content/public/browser/notification_observer.h" | |
21 #include "content/public/browser/notification_registrar.h" | |
22 #include "printing/backend/cups_connection.h" | |
23 | |
24 class Profile; | |
25 | |
26 namespace chromeos { | |
27 | |
28 struct QueryResult { | |
29 QueryResult(); | |
30 QueryResult(const QueryResult& other); | |
31 ~QueryResult(); | |
32 | |
33 bool success; | |
34 std::vector<::printing::QueueStatus> queues; | |
35 }; | |
36 | |
37 // A wrapper around the CUPS connection to ensure that it's always accessed on | |
38 // the same sequence. | |
39 class CupsWrapper { | |
40 public: | |
41 CupsWrapper(); | |
42 ~CupsWrapper(); | |
43 | |
44 // Query CUPS for the current jobs for the given |printer_ids|. Writes result | |
45 // to |result|. | |
46 void QueryCups(const std::vector<std::string>& printer_ids, | |
47 QueryResult* result); | |
48 | |
49 // Cancel the print job on the blocking thread. | |
50 void CancelJobImpl(const std::string& printer_id, const int job_id); | |
51 | |
52 private: | |
53 ::printing::CupsConnection cups_connection_; | |
54 SEQUENCE_CHECKER(sequence_checker_); | |
55 | |
56 DISALLOW_COPY_AND_ASSIGN(CupsWrapper); | |
57 }; | |
58 | |
59 class CupsPrintJobManagerImpl : public CupsPrintJobManager, | |
60 public content::NotificationObserver { | |
61 public: | |
62 explicit CupsPrintJobManagerImpl(Profile* profile); | |
63 ~CupsPrintJobManagerImpl() override; | |
64 | |
65 // CupsPrintJobManager overrides: | |
66 void CancelPrintJob(CupsPrintJob* job) override; | |
67 bool SuspendPrintJob(CupsPrintJob* job) override; | |
68 bool ResumePrintJob(CupsPrintJob* job) override; | |
69 | |
70 // NotificationObserver overrides: | |
71 void Observe(int type, | |
72 const content::NotificationSource& source, | |
73 const content::NotificationDetails& details) override; | |
74 | |
75 private: | |
76 // Begin monitoring a print job for a given |printer_name| with the given | |
77 // |title| with the pages |total_page_number|. | |
78 bool CreatePrintJob(const std::string& printer_name, | |
79 const std::string& title, | |
80 int job_id, | |
81 int total_page_number); | |
82 | |
83 // Schedule a query of CUPS for print job status with the default delay. | |
84 void ScheduleQuery(); | |
85 // Schedule a query of CUPS for print job status with a delay of |delay|. | |
86 void ScheduleQuery(const base::TimeDelta& delay); | |
87 | |
88 // Schedule the CUPS query off the UI thread. Posts results back to UI thread | |
89 // to UpdateJobs. | |
90 void PostQuery(); | |
91 | |
92 // Process jobs from CUPS and perform notifications. | |
93 void UpdateJobs(std::unique_ptr<QueryResult> results); | |
94 | |
95 // Mark remaining jobs as errors and remove active jobs. | |
96 void PurgeJobs(); | |
97 | |
98 // Notify observers that a state update has occured for |job|. | |
99 void NotifyJobStateUpdate(CupsPrintJob* job); | |
100 | |
101 // Ongoing print jobs. | |
102 std::map<std::string, std::unique_ptr<CupsPrintJob>> jobs_; | |
103 | |
104 // Prevents multiple queries from being scheduled simultaneously. | |
105 bool in_query_ = false; | |
106 | |
107 // Records the number of consecutive times the GetJobs query has failed. | |
108 int retry_count_ = 0; | |
109 | |
110 content::NotificationRegistrar registrar_; | |
111 // Task runner for queries to CUPS. | |
112 scoped_refptr<base::SequencedTaskRunner> query_runner_; | |
113 std::unique_ptr<CupsWrapper, base::OnTaskRunnerDeleter> cups_wrapper_; | |
114 base::WeakPtrFactory<CupsPrintJobManagerImpl> weak_ptr_factory_; | |
115 | |
116 DISALLOW_COPY_AND_ASSIGN(CupsPrintJobManagerImpl); | |
117 }; | |
118 | |
119 } // namespace chromeos | |
120 | |
121 #endif // CHROME_BROWSER_CHROMEOS_PRINTING_CUPS_PRINT_JOB_MANAGER_IMPL_H_ | |
OLD | NEW |