print_job_worker.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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_WORKER_H_
  5. #define CHROME_BROWSER_PRINTING_PRINT_JOB_WORKER_H_
  6. #include <memory>
  7. #include "base/location.h"
  8. #include "base/macros.h"
  9. #include "base/memory/ref_counted.h"
  10. #include "base/memory/weak_ptr.h"
  11. #include "base/threading/thread.h"
  12. #include "chrome/browser/printing/printer_query.h"
  13. #include "content/public/browser/browser_thread.h"
  14. #include "printing/page_number.h"
  15. #include "printing/print_job_constants.h"
  16. #include "printing/printing_context.h"
  17. namespace base {
  18. class DictionaryValue;
  19. }
  20. namespace printing {
  21. class PrintJob;
  22. class PrintJobWorkerOwner;
  23. class PrintedDocument;
  24. class PrintedPage;
  25. // Worker thread code. It manages the PrintingContext, which can be blocking
  26. // and/or run a message loop. This is the object that generates most
  27. // NOTIFY_PRINT_JOB_EVENT notifications, but they are generated through a
  28. // NotificationTask task to be executed from the right thread, the UI thread.
  29. // PrintJob always outlives its worker instance.
  30. class PrintJobWorker {
  31. public:
  32. PrintJobWorker(int render_process_id,
  33. int render_frame_id,
  34. PrintJobWorkerOwner* owner);
  35. virtual ~PrintJobWorker();
  36. void SetNewOwner(PrintJobWorkerOwner* new_owner);
  37. // Initializes the print settings. If |ask_user_for_settings| is true, a
  38. // Print... dialog box will be shown to ask the user their preference.
  39. // |is_scripted| should be true for calls coming straight from window.print().
  40. // |is_modifiable| implies HTML and not other formats like PDF.
  41. void GetSettings(bool ask_user_for_settings,
  42. int document_page_count,
  43. bool has_selection,
  44. MarginType margin_type,
  45. bool is_scripted,
  46. bool is_modifiable,
  47. const base::string16& device_name);
  48. // Set the new print settings.
  49. void SetSettings(std::unique_ptr<base::DictionaryValue> new_settings);
  50. // Starts the printing loop. Every pages are printed as soon as the data is
  51. // available. Makes sure the new_document is the right one.
  52. void StartPrinting(PrintedDocument* new_document);
  53. // Updates the printed document.
  54. void OnDocumentChanged(PrintedDocument* new_document);
  55. // Dequeues waiting pages. Called when PrintJob receives a
  56. // NOTIFY_PRINTED_DOCUMENT_UPDATED notification. It's time to look again if
  57. // the next page can be printed.
  58. void OnNewPage();
  59. // This is the only function that can be called in a thread.
  60. void Cancel();
  61. // Returns true if the thread has been started, and not yet stopped.
  62. bool IsRunning() const;
  63. // Posts the given task to be run.
  64. bool PostTask(const base::Location& from_here, const base::Closure& task);
  65. // Signals the thread to exit in the near future.
  66. void StopSoon();
  67. // Signals the thread to exit and returns once the thread has exited.
  68. void Stop();
  69. // Starts the thread.
  70. bool Start();
  71. protected:
  72. // Retrieves the context for testing only.
  73. PrintingContext* printing_context() { return printing_context_.get(); }
  74. private:
  75. // The shared NotificationService service can only be accessed from the UI
  76. // thread, so this class encloses the necessary information to send the
  77. // notification from the right thread. Most NOTIFY_PRINT_JOB_EVENT
  78. // notifications are sent this way, except USER_INIT_DONE, USER_INIT_CANCELED
  79. // and DEFAULT_INIT_DONE. These three are sent through PrintJob::InitDone().
  80. class NotificationTask;
  81. // Renders a page in the printer.
  82. void SpoolPage(PrintedPage* page);
  83. // Closes the job since spooling is done.
  84. void OnDocumentDone();
  85. // Discards the current document, the current page and cancels the printing
  86. // context.
  87. void OnFailure();
  88. // Asks the user for print settings. Must be called on the UI thread.
  89. // Required on Mac and Linux. Windows can display UI from non-main threads,
  90. // but sticks with this for consistency.
  91. void GetSettingsWithUI(int document_page_count,
  92. bool has_selection,
  93. bool is_scripted);
  94. // Called on the UI thread to update the print settings.
  95. void UpdatePrintSettings(std::unique_ptr<base::DictionaryValue> new_settings);
  96. // Reports settings back to owner_.
  97. void GetSettingsDone(PrintingContext::Result result);
  98. // Use the default settings. When using GTK+ or Mac, this can still end up
  99. // displaying a dialog. So this needs to happen from the UI thread on these
  100. // systems.
  101. void UseDefaultSettings();
  102. // set the printer name
  103. void InitWithDeviceName(const base::string16& device_name);
  104. // Printing context delegate.
  105. std::unique_ptr<PrintingContext::Delegate> printing_context_delegate_;
  106. // Information about the printer setting.
  107. std::unique_ptr<PrintingContext> printing_context_;
  108. // The printed document. Only has read-only access.
  109. scoped_refptr<PrintedDocument> document_;
  110. // The print job owning this worker thread. It is guaranteed to outlive this
  111. // object.
  112. PrintJobWorkerOwner* owner_;
  113. // Current page number to print.
  114. PageNumber page_number_;
  115. // Thread to run worker tasks.
  116. base::Thread thread_;
  117. // Tread-safe pointer to task runner of the |thread_|.
  118. scoped_refptr<base::SequencedTaskRunner> task_runner_;
  119. // Used to generate a WeakPtr for callbacks.
  120. base::WeakPtrFactory<PrintJobWorker> weak_factory_;
  121. DISALLOW_COPY_AND_ASSIGN(PrintJobWorker);
  122. };
  123. } // namespace printing
  124. #endif // CHROME_BROWSER_PRINTING_PRINT_JOB_WORKER_H_