blob: 599d4539051e3c780055df8f76391372bc286c32 [file] [log] [blame]
[email protected]225c8f52010-02-05 22:23:201// Copyright (c) 2010 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// 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
initial.commit09911bf2008-07-26 23:55:2910#include "base/ref_counted.h"
11#include "base/scoped_ptr.h"
[email protected]d9d42992010-09-13 19:39:1912#include "base/string16.h"
[email protected]20305ec2011-01-21 04:55:5213#include "base/synchronization/lock.h"
[email protected]5c7293a2010-03-17 06:40:5714#include "gfx/native_widget_types.h"
initial.commit09911bf2008-07-26 23:55:2915#include "googleurl/src/gurl.h"
[email protected]8ff1d422009-07-07 21:31:3916#include "printing/print_settings.h"
17#include "printing/native_metafile.h"
initial.commit09911bf2008-07-26 23:55:2918
[email protected]d9d42992010-09-13 19:39:1919class FilePath;
initial.commit09911bf2008-07-26 23:55:2920class MessageLoop;
21
22namespace gfx {
[email protected]7322c4402009-05-15 02:16:1023class Font;
initial.commit09911bf2008-07-26 23:55:2924}
25
26namespace printing {
27
28class PrintedPage;
29class PrintedPagesSource;
30
[email protected]4ecd07452009-03-31 14:34:4331// A collection of rendered pages. The settings are immutable. If the print
initial.commit09911bf2008-07-26 23:55:2932// settings are changed, a new PrintedDocument must be created.
33// Warning: May be accessed from many threads at the same time. Only one thread
34// will have write access. Sensible functions are protected by a lock.
35// Warning: Once a page is loaded, it cannot be replaced. Pages may be discarded
36// under low memory conditions.
37class PrintedDocument : public base::RefCountedThreadSafe<PrintedDocument> {
38 public:
39 // The cookie shall be unique and has a specific relationship with its
40 // originating source and settings.
41 PrintedDocument(const PrintSettings& settings,
42 PrintedPagesSource* source,
43 int cookie);
initial.commit09911bf2008-07-26 23:55:2944
[email protected]0e0fca32009-07-06 15:25:5045 // Sets a page's data. 0-based. Takes metafile ownership.
initial.commit09911bf2008-07-26 23:55:2946 // Note: locks for a short amount of time.
[email protected]40c7cfe2010-07-01 07:04:0547 void SetPage(int page_number, NativeMetafile* metafile, double shrink,
[email protected]3c5fb6b2010-07-02 05:32:3848 const gfx::Size& paper_size, const gfx::Rect& page_rect,
49 bool has_visible_overlays);
initial.commit09911bf2008-07-26 23:55:2950
51 // Retrieves a page. If the page is not available right now, it
52 // requests to have this page be rendered and returns false.
53 // Note: locks for a short amount of time.
54 bool GetPage(int page_number, scoped_refptr<PrintedPage>* page);
55
56 // Draws the page in the context.
57 // Note: locks for a short amount of time in debug only.
[email protected]b75dca82009-10-13 18:46:2158 void RenderPrintedPage(const PrintedPage& page,
59 gfx::NativeDrawingContext context) const;
initial.commit09911bf2008-07-26 23:55:2960
initial.commit09911bf2008-07-26 23:55:2961 // 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
initial.commit09911bf2008-07-26 23:55:2966 // 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.
[email protected]b5ab3982010-02-16 23:58:2772 uint32 MemoryUsage() const;
initial.commit09911bf2008-07-26 23:55:2973
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
[email protected]4ecd07452009-03-31 14:34:4389 // Getters. All these items are immutable hence thread-safe.
initial.commit09911bf2008-07-26 23:55:2990 const PrintSettings& settings() const { return immutable_.settings_; }
[email protected]d9d42992010-09-13 19:39:1991 const string16& name() const {
initial.commit09911bf2008-07-26 23:55:2992 return immutable_.name_;
93 }
94 const GURL& url() const { return immutable_.url_; }
[email protected]d9d42992010-09-13 19:39:1995 const string16& date() const { return immutable_.date_; }
96 const string16& time() const { return immutable_.time_; }
[email protected]225c8f52010-02-05 22:23:2097 int cookie() const { return immutable_.cookie_; }
initial.commit09911bf2008-07-26 23:55:2998
[email protected]e1504d82009-07-03 15:27:1599 // Sets a path where to dump printing output files for debugging. If never set
100 // no files are generated.
[email protected]d9d42992010-09-13 19:39:19101 static void set_debug_dump_path(const FilePath& debug_dump_path);
[email protected]e1504d82009-07-03 15:27:15102
[email protected]d9d42992010-09-13 19:39:19103 static const FilePath& debug_dump_path();
[email protected]e1504d82009-07-03 15:27:15104
initial.commit09911bf2008-07-26 23:55:29105 private:
[email protected]877d55d2009-11-05 21:53:08106 friend class base::RefCountedThreadSafe<PrintedDocument>;
107
[email protected]20f0487a2010-09-30 20:06:30108 virtual ~PrintedDocument();
[email protected]877d55d2009-11-05 21:53:08109
[email protected]0e0fca32009-07-06 15:25:50110 // Array of data for each print previewed page.
[email protected]8ff1d422009-07-07 21:31:39111 typedef std::map<int, scoped_refptr<PrintedPage> > PrintedPages;
initial.commit09911bf2008-07-26 23:55:29112
113 // Contains all the mutable stuff. All this stuff MUST be accessed with the
114 // lock held.
115 struct Mutable {
[email protected]5930cb62009-12-08 02:04:22116 explicit Mutable(PrintedPagesSource* source);
[email protected]20f0487a2010-09-30 20:06:30117 ~Mutable();
initial.commit09911bf2008-07-26 23:55:29118
[email protected]57c6a652009-05-04 07:58:34119 // Source that generates the PrintedPage's (i.e. a TabContents). It will be
initial.commit09911bf2008-07-26 23:55:29120 // set back to NULL if the source is deleted before this object.
121 PrintedPagesSource* source_;
122
123 // Contains the pages' representation. This is a collection of PrintedPage.
124 // Warning: Lock must be held when accessing this member.
125 PrintedPages pages_;
126
127 // Number of expected pages to be rendered.
128 // Warning: Lock must be held when accessing this member.
129 int expected_page_count_;
130
131 // The total number of pages in the document.
132 int page_count_;
133
134 // Shrink done in comparison to desired_dpi.
135 double shrink_factor;
136 };
137
138 // Contains all the immutable stuff. All this stuff can be accessed without
139 // any lock held. This is because it can't be changed after the object's
140 // construction.
141 struct Immutable {
142 Immutable(const PrintSettings& settings, PrintedPagesSource* source,
143 int cookie);
[email protected]20f0487a2010-09-30 20:06:30144 ~Immutable();
initial.commit09911bf2008-07-26 23:55:29145
[email protected]e0598782010-10-07 22:01:52146 // Sets the document's |date_| and |time_|.
147 void SetDocumentDate();
148
[email protected]4ecd07452009-03-31 14:34:43149 // Print settings used to generate this document. Immutable.
initial.commit09911bf2008-07-26 23:55:29150 PrintSettings settings_;
151
152 // Native thread for the render source.
153 MessageLoop* source_message_loop_;
154
[email protected]4ecd07452009-03-31 14:34:43155 // Document name. Immutable.
[email protected]d9d42992010-09-13 19:39:19156 string16 name_;
initial.commit09911bf2008-07-26 23:55:29157
[email protected]4ecd07452009-03-31 14:34:43158 // URL that generated this document. Immutable.
initial.commit09911bf2008-07-26 23:55:29159 GURL url_;
160
[email protected]4ecd07452009-03-31 14:34:43161 // The date on which this job started. Immutable.
[email protected]d9d42992010-09-13 19:39:19162 string16 date_;
initial.commit09911bf2008-07-26 23:55:29163
[email protected]4ecd07452009-03-31 14:34:43164 // The time at which this job started. Immutable.
[email protected]d9d42992010-09-13 19:39:19165 string16 time_;
initial.commit09911bf2008-07-26 23:55:29166
167 // Cookie to uniquely identify this document. It is used to make sure that a
168 // PrintedPage is correctly belonging to the PrintedDocument. Since
169 // PrintedPage generation is completely asynchronous, it could be easy to
170 // mess up and send the page to the wrong document. It can be viewed as a
171 // simpler hash of PrintSettings since a new document is made each time the
172 // print settings change.
173 int cookie_;
174 };
175
176 // Prints the headers and footers for one page in the specified context
177 // according to the current settings.
[email protected]b75dca82009-10-13 18:46:21178 void PrintHeaderFooter(gfx::NativeDrawingContext context,
initial.commit09911bf2008-07-26 23:55:29179 const PrintedPage& page,
180 PageOverlays::HorizontalPosition x,
181 PageOverlays::VerticalPosition y,
[email protected]7322c4402009-05-15 02:16:10182 const gfx::Font& font) const;
initial.commit09911bf2008-07-26 23:55:29183
[email protected]e0598782010-10-07 22:01:52184 // Draws the computed |text| into |context| taking into account the bounding
185 // region |bounds|. |bounds| is the position in which to draw |text| and
186 // the minimum area needed to contain |text| which may not be larger than the
187 // header or footer itself.
188 // TODO(jhawkins): string16.
189 void DrawHeaderFooter(gfx::NativeDrawingContext context,
190 std::wstring text,
191 gfx::Rect bounds) const;
192
[email protected]e1504d82009-07-03 15:27:15193 void DebugDump(const PrintedPage& page);
194
initial.commit09911bf2008-07-26 23:55:29195 // All writable data member access must be guarded by this lock. Needs to be
196 // mutable since it can be acquired from const member functions.
[email protected]20305ec2011-01-21 04:55:52197 mutable base::Lock lock_;
initial.commit09911bf2008-07-26 23:55:29198
199 // All the mutable members.
200 Mutable mutable_;
201
202 // All the immutable members.
203 const Immutable immutable_;
204
[email protected]5930cb62009-12-08 02:04:22205 DISALLOW_COPY_AND_ASSIGN(PrintedDocument);
initial.commit09911bf2008-07-26 23:55:29206};
207
208} // namespace printing
209
[email protected]8ff1d422009-07-07 21:31:39210#endif // PRINTING_PRINTED_DOCUMENT_H_