blob: aaf249588e45a0b1a9b51dd894fd812ee70cf9e2 [file] [log] [blame]
[email protected]71f40a72012-05-16 07:26:591// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]8ff1d422009-07-07 21:31:395#ifndef PRINTING_PRINTED_DOCUMENT_H_
6#define PRINTING_PRINTED_DOCUMENT_H_
initial.commit09911bf2008-07-26 23:55:297
8#include <map>
dchengc3df9ba2016-04-07 23:09:329#include <memory>
initial.commit09911bf2008-07-26 23:55:2910
[email protected]c95198b2014-06-12 16:56:5511#include "base/files/file_path.h"
avi126e93c2015-12-21 21:48:1612#include "base/macros.h"
[email protected]3b63f8f42011-03-28 01:54:1513#include "base/memory/ref_counted.h"
[email protected]896d161f2013-06-11 22:52:2414#include "base/strings/string16.h"
[email protected]20305ec2011-01-21 04:55:5215#include "base/synchronization/lock.h"
Lei Zhang764461802017-11-15 23:36:0716#include "build/build_config.h"
Thiago Farina45e319532017-03-23 02:44:4917#include "printing/native_drawing_context.h"
[email protected]8ff1d422009-07-07 21:31:3918#include "printing/print_settings.h"
initial.commit09911bf2008-07-26 23:55:2919
[email protected]a3ef4832013-02-02 05:12:3320namespace base {
[email protected]c95198b2014-06-12 16:56:5521class RefCountedMemory;
[email protected]a3ef4832013-02-02 05:12:3322}
23
initial.commit09911bf2008-07-26 23:55:2924namespace printing {
25
vitalybuka5d1290582014-09-12 09:19:5926class MetafilePlayer;
initial.commit09911bf2008-07-26 23:55:2927class PrintedPage;
[email protected]5cc4c422011-02-19 00:09:2228class PrintingContext;
initial.commit09911bf2008-07-26 23:55:2929
[email protected]4ecd07452009-03-31 14:34:4330// A collection of rendered pages. The settings are immutable. If the print
initial.commit09911bf2008-07-26 23:55:2931// 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.
[email protected]69f5b1e62011-09-01 06:34:0436class PRINTING_EXPORT PrintedDocument
37 : public base::RefCountedThreadSafe<PrintedDocument> {
initial.commit09911bf2008-07-26 23:55:2938 public:
39 // The cookie shall be unique and has a specific relationship with its
40 // originating source and settings.
41 PrintedDocument(const PrintSettings& settings,
Vladislav Kuzkokovccceef02017-10-04 15:30:1042 const base::string16& name,
Xiyuan Xia863ce9d2017-06-20 22:36:0043 int cookie);
initial.commit09911bf2008-07-26 23:55:2944
rbpotter80cbe042017-12-08 07:00:5245#if defined(OS_WIN)
rbpotterd20e4ef32018-03-09 20:50:3946 // 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
rbpotter80cbe042017-12-08 07:00:5251 // Sets a page's data. 0-based. Note: locks for a short amount of time.
[email protected]9de1347a2014-06-12 09:49:0852 void SetPage(int page_number,
dchengc3df9ba2016-04-07 23:09:3253 std::unique_ptr<MetafilePlayer> metafile,
pkasting46a62912014-10-09 21:30:0054 float shrink,
rbpotter80cbe042017-12-08 07:00:5255 const gfx::Size& page_size,
56 const gfx::Rect& page_content_rect);
initial.commit09911bf2008-07-26 23:55:2957
58 // Retrieves a page. If the page is not available right now, it
[email protected]c95198b2014-06-12 16:56:5559 // requests to have this page be rendered and returns NULL.
initial.commit09911bf2008-07-26 23:55:2960 // Note: locks for a short amount of time.
[email protected]c95198b2014-06-12 16:56:5561 scoped_refptr<PrintedPage> GetPage(int page_number);
rbpotter80cbe042017-12-08 07:00:5262#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);
initial.commit09911bf2008-07-26 23:55:2967
rbpotter80cbe042017-12-08 07:00:5268 // 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)
[email protected]b75dca82009-10-13 18:46:2176 void RenderPrintedPage(const PrintedPage& page,
Nico Weber8e559562017-10-03 01:25:2677 printing::NativeDrawingContext context) const;
[email protected]5cc4c422011-02-19 00:09:2278#elif defined(OS_POSIX)
rbpotter80cbe042017-12-08 07:00:5279 // 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);
[email protected]5cc4c422011-02-19 00:09:2282#endif
initial.commit09911bf2008-07-26 23:55:2983
initial.commit09911bf2008-07-26 23:55:2984 // Returns true if all the necessary pages for the settings are already
85 // rendered.
rbpotterd20e4ef32018-03-09 20:50:3986 // Note: This function always locks and may parse the whole tree.
initial.commit09911bf2008-07-26 23:55:2987 bool IsComplete() const;
88
initial.commit09911bf2008-07-26 23:55:2989 // 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
Lei Zhang99bb78d32018-05-04 18:38:3794 // Number of pages in the document.
initial.commit09911bf2008-07-26 23:55:2995 // 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
[email protected]4ecd07452009-03-31 14:34:43104 // Getters. All these items are immutable hence thread-safe.
initial.commit09911bf2008-07-26 23:55:29105 const PrintSettings& settings() const { return immutable_.settings_; }
[email protected]b5fa4ee2013-10-01 07:19:07106 const base::string16& name() const { return immutable_.name_; }
[email protected]225c8f52010-02-05 22:23:20107 int cookie() const { return immutable_.cookie_; }
initial.commit09911bf2008-07-26 23:55:29108
Lei Zhangbdd63812017-12-12 06:29:51109 // 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();
[email protected]e1504d82009-07-03 15:27:15115
[email protected]c95198b2014-06-12 16:56:55116 // Creates debug file name from given |document_name| and |extension|.
Lei Zhangbdd63812017-12-12 06:29:51117 // |extension| should include the leading dot. e.g. ".pdf"
118 // Should only be called when debug dumps are enabled.
[email protected]c95198b2014-06-12 16:56:55119 static base::FilePath CreateDebugDumpPath(
120 const base::string16& document_name,
121 const base::FilePath::StringType& extension);
122
Lei Zhangbdd63812017-12-12 06:29:51123 // Dump data on blocking task runner.
124 // Should only be called when debug dumps are enabled.
[email protected]c95198b2014-06-12 16:56:55125 void DebugDumpData(const base::RefCountedMemory* data,
126 const base::FilePath::StringType& extension);
[email protected]e1504d82009-07-03 15:27:15127
rbpotter80cbe042017-12-08 07:00:52128#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
initial.commit09911bf2008-07-26 23:55:29136 private:
[email protected]877d55d2009-11-05 21:53:08137 friend class base::RefCountedThreadSafe<PrintedDocument>;
138
Lei Zhang99bb78d32018-05-04 18:38:37139 ~PrintedDocument();
[email protected]877d55d2009-11-05 21:53:08140
[email protected]0e0fca32009-07-06 15:25:50141 // Array of data for each print previewed page.
Lei Zhang99bb78d32018-05-04 18:38:37142 using PrintedPages = std::map<int, scoped_refptr<PrintedPage>>;
initial.commit09911bf2008-07-26 23:55:29143
144 // Contains all the mutable stuff. All this stuff MUST be accessed with the
145 // lock held.
146 struct Mutable {
Vladislav Kuzkokovccceef02017-10-04 15:30:10147 Mutable();
[email protected]20f0487a2010-09-30 20:06:30148 ~Mutable();
initial.commit09911bf2008-07-26 23:55:29149
initial.commit09911bf2008-07-26 23:55:29150 // Number of expected pages to be rendered.
151 // Warning: Lock must be held when accessing this member.
Lei Zhang764461802017-11-15 23:36:07152 int expected_page_count_ = 0;
initial.commit09911bf2008-07-26 23:55:29153
154 // The total number of pages in the document.
Lei Zhang764461802017-11-15 23:36:07155 int page_count_ = 0;
initial.commit09911bf2008-07-26 23:55:29156
rbpotter80cbe042017-12-08 07:00:52157#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_;
rbpotterd20e4ef32018-03-09 20:50:39161
162 // Whether the PDF is being converted for printing.
163 bool converting_pdf_ = false;
rbpotter80cbe042017-12-08 07:00:52164#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_;
[email protected]da4eefd2011-03-03 23:40:27170#endif
initial.commit09911bf2008-07-26 23:55:29171 };
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 {
[email protected]c95198b2014-06-12 16:56:55177 Immutable(const PrintSettings& settings,
Vladislav Kuzkokovccceef02017-10-04 15:30:10178 const base::string16& name,
Xiyuan Xia863ce9d2017-06-20 22:36:00179 int cookie);
[email protected]20f0487a2010-09-30 20:06:30180 ~Immutable();
initial.commit09911bf2008-07-26 23:55:29181
[email protected]4ecd07452009-03-31 14:34:43182 // Print settings used to generate this document. Immutable.
initial.commit09911bf2008-07-26 23:55:29183 PrintSettings settings_;
184
[email protected]4ecd07452009-03-31 14:34:43185 // Document name. Immutable.
[email protected]b5fa4ee2013-10-01 07:19:07186 base::string16 name_;
initial.commit09911bf2008-07-26 23:55:29187
initial.commit09911bf2008-07-26 23:55:29188 // 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_;
[email protected]c95198b2014-06-12 16:56:55195 };
[email protected]e1504d82009-07-03 15:27:15196
initial.commit09911bf2008-07-26 23:55:29197 // 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.
[email protected]20305ec2011-01-21 04:55:52199 mutable base::Lock lock_;
initial.commit09911bf2008-07-26 23:55:29200
201 // All the mutable members.
202 Mutable mutable_;
203
204 // All the immutable members.
205 const Immutable immutable_;
206
[email protected]5930cb62009-12-08 02:04:22207 DISALLOW_COPY_AND_ASSIGN(PrintedDocument);
initial.commit09911bf2008-07-26 23:55:29208};
209
210} // namespace printing
211
[email protected]8ff1d422009-07-07 21:31:39212#endif // PRINTING_PRINTED_DOCUMENT_H_