Roll WebRTC 9745:9761, Libjingle 9742:9761
[chromium-blink-merge.git] / printing / printed_document.h
blobd309cc88fb0c72c08fbca060c669e0f951d60561
1 // Copyright (c) 2012 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/files/file_path.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/strings/string16.h"
14 #include "base/synchronization/lock.h"
15 #include "printing/print_settings.h"
16 #include "ui/gfx/native_widget_types.h"
18 namespace base {
19 class RefCountedMemory;
20 class TaskRunner;
23 namespace printing {
25 class MetafilePlayer;
26 class PrintedPage;
27 class PrintedPagesSource;
28 class PrintingContext;
30 // A collection of rendered pages. The settings are immutable. If the print
31 // settings are changed, a new PrintedDocument must be created.
32 // Warning: May be accessed from many threads at the same time. Only one thread
33 // will have write access. Sensible functions are protected by a lock.
34 // Warning: Once a page is loaded, it cannot be replaced. Pages may be discarded
35 // under low memory conditions.
36 class PRINTING_EXPORT PrintedDocument
37 : 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,
44 base::TaskRunner* blocking_runner);
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,
49 scoped_ptr<MetafilePlayer> metafile,
50 #if defined(OS_WIN)
51 float shrink,
52 #endif // OS_WIN
53 const gfx::Size& paper_size,
54 const gfx::Rect& page_rect);
56 // Retrieves a page. If the page is not available right now, it
57 // requests to have this page be rendered and returns NULL.
58 // Note: locks for a short amount of time.
59 scoped_refptr<PrintedPage> GetPage(int page_number);
61 // Draws the page in the context.
62 // Note: locks for a short amount of time in debug only.
63 #if defined(OS_WIN) || defined(OS_MACOSX) && !defined(USE_AURA)
64 void RenderPrintedPage(const PrintedPage& page,
65 gfx::NativeDrawingContext context) const;
66 #elif defined(OS_POSIX)
67 void RenderPrintedPage(const PrintedPage& page,
68 PrintingContext* context) const;
69 #endif
71 // Returns true if all the necessary pages for the settings are already
72 // rendered.
73 // Note: locks while parsing the whole tree.
74 bool IsComplete() const;
76 // Disconnects the PrintedPage source (PrintedPagesSource). It is done when
77 // the source is being destroyed.
78 void DisconnectSource();
80 // Sets the number of pages in the document to be rendered. Can only be set
81 // once.
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 base::string16& name() const { return immutable_.name_; }
98 int cookie() const { return immutable_.cookie_; }
100 // Sets a path where to dump printing output files for debugging. If never set
101 // no files are generated.
102 static void set_debug_dump_path(const base::FilePath& debug_dump_path);
104 // Creates debug file name from given |document_name| and |extension|.
105 // |extension| should include '.', example ".pdf"
106 // Returns empty |base::FilePath| if debug dumps is not enabled.
107 static base::FilePath CreateDebugDumpPath(
108 const base::string16& document_name,
109 const base::FilePath::StringType& extension);
111 // Dump data on blocking task runner if debug dumps enabled.
112 void DebugDumpData(const base::RefCountedMemory* data,
113 const base::FilePath::StringType& extension);
115 private:
116 friend class base::RefCountedThreadSafe<PrintedDocument>;
118 virtual ~PrintedDocument();
120 // Array of data for each print previewed page.
121 typedef std::map<int, scoped_refptr<PrintedPage> > PrintedPages;
123 // Contains all the mutable stuff. All this stuff MUST be accessed with the
124 // lock held.
125 struct Mutable {
126 explicit Mutable(PrintedPagesSource* source);
127 ~Mutable();
129 // Source that generates the PrintedPage's (i.e. a TabContents). It will be
130 // set back to NULL if the source is deleted before this object.
131 PrintedPagesSource* source_;
133 // Contains the pages' representation. This is a collection of PrintedPage.
134 // Warning: Lock must be held when accessing this member.
135 PrintedPages pages_;
137 // Number of expected pages to be rendered.
138 // Warning: Lock must be held when accessing this member.
139 int expected_page_count_;
141 // The total number of pages in the document.
142 int page_count_;
144 #if defined(OS_POSIX) && !defined(OS_MACOSX)
145 // Page number of the first page.
146 int first_page;
147 #endif
150 // Contains all the immutable stuff. All this stuff can be accessed without
151 // any lock held. This is because it can't be changed after the object's
152 // construction.
153 struct Immutable {
154 Immutable(const PrintSettings& settings,
155 PrintedPagesSource* source,
156 int cookie,
157 base::TaskRunner* blocking_runner);
158 ~Immutable();
160 // Print settings used to generate this document. Immutable.
161 PrintSettings settings_;
163 // Document name. Immutable.
164 base::string16 name_;
166 // Cookie to uniquely identify this document. It is used to make sure that a
167 // PrintedPage is correctly belonging to the PrintedDocument. Since
168 // PrintedPage generation is completely asynchronous, it could be easy to
169 // mess up and send the page to the wrong document. It can be viewed as a
170 // simpler hash of PrintSettings since a new document is made each time the
171 // print settings change.
172 int cookie_;
174 // Native thread for blocking operations, like file access.
175 scoped_refptr<base::TaskRunner> blocking_runner_;
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.
183 Mutable mutable_;
185 // All the immutable members.
186 const Immutable immutable_;
188 DISALLOW_COPY_AND_ASSIGN(PrintedDocument);
191 } // namespace printing
193 #endif // PRINTING_PRINTED_DOCUMENT_H_