[email protected] | 71f40a7 | 2012-05-16 07:26:59 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 5 | #ifndef PRINTING_PRINTED_DOCUMENT_H_ |
| 6 | #define PRINTING_PRINTED_DOCUMENT_H_ |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 7 | |
| 8 | #include <map> |
dcheng | c3df9ba | 2016-04-07 23:09:32 | [diff] [blame] | 9 | #include <memory> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 10 | |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 11 | #include "base/files/file_path.h" |
avi | 126e93c | 2015-12-21 21:48:16 | [diff] [blame] | 12 | #include "base/macros.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 13 | #include "base/memory/ref_counted.h" |
[email protected] | 896d161f | 2013-06-11 22:52:24 | [diff] [blame] | 14 | #include "base/strings/string16.h" |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 15 | #include "base/synchronization/lock.h" |
Lei Zhang | 76446180 | 2017-11-15 23:36:07 | [diff] [blame] | 16 | #include "build/build_config.h" |
Thiago Farina | 45e31953 | 2017-03-23 02:44:49 | [diff] [blame] | 17 | #include "printing/native_drawing_context.h" |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 18 | #include "printing/print_settings.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 19 | |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 20 | namespace base { |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 21 | class RefCountedMemory; |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 22 | } |
| 23 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 24 | namespace printing { |
| 25 | |
vitalybuka | 5d129058 | 2014-09-12 09:19:59 | [diff] [blame] | 26 | class MetafilePlayer; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 27 | class PrintedPage; |
[email protected] | 5cc4c42 | 2011-02-19 00:09:22 | [diff] [blame] | 28 | class PrintingContext; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 29 | |
[email protected] | 4ecd0745 | 2009-03-31 14:34:43 | [diff] [blame] | 30 | // A collection of rendered pages. The settings are immutable. If the print |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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. |
[email protected] | 69f5b1e6 | 2011-09-01 06:34:04 | [diff] [blame] | 36 | class PRINTING_EXPORT PrintedDocument |
| 37 | : public base::RefCountedThreadSafe<PrintedDocument> { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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, |
Vladislav Kuzkokov | ccceef0 | 2017-10-04 15:30:10 | [diff] [blame] | 42 | const base::string16& name, |
Xiyuan Xia | 863ce9d | 2017-06-20 22:36:00 | [diff] [blame] | 43 | int cookie); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 44 | |
rbpotter | 80cbe04 | 2017-12-08 07:00:52 | [diff] [blame] | 45 | #if defined(OS_WIN) |
rbpotter | d20e4ef3 | 2018-03-09 20:50:39 | [diff] [blame] | 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 | |
rbpotter | 80cbe04 | 2017-12-08 07:00:52 | [diff] [blame] | 51 | // Sets a page's data. 0-based. Note: locks for a short amount of time. |
[email protected] | 9de1347a | 2014-06-12 09:49:08 | [diff] [blame] | 52 | void SetPage(int page_number, |
dcheng | c3df9ba | 2016-04-07 23:09:32 | [diff] [blame] | 53 | std::unique_ptr<MetafilePlayer> metafile, |
pkasting | 46a6291 | 2014-10-09 21:30:00 | [diff] [blame] | 54 | float shrink, |
rbpotter | 80cbe04 | 2017-12-08 07:00:52 | [diff] [blame] | 55 | const gfx::Size& page_size, |
| 56 | const gfx::Rect& page_content_rect); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 57 | |
| 58 | // Retrieves a page. If the page is not available right now, it |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 59 | // requests to have this page be rendered and returns NULL. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 60 | // Note: locks for a short amount of time. |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 61 | scoped_refptr<PrintedPage> GetPage(int page_number); |
rbpotter | 80cbe04 | 2017-12-08 07:00:52 | [diff] [blame] | 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); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 67 | |
rbpotter | 80cbe04 | 2017-12-08 07:00:52 | [diff] [blame] | 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) |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 76 | void RenderPrintedPage(const PrintedPage& page, |
Nico Weber | 8e55956 | 2017-10-03 01:25:26 | [diff] [blame] | 77 | printing::NativeDrawingContext context) const; |
[email protected] | 5cc4c42 | 2011-02-19 00:09:22 | [diff] [blame] | 78 | #elif defined(OS_POSIX) |
rbpotter | 80cbe04 | 2017-12-08 07:00:52 | [diff] [blame] | 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); |
[email protected] | 5cc4c42 | 2011-02-19 00:09:22 | [diff] [blame] | 82 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 83 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 84 | // Returns true if all the necessary pages for the settings are already |
| 85 | // rendered. |
rbpotter | d20e4ef3 | 2018-03-09 20:50:39 | [diff] [blame] | 86 | // Note: This function always locks and may parse the whole tree. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 87 | bool IsComplete() const; |
| 88 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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 | |
Lei Zhang | 99bb78d3 | 2018-05-04 18:38:37 | [diff] [blame] | 94 | // Number of pages in the document. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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 | |
[email protected] | 4ecd0745 | 2009-03-31 14:34:43 | [diff] [blame] | 104 | // Getters. All these items are immutable hence thread-safe. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 105 | const PrintSettings& settings() const { return immutable_.settings_; } |
[email protected] | b5fa4ee | 2013-10-01 07:19:07 | [diff] [blame] | 106 | const base::string16& name() const { return immutable_.name_; } |
[email protected] | 225c8f5 | 2010-02-05 22:23:20 | [diff] [blame] | 107 | int cookie() const { return immutable_.cookie_; } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 108 | |
Lei Zhang | bdd6381 | 2017-12-12 06:29:51 | [diff] [blame] | 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(); |
[email protected] | e1504d8 | 2009-07-03 15:27:15 | [diff] [blame] | 115 | |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 116 | // Creates debug file name from given |document_name| and |extension|. |
Lei Zhang | bdd6381 | 2017-12-12 06:29:51 | [diff] [blame] | 117 | // |extension| should include the leading dot. e.g. ".pdf" |
| 118 | // Should only be called when debug dumps are enabled. |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 119 | static base::FilePath CreateDebugDumpPath( |
| 120 | const base::string16& document_name, |
| 121 | const base::FilePath::StringType& extension); |
| 122 | |
Lei Zhang | bdd6381 | 2017-12-12 06:29:51 | [diff] [blame] | 123 | // Dump data on blocking task runner. |
| 124 | // Should only be called when debug dumps are enabled. |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 125 | void DebugDumpData(const base::RefCountedMemory* data, |
| 126 | const base::FilePath::StringType& extension); |
[email protected] | e1504d8 | 2009-07-03 15:27:15 | [diff] [blame] | 127 | |
rbpotter | 80cbe04 | 2017-12-08 07:00:52 | [diff] [blame] | 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 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 136 | private: |
[email protected] | 877d55d | 2009-11-05 21:53:08 | [diff] [blame] | 137 | friend class base::RefCountedThreadSafe<PrintedDocument>; |
| 138 | |
Lei Zhang | 99bb78d3 | 2018-05-04 18:38:37 | [diff] [blame] | 139 | ~PrintedDocument(); |
[email protected] | 877d55d | 2009-11-05 21:53:08 | [diff] [blame] | 140 | |
[email protected] | 0e0fca3 | 2009-07-06 15:25:50 | [diff] [blame] | 141 | // Array of data for each print previewed page. |
Lei Zhang | 99bb78d3 | 2018-05-04 18:38:37 | [diff] [blame] | 142 | using PrintedPages = std::map<int, scoped_refptr<PrintedPage>>; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 143 | |
| 144 | // Contains all the mutable stuff. All this stuff MUST be accessed with the |
| 145 | // lock held. |
| 146 | struct Mutable { |
Vladislav Kuzkokov | ccceef0 | 2017-10-04 15:30:10 | [diff] [blame] | 147 | Mutable(); |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 148 | ~Mutable(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 149 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 150 | // Number of expected pages to be rendered. |
| 151 | // Warning: Lock must be held when accessing this member. |
Lei Zhang | 76446180 | 2017-11-15 23:36:07 | [diff] [blame] | 152 | int expected_page_count_ = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 153 | |
| 154 | // The total number of pages in the document. |
Lei Zhang | 76446180 | 2017-11-15 23:36:07 | [diff] [blame] | 155 | int page_count_ = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 156 | |
rbpotter | 80cbe04 | 2017-12-08 07:00:52 | [diff] [blame] | 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_; |
rbpotter | d20e4ef3 | 2018-03-09 20:50:39 | [diff] [blame] | 161 | |
| 162 | // Whether the PDF is being converted for printing. |
| 163 | bool converting_pdf_ = false; |
rbpotter | 80cbe04 | 2017-12-08 07:00:52 | [diff] [blame] | 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_; |
[email protected] | da4eefd | 2011-03-03 23:40:27 | [diff] [blame] | 170 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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 { |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 177 | Immutable(const PrintSettings& settings, |
Vladislav Kuzkokov | ccceef0 | 2017-10-04 15:30:10 | [diff] [blame] | 178 | const base::string16& name, |
Xiyuan Xia | 863ce9d | 2017-06-20 22:36:00 | [diff] [blame] | 179 | int cookie); |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 180 | ~Immutable(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 181 | |
[email protected] | 4ecd0745 | 2009-03-31 14:34:43 | [diff] [blame] | 182 | // Print settings used to generate this document. Immutable. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 183 | PrintSettings settings_; |
| 184 | |
[email protected] | 4ecd0745 | 2009-03-31 14:34:43 | [diff] [blame] | 185 | // Document name. Immutable. |
[email protected] | b5fa4ee | 2013-10-01 07:19:07 | [diff] [blame] | 186 | base::string16 name_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 187 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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_; |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 195 | }; |
[email protected] | e1504d8 | 2009-07-03 15:27:15 | [diff] [blame] | 196 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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. |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 199 | mutable base::Lock lock_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 200 | |
| 201 | // All the mutable members. |
| 202 | Mutable mutable_; |
| 203 | |
| 204 | // All the immutable members. |
| 205 | const Immutable immutable_; |
| 206 | |
[email protected] | 5930cb6 | 2009-12-08 02:04:22 | [diff] [blame] | 207 | DISALLOW_COPY_AND_ASSIGN(PrintedDocument); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 208 | }; |
| 209 | |
| 210 | } // namespace printing |
| 211 | |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 212 | #endif // PRINTING_PRINTED_DOCUMENT_H_ |