print_job_manager.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright (c) 2012 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. #ifndef CHROME_BROWSER_PRINTING_PRINT_JOB_MANAGER_H_
  5. #define CHROME_BROWSER_PRINTING_PRINT_JOB_MANAGER_H_
  6. #include <memory>
  7. #include <set>
  8. #include <vector>
  9. #include "base/logging.h"
  10. #include "base/macros.h"
  11. #include "base/memory/ref_counted.h"
  12. #include "base/synchronization/lock.h"
  13. #include "content/public/browser/notification_observer.h"
  14. #include "content/public/browser/notification_registrar.h"
  15. namespace printing {
  16. class JobEventDetails;
  17. class PrintJob;
  18. class PrinterQuery;
  19. class PrintQueriesQueue : public base::RefCountedThreadSafe<PrintQueriesQueue> {
  20. public:
  21. PrintQueriesQueue();
  22. // Queues a semi-initialized worker thread. Can be called from any thread.
  23. // Current use case is queuing from the I/O thread.
  24. // TODO(maruel): Have them vanish after a timeout (~5 minutes?)
  25. void QueuePrinterQuery(PrinterQuery* job);
  26. // Pops a queued PrintJobWorkerOwner object that was previously queued or
  27. // create new one. Can be called from any thread.
  28. scoped_refptr<PrinterQuery> PopPrinterQuery(int document_cookie);
  29. // Creates new query.
  30. scoped_refptr<PrinterQuery> CreatePrinterQuery(int render_process_id,
  31. int render_frame_id);
  32. void Shutdown();
  33. private:
  34. friend class base::RefCountedThreadSafe<PrintQueriesQueue>;
  35. typedef std::vector<scoped_refptr<PrinterQuery>> PrinterQueries;
  36. virtual ~PrintQueriesQueue();
  37. // Used to serialize access to queued_workers_.
  38. base::Lock lock_;
  39. PrinterQueries queued_queries_;
  40. DISALLOW_COPY_AND_ASSIGN(PrintQueriesQueue);
  41. };
  42. class PrintJobManager : public content::NotificationObserver {
  43. public:
  44. PrintJobManager();
  45. ~PrintJobManager() override;
  46. // On browser quit, we should wait to have the print job finished.
  47. void Shutdown();
  48. // content::NotificationObserver
  49. void Observe(int type,
  50. const content::NotificationSource& source,
  51. const content::NotificationDetails& details) override;
  52. // Returns queries queue. Never returns NULL. Must be called on Browser UI
  53. // Thread. Reference could be stored and used from any thread.
  54. scoped_refptr<PrintQueriesQueue> queue();
  55. private:
  56. typedef std::set<scoped_refptr<PrintJob>> PrintJobs;
  57. // Processes a NOTIFY_PRINT_JOB_EVENT notification.
  58. void OnPrintJobEvent(PrintJob* print_job,
  59. const JobEventDetails& event_details);
  60. // Stops all printing jobs. If wait_for_finish is true, tries to give jobs
  61. // a chance to complete before stopping them.
  62. void StopJobs(bool wait_for_finish);
  63. content::NotificationRegistrar registrar_;
  64. // Current print jobs that are active.
  65. PrintJobs current_jobs_;
  66. scoped_refptr<PrintQueriesQueue> queue_;
  67. bool is_shutdown_;
  68. DISALLOW_COPY_AND_ASSIGN(PrintJobManager);
  69. };
  70. } // namespace printing
  71. #endif // CHROME_BROWSER_PRINTING_PRINT_JOB_MANAGER_H_