blob: b8af4bb5f39e4984cf98d0d4278f7dc222176bd4 [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>
9
[email protected]3b63f8f42011-03-28 01:54:1510#include "base/memory/ref_counted.h"
[email protected]896d161f2013-06-11 22:52:2411#include "base/strings/string16.h"
[email protected]20305ec2011-01-21 04:55:5212#include "base/synchronization/lock.h"
[email protected]8ff1d422009-07-07 21:31:3913#include "printing/print_settings.h"
[email protected]08397d52011-02-05 01:53:3814#include "ui/gfx/native_widget_types.h"
initial.commit09911bf2008-07-26 23:55:2915
[email protected]a3ef4832013-02-02 05:12:3316namespace base {
17class FilePath;
[email protected]5e9e96a2013-03-31 02:29:2018class MessageLoop;
[email protected]a3ef4832013-02-02 05:12:3319}
20
initial.commit09911bf2008-07-26 23:55:2921namespace printing {
22
[email protected]7d7489902011-04-11 21:54:0623class Metafile;
initial.commit09911bf2008-07-26 23:55:2924class PrintedPage;
25class PrintedPagesSource;
[email protected]5cc4c422011-02-19 00:09:2226class PrintingContext;
initial.commit09911bf2008-07-26 23:55:2927
[email protected]4ecd07452009-03-31 14:34:4328// A collection of rendered pages. The settings are immutable. If the print
initial.commit09911bf2008-07-26 23:55:2929// settings are changed, a new PrintedDocument must be created.
30// Warning: May be accessed from many threads at the same time. Only one thread
31// will have write access. Sensible functions are protected by a lock.
32// Warning: Once a page is loaded, it cannot be replaced. Pages may be discarded
33// under low memory conditions.
[email protected]69f5b1e62011-09-01 06:34:0434class PRINTING_EXPORT PrintedDocument
35 : public base::RefCountedThreadSafe<PrintedDocument> {
initial.commit09911bf2008-07-26 23:55:2936 public:
37 // The cookie shall be unique and has a specific relationship with its
38 // originating source and settings.
39 PrintedDocument(const PrintSettings& settings,
40 PrintedPagesSource* source,
41 int cookie);
initial.commit09911bf2008-07-26 23:55:2942
[email protected]0e0fca32009-07-06 15:25:5043 // Sets a page's data. 0-based. Takes metafile ownership.
initial.commit09911bf2008-07-26 23:55:2944 // Note: locks for a short amount of time.
[email protected]7d7489902011-04-11 21:54:0645 void SetPage(int page_number, Metafile* metafile, double shrink,
[email protected]3cce5382011-09-23 23:21:2146 const gfx::Size& paper_size, const gfx::Rect& page_rect);
initial.commit09911bf2008-07-26 23:55:2947
48 // Retrieves a page. If the page is not available right now, it
49 // requests to have this page be rendered and returns false.
50 // Note: locks for a short amount of time.
51 bool GetPage(int page_number, scoped_refptr<PrintedPage>* page);
52
53 // Draws the page in the context.
54 // Note: locks for a short amount of time in debug only.
[email protected]2f47fc452011-11-21 03:12:1755#if defined(OS_WIN) || defined(OS_MACOSX) && !defined(USE_AURA)
[email protected]b75dca82009-10-13 18:46:2156 void RenderPrintedPage(const PrintedPage& page,
57 gfx::NativeDrawingContext context) const;
[email protected]5cc4c422011-02-19 00:09:2258#elif defined(OS_POSIX)
59 void RenderPrintedPage(const PrintedPage& page,
60 PrintingContext* context) const;
61#endif
initial.commit09911bf2008-07-26 23:55:2962
initial.commit09911bf2008-07-26 23:55:2963 // Returns true if all the necessary pages for the settings are already
64 // rendered.
65 // Note: locks while parsing the whole tree.
66 bool IsComplete() const;
67
initial.commit09911bf2008-07-26 23:55:2968 // Disconnects the PrintedPage source (PrintedPagesSource). It is done when
69 // the source is being destroyed.
70 void DisconnectSource();
71
72 // Retrieves the current memory usage of the renderer pages.
73 // Note: locks for a short amount of time.
[email protected]b5ab3982010-02-16 23:58:2774 uint32 MemoryUsage() const;
initial.commit09911bf2008-07-26 23:55:2975
76 // Sets the number of pages in the document to be rendered. Can only be set
77 // once.
78 // Note: locks for a short amount of time.
79 void set_page_count(int max_page);
80
81 // Number of pages in the document. Used for headers/footers.
82 // Note: locks for a short amount of time.
83 int page_count() const;
84
85 // Returns the number of expected pages to be rendered. It is a non-linear
86 // series if settings().ranges is not empty. It is the same value as
87 // document_page_count() otherwise.
88 // Note: locks for a short amount of time.
89 int expected_page_count() const;
90
[email protected]4ecd07452009-03-31 14:34:4391 // Getters. All these items are immutable hence thread-safe.
initial.commit09911bf2008-07-26 23:55:2992 const PrintSettings& settings() const { return immutable_.settings_; }
[email protected]b5fa4ee2013-10-01 07:19:0793 const base::string16& name() const { return immutable_.name_; }
[email protected]225c8f52010-02-05 22:23:2094 int cookie() const { return immutable_.cookie_; }
initial.commit09911bf2008-07-26 23:55:2995
[email protected]e1504d82009-07-03 15:27:1596 // Sets a path where to dump printing output files for debugging. If never set
97 // no files are generated.
[email protected]a3ef4832013-02-02 05:12:3398 static void set_debug_dump_path(const base::FilePath& debug_dump_path);
[email protected]e1504d82009-07-03 15:27:1599
[email protected]a3ef4832013-02-02 05:12:33100 static const base::FilePath& debug_dump_path();
[email protected]e1504d82009-07-03 15:27:15101
initial.commit09911bf2008-07-26 23:55:29102 private:
[email protected]877d55d2009-11-05 21:53:08103 friend class base::RefCountedThreadSafe<PrintedDocument>;
104
[email protected]20f0487a2010-09-30 20:06:30105 virtual ~PrintedDocument();
[email protected]877d55d2009-11-05 21:53:08106
[email protected]0e0fca32009-07-06 15:25:50107 // Array of data for each print previewed page.
[email protected]8ff1d422009-07-07 21:31:39108 typedef std::map<int, scoped_refptr<PrintedPage> > PrintedPages;
initial.commit09911bf2008-07-26 23:55:29109
110 // Contains all the mutable stuff. All this stuff MUST be accessed with the
111 // lock held.
112 struct Mutable {
[email protected]5930cb62009-12-08 02:04:22113 explicit Mutable(PrintedPagesSource* source);
[email protected]20f0487a2010-09-30 20:06:30114 ~Mutable();
initial.commit09911bf2008-07-26 23:55:29115
[email protected]57c6a652009-05-04 07:58:34116 // Source that generates the PrintedPage's (i.e. a TabContents). It will be
initial.commit09911bf2008-07-26 23:55:29117 // set back to NULL if the source is deleted before this object.
118 PrintedPagesSource* source_;
119
120 // Contains the pages' representation. This is a collection of PrintedPage.
121 // Warning: Lock must be held when accessing this member.
122 PrintedPages pages_;
123
124 // Number of expected pages to be rendered.
125 // Warning: Lock must be held when accessing this member.
126 int expected_page_count_;
127
128 // The total number of pages in the document.
129 int page_count_;
130
[email protected]da4eefd2011-03-03 23:40:27131#if defined(OS_POSIX) && !defined(OS_MACOSX)
132 // Page number of the first page.
133 int first_page;
134#endif
initial.commit09911bf2008-07-26 23:55:29135 };
136
137 // Contains all the immutable stuff. All this stuff can be accessed without
138 // any lock held. This is because it can't be changed after the object's
139 // construction.
140 struct Immutable {
141 Immutable(const PrintSettings& settings, PrintedPagesSource* source,
142 int cookie);
[email protected]20f0487a2010-09-30 20:06:30143 ~Immutable();
initial.commit09911bf2008-07-26 23:55:29144
[email protected]4ecd07452009-03-31 14:34:43145 // Print settings used to generate this document. Immutable.
initial.commit09911bf2008-07-26 23:55:29146 PrintSettings settings_;
147
148 // Native thread for the render source.
[email protected]5e9e96a2013-03-31 02:29:20149 base::MessageLoop* source_message_loop_;
initial.commit09911bf2008-07-26 23:55:29150
[email protected]4ecd07452009-03-31 14:34:43151 // Document name. Immutable.
[email protected]b5fa4ee2013-10-01 07:19:07152 base::string16 name_;
initial.commit09911bf2008-07-26 23:55:29153
initial.commit09911bf2008-07-26 23:55:29154 // Cookie to uniquely identify this document. It is used to make sure that a
155 // PrintedPage is correctly belonging to the PrintedDocument. Since
156 // PrintedPage generation is completely asynchronous, it could be easy to
157 // mess up and send the page to the wrong document. It can be viewed as a
158 // simpler hash of PrintSettings since a new document is made each time the
159 // print settings change.
160 int cookie_;
161 };
162
[email protected]e1504d82009-07-03 15:27:15163 void DebugDump(const PrintedPage& page);
164
initial.commit09911bf2008-07-26 23:55:29165 // All writable data member access must be guarded by this lock. Needs to be
166 // mutable since it can be acquired from const member functions.
[email protected]20305ec2011-01-21 04:55:52167 mutable base::Lock lock_;
initial.commit09911bf2008-07-26 23:55:29168
169 // All the mutable members.
170 Mutable mutable_;
171
172 // All the immutable members.
173 const Immutable immutable_;
174
[email protected]5930cb62009-12-08 02:04:22175 DISALLOW_COPY_AND_ASSIGN(PrintedDocument);
initial.commit09911bf2008-07-26 23:55:29176};
177
178} // namespace printing
179
[email protected]8ff1d422009-07-07 21:31:39180#endif // PRINTING_PRINTED_DOCUMENT_H_