Updating trunk VERSION from 1010.0 to 1011.0
[chromium-blink-merge.git] / printing / printed_document.h
blob3a61541d59373ebfce817679079e351a991ba089
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_
8 #include <map>
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 "printing/print_settings.h"
15 #include "ui/gfx/native_widget_types.h"
17 class FilePath;
18 class MessageLoop;
20 namespace printing {
22 class Metafile;
23 class PrintedPage;
24 class PrintedPagesSource;
25 class PrintingContext;
27 // A collection of rendered pages. The settings are immutable. If the print
28 // settings are changed, a new PrintedDocument must be created.
29 // Warning: May be accessed from many threads at the same time. Only one thread
30 // will have write access. Sensible functions are protected by a lock.
31 // Warning: Once a page is loaded, it cannot be replaced. Pages may be discarded
32 // under low memory conditions.
33 class PRINTING_EXPORT PrintedDocument
34 : 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);
42 // Sets a page's data. 0-based. Takes metafile ownership.
43 // Note: locks for a short amount of time.
44 void SetPage(int page_number, Metafile* metafile, double shrink,
45 const gfx::Size& paper_size, const gfx::Rect& page_rect);
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);
52 // Draws the page in the context.
53 // Note: locks for a short amount of time in debug only.
54 #if defined(OS_WIN) || defined(OS_MACOSX) && !defined(USE_AURA)
55 void RenderPrintedPage(const PrintedPage& page,
56 gfx::NativeDrawingContext context) const;
57 #elif defined(OS_POSIX)
58 void RenderPrintedPage(const PrintedPage& page,
59 PrintingContext* context) const;
60 #endif
62 // Returns true if all the necessary pages for the settings are already
63 // rendered.
64 // Note: locks while parsing the whole tree.
65 bool IsComplete() const;
67 // Disconnects the PrintedPage source (PrintedPagesSource). It is done when
68 // the source is being destroyed.
69 void DisconnectSource();
71 // Retrieves the current memory usage of the renderer pages.
72 // Note: locks for a short amount of time.
73 uint32 MemoryUsage() const;
75 // Sets the number of pages in the document to be rendered. Can only be set
76 // once.
77 // Note: locks for a short amount of time.
78 void set_page_count(int max_page);
80 // Number of pages in the document. Used for headers/footers.
81 // Note: locks for a short amount of time.
82 int page_count() const;
84 // Returns the number of expected pages to be rendered. It is a non-linear
85 // series if settings().ranges is not empty. It is the same value as
86 // document_page_count() otherwise.
87 // Note: locks for a short amount of time.
88 int expected_page_count() const;
90 // Getters. All these items are immutable hence thread-safe.
91 const PrintSettings& settings() const { return immutable_.settings_; }
92 const string16& name() const { return immutable_.name_; }
93 int cookie() const { return immutable_.cookie_; }
95 // Sets a path where to dump printing output files for debugging. If never set
96 // no files are generated.
97 static void set_debug_dump_path(const FilePath& debug_dump_path);
99 static const FilePath& debug_dump_path();
101 private:
102 friend class base::RefCountedThreadSafe<PrintedDocument>;
104 virtual ~PrintedDocument();
106 // Array of data for each print previewed page.
107 typedef std::map<int, scoped_refptr<PrintedPage> > PrintedPages;
109 // Contains all the mutable stuff. All this stuff MUST be accessed with the
110 // lock held.
111 struct Mutable {
112 explicit Mutable(PrintedPagesSource* source);
113 ~Mutable();
115 // Source that generates the PrintedPage's (i.e. a TabContents). It will be
116 // set back to NULL if the source is deleted before this object.
117 PrintedPagesSource* source_;
119 // Contains the pages' representation. This is a collection of PrintedPage.
120 // Warning: Lock must be held when accessing this member.
121 PrintedPages pages_;
123 // Number of expected pages to be rendered.
124 // Warning: Lock must be held when accessing this member.
125 int expected_page_count_;
127 // The total number of pages in the document.
128 int page_count_;
130 #if defined(OS_POSIX) && !defined(OS_MACOSX)
131 // Page number of the first page.
132 int first_page;
133 #endif
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 ~Immutable();
144 // Print settings used to generate this document. Immutable.
145 PrintSettings settings_;
147 // Native thread for the render source.
148 MessageLoop* source_message_loop_;
150 // Document name. Immutable.
151 string16 name_;
153 // Cookie to uniquely identify this document. It is used to make sure that a
154 // PrintedPage is correctly belonging to the PrintedDocument. Since
155 // PrintedPage generation is completely asynchronous, it could be easy to
156 // mess up and send the page to the wrong document. It can be viewed as a
157 // simpler hash of PrintSettings since a new document is made each time the
158 // print settings change.
159 int cookie_;
162 void DebugDump(const PrintedPage& page);
164 // All writable data member access must be guarded by this lock. Needs to be
165 // mutable since it can be acquired from const member functions.
166 mutable base::Lock lock_;
168 // All the mutable members.
169 Mutable mutable_;
171 // All the immutable members.
172 const Immutable immutable_;
174 DISALLOW_COPY_AND_ASSIGN(PrintedDocument);
177 } // namespace printing
179 #endif // PRINTING_PRINTED_DOCUMENT_H_