metafile.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 PRINTING_METAFILE_H_
  5. #define PRINTING_METAFILE_H_
  6. #include <stdint.h>
  7. #include <vector>
  8. #include "base/component_export.h"
  9. #include "base/containers/span.h"
  10. #include "base/memory/read_only_shared_memory_region.h"
  11. #include "build/build_config.h"
  12. #include "printing/mojom/print.mojom-forward.h"
  13. #include "printing/native_drawing_context.h"
  14. #if BUILDFLAG(IS_WIN)
  15. #include <windows.h>
  16. #elif BUILDFLAG(IS_MAC)
  17. #include <ApplicationServices/ApplicationServices.h>
  18. #include <CoreFoundation/CoreFoundation.h>
  19. #include "base/mac/scoped_cftyperef.h"
  20. #endif
  21. namespace base {
  22. class File;
  23. }
  24. namespace gfx {
  25. class Rect;
  26. class Size;
  27. }
  28. namespace printing {
  29. // This class plays metafiles from data stream (usually PDF or EMF).
  30. class COMPONENT_EXPORT(PRINTING_METAFILE) MetafilePlayer {
  31. public:
  32. MetafilePlayer();
  33. MetafilePlayer(const MetafilePlayer&) = delete;
  34. MetafilePlayer& operator=(const MetafilePlayer&) = delete;
  35. virtual ~MetafilePlayer();
  36. #if BUILDFLAG(IS_WIN)
  37. // The slow version of Playback(). It enumerates all the records and play them
  38. // back in the HDC. The trick is that it skip over the records known to have
  39. // issue with some printers. See Emf::Record::SafePlayback implementation for
  40. // details.
  41. virtual bool SafePlayback(printing::NativeDrawingContext hdc) const = 0;
  42. #elif BUILDFLAG(IS_MAC)
  43. // Renders the given page into `rect` in the given context.
  44. // Pages use a 1-based index. `autorotate` determines whether the source PDF
  45. // should be autorotated to fit on the destination page. `fit_to_page`
  46. // determines whether the source PDF should be scaled to fit on the
  47. // destination page.
  48. virtual bool RenderPage(unsigned int page_number,
  49. printing::NativeDrawingContext context,
  50. const CGRect& rect,
  51. bool autorotate,
  52. bool fit_to_page) const = 0;
  53. #endif // BUILDFLAG(IS_WIN)
  54. // Populates the buffer with the underlying data. This function should ONLY be
  55. // called after the metafile is closed. Returns true if writing succeeded.
  56. virtual bool GetDataAsVector(std::vector<char>* buffer) const = 0;
  57. // Generates a read-only shared memory region for the underlying data. This
  58. // function should ONLY be called after the metafile is closed. The returned
  59. // region will be invalid if there is an error trying to generate the mapping.
  60. virtual base::MappedReadOnlyRegion GetDataAsSharedMemoryRegion() const = 0;
  61. // Determine if a copy of the data should be explicitly made before operating
  62. // on metafile data. For security purposes it is important to not operate
  63. // directly on the metafile data shared across processes, but instead work on
  64. // a local copy made of such data. This query determines if such a copy needs
  65. // to be made by the caller, since not all implementations are required to
  66. // automatically do so.
  67. // TODO(crbug.com/1135729) Eliminate concern about making a copy when the
  68. // shared memory can't be written by the sender.
  69. virtual bool ShouldCopySharedMemoryRegionData() const = 0;
  70. // Identifies the type of encapsulated.
  71. virtual mojom::MetafileDataType GetDataType() const = 0;
  72. #if BUILDFLAG(IS_ANDROID)
  73. // Similar to bool SaveTo(base::File* file) const, but write the data to the
  74. // file descriptor directly. This is because Android doesn't allow file
  75. // ownership exchange. This function should ONLY be called after the metafile
  76. // is closed. Returns true if writing succeeded.
  77. virtual bool SaveToFileDescriptor(int fd) const = 0;
  78. #else
  79. // Saves the underlying data to the given file. This function should ONLY be
  80. // called after the metafile is closed. Returns true if writing succeeded.
  81. virtual bool SaveTo(base::File* file) const = 0;
  82. #endif // BUILDFLAG(IS_ANDROID)
  83. };
  84. // This class creates a graphics context that renders into a data stream
  85. // (usually PDF or EMF).
  86. class COMPONENT_EXPORT(PRINTING_METAFILE) Metafile : public MetafilePlayer {
  87. public:
  88. Metafile();
  89. Metafile(const Metafile&) = delete;
  90. Metafile& operator=(const Metafile&) = delete;
  91. ~Metafile() override;
  92. // Initializes a fresh new metafile for rendering. Returns false on failure.
  93. // Note: It should only be called from within the renderer process to allocate
  94. // rendering resources.
  95. virtual bool Init() = 0;
  96. // Initializes the metafile with `data`. Returns true on success.
  97. // Note: It should only be called from within the browser process.
  98. virtual bool InitFromData(base::span<const uint8_t> data) = 0;
  99. // Prepares a context for rendering a new page with the given `page_size`,
  100. // `content_area` and a `scale_factor` to use for the drawing. The units are
  101. // in points (=1/72 in).
  102. virtual void StartPage(const gfx::Size& page_size,
  103. const gfx::Rect& content_area,
  104. float scale_factor,
  105. mojom::PageOrientation page_orientation) = 0;
  106. // Closes the current page and destroys the context used in rendering that
  107. // page. The results of current page will be appended into the underlying
  108. // data stream. Returns true on success.
  109. virtual bool FinishPage() = 0;
  110. // Closes the metafile. No further rendering is allowed (the current page
  111. // is implicitly closed).
  112. virtual bool FinishDocument() = 0;
  113. // Returns the size of the underlying data stream. Only valid after Close()
  114. // has been called.
  115. virtual uint32_t GetDataSize() const = 0;
  116. // Copies the first `dst_buffer_size` bytes of the underlying data stream into
  117. // `dst_buffer`. This function should ONLY be called after Close() is invoked.
  118. // Returns true if the copy succeeds.
  119. virtual bool GetData(void* dst_buffer, uint32_t dst_buffer_size) const = 0;
  120. virtual gfx::Rect GetPageBounds(unsigned int page_number) const = 0;
  121. virtual unsigned int GetPageCount() const = 0;
  122. virtual printing::NativeDrawingContext context() const = 0;
  123. #if BUILDFLAG(IS_WIN)
  124. // "Plays" the EMF buffer in a HDC. It is the same effect as calling the
  125. // original GDI function that were called when recording the EMF. `rect` is in
  126. // "logical units" and is optional. If `rect` is NULL, the natural EMF bounds
  127. // are used.
  128. // Note: Windows has been known to have stack buffer overflow in its GDI
  129. // functions, whether used directly or indirectly through precompiled EMF
  130. // data. We have to accept the risk here. Since it is used only for printing,
  131. // it requires user intervention.
  132. virtual bool Playback(printing::NativeDrawingContext hdc,
  133. const RECT* rect) const = 0;
  134. #endif // BUILDFLAG(IS_WIN)
  135. // MetfilePlayer implementation.
  136. bool GetDataAsVector(std::vector<char>* buffer) const override;
  137. base::MappedReadOnlyRegion GetDataAsSharedMemoryRegion() const override;
  138. #if !BUILDFLAG(IS_ANDROID)
  139. bool SaveTo(base::File* file) const override;
  140. #endif // !BUILDFLAG(IS_ANDROID)
  141. };
  142. } // namespace printing
  143. #endif // PRINTING_METAFILE_H_