[M120 Migration][VD] Remove accessing oom_score_adj in zygote process
[platform/framework/web/chromium-efl.git] / printing / emf_win.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_EMF_WIN_H_
6 #define PRINTING_EMF_WIN_H_
7
8 #include <stddef.h>
9 #include <stdint.h>
10 #include <windows.h>
11
12 #include <memory>
13 #include <vector>
14
15 #include "base/compiler_specific.h"
16 #include "base/component_export.h"
17 #include "base/gtest_prod_util.h"
18 #include "base/memory/raw_ptr.h"
19 #include "printing/metafile.h"
20
21 namespace base {
22 class FilePath;
23 }
24
25 namespace gfx {
26 class Rect;
27 class Size;
28 }  // namespace gfx
29
30 namespace printing {
31
32 // Simple wrapper class that manage an EMF data stream and its virtual HDC.
33 class COMPONENT_EXPORT(PRINTING_METAFILE) Emf : public Metafile {
34  public:
35   class Record;
36   class Enumerator;
37   struct EnumerationContext;
38
39   // Generates a virtual HDC that will record every GDI commands and compile
40   // it in a EMF data stream.
41   Emf();
42   Emf(const Emf&) = delete;
43   Emf& operator=(const Emf&) = delete;
44   ~Emf() override;
45
46   // Closes metafile.
47   void Close();
48
49   // Generates a new metafile that will record every GDI command, and will
50   // be saved to `metafile_path`.
51   bool InitToFile(const base::FilePath& metafile_path);
52
53   // Initializes the Emf with the data in `metafile_path`.
54   bool InitFromFile(const base::FilePath& metafile_path);
55
56   // Metafile methods.
57   bool Init() override;
58   bool InitFromData(base::span<const uint8_t> data) override;
59
60   // Inserts a custom GDICOMMENT records indicating StartPage/EndPage calls
61   // (since StartPage and EndPage do not work in a metafile DC). Only valid
62   // when hdc_ is non-NULL. `page_size`, `content_area`, and `scale_factor` are
63   // ignored.
64   void StartPage(const gfx::Size& page_size,
65                  const gfx::Rect& content_area,
66                  float scale_factor,
67                  mojom::PageOrientation page_orientation) override;
68   bool FinishPage() override;
69   bool FinishDocument() override;
70
71   uint32_t GetDataSize() const override;
72   bool GetData(void* buffer, uint32_t size) const override;
73   bool ShouldCopySharedMemoryRegionData() const override;
74   mojom::MetafileDataType GetDataType() const override;
75
76   // Should be passed to Playback to keep the exact same size.
77   gfx::Rect GetPageBounds(unsigned int page_number) const override;
78
79   unsigned int GetPageCount() const override;
80   HDC context() const override;
81   bool Playback(HDC hdc, const RECT* rect) const override;
82   bool SafePlayback(HDC hdc) const override;
83
84   HENHMETAFILE emf() const { return emf_; }
85
86  private:
87   FRIEND_TEST_ALL_PREFIXES(EmfTest, DC);
88   FRIEND_TEST_ALL_PREFIXES(EmfPrintingTest, PageBreak);
89   FRIEND_TEST_ALL_PREFIXES(EmfTest, FileBackedEmf);
90
91   // Playbacks safely one EMF record.
92   static int CALLBACK SafePlaybackProc(HDC hdc,
93                                        HANDLETABLE* handle_table,
94                                        const ENHMETARECORD* record,
95                                        int objects_count,
96                                        LPARAM param);
97
98   // Compiled EMF data handle.
99   HENHMETAFILE emf_;
100
101   // Valid when generating EMF data through a virtual HDC.
102   HDC hdc_;
103 };
104
105 struct Emf::EnumerationContext {
106   EnumerationContext();
107
108   raw_ptr<HANDLETABLE> handle_table;
109   int objects_count;
110   HDC hdc;
111   raw_ptr<const XFORM> base_matrix;
112   int dc_on_page_start;
113 };
114
115 // One EMF record. It keeps pointers to the EMF buffer held by Emf::emf_.
116 // The entries become invalid once Emf::CloseEmf() is called.
117 class COMPONENT_EXPORT(PRINTING_METAFILE) Emf::Record {
118  public:
119   // Plays the record.
120   bool Play(EnumerationContext* context) const;
121
122   // Plays the record working around quirks with SetLayout,
123   // SetWorldTransform and ModifyWorldTransform. See implementation for details.
124   bool SafePlayback(EnumerationContext* context) const;
125
126   // Access the underlying EMF record.
127   const ENHMETARECORD* record() const { return record_; }
128
129  protected:
130   explicit Record(const ENHMETARECORD* record);
131
132  private:
133   friend class Emf;
134   friend class Enumerator;
135   raw_ptr<const ENHMETARECORD> record_;
136 };
137
138 // Retrieves individual records out of a Emf buffer. The main use is to skip
139 // over records that are unsupported on a specific printer or to play back
140 // only a part of an EMF buffer.
141 class COMPONENT_EXPORT(PRINTING_METAFILE) Emf::Enumerator {
142  public:
143   // Iterator type used for iterating the records.
144   typedef std::vector<Record>::const_iterator const_iterator;
145
146   // Enumerates the records at construction time. `hdc` and `rect` are
147   // both optional at the same time or must both be valid.
148   // Warning: `emf` must be kept valid for the time this object is alive.
149   Enumerator(const Emf& emf, HDC hdc, const RECT* rect);
150   Enumerator(const Enumerator&) = delete;
151   Enumerator& operator=(const Enumerator&) = delete;
152   ~Enumerator();
153
154   // Retrieves the first Record.
155   const_iterator begin() const;
156
157   // Retrieves the end of the array.
158   const_iterator end() const;
159
160  private:
161   FRIEND_TEST_ALL_PREFIXES(EmfPrintingTest, Enumerate);
162
163   // Processes one EMF record and saves it in the items_ array.
164   static int CALLBACK EnhMetaFileProc(HDC hdc,
165                                       HANDLETABLE* handle_table,
166                                       const ENHMETARECORD* record,
167                                       int objects_count,
168                                       LPARAM param);
169
170   // The collection of every EMF records in the currently loaded EMF buffer.
171   // Initialized by Enumerate(). It keeps pointers to the EMF buffer held by
172   // Emf::emf_. The entries become invalid once Emf::CloseEmf() is called.
173   std::vector<Record> items_;
174
175   EnumerationContext context_;
176 };
177
178 }  // namespace printing
179
180 #endif  // PRINTING_EMF_WIN_H_