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

Side by Side Diff: chrome/browser/chromeos/printing/cups_print_job_manager_impl.h

Issue 2943843002: Convert to CupsPrintJobManagerImpl to TaskScheduler. (Closed)
Patch Set: strong pointers and DCHECKs Created 3 years, 5 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 unified diff | Download patch
OLDNEW
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>
12 #include <vector> 11 #include <vector>
13 12
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/weak_ptr.h" 14 #include "base/memory/weak_ptr.h"
15 #include "base/sequence_checker.h"
16 #include "base/sequenced_task_runner.h"
15 #include "chrome/browser/chromeos/printing/cups_print_job.h" 17 #include "chrome/browser/chromeos/printing/cups_print_job.h"
16 #include "chrome/browser/chromeos/printing/cups_print_job_manager.h" 18 #include "chrome/browser/chromeos/printing/cups_print_job_manager.h"
17 #include "chrome/browser/chromeos/printing/printers_manager.h" 19 #include "chrome/browser/chromeos/printing/printers_manager.h"
18 #include "content/public/browser/notification_observer.h" 20 #include "content/public/browser/notification_observer.h"
19 #include "content/public/browser/notification_registrar.h" 21 #include "content/public/browser/notification_registrar.h"
20 #include "printing/backend/cups_connection.h" 22 #include "printing/backend/cups_connection.h"
21 23
22 class Profile; 24 class Profile;
23 25
24 namespace chromeos { 26 namespace chromeos {
25 27
26 struct QueryResult { 28 struct QueryResult {
27 QueryResult(); 29 QueryResult();
28 QueryResult(const QueryResult& other); 30 QueryResult(const QueryResult& other);
29 ~QueryResult(); 31 ~QueryResult();
30 32
31 bool success; 33 bool success;
32 std::vector<::printing::QueueStatus> queues; 34 std::vector<::printing::QueueStatus> queues;
33 }; 35 };
34 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
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
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_;
96 content::NotificationRegistrar registrar_; 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_;
97 base::WeakPtrFactory<CupsPrintJobManagerImpl> weak_ptr_factory_; 114 base::WeakPtrFactory<CupsPrintJobManagerImpl> weak_ptr_factory_;
98 115
99 DISALLOW_COPY_AND_ASSIGN(CupsPrintJobManagerImpl); 116 DISALLOW_COPY_AND_ASSIGN(CupsPrintJobManagerImpl);
100 }; 117 };
101 118
102 } // namespace chromeos 119 } // namespace chromeos
103 120
104 #endif // CHROME_BROWSER_CHROMEOS_PRINTING_CUPS_PRINT_JOB_MANAGER_IMPL_H_ 121 #endif // CHROME_BROWSER_CHROMEOS_PRINTING_CUPS_PRINT_JOB_MANAGER_IMPL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698