print_job.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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_H_
  5. #define CHROME_BROWSER_PRINTING_PRINT_JOB_H_
  6. #include <memory>
  7. #include <vector>
  8. #include "base/macros.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "build/build_config.h"
  11. #include "chrome/browser/printing/print_job_worker_owner.h"
  12. #include "content/public/browser/notification_observer.h"
  13. #include "content/public/browser/notification_registrar.h"
  14. namespace base {
  15. class RefCountedMemory;
  16. }
  17. namespace printing {
  18. class JobEventDetails;
  19. class MetafilePlayer;
  20. class PdfToEmfConverter;
  21. class PrintJobWorker;
  22. class PrintedDocument;
  23. class PrintedPage;
  24. class PrinterQuery;
  25. // Manages the print work for a specific document. Talks to the printer through
  26. // PrintingContext through PrintJobWorker. Hides access to PrintingContext in a
  27. // worker thread so the caller never blocks. PrintJob will send notifications on
  28. // any state change. While printing, the PrintJobManager instance keeps a
  29. // reference to the job to be sure it is kept alive. All the code in this class
  30. // runs in the UI thread.
  31. class PrintJob : public PrintJobWorkerOwner,
  32. public content::NotificationObserver {
  33. public:
  34. // Create a empty PrintJob. When initializing with this constructor,
  35. // post-constructor initialization must be done with Initialize().
  36. PrintJob();
  37. // Grabs the ownership of the PrintJobWorker from another job, which is
  38. // usually a PrinterQuery. Set the expected page count of the print job.
  39. void Initialize(PrintJobWorkerOwner* job,
  40. const base::string16& name,
  41. int page_count);
  42. // content::NotificationObserver implementation.
  43. void Observe(int type,
  44. const content::NotificationSource& source,
  45. const content::NotificationDetails& details) override;
  46. // PrintJobWorkerOwner implementation.
  47. void GetSettingsDone(const PrintSettings& new_settings,
  48. PrintingContext::Result result) override;
  49. std::unique_ptr<PrintJobWorker> DetachWorker(
  50. PrintJobWorkerOwner* new_owner) override;
  51. const PrintSettings& settings() const override;
  52. int cookie() const override;
  53. // Starts the actual printing. Signals the worker that it should begin to
  54. // spool as soon as data is available.
  55. void StartPrinting();
  56. // Asks for the worker thread to finish its queued tasks and disconnects the
  57. // delegate object. The PrintJobManager will remove its reference. This may
  58. // have the side-effect of destroying the object if the caller doesn't have a
  59. // handle to the object. Use PrintJob::is_stopped() to check whether the
  60. // worker thread has actually stopped.
  61. void Stop();
  62. // Cancels printing job and stops the worker thread. Takes effect immediately.
  63. void Cancel();
  64. // Synchronously wait for the job to finish. It is mainly useful when the
  65. // process is about to be shut down and we're waiting for the spooler to eat
  66. // our data.
  67. bool FlushJob(base::TimeDelta timeout);
  68. // Returns true if the print job is pending, i.e. between a StartPrinting()
  69. // and the end of the spooling.
  70. bool is_job_pending() const;
  71. // Access the current printed document. Warning: may be NULL.
  72. PrintedDocument* document() const;
  73. #if defined(OS_WIN)
  74. // Let the PrintJob know the 0-based |page_number| of a given printed page.
  75. void AppendPrintedPage(int page_number);
  76. void StartPdfToEmfConversion(
  77. const scoped_refptr<base::RefCountedMemory>& bytes,
  78. const gfx::Size& page_size,
  79. const gfx::Rect& content_area,
  80. bool print_text_with_gdi);
  81. void StartPdfToPostScriptConversion(
  82. const scoped_refptr<base::RefCountedMemory>& bytes,
  83. const gfx::Rect& content_area,
  84. const gfx::Point& physical_offset,
  85. bool ps_level2);
  86. #endif // defined(OS_WIN)
  87. protected:
  88. ~PrintJob() override;
  89. private:
  90. // Updates |document_| to a new instance.
  91. void UpdatePrintedDocument(PrintedDocument* new_document);
  92. // Processes a NOTIFY_PRINT_JOB_EVENT notification.
  93. void OnNotifyPrintJobEvent(const JobEventDetails& event_details);
  94. // Releases the worker thread by calling Stop(), then broadcasts a JOB_DONE
  95. // notification.
  96. void OnDocumentDone();
  97. // Terminates the worker thread in a very controlled way, to work around any
  98. // eventual deadlock.
  99. void ControlledWorkerShutdown();
  100. // Called at shutdown when running a nested message loop.
  101. void Quit();
  102. void HoldUntilStopIsCalled();
  103. #if defined(OS_WIN)
  104. void OnPdfConversionStarted(int page_count);
  105. void OnPdfPageConverted(int page_number,
  106. float scale_factor,
  107. std::unique_ptr<MetafilePlayer> emf);
  108. #endif // defined(OS_WIN)
  109. content::NotificationRegistrar registrar_;
  110. // All the UI is done in a worker thread because many Win32 print functions
  111. // are blocking and enters a message loop without your consent. There is one
  112. // worker thread per print job.
  113. std::unique_ptr<PrintJobWorker> worker_;
  114. // Cache of the print context settings for access in the UI thread.
  115. PrintSettings settings_;
  116. // The printed document.
  117. scoped_refptr<PrintedDocument> document_;
  118. // Is the worker thread printing.
  119. bool is_job_pending_;
  120. // Is Canceling? If so, try to not cause recursion if on FAILED notification,
  121. // the notified calls Cancel() again.
  122. bool is_canceling_;
  123. #if defined(OS_WIN)
  124. class PdfConversionState;
  125. std::unique_ptr<PdfConversionState> pdf_conversion_state_;
  126. std::vector<int> pdf_page_mapping_;
  127. #endif // defined(OS_WIN)
  128. // Used at shutdown so that we can quit a nested message loop.
  129. base::WeakPtrFactory<PrintJob> quit_factory_;
  130. DISALLOW_COPY_AND_ASSIGN(PrintJob);
  131. };
  132. // Details for a NOTIFY_PRINT_JOB_EVENT notification. The members may be NULL.
  133. class JobEventDetails : public base::RefCountedThreadSafe<JobEventDetails> {
  134. public:
  135. // Event type.
  136. enum Type {
  137. // Print... dialog box has been closed with OK button.
  138. USER_INIT_DONE,
  139. // Print... dialog box has been closed with CANCEL button.
  140. USER_INIT_CANCELED,
  141. // An automated initialization has been done, e.g. Init(false, NULL).
  142. DEFAULT_INIT_DONE,
  143. // A new document started printing.
  144. NEW_DOC,
  145. // A new page started printing.
  146. NEW_PAGE,
  147. // A page is done printing.
  148. PAGE_DONE,
  149. // A document is done printing. The worker thread is still alive. Warning:
  150. // not a good moment to release the handle to PrintJob.
  151. DOC_DONE,
  152. // The worker thread is finished. A good moment to release the handle to
  153. // PrintJob.
  154. JOB_DONE,
  155. // All missing pages have been requested.
  156. ALL_PAGES_REQUESTED,
  157. // An error occured. Printing is canceled.
  158. FAILED,
  159. };
  160. JobEventDetails(Type type, PrintedDocument* document, PrintedPage* page);
  161. // Getters.
  162. PrintedDocument* document() const;
  163. PrintedPage* page() const;
  164. Type type() const { return type_; }
  165. private:
  166. friend class base::RefCountedThreadSafe<JobEventDetails>;
  167. ~JobEventDetails();
  168. scoped_refptr<PrintedDocument> document_;
  169. scoped_refptr<PrintedPage> page_;
  170. const Type type_;
  171. DISALLOW_COPY_AND_ASSIGN(JobEventDetails);
  172. };
  173. } // namespace printing
  174. #endif // CHROME_BROWSER_PRINTING_PRINT_JOB_H_