printing_context.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // Copyright (c) 2011 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 PRINTING_PRINTING_CONTEXT_H_
  5. #define PRINTING_PRINTING_CONTEXT_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/callback.h"
  9. #include "base/memory/raw_ptr.h"
  10. #include "base/values.h"
  11. #include "build/build_config.h"
  12. #include "printing/buildflags/buildflags.h"
  13. #include "printing/mojom/print.mojom.h"
  14. #include "printing/native_drawing_context.h"
  15. #include "printing/print_settings.h"
  16. #include "ui/gfx/native_widget_types.h"
  17. namespace printing {
  18. class MetafilePlayer;
  19. class PrintingContextFactoryForTest;
  20. #if BUILDFLAG(IS_WIN)
  21. class PageSetup;
  22. class PrintedPage;
  23. #endif
  24. // An abstraction of a printer context, implemented by objects that describe the
  25. // user selected printing context. This includes the OS-dependent UI to ask the
  26. // user about the print settings. Concrete implementations directly talk to the
  27. // printer and manage the document and page breaks.
  28. class COMPONENT_EXPORT(PRINTING) PrintingContext {
  29. public:
  30. // Printing context delegate.
  31. class Delegate {
  32. public:
  33. Delegate() {}
  34. virtual ~Delegate() {}
  35. // Returns parent view to use for modal dialogs.
  36. virtual gfx::NativeView GetParentView() = 0;
  37. // Returns application locale.
  38. virtual std::string GetAppLocale() = 0;
  39. };
  40. struct PrinterSettings {
  41. #if BUILDFLAG(IS_MAC)
  42. // True if the to-be-printed PDF is going to be opened in external
  43. // preview. Used by macOS only to open Preview.app.
  44. bool external_preview;
  45. #endif
  46. // Whether to show the system dialog.
  47. bool show_system_dialog;
  48. #if BUILDFLAG(IS_WIN)
  49. // If showing the system dialog, the number of pages in the to-be-printed
  50. // PDF. Only used on Windows.
  51. int page_count;
  52. #endif
  53. };
  54. PrintingContext(const PrintingContext&) = delete;
  55. PrintingContext& operator=(const PrintingContext&) = delete;
  56. virtual ~PrintingContext();
  57. // Callback of AskUserForSettings, used to notify the PrintJobWorker when
  58. // print settings are available.
  59. using PrintSettingsCallback = base::OnceCallback<void(mojom::ResultCode)>;
  60. // Asks the user what printer and format should be used to print. Updates the
  61. // context with the select device settings. The result of the call is returned
  62. // in the callback. This is necessary for Linux, which only has an
  63. // asynchronous printing API.
  64. // On Android, when `is_scripted` is true, calling it initiates a full
  65. // printing flow from the framework's PrintManager.
  66. // (see https://codereview.chromium.org/740983002/)
  67. virtual void AskUserForSettings(int max_pages,
  68. bool has_selection,
  69. bool is_scripted,
  70. PrintSettingsCallback callback) = 0;
  71. // Selects the user's default printer and format. Updates the context with the
  72. // default device settings.
  73. virtual mojom::ResultCode UseDefaultSettings() = 0;
  74. // Updates the context with PDF printer settings. The PDF settings are
  75. // guaranteed to be valid.
  76. void UsePdfSettings();
  77. // Returns paper size to be used for PDF or Cloud Print in device units.
  78. virtual gfx::Size GetPdfPaperSizeDeviceUnits() = 0;
  79. // Updates printer settings.
  80. virtual mojom::ResultCode UpdatePrinterSettings(
  81. const PrinterSettings& printer_settings) = 0;
  82. // Updates Print Settings. `job_settings` contains all print job settings
  83. // information.
  84. mojom::ResultCode UpdatePrintSettings(base::Value::Dict job_settings);
  85. #if BUILDFLAG(IS_CHROMEOS)
  86. // Updates Print Settings.
  87. mojom::ResultCode UpdatePrintSettingsFromPOD(
  88. std::unique_ptr<PrintSettings> job_settings);
  89. #endif
  90. // Applies the print settings to this context. Intended to be used only by
  91. // the Print Backend service process.
  92. void ApplyPrintSettings(const PrintSettings& settings);
  93. // Does platform specific setup of the printer before the printing. Signal the
  94. // printer that a document is about to be spooled.
  95. // Warning: This function enters a message loop. That may cause side effects
  96. // like IPC message processing! Some printers have side-effects on this call
  97. // like virtual printers that ask the user for the path of the saved document;
  98. // for example a PDF printer.
  99. virtual mojom::ResultCode NewDocument(
  100. const std::u16string& document_name) = 0;
  101. #if BUILDFLAG(IS_WIN)
  102. // Renders a page.
  103. virtual mojom::ResultCode RenderPage(const PrintedPage& page,
  104. const PageSetup& page_setup) = 0;
  105. #endif
  106. // Prints the document contained in `metafile`.
  107. virtual mojom::ResultCode PrintDocument(const MetafilePlayer& metafile,
  108. const PrintSettings& settings,
  109. uint32_t num_pages) = 0;
  110. // Closes the printing job. After this call the object is ready to start a new
  111. // document.
  112. virtual mojom::ResultCode DocumentDone() = 0;
  113. // Cancels printing. Can be used in a multi-threaded context. Takes effect
  114. // immediately.
  115. virtual void Cancel() = 0;
  116. // Releases the native printing context.
  117. virtual void ReleaseContext() = 0;
  118. // Returns the native context used to print.
  119. virtual printing::NativeDrawingContext context() const = 0;
  120. #if BUILDFLAG(IS_WIN)
  121. // Initializes with predefined settings.
  122. virtual mojom::ResultCode InitWithSettingsForTest(
  123. std::unique_ptr<PrintSettings> settings) = 0;
  124. #endif
  125. // Creates an instance of this object.
  126. static std::unique_ptr<PrintingContext> Create(Delegate* delegate,
  127. bool skip_system_calls);
  128. // Test method for generating printing contexts for testing. This overrides
  129. // the platform-specific implementations of CreateImpl().
  130. static void SetPrintingContextFactoryForTest(
  131. PrintingContextFactoryForTest* factory);
  132. void set_margin_type(mojom::MarginType type);
  133. void set_is_modifiable(bool is_modifiable);
  134. const PrintSettings& settings() const;
  135. std::unique_ptr<PrintSettings> TakeAndResetSettings();
  136. bool PrintingAborted() const { return abort_printing_; }
  137. int job_id() const { return job_id_; }
  138. protected:
  139. explicit PrintingContext(Delegate* delegate);
  140. // Creates an instance of this object. Implementers of this interface should
  141. // implement this method to create an object of their implementation.
  142. static std::unique_ptr<PrintingContext> CreateImpl(Delegate* delegate,
  143. bool skip_system_calls);
  144. // Reinitializes the settings for object reuse.
  145. void ResetSettings();
  146. // Determine if system calls should be skipped by this instance.
  147. bool skip_system_calls() const {
  148. #if BUILDFLAG(ENABLE_OOP_PRINTING)
  149. return skip_system_calls_;
  150. #else
  151. return false;
  152. #endif
  153. }
  154. #if BUILDFLAG(ENABLE_OOP_PRINTING)
  155. // Make the one-way adjustment to have all system calls skipped by this
  156. // `PrintingContext` instance.
  157. void set_skip_system_calls() { skip_system_calls_ = true; }
  158. #endif
  159. // Does bookkeeping when an error occurs.
  160. virtual mojom::ResultCode OnError();
  161. void SetDefaultPrintableAreaForVirtualPrinters();
  162. // Complete print context settings.
  163. std::unique_ptr<PrintSettings> settings_;
  164. // Printing context delegate.
  165. const raw_ptr<Delegate> delegate_;
  166. // Is a print job being done.
  167. volatile bool in_print_job_;
  168. // Did the user cancel the print job.
  169. volatile bool abort_printing_;
  170. // The job id for the current job. The value is 0 if no jobs are active.
  171. int job_id_;
  172. private:
  173. #if BUILDFLAG(ENABLE_OOP_PRINTING)
  174. // If this instance of PrintingContext should skip making any system calls
  175. // to the operating system.
  176. bool skip_system_calls_ = false;
  177. #endif
  178. };
  179. } // namespace printing
  180. #endif // PRINTING_PRINTING_CONTEXT_H_