Upload upstream chromium 73.0.3683.0
[platform/framework/web/chromium-efl.git] / printing / printed_document.h
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 #ifndef PRINTING_PRINTED_DOCUMENT_H_
6 #define PRINTING_PRINTED_DOCUMENT_H_
7
8 #include <map>
9 #include <memory>
10
11 #include "base/files/file_path.h"
12 #include "base/macros.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/strings/string16.h"
15 #include "base/synchronization/lock.h"
16 #include "build/build_config.h"
17 #include "printing/native_drawing_context.h"
18 #include "printing/print_settings.h"
19
20 namespace base {
21 class RefCountedMemory;
22 }
23
24 namespace printing {
25
26 class MetafilePlayer;
27 class PrintedPage;
28 class PrintingContext;
29
30 // A collection of rendered pages. The settings are immutable. If the print
31 // settings are changed, a new PrintedDocument must be created.
32 // Warning: May be accessed from many threads at the same time. Only one thread
33 // will have write access. Sensible functions are protected by a lock.
34 // Warning: Once a page is loaded, it cannot be replaced. Pages may be discarded
35 // under low memory conditions.
36 class PRINTING_EXPORT PrintedDocument
37     : public base::RefCountedThreadSafe<PrintedDocument> {
38  public:
39   // The cookie shall be unique and has a specific relationship with its
40   // originating source and settings.
41   PrintedDocument(const PrintSettings& settings,
42                   const base::string16& name,
43                   int cookie);
44
45 #if defined(OS_WIN)
46   // Indicates that the PDF has been generated and the document is waiting for
47   // conversion for printing. This is needed on Windows so that the print job
48   // is not cancelled if the web contents dies before PDF conversion finishes.
49   void SetConvertingPdf();
50
51   // Sets a page's data. 0-based. Note: locks for a short amount of time.
52   void SetPage(int page_number,
53                std::unique_ptr<MetafilePlayer> metafile,
54                float shrink,
55                const gfx::Size& page_size,
56                const gfx::Rect& page_content_rect);
57
58   // Retrieves a page. If the page is not available right now, it
59   // requests to have this page be rendered and returns NULL.
60   // Note: locks for a short amount of time.
61   scoped_refptr<PrintedPage> GetPage(int page_number);
62 #else
63   // Sets the document data. Note: locks for a short amount of time.
64   void SetDocument(std::unique_ptr<MetafilePlayer> metafile,
65                    const gfx::Size& page_size,
66                    const gfx::Rect& page_content_rect);
67
68   // Retrieves the metafile with the data to print. Lock must be held when
69   // calling this function
70   const MetafilePlayer* GetMetafile();
71 #endif
72
73 // Draws the page in the context.
74 // Note: locks for a short amount of time in debug only.
75 #if defined(OS_WIN)
76   void RenderPrintedPage(const PrintedPage& page,
77                          printing::NativeDrawingContext context) const;
78 #elif defined(OS_POSIX)
79   // Draws the document in the context. Returns true on success and false on
80   // failure. Fails if context->NewPage() or context->PageDone() fails.
81   bool RenderPrintedDocument(PrintingContext* context);
82 #endif
83
84   // Returns true if all the necessary pages for the settings are already
85   // rendered.
86   // Note: This function always locks and may parse the whole tree.
87   bool IsComplete() const;
88
89   // Sets the number of pages in the document to be rendered. Can only be set
90   // once.
91   // Note: locks for a short amount of time.
92   void set_page_count(int max_page);
93
94   // Number of pages in the document.
95   // Note: locks for a short amount of time.
96   int page_count() const;
97
98   // Returns the number of expected pages to be rendered. It is a non-linear
99   // series if settings().ranges is not empty. It is the same value as
100   // document_page_count() otherwise.
101   // Note: locks for a short amount of time.
102   int expected_page_count() const;
103
104   // Getters. All these items are immutable hence thread-safe.
105   const PrintSettings& settings() const { return immutable_.settings_; }
106   const base::string16& name() const { return immutable_.name_; }
107   int cookie() const { return immutable_.cookie_; }
108
109   // Sets a path where to dump printing output files for debugging. If never
110   // set, no files are generated. |debug_dump_path| must not be empty.
111   static void SetDebugDumpPath(const base::FilePath& debug_dump_path);
112
113   // Returns true if SetDebugDumpPath() has been called.
114   static bool HasDebugDumpPath();
115
116   // Creates debug file name from given |document_name| and |extension|.
117   // |extension| should include the leading dot. e.g. ".pdf"
118   // Should only be called when debug dumps are enabled.
119   static base::FilePath CreateDebugDumpPath(
120       const base::string16& document_name,
121       const base::FilePath::StringType& extension);
122
123   // Dump data on blocking task runner.
124   // Should only be called when debug dumps are enabled.
125   void DebugDumpData(const base::RefCountedMemory* data,
126                      const base::FilePath::StringType& extension);
127
128 #if defined(OS_WIN) || defined(OS_MACOSX)
129   // Get page content rect adjusted based on
130   // http://dev.w3.org/csswg/css3-page/#positioning-page-box
131   gfx::Rect GetCenteredPageContentRect(const gfx::Size& paper_size,
132                                        const gfx::Size& page_size,
133                                        const gfx::Rect& content_rect) const;
134 #endif
135
136  private:
137   friend class base::RefCountedThreadSafe<PrintedDocument>;
138
139   ~PrintedDocument();
140
141   // Array of data for each print previewed page.
142   using PrintedPages = std::map<int, scoped_refptr<PrintedPage>>;
143
144   // Contains all the mutable stuff. All this stuff MUST be accessed with the
145   // lock held.
146   struct Mutable {
147     Mutable();
148     ~Mutable();
149
150     // Number of expected pages to be rendered.
151     // Warning: Lock must be held when accessing this member.
152     int expected_page_count_ = 0;
153
154     // The total number of pages in the document.
155     int page_count_ = 0;
156
157 #if defined(OS_WIN)
158     // Contains the pages' representation. This is a collection of PrintedPage.
159     // Warning: Lock must be held when accessing this member.
160     PrintedPages pages_;
161
162     // Whether the PDF is being converted for printing.
163     bool converting_pdf_ = false;
164 #else
165     std::unique_ptr<MetafilePlayer> metafile_;
166 #endif
167 #if defined(OS_MACOSX)
168     gfx::Size page_size_;
169     gfx::Rect page_content_rect_;
170 #endif
171   };
172
173   // Contains all the immutable stuff. All this stuff can be accessed without
174   // any lock held. This is because it can't be changed after the object's
175   // construction.
176   struct Immutable {
177     Immutable(const PrintSettings& settings,
178               const base::string16& name,
179               int cookie);
180     ~Immutable();
181
182     // Print settings used to generate this document. Immutable.
183     PrintSettings settings_;
184
185     // Document name. Immutable.
186     base::string16 name_;
187
188     // Cookie to uniquely identify this document. It is used to make sure that a
189     // PrintedPage is correctly belonging to the PrintedDocument. Since
190     // PrintedPage generation is completely asynchronous, it could be easy to
191     // mess up and send the page to the wrong document. It can be viewed as a
192     // simpler hash of PrintSettings since a new document is made each time the
193     // print settings change.
194     int cookie_;
195   };
196
197   // All writable data member access must be guarded by this lock. Needs to be
198   // mutable since it can be acquired from const member functions.
199   mutable base::Lock lock_;
200
201   // All the mutable members.
202   Mutable mutable_;
203
204   // All the immutable members.
205   const Immutable immutable_;
206
207   DISALLOW_COPY_AND_ASSIGN(PrintedDocument);
208 };
209
210 }  // namespace printing
211
212 #endif  // PRINTING_PRINTED_DOCUMENT_H_