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.
5 #ifndef PRINTING_PRINTED_DOCUMENT_H_
6 #define PRINTING_PRINTED_DOCUMENT_H_
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"
21 class RefCountedMemory;
28 class PrintingContext;
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> {
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,
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();
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,
55 const gfx::Size& page_size,
56 const gfx::Rect& page_content_rect);
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);
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);
68 // Retrieves the metafile with the data to print. Lock must be held when
69 // calling this function
70 const MetafilePlayer* GetMetafile();
73 // Draws the page in the context.
74 // Note: locks for a short amount of time in debug only.
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);
84 // Returns true if all the necessary pages for the settings are already
86 // Note: This function always locks and may parse the whole tree.
87 bool IsComplete() const;
89 // Sets the number of pages in the document to be rendered. Can only be set
91 // Note: locks for a short amount of time.
92 void set_page_count(int max_page);
94 // Number of pages in the document.
95 // Note: locks for a short amount of time.
96 int page_count() const;
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;
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_; }
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);
113 // Returns true if SetDebugDumpPath() has been called.
114 static bool HasDebugDumpPath();
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);
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);
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;
137 friend class base::RefCountedThreadSafe<PrintedDocument>;
141 // Array of data for each print previewed page.
142 using PrintedPages = std::map<int, scoped_refptr<PrintedPage>>;
144 // Contains all the mutable stuff. All this stuff MUST be accessed with the
150 // Number of expected pages to be rendered.
151 // Warning: Lock must be held when accessing this member.
152 int expected_page_count_ = 0;
154 // The total number of pages in the document.
158 // Contains the pages' representation. This is a collection of PrintedPage.
159 // Warning: Lock must be held when accessing this member.
162 // Whether the PDF is being converted for printing.
163 bool converting_pdf_ = false;
165 std::unique_ptr<MetafilePlayer> metafile_;
167 #if defined(OS_MACOSX)
168 gfx::Size page_size_;
169 gfx::Rect page_content_rect_;
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
177 Immutable(const PrintSettings& settings,
178 const base::string16& name,
182 // Print settings used to generate this document. Immutable.
183 PrintSettings settings_;
185 // Document name. Immutable.
186 base::string16 name_;
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.
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_;
201 // All the mutable members.
204 // All the immutable members.
205 const Immutable immutable_;
207 DISALLOW_COPY_AND_ASSIGN(PrintedDocument);
210 } // namespace printing
212 #endif // PRINTING_PRINTED_DOCUMENT_H_