blob: 6e20d334bb970ef064d1098c6bddd9188c51958f [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
[email protected]8ff1d422009-07-07 21:31:395#ifndef PRINTING_PRINTED_DOCUMENT_H_
6#define PRINTING_PRINTED_DOCUMENT_H_
initial.commit09911bf2008-07-26 23:55:297
8#include <map>
9
[email protected]b75dca82009-10-13 18:46:2110#include "app/gfx/native_widget_types.h"
initial.commit09911bf2008-07-26 23:55:2911#include "base/lock.h"
12#include "base/ref_counted.h"
13#include "base/scoped_ptr.h"
initial.commit09911bf2008-07-26 23:55:2914#include "googleurl/src/gurl.h"
[email protected]8ff1d422009-07-07 21:31:3915#include "printing/print_settings.h"
16#include "printing/native_metafile.h"
initial.commit09911bf2008-07-26 23:55:2917
initial.commit09911bf2008-07-26 23:55:2918class MessageLoop;
19
20namespace gfx {
[email protected]7322c4402009-05-15 02:16:1021class Font;
initial.commit09911bf2008-07-26 23:55:2922}
23
24namespace printing {
25
26class PrintedPage;
27class PrintedPagesSource;
28
[email protected]4ecd07452009-03-31 14:34:4329// A collection of rendered pages. The settings are immutable. If the print
initial.commit09911bf2008-07-26 23:55:2930// 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.
35class 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);
42 ~PrintedDocument();
43
[email protected]0e0fca32009-07-06 15:25:5044 // Sets a page's data. 0-based. Takes metafile ownership.
initial.commit09911bf2008-07-26 23:55:2945 // Note: locks for a short amount of time.
[email protected]0e0fca32009-07-06 15:25:5046 void SetPage(int page_number, NativeMetafile* metafile, double shrink);
initial.commit09911bf2008-07-26 23:55:2947
48 // Retrieves a page. If the page is not available right now, it
49 // requests to have this page be rendered and returns false.
50 // Note: locks for a short amount of time.
51 bool GetPage(int page_number, scoped_refptr<PrintedPage>* page);
52
53 // Draws the page in the context.
54 // Note: locks for a short amount of time in debug only.
[email protected]b75dca82009-10-13 18:46:2155 void RenderPrintedPage(const PrintedPage& page,
56 gfx::NativeDrawingContext context) const;
initial.commit09911bf2008-07-26 23:55:2957
58 // Draws the page in the context. If the page is not available right now, it
59 // requests to have this page be rendered and returns false.
60 // Note: locks for a short amount of time.
[email protected]b75dca82009-10-13 18:46:2161 bool RenderPrintedPageNumber(int page_number,
62 gfx::NativeDrawingContext context);
initial.commit09911bf2008-07-26 23:55:2963
64 // Returns true if all the necessary pages for the settings are already
65 // rendered.
66 // Note: locks while parsing the whole tree.
67 bool IsComplete() const;
68
initial.commit09911bf2008-07-26 23:55:2969 // Disconnects the PrintedPage source (PrintedPagesSource). It is done when
70 // the source is being destroyed.
71 void DisconnectSource();
72
73 // Retrieves the current memory usage of the renderer pages.
74 // Note: locks for a short amount of time.
75 size_t MemoryUsage() const;
76
77 // Sets the number of pages in the document to be rendered. Can only be set
78 // once.
79 // Note: locks for a short amount of time.
80 void set_page_count(int max_page);
81
82 // Number of pages in the document. Used for headers/footers.
83 // Note: locks for a short amount of time.
84 int page_count() const;
85
86 // Returns the number of expected pages to be rendered. It is a non-linear
87 // series if settings().ranges is not empty. It is the same value as
88 // document_page_count() otherwise.
89 // Note: locks for a short amount of time.
90 int expected_page_count() const;
91
[email protected]4ecd07452009-03-31 14:34:4392 // Getters. All these items are immutable hence thread-safe.
initial.commit09911bf2008-07-26 23:55:2993 const PrintSettings& settings() const { return immutable_.settings_; }
94 const std::wstring& name() const {
95 return immutable_.name_;
96 }
97 const GURL& url() const { return immutable_.url_; }
98 const std::wstring& date() const { return immutable_.date_; }
99 const std::wstring& time() const { return immutable_.time_; }
100 const int cookie() const { return immutable_.cookie_; }
101
[email protected]e1504d82009-07-03 15:27:15102 // Sets a path where to dump printing output files for debugging. If never set
103 // no files are generated.
104 static void set_debug_dump_path(const std::wstring& debug_dump_path);
105
106 static const std::wstring& debug_dump_path();
107
initial.commit09911bf2008-07-26 23:55:29108 private:
[email protected]0e0fca32009-07-06 15:25:50109 // Array of data for each print previewed page.
[email protected]8ff1d422009-07-07 21:31:39110 typedef std::map<int, scoped_refptr<PrintedPage> > PrintedPages;
initial.commit09911bf2008-07-26 23:55:29111
112 // Contains all the mutable stuff. All this stuff MUST be accessed with the
113 // lock held.
114 struct Mutable {
115 Mutable(PrintedPagesSource* source);
116
[email protected]57c6a652009-05-04 07:58:34117 // Source that generates the PrintedPage's (i.e. a TabContents). It will be
initial.commit09911bf2008-07-26 23:55:29118 // set back to NULL if the source is deleted before this object.
119 PrintedPagesSource* source_;
120
121 // Contains the pages' representation. This is a collection of PrintedPage.
122 // Warning: Lock must be held when accessing this member.
123 PrintedPages pages_;
124
125 // Number of expected pages to be rendered.
126 // Warning: Lock must be held when accessing this member.
127 int expected_page_count_;
128
129 // The total number of pages in the document.
130 int page_count_;
131
132 // Shrink done in comparison to desired_dpi.
133 double shrink_factor;
134 };
135
136 // Contains all the immutable stuff. All this stuff can be accessed without
137 // any lock held. This is because it can't be changed after the object's
138 // construction.
139 struct Immutable {
140 Immutable(const PrintSettings& settings, PrintedPagesSource* source,
141 int cookie);
142
[email protected]4ecd07452009-03-31 14:34:43143 // Print settings used to generate this document. Immutable.
initial.commit09911bf2008-07-26 23:55:29144 PrintSettings settings_;
145
146 // Native thread for the render source.
147 MessageLoop* source_message_loop_;
148
[email protected]4ecd07452009-03-31 14:34:43149 // Document name. Immutable.
initial.commit09911bf2008-07-26 23:55:29150 std::wstring name_;
151
[email protected]4ecd07452009-03-31 14:34:43152 // URL that generated this document. Immutable.
initial.commit09911bf2008-07-26 23:55:29153 GURL url_;
154
[email protected]4ecd07452009-03-31 14:34:43155 // The date on which this job started. Immutable.
initial.commit09911bf2008-07-26 23:55:29156 std::wstring date_;
157
[email protected]4ecd07452009-03-31 14:34:43158 // The time at which this job started. Immutable.
initial.commit09911bf2008-07-26 23:55:29159 std::wstring time_;
160
161 // Cookie to uniquely identify this document. It is used to make sure that a
162 // PrintedPage is correctly belonging to the PrintedDocument. Since
163 // PrintedPage generation is completely asynchronous, it could be easy to
164 // mess up and send the page to the wrong document. It can be viewed as a
165 // simpler hash of PrintSettings since a new document is made each time the
166 // print settings change.
167 int cookie_;
168 };
169
170 // Prints the headers and footers for one page in the specified context
171 // according to the current settings.
[email protected]b75dca82009-10-13 18:46:21172 void PrintHeaderFooter(gfx::NativeDrawingContext context,
initial.commit09911bf2008-07-26 23:55:29173 const PrintedPage& page,
174 PageOverlays::HorizontalPosition x,
175 PageOverlays::VerticalPosition y,
[email protected]7322c4402009-05-15 02:16:10176 const gfx::Font& font) const;
initial.commit09911bf2008-07-26 23:55:29177
[email protected]e1504d82009-07-03 15:27:15178 void DebugDump(const PrintedPage& page);
179
initial.commit09911bf2008-07-26 23:55:29180 // 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
[email protected]8ff1d422009-07-07 21:31:39195#endif // PRINTING_PRINTED_DOCUMENT_H_