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