license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 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. |
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] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 10 | #include "app/gfx/native_widget_types.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 11 | #include "base/lock.h" |
| 12 | #include "base/ref_counted.h" |
| 13 | #include "base/scoped_ptr.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 14 | #include "googleurl/src/gurl.h" |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 15 | #include "printing/print_settings.h" |
| 16 | #include "printing/native_metafile.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 17 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 18 | class MessageLoop; |
| 19 | |
| 20 | namespace gfx { |
[email protected] | 7322c440 | 2009-05-15 02:16:10 | [diff] [blame] | 21 | class Font; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | namespace printing { |
| 25 | |
| 26 | class PrintedPage; |
| 27 | class PrintedPagesSource; |
| 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. |
| 35 | class PrintedDocument : public base::RefCountedThreadSafe<PrintedDocument> { |
| 36 | public: |
| 37 | // The cookie shall be unique and has a specific relationship with its |
| 38 | // originating source and settings. |
| 39 | PrintedDocument(const PrintSettings& settings, |
| 40 | PrintedPagesSource* source, |
| 41 | int cookie); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 42 | |
[email protected] | 0e0fca3 | 2009-07-06 15:25:50 | [diff] [blame] | 43 | // Sets a page's data. 0-based. Takes metafile ownership. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 44 | // Note: locks for a short amount of time. |
[email protected] | 0e0fca3 | 2009-07-06 15:25:50 | [diff] [blame] | 45 | void SetPage(int page_number, NativeMetafile* metafile, double shrink); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 46 | |
| 47 | // Retrieves a page. If the page is not available right now, it |
| 48 | // requests to have this page be rendered and returns false. |
| 49 | // Note: locks for a short amount of time. |
| 50 | bool GetPage(int page_number, scoped_refptr<PrintedPage>* page); |
| 51 | |
| 52 | // Draws the page in the context. |
| 53 | // Note: locks for a short amount of time in debug only. |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 54 | void RenderPrintedPage(const PrintedPage& page, |
| 55 | gfx::NativeDrawingContext context) const; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 56 | |
| 57 | // Draws the page in the context. If the page is not available right now, it |
| 58 | // requests to have this page be rendered and returns false. |
| 59 | // Note: locks for a short amount of time. |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 60 | bool RenderPrintedPageNumber(int page_number, |
| 61 | gfx::NativeDrawingContext context); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 62 | |
| 63 | // Returns true if all the necessary pages for the settings are already |
| 64 | // rendered. |
| 65 | // Note: locks while parsing the whole tree. |
| 66 | bool IsComplete() const; |
| 67 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 68 | // Disconnects the PrintedPage source (PrintedPagesSource). It is done when |
| 69 | // the source is being destroyed. |
| 70 | void DisconnectSource(); |
| 71 | |
| 72 | // Retrieves the current memory usage of the renderer pages. |
| 73 | // Note: locks for a short amount of time. |
| 74 | size_t MemoryUsage() const; |
| 75 | |
| 76 | // Sets the number of pages in the document to be rendered. Can only be set |
| 77 | // once. |
| 78 | // Note: locks for a short amount of time. |
| 79 | void set_page_count(int max_page); |
| 80 | |
| 81 | // Number of pages in the document. Used for headers/footers. |
| 82 | // Note: locks for a short amount of time. |
| 83 | int page_count() const; |
| 84 | |
| 85 | // Returns the number of expected pages to be rendered. It is a non-linear |
| 86 | // series if settings().ranges is not empty. It is the same value as |
| 87 | // document_page_count() otherwise. |
| 88 | // Note: locks for a short amount of time. |
| 89 | int expected_page_count() const; |
| 90 | |
[email protected] | 4ecd0745 | 2009-03-31 14:34:43 | [diff] [blame] | 91 | // Getters. All these items are immutable hence thread-safe. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 92 | const PrintSettings& settings() const { return immutable_.settings_; } |
| 93 | const std::wstring& name() const { |
| 94 | return immutable_.name_; |
| 95 | } |
| 96 | const GURL& url() const { return immutable_.url_; } |
| 97 | const std::wstring& date() const { return immutable_.date_; } |
| 98 | const std::wstring& time() const { return immutable_.time_; } |
| 99 | const int cookie() const { return immutable_.cookie_; } |
| 100 | |
[email protected] | e1504d8 | 2009-07-03 15:27:15 | [diff] [blame] | 101 | // Sets a path where to dump printing output files for debugging. If never set |
| 102 | // no files are generated. |
| 103 | static void set_debug_dump_path(const std::wstring& debug_dump_path); |
| 104 | |
| 105 | static const std::wstring& debug_dump_path(); |
| 106 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 107 | private: |
[email protected] | 877d55d | 2009-11-05 21:53:08 | [diff] [blame^] | 108 | friend class base::RefCountedThreadSafe<PrintedDocument>; |
| 109 | |
| 110 | ~PrintedDocument(); |
| 111 | |
[email protected] | 0e0fca3 | 2009-07-06 15:25:50 | [diff] [blame] | 112 | // Array of data for each print previewed page. |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 113 | typedef std::map<int, scoped_refptr<PrintedPage> > PrintedPages; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 114 | |
| 115 | // Contains all the mutable stuff. All this stuff MUST be accessed with the |
| 116 | // lock held. |
| 117 | struct Mutable { |
| 118 | Mutable(PrintedPagesSource* source); |
| 119 | |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 120 | // Source that generates the PrintedPage's (i.e. a TabContents). It will be |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 121 | // set back to NULL if the source is deleted before this object. |
| 122 | PrintedPagesSource* source_; |
| 123 | |
| 124 | // Contains the pages' representation. This is a collection of PrintedPage. |
| 125 | // Warning: Lock must be held when accessing this member. |
| 126 | PrintedPages pages_; |
| 127 | |
| 128 | // Number of expected pages to be rendered. |
| 129 | // Warning: Lock must be held when accessing this member. |
| 130 | int expected_page_count_; |
| 131 | |
| 132 | // The total number of pages in the document. |
| 133 | int page_count_; |
| 134 | |
| 135 | // Shrink done in comparison to desired_dpi. |
| 136 | double shrink_factor; |
| 137 | }; |
| 138 | |
| 139 | // Contains all the immutable stuff. All this stuff can be accessed without |
| 140 | // any lock held. This is because it can't be changed after the object's |
| 141 | // construction. |
| 142 | struct Immutable { |
| 143 | Immutable(const PrintSettings& settings, PrintedPagesSource* source, |
| 144 | int cookie); |
| 145 | |
[email protected] | 4ecd0745 | 2009-03-31 14:34:43 | [diff] [blame] | 146 | // Print settings used to generate this document. Immutable. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 147 | PrintSettings settings_; |
| 148 | |
| 149 | // Native thread for the render source. |
| 150 | MessageLoop* source_message_loop_; |
| 151 | |
[email protected] | 4ecd0745 | 2009-03-31 14:34:43 | [diff] [blame] | 152 | // Document name. Immutable. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 153 | std::wstring name_; |
| 154 | |
[email protected] | 4ecd0745 | 2009-03-31 14:34:43 | [diff] [blame] | 155 | // URL that generated this document. Immutable. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 156 | GURL url_; |
| 157 | |
[email protected] | 4ecd0745 | 2009-03-31 14:34:43 | [diff] [blame] | 158 | // The date on which this job started. Immutable. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 159 | std::wstring date_; |
| 160 | |
[email protected] | 4ecd0745 | 2009-03-31 14:34:43 | [diff] [blame] | 161 | // The time at which this job started. Immutable. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 162 | std::wstring time_; |
| 163 | |
| 164 | // Cookie to uniquely identify this document. It is used to make sure that a |
| 165 | // PrintedPage is correctly belonging to the PrintedDocument. Since |
| 166 | // PrintedPage generation is completely asynchronous, it could be easy to |
| 167 | // mess up and send the page to the wrong document. It can be viewed as a |
| 168 | // simpler hash of PrintSettings since a new document is made each time the |
| 169 | // print settings change. |
| 170 | int cookie_; |
| 171 | }; |
| 172 | |
| 173 | // Prints the headers and footers for one page in the specified context |
| 174 | // according to the current settings. |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 175 | void PrintHeaderFooter(gfx::NativeDrawingContext context, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 176 | const PrintedPage& page, |
| 177 | PageOverlays::HorizontalPosition x, |
| 178 | PageOverlays::VerticalPosition y, |
[email protected] | 7322c440 | 2009-05-15 02:16:10 | [diff] [blame] | 179 | const gfx::Font& font) const; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 180 | |
[email protected] | e1504d8 | 2009-07-03 15:27:15 | [diff] [blame] | 181 | void DebugDump(const PrintedPage& page); |
| 182 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 183 | // All writable data member access must be guarded by this lock. Needs to be |
| 184 | // mutable since it can be acquired from const member functions. |
| 185 | mutable Lock lock_; |
| 186 | |
| 187 | // All the mutable members. |
| 188 | Mutable mutable_; |
| 189 | |
| 190 | // All the immutable members. |
| 191 | const Immutable immutable_; |
| 192 | |
| 193 | DISALLOW_EVIL_CONSTRUCTORS(PrintedDocument); |
| 194 | }; |
| 195 | |
| 196 | } // namespace printing |
| 197 | |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 198 | #endif // PRINTING_PRINTED_DOCUMENT_H_ |