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