[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" |
Thiago Farina | 45e31953 | 2017-03-23 02:44:49 | [diff] [blame] | 16 | #include "printing/native_drawing_context.h" |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 17 | #include "printing/print_settings.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 18 | |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 19 | namespace base { |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 20 | class RefCountedMemory; |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 21 | } |
| 22 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 23 | namespace printing { |
| 24 | |
vitalybuka | 5d129058 | 2014-09-12 09:19:59 | [diff] [blame] | 25 | class MetafilePlayer; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 26 | class PrintedPage; |
[email protected] | 5cc4c42 | 2011-02-19 00:09:22 | [diff] [blame] | 27 | class PrintingContext; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 28 | |
[email protected] | 4ecd0745 | 2009-03-31 14:34:43 | [diff] [blame] | 29 | // A collection of rendered pages. The settings are immutable. If the print |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 30 | // settings are changed, a new PrintedDocument must be created. |
| 31 | // Warning: May be accessed from many threads at the same time. Only one thread |
| 32 | // will have write access. Sensible functions are protected by a lock. |
| 33 | // Warning: Once a page is loaded, it cannot be replaced. Pages may be discarded |
| 34 | // under low memory conditions. |
[email protected] | 69f5b1e6 | 2011-09-01 06:34:04 | [diff] [blame] | 35 | class PRINTING_EXPORT PrintedDocument |
| 36 | : public base::RefCountedThreadSafe<PrintedDocument> { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 37 | public: |
| 38 | // The cookie shall be unique and has a specific relationship with its |
| 39 | // originating source and settings. |
| 40 | PrintedDocument(const PrintSettings& settings, |
Vladislav Kuzkokov | ccceef0 | 2017-10-04 15:30:10 | [diff] [blame] | 41 | const base::string16& name, |
Xiyuan Xia | 863ce9d | 2017-06-20 22:36:00 | [diff] [blame] | 42 | int cookie); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 43 | |
[email protected] | 0e0fca3 | 2009-07-06 15:25:50 | [diff] [blame] | 44 | // Sets a page's data. 0-based. Takes metafile ownership. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 45 | // Note: locks for a short amount of time. |
[email protected] | 9de1347a | 2014-06-12 09:49:08 | [diff] [blame] | 46 | void SetPage(int page_number, |
dcheng | c3df9ba | 2016-04-07 23:09:32 | [diff] [blame] | 47 | std::unique_ptr<MetafilePlayer> metafile, |
[email protected] | 9de1347a | 2014-06-12 09:49:08 | [diff] [blame] | 48 | #if defined(OS_WIN) |
pkasting | 46a6291 | 2014-10-09 21:30:00 | [diff] [blame] | 49 | float shrink, |
[email protected] | 9de1347a | 2014-06-12 09:49:08 | [diff] [blame] | 50 | #endif // OS_WIN |
| 51 | const gfx::Size& paper_size, |
| 52 | const gfx::Rect& page_rect); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 53 | |
| 54 | // Retrieves a page. If the page is not available right now, it |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 55 | // requests to have this page be rendered and returns NULL. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 56 | // Note: locks for a short amount of time. |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 57 | scoped_refptr<PrintedPage> GetPage(int page_number); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 58 | |
| 59 | // Draws the page in the context. |
| 60 | // Note: locks for a short amount of time in debug only. |
[email protected] | 2f47fc45 | 2011-11-21 03:12:17 | [diff] [blame] | 61 | #if defined(OS_WIN) || defined(OS_MACOSX) && !defined(USE_AURA) |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 62 | void RenderPrintedPage(const PrintedPage& page, |
Nico Weber | 8e55956 | 2017-10-03 01:25:26 | [diff] [blame] | 63 | printing::NativeDrawingContext context) const; |
[email protected] | 5cc4c42 | 2011-02-19 00:09:22 | [diff] [blame] | 64 | #elif defined(OS_POSIX) |
| 65 | void RenderPrintedPage(const PrintedPage& page, |
| 66 | PrintingContext* context) const; |
| 67 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 68 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 69 | // Returns true if all the necessary pages for the settings are already |
| 70 | // rendered. |
| 71 | // Note: locks while parsing the whole tree. |
| 72 | bool IsComplete() const; |
| 73 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 74 | // Sets the number of pages in the document to be rendered. Can only be set |
| 75 | // once. |
| 76 | // Note: locks for a short amount of time. |
| 77 | void set_page_count(int max_page); |
| 78 | |
| 79 | // Number of pages in the document. Used for headers/footers. |
| 80 | // Note: locks for a short amount of time. |
| 81 | int page_count() const; |
| 82 | |
| 83 | // Returns the number of expected pages to be rendered. It is a non-linear |
| 84 | // series if settings().ranges is not empty. It is the same value as |
| 85 | // document_page_count() otherwise. |
| 86 | // Note: locks for a short amount of time. |
| 87 | int expected_page_count() const; |
| 88 | |
[email protected] | 4ecd0745 | 2009-03-31 14:34:43 | [diff] [blame] | 89 | // Getters. All these items are immutable hence thread-safe. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 90 | const PrintSettings& settings() const { return immutable_.settings_; } |
[email protected] | b5fa4ee | 2013-10-01 07:19:07 | [diff] [blame] | 91 | const base::string16& name() const { return immutable_.name_; } |
[email protected] | 225c8f5 | 2010-02-05 22:23:20 | [diff] [blame] | 92 | int cookie() const { return immutable_.cookie_; } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 93 | |
[email protected] | e1504d8 | 2009-07-03 15:27:15 | [diff] [blame] | 94 | // Sets a path where to dump printing output files for debugging. If never set |
| 95 | // no files are generated. |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 96 | static void set_debug_dump_path(const base::FilePath& debug_dump_path); |
[email protected] | e1504d8 | 2009-07-03 15:27:15 | [diff] [blame] | 97 | |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 98 | // Creates debug file name from given |document_name| and |extension|. |
| 99 | // |extension| should include '.', example ".pdf" |
| 100 | // Returns empty |base::FilePath| if debug dumps is not enabled. |
| 101 | static base::FilePath CreateDebugDumpPath( |
| 102 | const base::string16& document_name, |
| 103 | const base::FilePath::StringType& extension); |
| 104 | |
| 105 | // Dump data on blocking task runner if debug dumps enabled. |
| 106 | void DebugDumpData(const base::RefCountedMemory* data, |
| 107 | const base::FilePath::StringType& extension); |
[email protected] | e1504d8 | 2009-07-03 15:27:15 | [diff] [blame] | 108 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 109 | private: |
[email protected] | 877d55d | 2009-11-05 21:53:08 | [diff] [blame] | 110 | friend class base::RefCountedThreadSafe<PrintedDocument>; |
| 111 | |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 112 | virtual ~PrintedDocument(); |
[email protected] | 877d55d | 2009-11-05 21:53:08 | [diff] [blame] | 113 | |
[email protected] | 0e0fca3 | 2009-07-06 15:25:50 | [diff] [blame] | 114 | // Array of data for each print previewed page. |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 115 | typedef std::map<int, scoped_refptr<PrintedPage> > PrintedPages; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 116 | |
| 117 | // Contains all the mutable stuff. All this stuff MUST be accessed with the |
| 118 | // lock held. |
| 119 | struct Mutable { |
Vladislav Kuzkokov | ccceef0 | 2017-10-04 15:30:10 | [diff] [blame] | 120 | Mutable(); |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 121 | ~Mutable(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 122 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 123 | // Contains the pages' representation. This is a collection of PrintedPage. |
| 124 | // Warning: Lock must be held when accessing this member. |
| 125 | PrintedPages pages_; |
| 126 | |
| 127 | // Number of expected pages to be rendered. |
| 128 | // Warning: Lock must be held when accessing this member. |
| 129 | int expected_page_count_; |
| 130 | |
| 131 | // The total number of pages in the document. |
| 132 | int page_count_; |
| 133 | |
[email protected] | da4eefd | 2011-03-03 23:40:27 | [diff] [blame] | 134 | #if defined(OS_POSIX) && !defined(OS_MACOSX) |
| 135 | // Page number of the first page. |
| 136 | int first_page; |
| 137 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | // Contains all the immutable stuff. All this stuff can be accessed without |
| 141 | // any lock held. This is because it can't be changed after the object's |
| 142 | // construction. |
| 143 | struct Immutable { |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 144 | Immutable(const PrintSettings& settings, |
Vladislav Kuzkokov | ccceef0 | 2017-10-04 15:30:10 | [diff] [blame] | 145 | const base::string16& name, |
Xiyuan Xia | 863ce9d | 2017-06-20 22:36:00 | [diff] [blame] | 146 | int cookie); |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 147 | ~Immutable(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 148 | |
[email protected] | 4ecd0745 | 2009-03-31 14:34:43 | [diff] [blame] | 149 | // Print settings used to generate this document. Immutable. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 150 | PrintSettings settings_; |
| 151 | |
[email protected] | 4ecd0745 | 2009-03-31 14:34:43 | [diff] [blame] | 152 | // Document name. Immutable. |
[email protected] | b5fa4ee | 2013-10-01 07:19:07 | [diff] [blame] | 153 | base::string16 name_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 154 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 155 | // Cookie to uniquely identify this document. It is used to make sure that a |
| 156 | // PrintedPage is correctly belonging to the PrintedDocument. Since |
| 157 | // PrintedPage generation is completely asynchronous, it could be easy to |
| 158 | // mess up and send the page to the wrong document. It can be viewed as a |
| 159 | // simpler hash of PrintSettings since a new document is made each time the |
| 160 | // print settings change. |
| 161 | int cookie_; |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 162 | }; |
[email protected] | e1504d8 | 2009-07-03 15:27:15 | [diff] [blame] | 163 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 164 | // All writable data member access must be guarded by this lock. Needs to be |
| 165 | // mutable since it can be acquired from const member functions. |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 166 | mutable base::Lock lock_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 167 | |
| 168 | // All the mutable members. |
| 169 | Mutable mutable_; |
| 170 | |
| 171 | // All the immutable members. |
| 172 | const Immutable immutable_; |
| 173 | |
[email protected] | 5930cb6 | 2009-12-08 02:04:22 | [diff] [blame] | 174 | DISALLOW_COPY_AND_ASSIGN(PrintedDocument); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 175 | }; |
| 176 | |
| 177 | } // namespace printing |
| 178 | |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 179 | #endif // PRINTING_PRINTED_DOCUMENT_H_ |