blob: 41fb570492f864a10f40143dc4cd3567afca62db [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// 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.commit09911bf2008-07-26 23:55:294
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 "googleurl/src/gurl.h"
15
16class ChromeFont;
17class MessageLoop;
18
19namespace gfx {
20class Emf;
21}
22
23namespace printing {
24
25class PrintedPage;
26class PrintedPagesSource;
27
28// A collection of rendered pages. The settings are immuable. 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.
34class 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 emf ownership.
44 // Note: locks for a short amount of time.
45 void SetPage(int page_number, gfx::Emf* emf, 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 // Requests all the missing pages. Returns true if at least one page has been
67 // requested. Returns false if there was not enough information to request the
68 // missing pages, i.e. document_page_count_ is not initialized or no page has
69 // been requested.
70 // Note: locks while parsing the whole tree.
71 bool RequestMissingPages();
72
73 // Disconnects the PrintedPage source (PrintedPagesSource). It is done when
74 // the source is being destroyed.
75 void DisconnectSource();
76
77 // Retrieves the current memory usage of the renderer pages.
78 // Note: locks for a short amount of time.
79 size_t MemoryUsage() const;
80
81 // Sets the number of pages in the document to be rendered. Can only be set
82 // once.
83 // Note: locks for a short amount of time.
84 void set_page_count(int max_page);
85
86 // Number of pages in the document. Used for headers/footers.
87 // Note: locks for a short amount of time.
88 int page_count() const;
89
90 // Returns the number of expected pages to be rendered. It is a non-linear
91 // series if settings().ranges is not empty. It is the same value as
92 // document_page_count() otherwise.
93 // Note: locks for a short amount of time.
94 int expected_page_count() const;
95
96 // Getters. All these items are immuable hence thread-safe.
97 const PrintSettings& settings() const { return immutable_.settings_; }
98 const std::wstring& name() const {
99 return immutable_.name_;
100 }
101 const GURL& url() const { return immutable_.url_; }
102 const std::wstring& date() const { return immutable_.date_; }
103 const std::wstring& time() const { return immutable_.time_; }
104 const int cookie() const { return immutable_.cookie_; }
105
106 private:
107 // Array of EMF data for each print previewed page.
108 typedef std::map<int, scoped_refptr<PrintedPage>> PrintedPages;
109
110 // Contains all the mutable stuff. All this stuff MUST be accessed with the
111 // lock held.
112 struct Mutable {
113 Mutable(PrintedPagesSource* source);
114
115 // Source that generates the PrintedPage's (i.e. a WebContents). It will be
116 // set back to NULL if the source is deleted before this object.
117 PrintedPagesSource* source_;
118
119 // Contains the pages' representation. This is a collection of PrintedPage.
120 // Warning: Lock must be held when accessing this member.
121 PrintedPages pages_;
122
123 // Number of expected pages to be rendered.
124 // Warning: Lock must be held when accessing this member.
125 int expected_page_count_;
126
127 // The total number of pages in the document.
128 int page_count_;
129
130 // Shrink done in comparison to desired_dpi.
131 double shrink_factor;
132 };
133
134 // Contains all the immutable stuff. All this stuff can be accessed without
135 // any lock held. This is because it can't be changed after the object's
136 // construction.
137 struct Immutable {
138 Immutable(const PrintSettings& settings, PrintedPagesSource* source,
139 int cookie);
140
141 // Print settings used to generate this document. Immuable.
142 PrintSettings settings_;
143
144 // Native thread for the render source.
145 MessageLoop* source_message_loop_;
146
147 // Document name. Immuable.
148 std::wstring name_;
149
150 // URL that generated this document. Immuable.
151 GURL url_;
152
153 // The date on which this job started. Immuable.
154 std::wstring date_;
155
156 // The time at which this job started. Immuable.
157 std::wstring time_;
158
159 // Cookie to uniquely identify this document. It is used to make sure that a
160 // PrintedPage is correctly belonging to the PrintedDocument. Since
161 // PrintedPage generation is completely asynchronous, it could be easy to
162 // mess up and send the page to the wrong document. It can be viewed as a
163 // simpler hash of PrintSettings since a new document is made each time the
164 // print settings change.
165 int cookie_;
166 };
167
168 // Prints the headers and footers for one page in the specified context
169 // according to the current settings.
170 void PrintHeaderFooter(HDC context,
171 const PrintedPage& page,
172 PageOverlays::HorizontalPosition x,
173 PageOverlays::VerticalPosition y,
174 const ChromeFont& font) const;
175
176 // Calls the render source to render a page. Makes sure to execute the call in
177 // the right thread context.
178 void PrintPage_ThreadJump(int page_number);
179
180 // All writable data member access must be guarded by this lock. Needs to be
181 // mutable since it can be acquired from const member functions.
182 mutable Lock lock_;
183
184 // All the mutable members.
185 Mutable mutable_;
186
187 // All the immutable members.
188 const Immutable immutable_;
189
190 DISALLOW_EVIL_CONSTRUCTORS(PrintedDocument);
191};
192
193} // namespace printing
194
195#endif // CHROME_BROWSER_PRINTING_PRINTED_DOCUMENT_H__
license.botbf09a502008-08-24 00:55:55196