blob: 4858376d926adc5ea1160a4a733f44176ff6d04c [file] [log] [blame]
[email protected]71f40a72012-05-16 07:26:591// Copyright (c) 2012 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>
dchengc3df9ba2016-04-07 23:09:329#include <memory>
initial.commit09911bf2008-07-26 23:55:2910
[email protected]c95198b2014-06-12 16:56:5511#include "base/files/file_path.h"
avi126e93c2015-12-21 21:48:1612#include "base/macros.h"
[email protected]3b63f8f42011-03-28 01:54:1513#include "base/memory/ref_counted.h"
[email protected]896d161f2013-06-11 22:52:2414#include "base/strings/string16.h"
[email protected]20305ec2011-01-21 04:55:5215#include "base/synchronization/lock.h"
Thiago Farina45e319532017-03-23 02:44:4916#include "printing/native_drawing_context.h"
[email protected]8ff1d422009-07-07 21:31:3917#include "printing/print_settings.h"
initial.commit09911bf2008-07-26 23:55:2918
[email protected]a3ef4832013-02-02 05:12:3319namespace base {
[email protected]c95198b2014-06-12 16:56:5520class RefCountedMemory;
[email protected]a3ef4832013-02-02 05:12:3321}
22
initial.commit09911bf2008-07-26 23:55:2923namespace printing {
24
vitalybuka5d1290582014-09-12 09:19:5925class MetafilePlayer;
initial.commit09911bf2008-07-26 23:55:2926class PrintedPage;
[email protected]5cc4c422011-02-19 00:09:2227class PrintingContext;
initial.commit09911bf2008-07-26 23:55:2928
[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.
[email protected]69f5b1e62011-09-01 06:34:0435class PRINTING_EXPORT PrintedDocument
36 : public base::RefCountedThreadSafe<PrintedDocument> {
initial.commit09911bf2008-07-26 23:55:2937 public:
38 // The cookie shall be unique and has a specific relationship with its
39 // originating source and settings.
40 PrintedDocument(const PrintSettings& settings,
Vladislav Kuzkokovccceef02017-10-04 15:30:1041 const base::string16& name,
Xiyuan Xia863ce9d2017-06-20 22:36:0042 int cookie);
initial.commit09911bf2008-07-26 23:55:2943
[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]9de1347a2014-06-12 09:49:0846 void SetPage(int page_number,
dchengc3df9ba2016-04-07 23:09:3247 std::unique_ptr<MetafilePlayer> metafile,
[email protected]9de1347a2014-06-12 09:49:0848#if defined(OS_WIN)
pkasting46a62912014-10-09 21:30:0049 float shrink,
[email protected]9de1347a2014-06-12 09:49:0850#endif // OS_WIN
51 const gfx::Size& paper_size,
52 const gfx::Rect& page_rect);
initial.commit09911bf2008-07-26 23:55:2953
54 // Retrieves a page. If the page is not available right now, it
[email protected]c95198b2014-06-12 16:56:5555 // requests to have this page be rendered and returns NULL.
initial.commit09911bf2008-07-26 23:55:2956 // Note: locks for a short amount of time.
[email protected]c95198b2014-06-12 16:56:5557 scoped_refptr<PrintedPage> GetPage(int page_number);
initial.commit09911bf2008-07-26 23:55:2958
59 // Draws the page in the context.
60 // Note: locks for a short amount of time in debug only.
[email protected]2f47fc452011-11-21 03:12:1761#if defined(OS_WIN) || defined(OS_MACOSX) && !defined(USE_AURA)
[email protected]b75dca82009-10-13 18:46:2162 void RenderPrintedPage(const PrintedPage& page,
Nico Weber8e559562017-10-03 01:25:2663 printing::NativeDrawingContext context) const;
[email protected]5cc4c422011-02-19 00:09:2264#elif defined(OS_POSIX)
65 void RenderPrintedPage(const PrintedPage& page,
66 PrintingContext* context) const;
67#endif
initial.commit09911bf2008-07-26 23:55:2968
initial.commit09911bf2008-07-26 23:55:2969 // Returns true if all the necessary pages for the settings are already
70 // rendered.
71 // Note: locks while parsing the whole tree.
72 bool IsComplete() const;
73
initial.commit09911bf2008-07-26 23:55:2974 // 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]b5fa4ee2013-10-01 07:19:0791 const base::string16& name() const { return immutable_.name_; }
[email protected]225c8f52010-02-05 22:23:2092 int cookie() const { return immutable_.cookie_; }
initial.commit09911bf2008-07-26 23:55:2993
[email protected]e1504d82009-07-03 15:27:1594 // Sets a path where to dump printing output files for debugging. If never set
95 // no files are generated.
[email protected]a3ef4832013-02-02 05:12:3396 static void set_debug_dump_path(const base::FilePath& debug_dump_path);
[email protected]e1504d82009-07-03 15:27:1597
[email protected]c95198b2014-06-12 16:56:5598 // Creates debug file name from given |document_name| and |extension|.
99 // |extension| should include '.', example ".pdf"
100 // Returns empty |base::FilePath| if debug dumps is not enabled.
101 static base::FilePath CreateDebugDumpPath(
102 const base::string16& document_name,
103 const base::FilePath::StringType& extension);
104
105 // Dump data on blocking task runner if debug dumps enabled.
106 void DebugDumpData(const base::RefCountedMemory* data,
107 const base::FilePath::StringType& extension);
[email protected]e1504d82009-07-03 15:27:15108
initial.commit09911bf2008-07-26 23:55:29109 private:
[email protected]877d55d2009-11-05 21:53:08110 friend class base::RefCountedThreadSafe<PrintedDocument>;
111
[email protected]20f0487a2010-09-30 20:06:30112 virtual ~PrintedDocument();
[email protected]877d55d2009-11-05 21:53:08113
[email protected]0e0fca32009-07-06 15:25:50114 // Array of data for each print previewed page.
[email protected]8ff1d422009-07-07 21:31:39115 typedef std::map<int, scoped_refptr<PrintedPage> > PrintedPages;
initial.commit09911bf2008-07-26 23:55:29116
117 // Contains all the mutable stuff. All this stuff MUST be accessed with the
118 // lock held.
119 struct Mutable {
Vladislav Kuzkokovccceef02017-10-04 15:30:10120 Mutable();
[email protected]20f0487a2010-09-30 20:06:30121 ~Mutable();
initial.commit09911bf2008-07-26 23:55:29122
initial.commit09911bf2008-07-26 23:55:29123 // 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
[email protected]da4eefd2011-03-03 23:40:27134#if defined(OS_POSIX) && !defined(OS_MACOSX)
135 // Page number of the first page.
136 int first_page;
137#endif
initial.commit09911bf2008-07-26 23:55:29138 };
139
140 // Contains all the immutable stuff. All this stuff can be accessed without
141 // any lock held. This is because it can't be changed after the object's
142 // construction.
143 struct Immutable {
[email protected]c95198b2014-06-12 16:56:55144 Immutable(const PrintSettings& settings,
Vladislav Kuzkokovccceef02017-10-04 15:30:10145 const base::string16& name,
Xiyuan Xia863ce9d2017-06-20 22:36:00146 int cookie);
[email protected]20f0487a2010-09-30 20:06:30147 ~Immutable();
initial.commit09911bf2008-07-26 23:55:29148
[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
[email protected]4ecd07452009-03-31 14:34:43152 // Document name. Immutable.
[email protected]b5fa4ee2013-10-01 07:19:07153 base::string16 name_;
initial.commit09911bf2008-07-26 23:55:29154
initial.commit09911bf2008-07-26 23:55:29155 // Cookie to uniquely identify this document. It is used to make sure that a
156 // PrintedPage is correctly belonging to the PrintedDocument. Since
157 // PrintedPage generation is completely asynchronous, it could be easy to
158 // mess up and send the page to the wrong document. It can be viewed as a
159 // simpler hash of PrintSettings since a new document is made each time the
160 // print settings change.
161 int cookie_;
[email protected]c95198b2014-06-12 16:56:55162 };
[email protected]e1504d82009-07-03 15:27:15163
initial.commit09911bf2008-07-26 23:55:29164 // 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.
[email protected]20305ec2011-01-21 04:55:52166 mutable base::Lock lock_;
initial.commit09911bf2008-07-26 23:55:29167
168 // All the mutable members.
169 Mutable mutable_;
170
171 // All the immutable members.
172 const Immutable immutable_;
173
[email protected]5930cb62009-12-08 02:04:22174 DISALLOW_COPY_AND_ASSIGN(PrintedDocument);
initial.commit09911bf2008-07-26 23:55:29175};
176
177} // namespace printing
178
[email protected]8ff1d422009-07-07 21:31:39179#endif // PRINTING_PRINTED_DOCUMENT_H_