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