| 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/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 15 #include "chrome/browser/chromeos/printing/cups_print_job.h" | 15 #include "chrome/browser/chromeos/printing/cups_print_job.h" |
| 16 #include "chrome/browser/chromeos/printing/cups_print_job_manager.h" | 16 #include "chrome/browser/chromeos/printing/cups_print_job_manager.h" |
| 17 #include "chrome/browser/chromeos/printing/printers_manager.h" | 17 #include "chrome/browser/chromeos/printing/printers_manager.h" |
| 18 #include "content/public/browser/notification_observer.h" | 18 #include "content/public/browser/notification_observer.h" |
| 19 #include "content/public/browser/notification_registrar.h" | 19 #include "content/public/browser/notification_registrar.h" |
| 20 #include "printing/backend/cups_connection.h" | 20 #include "printing/backend/cups_connection.h" |
| 21 | 21 |
| 22 class Profile; | 22 class Profile; |
| 23 | 23 |
| 24 namespace chromeos { | 24 namespace chromeos { |
| 25 | 25 |
| 26 struct QueryResult { |
| 27 bool success; |
| 28 std::vector<::printing::QueueStatus> queues; |
| 29 }; |
| 30 |
| 26 class CupsPrintJobManagerImpl : public CupsPrintJobManager, | 31 class CupsPrintJobManagerImpl : public CupsPrintJobManager, |
| 27 public content::NotificationObserver { | 32 public content::NotificationObserver { |
| 28 public: | 33 public: |
| 29 explicit CupsPrintJobManagerImpl(Profile* profile); | 34 explicit CupsPrintJobManagerImpl(Profile* profile); |
| 30 ~CupsPrintJobManagerImpl() override; | 35 ~CupsPrintJobManagerImpl() override; |
| 31 | 36 |
| 32 // CupsPrintJobManager overrides: | 37 // CupsPrintJobManager overrides: |
| 33 bool CancelPrintJob(CupsPrintJob* job) override; | 38 bool CancelPrintJob(CupsPrintJob* job) override; |
| 34 bool SuspendPrintJob(CupsPrintJob* job) override; | 39 bool SuspendPrintJob(CupsPrintJob* job) override; |
| 35 bool ResumePrintJob(CupsPrintJob* job) override; | 40 bool ResumePrintJob(CupsPrintJob* job) override; |
| 36 | 41 |
| 37 // NotificationObserver overrides: | 42 // NotificationObserver overrides: |
| 38 void Observe(int type, | 43 void Observe(int type, |
| 39 const content::NotificationSource& source, | 44 const content::NotificationSource& source, |
| 40 const content::NotificationDetails& details) override; | 45 const content::NotificationDetails& details) override; |
| 41 | 46 |
| 42 private: | 47 private: |
| 43 // Begin monitoring a print job for a given |printer_name| with the given | 48 // Begin monitoring a print job for a given |printer_name| with the given |
| 44 // |title| with the pages |total_page_number|. | 49 // |title| with the pages |total_page_number|. |
| 45 bool CreatePrintJob(const std::string& printer_name, | 50 bool CreatePrintJob(const std::string& printer_name, |
| 46 const std::string& title, | 51 const std::string& title, |
| 47 int job_id, | 52 int job_id, |
| 48 int total_page_number); | 53 int total_page_number); |
| 49 | 54 |
| 50 // Schedule a query of CUPS for print job status with the default delay. | 55 // Schedule a query of CUPS for print job status with the default delay. |
| 51 void ScheduleQuery(); | 56 void ScheduleQuery(); |
| 52 // Schedule a query of CUPS for print job status with a delay of |delay|. | 57 // Schedule a query of CUPS for print job status with a delay of |delay|. |
| 53 void ScheduleQuery(const base::TimeDelta& delay); | 58 void ScheduleQuery(const base::TimeDelta& delay); |
| 54 | 59 |
| 55 // Query CUPS for print job status. | 60 // Schedule the CUPS query off the UI thread. Posts results back to UI thread |
| 56 void QueryCups(); | 61 // to UpdateJobs. |
| 62 void PostQuery(); |
| 57 | 63 |
| 58 // Process jobs from CUPS and perform notifications. | 64 // Process jobs from CUPS and perform notifications. |
| 59 void UpdateJobs(const std::vector<::printing::CupsJob>& jobs); | 65 void UpdateJobs(const QueryResult& results); |
| 66 |
| 67 // Mark remaining jobs as errors and remove active jobs. |
| 68 void PurgeJobs(); |
| 60 | 69 |
| 61 // Updates the state and performs the appropriate notifications. | 70 // Updates the state and performs the appropriate notifications. |
| 62 void JobStateUpdated(CupsPrintJob* job, CupsPrintJob::State new_state); | 71 void JobStateUpdated(CupsPrintJob* job, CupsPrintJob::State new_state); |
| 63 | 72 |
| 64 // Ongoing print jobs. | 73 // Ongoing print jobs. |
| 65 std::map<std::string, std::unique_ptr<CupsPrintJob>> jobs_; | 74 std::map<std::string, std::unique_ptr<CupsPrintJob>> jobs_; |
| 66 | 75 |
| 67 // Prevents multiple queries from being scheduled simultaneously. | 76 // Prevents multiple queries from being scheduled simultaneously. |
| 68 bool in_query_ = false; | 77 bool in_query_ = false; |
| 69 | 78 |
| 79 // Records the number of consecutive times the GetJobs query has failed. |
| 80 int retry_count_ = 0; |
| 81 |
| 70 ::printing::CupsConnection cups_connection_; | 82 ::printing::CupsConnection cups_connection_; |
| 71 content::NotificationRegistrar registrar_; | 83 content::NotificationRegistrar registrar_; |
| 72 base::WeakPtrFactory<CupsPrintJobManagerImpl> weak_ptr_factory_; | 84 base::WeakPtrFactory<CupsPrintJobManagerImpl> weak_ptr_factory_; |
| 73 | 85 |
| 74 DISALLOW_COPY_AND_ASSIGN(CupsPrintJobManagerImpl); | 86 DISALLOW_COPY_AND_ASSIGN(CupsPrintJobManagerImpl); |
| 75 }; | 87 }; |
| 76 | 88 |
| 77 } // namespace chromeos | 89 } // namespace chromeos |
| 78 | 90 |
| 79 #endif // CHROME_BROWSER_CHROMEOS_PRINTING_CUPS_PRINT_JOB_MANAGER_IMPL_H_ | 91 #endif // CHROME_BROWSER_CHROMEOS_PRINTING_CUPS_PRINT_JOB_MANAGER_IMPL_H_ |
| OLD | NEW |