1 // Copyright (c) 2011 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.
5 #ifndef PRINTING_PRINTED_DOCUMENT_H_
6 #define PRINTING_PRINTED_DOCUMENT_H_
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/string16.h"
13 #include "base/synchronization/lock.h"
14 #include "googleurl/src/gurl.h"
15 #include "printing/print_settings.h"
16 #include "ui/gfx/native_widget_types.h"
29 class PrintedPagesSource
;
30 class PrintingContext
;
32 // A collection of rendered pages. The settings are immutable. If the print
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
> {
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
,
46 // Sets a page's data. 0-based. Takes metafile ownership.
47 // Note: locks for a short amount of time.
48 void SetPage(int page_number
, Metafile
* metafile
, double shrink
,
49 const gfx::Size
& paper_size
, const gfx::Rect
& page_rect
,
50 bool has_visible_overlays
);
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
);
57 // Draws the page in the context.
58 // Note: locks for a short amount of time in debug only.
59 #if defined(OS_WIN) || defined(OS_MACOSX)
60 void RenderPrintedPage(const PrintedPage
& page
,
61 gfx::NativeDrawingContext context
) const;
62 #elif defined(OS_POSIX)
63 void RenderPrintedPage(const PrintedPage
& page
,
64 PrintingContext
* context
) const;
67 // Returns true if all the necessary pages for the settings are already
69 // Note: locks while parsing the whole tree.
70 bool IsComplete() const;
72 // Disconnects the PrintedPage source (PrintedPagesSource). It is done when
73 // the source is being destroyed.
74 void DisconnectSource();
76 // Retrieves the current memory usage of the renderer pages.
77 // Note: locks for a short amount of time.
78 uint32
MemoryUsage() const;
80 // Sets the number of pages in the document to be rendered. Can only be set
82 // Note: locks for a short amount of time.
83 void set_page_count(int max_page
);
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;
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;
95 // Getters. All these items are immutable hence thread-safe.
96 const PrintSettings
& settings() const { return immutable_
.settings_
; }
97 const string16
& name() const {
98 return immutable_
.name_
;
100 const GURL
& url() const { return immutable_
.url_
; }
101 int cookie() const { return immutable_
.cookie_
; }
103 // Sets a path where to dump printing output files for debugging. If never set
104 // no files are generated.
105 static void set_debug_dump_path(const FilePath
& debug_dump_path
);
107 static const FilePath
& debug_dump_path();
110 friend class base::RefCountedThreadSafe
<PrintedDocument
>;
112 virtual ~PrintedDocument();
114 // Array of data for each print previewed page.
115 typedef std::map
<int, scoped_refptr
<PrintedPage
> > PrintedPages
;
117 // Contains all the mutable stuff. All this stuff MUST be accessed with the
120 explicit Mutable(PrintedPagesSource
* source
);
123 // Source that generates the PrintedPage's (i.e. a TabContents). It will be
124 // set back to NULL if the source is deleted before this object.
125 PrintedPagesSource
* source_
;
127 // Contains the pages' representation. This is a collection of PrintedPage.
128 // Warning: Lock must be held when accessing this member.
131 // Number of expected pages to be rendered.
132 // Warning: Lock must be held when accessing this member.
133 int expected_page_count_
;
135 // The total number of pages in the document.
138 // Shrink done in comparison to desired_dpi.
139 double shrink_factor
;
141 #if defined(OS_POSIX) && !defined(OS_MACOSX)
142 // Page number of the first page.
147 // Contains all the immutable stuff. All this stuff can be accessed without
148 // any lock held. This is because it can't be changed after the object's
151 Immutable(const PrintSettings
& settings
, PrintedPagesSource
* source
,
155 // Print settings used to generate this document. Immutable.
156 PrintSettings settings_
;
158 // Native thread for the render source.
159 MessageLoop
* source_message_loop_
;
161 // Document name. Immutable.
164 // URL that generated this document. Immutable.
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.
176 void DebugDump(const PrintedPage
& page
);
178 // All writable data member access must be guarded by this lock. Needs to be
179 // mutable since it can be acquired from const member functions.
180 mutable base::Lock lock_
;
182 // All the mutable members.
185 // All the immutable members.
186 const Immutable immutable_
;
188 DISALLOW_COPY_AND_ASSIGN(PrintedDocument
);
191 } // namespace printing
193 #endif // PRINTING_PRINTED_DOCUMENT_H_