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