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 
5 // IPC messages for printing.
6 #ifndef COMPONENTS_PRINTING_COMMON_PRINT_MESSAGES_H_
7 #define COMPONENTS_PRINTING_COMMON_PRINT_MESSAGES_H_
8 
9 #include <stdint.h>
10 
11 #include <string>
12 #include <vector>
13 
14 #include "base/memory/read_only_shared_memory_region.h"
15 #include "base/values.h"
16 #include "build/build_config.h"
17 #include "components/printing/common/printing_param_traits_macros.h"
18 #include "ipc/ipc_message_macros.h"
19 #include "printing/buildflags/buildflags.h"
20 #include "printing/common/metafile_utils.h"
21 #include "printing/page_range.h"
22 #include "printing/page_size_margins.h"
23 #include "printing/print_job_constants.h"
24 #include "third_party/blink/public/web/web_print_scaling_option.h"
25 #include "ui/accessibility/ax_param_traits.h"
26 #include "ui/gfx/geometry/point.h"
27 #include "ui/gfx/geometry/rect.h"
28 #include "ui/gfx/ipc/geometry/gfx_param_traits.h"
29 #include "ui/gfx/ipc/skia/gfx_skia_param_traits.h"
30 
31 #ifndef INTERNAL_COMPONENTS_PRINTING_COMMON_PRINT_MESSAGES_H_
32 #define INTERNAL_COMPONENTS_PRINTING_COMMON_PRINT_MESSAGES_H_
33 
34 struct PrintMsg_Print_Params {
35   PrintMsg_Print_Params();
36   PrintMsg_Print_Params(const PrintMsg_Print_Params& other);
37   ~PrintMsg_Print_Params();
38 
39   // Resets the members of the struct to 0.
40   void Reset();
41 
42   gfx::Size page_size;
43   gfx::Size content_size;
44   gfx::Rect printable_area;
45   int margin_top;
46   int margin_left;
47   gfx::Size dpi;
48   double scale_factor;
49   bool rasterize_pdf;
50   int document_cookie;
51   bool selection_only;
52   bool supports_alpha_blend;
53   int32_t preview_ui_id;
54   int preview_request_id;
55   bool is_first_request;
56   blink::WebPrintScalingOption print_scaling_option;
57   bool print_to_pdf;
58   bool display_header_footer;
59   base::string16 title;
60   base::string16 url;
61   base::string16 header_template;
62   base::string16 footer_template;
63   bool should_print_backgrounds;
64   printing::SkiaDocumentType printed_doc_type;
65   bool prefer_css_page_size;
66   int pages_per_sheet;
67 };
68 
69 struct PrintMsg_PrintPages_Params {
70   PrintMsg_PrintPages_Params();
71   PrintMsg_PrintPages_Params(const PrintMsg_PrintPages_Params& other);
72   ~PrintMsg_PrintPages_Params();
73 
74   // Resets the members of the struct to 0.
75   void Reset();
76 
77   PrintMsg_Print_Params params;
78   std::vector<int> pages;
79 };
80 
81 #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
82 struct PrintHostMsg_RequestPrintPreview_Params {
83   PrintHostMsg_RequestPrintPreview_Params();
84   ~PrintHostMsg_RequestPrintPreview_Params();
85   bool is_from_arc;
86   bool is_modifiable;
87   bool is_pdf;
88   bool webnode_only;
89   bool has_selection;
90   bool selection_only;
91 };
92 
93 struct PrintHostMsg_PreviewIds {
94   PrintHostMsg_PreviewIds();
95   PrintHostMsg_PreviewIds(int request_id, int ui_id);
96   ~PrintHostMsg_PreviewIds();
97   int request_id;
98   int ui_id;
99 };
100 
101 struct PrintHostMsg_SetOptionsFromDocument_Params {
102   PrintHostMsg_SetOptionsFromDocument_Params();
103   ~PrintHostMsg_SetOptionsFromDocument_Params();
104 
105   bool is_scaling_disabled;
106   int copies;
107   printing::DuplexMode duplex;
108   printing::PageRanges page_ranges;
109 };
110 #endif  // BUILDFLAG(ENABLE_PRINT_PREVIEW)
111 
112 #endif  // INTERNAL_COMPONENTS_PRINTING_COMMON_PRINT_MESSAGES_H_
113 
114 #define IPC_MESSAGE_START PrintMsgStart
115 
116 IPC_ENUM_TRAITS_MAX_VALUE(blink::WebPrintScalingOption,
117                           blink::kWebPrintScalingOptionLast)
118 IPC_ENUM_TRAITS_MAX_VALUE(printing::SkiaDocumentType,
119                           printing::SkiaDocumentType::MAX)
120 
121 // Parameters for a render request.
122 IPC_STRUCT_TRAITS_BEGIN(PrintMsg_Print_Params)
123   // Physical size of the page, including non-printable margins,
124   // in pixels according to dpi.
125   IPC_STRUCT_TRAITS_MEMBER(page_size)
126 
127   // In pixels according to dpi.
128   IPC_STRUCT_TRAITS_MEMBER(content_size)
129 
130   // Physical printable area of the page in pixels according to dpi.
131   IPC_STRUCT_TRAITS_MEMBER(printable_area)
132 
133   // The y-offset of the printable area, in pixels according to dpi.
134   IPC_STRUCT_TRAITS_MEMBER(margin_top)
135 
136   // The x-offset of the printable area, in pixels according to dpi.
137   IPC_STRUCT_TRAITS_MEMBER(margin_left)
138 
139   // Specifies dots per inch in the x and y direction.
140   IPC_STRUCT_TRAITS_MEMBER(dpi)
141 
142   // Specifies the scale factor in percent
143   IPC_STRUCT_TRAITS_MEMBER(scale_factor)
144 
145   // Cookie for the document to ensure correctness.
146   IPC_STRUCT_TRAITS_MEMBER(document_cookie)
147 
148   // Should only print currently selected text.
149   IPC_STRUCT_TRAITS_MEMBER(selection_only)
150 
151   // Does the printer support alpha blending?
152   IPC_STRUCT_TRAITS_MEMBER(supports_alpha_blend)
153 
154   // *** Parameters below are used only for print preview. ***
155 
156   // The print preview ui associated with this request.
157   IPC_STRUCT_TRAITS_MEMBER(preview_ui_id)
158 
159   // The id of the preview request.
160   IPC_STRUCT_TRAITS_MEMBER(preview_request_id)
161 
162   // True if this is the first preview request.
163   IPC_STRUCT_TRAITS_MEMBER(is_first_request)
164 
165   // Specifies the page scaling option for preview printing.
166   IPC_STRUCT_TRAITS_MEMBER(print_scaling_option)
167 
168   // True if print to pdf is requested.
169   IPC_STRUCT_TRAITS_MEMBER(print_to_pdf)
170 
171   // Specifies if the header and footer should be rendered.
172   IPC_STRUCT_TRAITS_MEMBER(display_header_footer)
173 
174   // Title string to be printed as header if requested by the user.
175   IPC_STRUCT_TRAITS_MEMBER(title)
176 
177   // URL string to be printed as footer if requested by the user.
178   IPC_STRUCT_TRAITS_MEMBER(url)
179 
180   // HTML template to use as a print header.
181   IPC_STRUCT_TRAITS_MEMBER(header_template)
182 
183   // HTML template to use as a print footer.
184   IPC_STRUCT_TRAITS_MEMBER(footer_template)
185 
186   // Whether to rasterize a PDF for printing
187   IPC_STRUCT_TRAITS_MEMBER(rasterize_pdf)
188 
189   // True if print backgrounds is requested by the user.
190   IPC_STRUCT_TRAITS_MEMBER(should_print_backgrounds)
191 
192   // The document type of printed page(s) from render.
193   IPC_STRUCT_TRAITS_MEMBER(printed_doc_type)
194 
195   // True if page size defined by css should be preferred.
196   IPC_STRUCT_TRAITS_MEMBER(prefer_css_page_size)
197 
198   // Number of pages per sheet.  This parameter is for N-up mode.
199   // Defaults to 1 if the feature is disabled, and some number greater
200   // than 1 otherwise.  See printing::NupParameters for supported values.
201   IPC_STRUCT_TRAITS_MEMBER(pages_per_sheet)
202 IPC_STRUCT_TRAITS_END()
203 
204 IPC_STRUCT_TRAITS_BEGIN(printing::PageRange)
205   IPC_STRUCT_TRAITS_MEMBER(from)
206   IPC_STRUCT_TRAITS_MEMBER(to)
207 IPC_STRUCT_TRAITS_END()
208 
209 #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
210 IPC_STRUCT_TRAITS_BEGIN(PrintHostMsg_RequestPrintPreview_Params)
211   IPC_STRUCT_TRAITS_MEMBER(is_from_arc)
212   IPC_STRUCT_TRAITS_MEMBER(is_modifiable)
213   IPC_STRUCT_TRAITS_MEMBER(is_pdf)
214   IPC_STRUCT_TRAITS_MEMBER(webnode_only)
215   IPC_STRUCT_TRAITS_MEMBER(has_selection)
216   IPC_STRUCT_TRAITS_MEMBER(selection_only)
217 IPC_STRUCT_TRAITS_END()
218 
219 IPC_STRUCT_TRAITS_BEGIN(PrintHostMsg_PreviewIds)
220   IPC_STRUCT_TRAITS_MEMBER(request_id)
221   IPC_STRUCT_TRAITS_MEMBER(ui_id)
222 IPC_STRUCT_TRAITS_END()
223 
224 IPC_STRUCT_TRAITS_BEGIN(PrintHostMsg_SetOptionsFromDocument_Params)
225   // Specifies whether print scaling is enabled or not.
226   IPC_STRUCT_TRAITS_MEMBER(is_scaling_disabled)
227 
228   // Specifies number of copies to be printed.
229   IPC_STRUCT_TRAITS_MEMBER(copies)
230 
231   // Specifies paper handling option.
232   IPC_STRUCT_TRAITS_MEMBER(duplex)
233 
234   // Specifies page range to be printed.
235   IPC_STRUCT_TRAITS_MEMBER(page_ranges)
236 IPC_STRUCT_TRAITS_END()
237 #endif  // BUILDFLAG(ENABLE_PRINT_PREVIEW)
238 
239 IPC_STRUCT_TRAITS_BEGIN(printing::PageSizeMargins)
240   IPC_STRUCT_TRAITS_MEMBER(content_width)
241   IPC_STRUCT_TRAITS_MEMBER(content_height)
242   IPC_STRUCT_TRAITS_MEMBER(margin_left)
243   IPC_STRUCT_TRAITS_MEMBER(margin_right)
244   IPC_STRUCT_TRAITS_MEMBER(margin_top)
245   IPC_STRUCT_TRAITS_MEMBER(margin_bottom)
246 IPC_STRUCT_TRAITS_END()
247 
248 IPC_STRUCT_TRAITS_BEGIN(PrintMsg_PrintPages_Params)
249   // Parameters to render the page as a printed page. It must always be the same
250   // value for all the document.
251   IPC_STRUCT_TRAITS_MEMBER(params)
252 
253   // If empty, this means a request to render all the printed pages.
254   IPC_STRUCT_TRAITS_MEMBER(pages)
255 IPC_STRUCT_TRAITS_END()
256 
257 // Holds the printed content information.
258 // The printed content is in shared memory, and passed as a region.
259 // A map on out-of-process subframe contents is also included so the printed
260 // content can be composited as needed.
261 IPC_STRUCT_BEGIN(PrintHostMsg_DidPrintContent_Params)
262   // A shared memory region for the metafile data.
263   IPC_STRUCT_MEMBER(base::ReadOnlySharedMemoryRegion, metafile_data_region)
264 
265   // Content id to render frame proxy id mapping for out-of-process subframes.
266   IPC_STRUCT_MEMBER(printing::ContentToProxyIdMap, subframe_content_info)
267 IPC_STRUCT_END()
268 
269 #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
270 // Parameters to describe the to-be-rendered preview document.
271 IPC_STRUCT_BEGIN(PrintHostMsg_DidStartPreview_Params)
272   // Total page count for the rendered preview. (Not the number of pages the
273   // user selected to print.)
274   IPC_STRUCT_MEMBER(int, page_count)
275 
276   // The list of 0-based page numbers that will be rendered.
277   IPC_STRUCT_MEMBER(std::vector<int>, pages_to_render)
278 
279   // number of pages per sheet and should be greater or equal to 1.
280   IPC_STRUCT_MEMBER(int, pages_per_sheet)
281 
282   // Physical size of the page, including non-printable margins.
283   IPC_STRUCT_MEMBER(gfx::Size, page_size)
284 
285   // Scaling % to fit to page
286   IPC_STRUCT_MEMBER(int, fit_to_page_scaling)
287 IPC_STRUCT_END()
288 
289 // Parameters to describe a rendered preview page.
290 IPC_STRUCT_BEGIN(PrintHostMsg_DidPreviewPage_Params)
291   // Page's content including metafile data and subframe info.
292   IPC_STRUCT_MEMBER(PrintHostMsg_DidPrintContent_Params, content)
293 
294   // |page_number| is zero-based and should not be negative.
295   IPC_STRUCT_MEMBER(int, page_number)
296 
297   // Cookie for the document to ensure correctness.
298   IPC_STRUCT_MEMBER(int, document_cookie)
299 IPC_STRUCT_END()
300 
301 // Parameters to describe the final rendered preview document.
302 IPC_STRUCT_BEGIN(PrintHostMsg_DidPreviewDocument_Params)
303   // Document's content including metafile data and subframe info.
304   IPC_STRUCT_MEMBER(PrintHostMsg_DidPrintContent_Params, content)
305 
306   // Cookie for the document to ensure correctness.
307   IPC_STRUCT_MEMBER(int, document_cookie)
308 
309   // Store the expected pages count.
310   IPC_STRUCT_MEMBER(int, expected_pages_count)
311 IPC_STRUCT_END()
312 #endif  // BUILDFLAG(ENABLE_PRINT_PREVIEW)
313 
314 // Parameters to describe a rendered page.
315 IPC_STRUCT_BEGIN(PrintHostMsg_DidPrintDocument_Params)
316   // Document's content including metafile data and subframe info.
317   IPC_STRUCT_MEMBER(PrintHostMsg_DidPrintContent_Params, content)
318 
319   // Cookie for the document to ensure correctness.
320   IPC_STRUCT_MEMBER(int, document_cookie)
321 
322   // The size of the page the page author specified.
323   IPC_STRUCT_MEMBER(gfx::Size, page_size)
324 
325   // The printable area the page author specified.
326   IPC_STRUCT_MEMBER(gfx::Rect, content_area)
327 
328   // The physical offsets of the printer in DPI. Used for PS printing.
329   IPC_STRUCT_MEMBER(gfx::Point, physical_offsets)
330 IPC_STRUCT_END()
331 
332 // TODO(dgn) Rename *ScriptedPrint messages because they are not called only
333 //           from scripts.
334 // Parameters for the IPC message ViewHostMsg_ScriptedPrint
335 IPC_STRUCT_BEGIN(PrintHostMsg_ScriptedPrint_Params)
336   IPC_STRUCT_MEMBER(int, cookie)
337   IPC_STRUCT_MEMBER(int, expected_pages_count)
338   IPC_STRUCT_MEMBER(bool, has_selection)
339   IPC_STRUCT_MEMBER(bool, is_scripted)
340   IPC_STRUCT_MEMBER(bool, is_modifiable)
341   IPC_STRUCT_MEMBER(printing::MarginType, margin_type)
342 IPC_STRUCT_END()
343 
344 
345 // Messages sent from the renderer to the browser.
346 
347 // Tells the browser that the renderer is done calculating the number of
348 // rendered pages according to the specified settings.
349 IPC_MESSAGE_ROUTED2(PrintHostMsg_DidGetPrintedPagesCount,
350                     int /* rendered document cookie */,
351                     int /* number of rendered pages */)
352 
353 // Sends the document cookie of the current printer query to the browser.
354 IPC_MESSAGE_ROUTED1(PrintHostMsg_DidGetDocumentCookie,
355                     int /* rendered document cookie */)
356 
357 // Tells the browser that the print dialog has been shown.
358 IPC_MESSAGE_ROUTED0(PrintHostMsg_DidShowPrintDialog)
359 
360 // Sends back to the browser the rendered document that was requested by a
361 // PrintMsg_PrintPages message or from scripted printing. The memory handle in
362 // this message is already valid in the browser process. Waits until the
363 // document is complete ready before replying.
364 IPC_SYNC_MESSAGE_ROUTED1_1(PrintHostMsg_DidPrintDocument,
365                            PrintHostMsg_DidPrintDocument_Params
366                            /* page content */,
367                            bool /* completed */)
368 
369 // Sends back to the browser the rendered subframe content that was
370 // requested by a PrintMsg_PrintFrameContent message.
371 IPC_MESSAGE_ROUTED2(PrintHostMsg_DidPrintFrameContent,
372                     int /* rendered document cookie */,
373                     PrintHostMsg_DidPrintContent_Params)
374 
375 #if BUILDFLAG(ENABLE_TAGGED_PDF)
376 // Sends the accessibility tree corresponding to a document being
377 // printed, needed for a tagged (accessible) PDF.
378 IPC_MESSAGE_ROUTED2(PrintHostMsg_AccessibilityTree,
379                     int /* rendered document cookie */,
380                     ui::AXTreeUpdate)
381 #endif
382 
383 // The renderer wants to know the default print settings.
384 IPC_SYNC_MESSAGE_ROUTED0_1(PrintHostMsg_GetDefaultPrintSettings,
385                            PrintMsg_Print_Params /* default_settings */)
386 
387 // The renderer wants to update the current print settings with new
388 // |job_settings|.
389 IPC_SYNC_MESSAGE_ROUTED2_2(PrintHostMsg_UpdatePrintSettings,
390                            int /* document_cookie */,
391                            base::DictionaryValue /* job_settings */,
392                            PrintMsg_PrintPages_Params /* current_settings */,
393                            bool /* canceled */)
394 
395 // It's the renderer that controls the printing process when it is generated
396 // by javascript. This step is about showing UI to the user to select the
397 // final print settings. The output parameter is the same as
398 // PrintMsg_PrintPages which is executed implicitly.
399 IPC_SYNC_MESSAGE_ROUTED1_1(PrintHostMsg_ScriptedPrint,
400                            PrintHostMsg_ScriptedPrint_Params,
401                            PrintMsg_PrintPages_Params
402                                /* settings chosen by the user*/)
403 
404 #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
405 // Asks the browser to do print preview.
406 IPC_MESSAGE_ROUTED1(PrintHostMsg_RequestPrintPreview,
407                     PrintHostMsg_RequestPrintPreview_Params /* params */)
408 
409 // Notify the browser the about the to-be-rendered print preview document.
410 IPC_MESSAGE_ROUTED2(PrintHostMsg_DidStartPreview,
411                     PrintHostMsg_DidStartPreview_Params /* params */,
412                     PrintHostMsg_PreviewIds /* ids */)
413 
414 // Notify the browser of preparing to print the document, for cases where
415 // the document will be collected from the individual pages instead of being
416 // provided by an extra metafile at end containing all pages.
417 IPC_MESSAGE_ROUTED2(PrintHostMsg_DidPrepareDocumentForPreview,
418                     int /* document_cookie */,
419                     PrintHostMsg_PreviewIds /* ids */)
420 
421 // Notify the browser of the default page layout according to the currently
422 // selected printer and page size.
423 // |printable_area_in_points| Specifies the printable area in points.
424 // |has_custom_page_size_style| is true when the printing frame has a custom
425 // page size css otherwise false.
426 IPC_MESSAGE_ROUTED4(PrintHostMsg_DidGetDefaultPageLayout,
427                     printing::PageSizeMargins /* page layout in points */,
428                     gfx::Rect /* printable area in points */,
429                     bool /* has custom page size style */,
430                     PrintHostMsg_PreviewIds /* ids */)
431 
432 // Notify the browser a print preview page has been rendered.
433 IPC_MESSAGE_ROUTED2(PrintHostMsg_DidPreviewPage,
434                     PrintHostMsg_DidPreviewPage_Params /* params */,
435                     PrintHostMsg_PreviewIds /* ids */)
436 
437 // Asks the browser whether the print preview has been cancelled.
438 IPC_SYNC_MESSAGE_ROUTED1_1(PrintHostMsg_CheckForCancel,
439                            PrintHostMsg_PreviewIds /* ids */,
440                            bool /* print preview cancelled */)
441 
442 // Sends back to the browser the complete rendered document (non-draft mode,
443 // used for printing) that was requested by a PrintMsg_PrintPreview message.
444 // The memory handle in this message is already valid in the browser process.
445 IPC_MESSAGE_ROUTED2(PrintHostMsg_MetafileReadyForPrinting,
446                     PrintHostMsg_DidPreviewDocument_Params /* params */,
447                     PrintHostMsg_PreviewIds /* ids */)
448 #endif  // BUILDFLAG(ENABLE_PRINT_PREVIEW)
449 
450 // This is sent when there are invalid printer settings.
451 IPC_MESSAGE_ROUTED0(PrintHostMsg_ShowInvalidPrinterSettingsError)
452 
453 // Tell the browser printing failed.
454 IPC_MESSAGE_ROUTED1(PrintHostMsg_PrintingFailed,
455                     int /* document cookie */)
456 
457 #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
458 // Tell the browser print preview failed.
459 IPC_MESSAGE_ROUTED2(PrintHostMsg_PrintPreviewFailed,
460                     int /* document cookie */,
461                     PrintHostMsg_PreviewIds /* ids */)
462 
463 // Tell the browser print preview was cancelled.
464 IPC_MESSAGE_ROUTED2(PrintHostMsg_PrintPreviewCancelled,
465                     int /* document cookie */,
466                     PrintHostMsg_PreviewIds /* ids */)
467 
468 // Tell the browser print preview found the selected printer has invalid
469 // settings (which typically caused by disconnected network printer or printer
470 // driver is bogus).
471 IPC_MESSAGE_ROUTED2(PrintHostMsg_PrintPreviewInvalidPrinterSettings,
472                     int /* document cookie */,
473                     PrintHostMsg_PreviewIds /* ids */)
474 
475 // Run a nested run loop in the renderer until print preview for
476 // window.print() finishes.
477 IPC_SYNC_MESSAGE_ROUTED0_0(PrintHostMsg_SetupScriptedPrintPreview)
478 
479 // Tell the browser to show the print preview, when the document is sufficiently
480 // loaded such that the renderer can determine whether it is modifiable or not.
481 IPC_MESSAGE_ROUTED1(PrintHostMsg_ShowScriptedPrintPreview,
482                     bool /* is_modifiable */)
483 
484 // Notify the browser to set print presets based on source PDF document.
485 IPC_MESSAGE_ROUTED2(PrintHostMsg_SetOptionsFromDocument,
486                     PrintHostMsg_SetOptionsFromDocument_Params /* params */,
487                     PrintHostMsg_PreviewIds /* ids */)
488 #endif  // BUILDFLAG(ENABLE_PRINT_PREVIEW)
489 
490 #endif  // COMPONENTS_PRINTING_COMMON_PRINT_MESSAGES_H_
491