OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef CHROME_BROWSER_PRINTING_PRINTED_DOCUMENT_H__ | |
6 #define CHROME_BROWSER_PRINTING_PRINTED_DOCUMENT_H__ | |
7 | |
8 #include <map> | |
9 | |
10 #include "base/lock.h" | |
11 #include "base/ref_counted.h" | |
12 #include "base/scoped_ptr.h" | |
13 #include "chrome/browser/printing/print_settings.h" | |
14 #include "printing/native_metafile.h" | |
15 #include "googleurl/src/gurl.h" | |
16 | |
17 class MessageLoop; | |
18 | |
19 namespace gfx { | |
20 class Font; | |
21 } | |
22 | |
23 namespace printing { | |
24 | |
25 class PrintedPage; | |
26 class PrintedPagesSource; | |
27 | |
28 // A collection of rendered pages. The settings are immutable. If the print | |
29 // settings are changed, a new PrintedDocument must be created. | |
30 // Warning: May be accessed from many threads at the same time. Only one thread | |
31 // will have write access. Sensible functions are protected by a lock. | |
32 // Warning: Once a page is loaded, it cannot be replaced. Pages may be discarded | |
33 // under low memory conditions. | |
34 class PrintedDocument : public base::RefCountedThreadSafe<PrintedDocument> { | |
35 public: | |
36 // The cookie shall be unique and has a specific relationship with its | |
37 // originating source and settings. | |
38 PrintedDocument(const PrintSettings& settings, | |
39 PrintedPagesSource* source, | |
40 int cookie); | |
41 ~PrintedDocument(); | |
42 | |
43 // Sets a page's data. 0-based. Takes metafile ownership. | |
44 // Note: locks for a short amount of time. | |
45 void SetPage(int page_number, NativeMetafile* metafile, double shrink); | |
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. | |
54 void RenderPrintedPage(const PrintedPage& page, HDC context) const; | |
55 | |
56 // Draws the page in the context. If the page is not available right now, it | |
57 // requests to have this page be rendered and returns false. | |
58 // Note: locks for a short amount of time. | |
59 bool RenderPrintedPageNumber(int page_number, HDC context); | |
60 | |
61 // Returns true if all the necessary pages for the settings are already | |
62 // rendered. | |
63 // Note: locks while parsing the whole tree. | |
64 bool IsComplete() const; | |
65 | |
66 // Disconnects the PrintedPage source (PrintedPagesSource). It is done when | |
67 // the source is being destroyed. | |
68 void DisconnectSource(); | |
69 | |
70 // Retrieves the current memory usage of the renderer pages. | |
71 // Note: locks for a short amount of time. | |
72 size_t MemoryUsage() const; | |
73 | |
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 | |
89 // Getters. All these items are immutable hence thread-safe. | |
90 const PrintSettings& settings() const { return immutable_.settings_; } | |
91 const std::wstring& name() const { | |
92 return immutable_.name_; | |
93 } | |
94 const GURL& url() const { return immutable_.url_; } | |
95 const std::wstring& date() const { return immutable_.date_; } | |
96 const std::wstring& time() const { return immutable_.time_; } | |
97 const int cookie() const { return immutable_.cookie_; } | |
98 | |
99 // Sets a path where to dump printing output files for debugging. If never set | |
100 // no files are generated. | |
101 static void set_debug_dump_path(const std::wstring& debug_dump_path); | |
102 | |
103 static const std::wstring& debug_dump_path(); | |
104 | |
105 private: | |
106 // Array of data for each print previewed page. | |
107 typedef std::map<int, scoped_refptr<PrintedPage>> PrintedPages; | |
108 | |
109 // Contains all the mutable stuff. All this stuff MUST be accessed with the | |
110 // lock held. | |
111 struct Mutable { | |
112 Mutable(PrintedPagesSource* source); | |
113 | |
114 // Source that generates the PrintedPage's (i.e. a TabContents). It will be | |
115 // set back to NULL if the source is deleted before this object. | |
116 PrintedPagesSource* source_; | |
117 | |
118 // Contains the pages' representation. This is a collection of PrintedPage. | |
119 // Warning: Lock must be held when accessing this member. | |
120 PrintedPages pages_; | |
121 | |
122 // Number of expected pages to be rendered. | |
123 // Warning: Lock must be held when accessing this member. | |
124 int expected_page_count_; | |
125 | |
126 // The total number of pages in the document. | |
127 int page_count_; | |
128 | |
129 // Shrink done in comparison to desired_dpi. | |
130 double shrink_factor; | |
131 }; | |
132 | |
133 // Contains all the immutable stuff. All this stuff can be accessed without | |
134 // any lock held. This is because it can't be changed after the object's | |
135 // construction. | |
136 struct Immutable { | |
137 Immutable(const PrintSettings& settings, PrintedPagesSource* source, | |
138 int cookie); | |
139 | |
140 // Print settings used to generate this document. Immutable. | |
141 PrintSettings settings_; | |
142 | |
143 // Native thread for the render source. | |
144 MessageLoop* source_message_loop_; | |
145 | |
146 // Document name. Immutable. | |
147 std::wstring name_; | |
148 | |
149 // URL that generated this document. Immutable. | |
150 GURL url_; | |
151 | |
152 // The date on which this job started. Immutable. | |
153 std::wstring date_; | |
154 | |
155 // The time at which this job started. Immutable. | |
156 std::wstring time_; | |
157 | |
158 // Cookie to uniquely identify this document. It is used to make sure that a | |
159 // PrintedPage is correctly belonging to the PrintedDocument. Since | |
160 // PrintedPage generation is completely asynchronous, it could be easy to | |
161 // mess up and send the page to the wrong document. It can be viewed as a | |
162 // simpler hash of PrintSettings since a new document is made each time the | |
163 // print settings change. | |
164 int cookie_; | |
165 }; | |
166 | |
167 // Prints the headers and footers for one page in the specified context | |
168 // according to the current settings. | |
169 void PrintHeaderFooter(HDC context, | |
170 const PrintedPage& page, | |
171 PageOverlays::HorizontalPosition x, | |
172 PageOverlays::VerticalPosition y, | |
173 const gfx::Font& font) const; | |
174 | |
175 void DebugDump(const PrintedPage& page); | |
176 | |
177 // All writable data member access must be guarded by this lock. Needs to be | |
178 // mutable since it can be acquired from const member functions. | |
179 mutable Lock lock_; | |
180 | |
181 // All the mutable members. | |
182 Mutable mutable_; | |
183 | |
184 // All the immutable members. | |
185 const Immutable immutable_; | |
186 | |
187 DISALLOW_EVIL_CONSTRUCTORS(PrintedDocument); | |
188 }; | |
189 | |
190 } // namespace printing | |
191 | |
192 #endif // CHROME_BROWSER_PRINTING_PRINTED_DOCUMENT_H__ | |
OLD | NEW |