Don't consider a Bluetooth adapter present until it has an address.
[chromium-blink-merge.git] / printing / printed_document.h
blobbdb092fc3223d6055f138c63c8de9443e75eda61
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/memory/ref_counted.h"
11 #include "base/string16.h"
12 #include "base/synchronization/lock.h"
13 #include "printing/print_settings.h"
14 #include "ui/gfx/native_widget_types.h"
16 class FilePath;
17 class MessageLoop;
19 namespace printing {
21 class Metafile;
22 class PrintedPage;
23 class PrintedPagesSource;
24 class PrintingContext;
26 // A collection of rendered pages. The settings are immutable. If the print
27 // settings are changed, a new PrintedDocument must be created.
28 // Warning: May be accessed from many threads at the same time. Only one thread
29 // will have write access. Sensible functions are protected by a lock.
30 // Warning: Once a page is loaded, it cannot be replaced. Pages may be discarded
31 // under low memory conditions.
32 class PRINTING_EXPORT PrintedDocument
33 : public base::RefCountedThreadSafe<PrintedDocument> {
34 public:
35 // The cookie shall be unique and has a specific relationship with its
36 // originating source and settings.
37 PrintedDocument(const PrintSettings& settings,
38 PrintedPagesSource* source,
39 int cookie);
41 // Sets a page's data. 0-based. Takes metafile ownership.
42 // Note: locks for a short amount of time.
43 void SetPage(int page_number, Metafile* metafile, double shrink,
44 const gfx::Size& paper_size, const gfx::Rect& page_rect);
46 // Retrieves a page. If the page is not available right now, it
47 // requests to have this page be rendered and returns false.
48 // Note: locks for a short amount of time.
49 bool GetPage(int page_number, scoped_refptr<PrintedPage>* page);
51 // Draws the page in the context.
52 // Note: locks for a short amount of time in debug only.
53 #if defined(OS_WIN) || defined(OS_MACOSX) && !defined(USE_AURA)
54 void RenderPrintedPage(const PrintedPage& page,
55 gfx::NativeDrawingContext context) const;
56 #elif defined(OS_POSIX)
57 void RenderPrintedPage(const PrintedPage& page,
58 PrintingContext* context) const;
59 #endif
61 // Returns true if all the necessary pages for the settings are already
62 // rendered.
63 // Note: locks while parsing the whole tree.
64 bool IsComplete() const;
66 // Disconnects the PrintedPage source (PrintedPagesSource). It is done when
67 // the source is being destroyed.
68 void DisconnectSource();
70 // Retrieves the current memory usage of the renderer pages.
71 // Note: locks for a short amount of time.
72 uint32 MemoryUsage() const;
74 // 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);
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;
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;
89 // Getters. All these items are immutable hence thread-safe.
90 const PrintSettings& settings() const { return immutable_.settings_; }
91 const string16& name() const { return immutable_.name_; }
92 int cookie() const { return immutable_.cookie_; }
94 // Sets a path where to dump printing output files for debugging. If never set
95 // no files are generated.
96 static void set_debug_dump_path(const FilePath& debug_dump_path);
98 static const FilePath& debug_dump_path();
100 private:
101 friend class base::RefCountedThreadSafe<PrintedDocument>;
103 virtual ~PrintedDocument();
105 // Array of data for each print previewed page.
106 typedef std::map<int, scoped_refptr<PrintedPage> > PrintedPages;
108 // Contains all the mutable stuff. All this stuff MUST be accessed with the
109 // lock held.
110 struct Mutable {
111 explicit Mutable(PrintedPagesSource* source);
112 ~Mutable();
114 // Source that generates the PrintedPage's (i.e. a TabContents). It will be
115 // set back to NULL if the source is deleted before this object.
116 PrintedPagesSource* source_;
118 // Contains the pages' representation. This is a collection of PrintedPage.
119 // Warning: Lock must be held when accessing this member.
120 PrintedPages pages_;
122 // Number of expected pages to be rendered.
123 // Warning: Lock must be held when accessing this member.
124 int expected_page_count_;
126 // The total number of pages in the document.
127 int page_count_;
129 #if defined(OS_POSIX) && !defined(OS_MACOSX)
130 // Page number of the first page.
131 int first_page;
132 #endif
135 // Contains all the immutable stuff. All this stuff can be accessed without
136 // any lock held. This is because it can't be changed after the object's
137 // construction.
138 struct Immutable {
139 Immutable(const PrintSettings& settings, PrintedPagesSource* source,
140 int cookie);
141 ~Immutable();
143 // Print settings used to generate this document. Immutable.
144 PrintSettings settings_;
146 // Native thread for the render source.
147 MessageLoop* source_message_loop_;
149 // Document name. Immutable.
150 string16 name_;
152 // Cookie to uniquely identify this document. It is used to make sure that a
153 // PrintedPage is correctly belonging to the PrintedDocument. Since
154 // PrintedPage generation is completely asynchronous, it could be easy to
155 // mess up and send the page to the wrong document. It can be viewed as a
156 // simpler hash of PrintSettings since a new document is made each time the
157 // print settings change.
158 int cookie_;
161 void DebugDump(const PrintedPage& page);
163 // All writable data member access must be guarded by this lock. Needs to be
164 // mutable since it can be acquired from const member functions.
165 mutable base::Lock lock_;
167 // All the mutable members.
168 Mutable mutable_;
170 // All the immutable members.
171 const Immutable immutable_;
173 DISALLOW_COPY_AND_ASSIGN(PrintedDocument);
176 } // namespace printing
178 #endif // PRINTING_PRINTED_DOCUMENT_H_