OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef PRINTING_PRINTED_DOCUMENT_H_ | 5 #ifndef PRINTING_PRINTED_DOCUMENT_H_ |
6 #define PRINTING_PRINTED_DOCUMENT_H_ | 6 #define PRINTING_PRINTED_DOCUMENT_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 | 9 |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/strings/string16.h" | 11 #include "base/strings/string16.h" |
12 #include "base/synchronization/lock.h" | 12 #include "base/synchronization/lock.h" |
13 #include "printing/print_settings.h" | 13 #include "printing/print_settings.h" |
14 #include "ui/gfx/native_widget_types.h" | 14 #include "ui/gfx/native_widget_types.h" |
15 | 15 |
16 namespace base { | 16 namespace base { |
17 class FilePath; | 17 class FilePath; |
18 class MessageLoop; | 18 class RefCountedMemory; |
19 class TaskRunner; | |
19 } | 20 } |
20 | 21 |
21 namespace printing { | 22 namespace printing { |
22 | 23 |
23 class Metafile; | 24 class Metafile; |
24 class PrintedPage; | 25 class PrintedPage; |
25 class PrintedPagesSource; | 26 class PrintedPagesSource; |
26 class PrintingContext; | 27 class PrintingContext; |
27 | 28 |
28 // A collection of rendered pages. The settings are immutable. If the print | 29 // A collection of rendered pages. The settings are immutable. If the print |
29 // settings are changed, a new PrintedDocument must be created. | 30 // 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 // 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 // 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 // Warning: Once a page is loaded, it cannot be replaced. Pages may be discarded |
33 // under low memory conditions. | 34 // under low memory conditions. |
34 class PRINTING_EXPORT PrintedDocument | 35 class PRINTING_EXPORT PrintedDocument |
35 : public base::RefCountedThreadSafe<PrintedDocument> { | 36 : public base::RefCountedThreadSafe<PrintedDocument> { |
36 public: | 37 public: |
37 // The cookie shall be unique and has a specific relationship with its | 38 // The cookie shall be unique and has a specific relationship with its |
38 // originating source and settings. | 39 // originating source and settings. |
39 PrintedDocument(const PrintSettings& settings, | 40 PrintedDocument(const PrintSettings& settings, |
40 PrintedPagesSource* source, | 41 PrintedPagesSource* source, |
41 int cookie); | 42 int cookie, |
43 base::TaskRunner* file_runner); | |
42 | 44 |
43 // Sets a page's data. 0-based. Takes metafile ownership. | 45 // Sets a page's data. 0-based. Takes metafile ownership. |
44 // Note: locks for a short amount of time. | 46 // Note: locks for a short amount of time. |
45 void SetPage(int page_number, Metafile* metafile, double shrink, | 47 void SetPage(int page_number, Metafile* metafile, double shrink, |
46 const gfx::Size& paper_size, const gfx::Rect& page_rect); | 48 const gfx::Size& paper_size, const gfx::Rect& page_rect); |
47 | 49 |
48 // Retrieves a page. If the page is not available right now, it | 50 // Retrieves a page. If the page is not available right now, it |
49 // requests to have this page be rendered and returns false. | 51 // requests to have this page be rendered and returns NULL. |
50 // Note: locks for a short amount of time. | 52 // Note: locks for a short amount of time. |
51 bool GetPage(int page_number, scoped_refptr<PrintedPage>* page); | 53 scoped_refptr<PrintedPage> GetPage(int page_number); |
52 | 54 |
53 // Draws the page in the context. | 55 // Draws the page in the context. |
54 // Note: locks for a short amount of time in debug only. | 56 // Note: locks for a short amount of time in debug only. |
55 #if defined(OS_WIN) || defined(OS_MACOSX) && !defined(USE_AURA) | 57 #if defined(OS_WIN) || defined(OS_MACOSX) && !defined(USE_AURA) |
56 void RenderPrintedPage(const PrintedPage& page, | 58 void RenderPrintedPage(const PrintedPage& page, |
57 gfx::NativeDrawingContext context) const; | 59 gfx::NativeDrawingContext context) const; |
58 #elif defined(OS_POSIX) | 60 #elif defined(OS_POSIX) |
59 void RenderPrintedPage(const PrintedPage& page, | 61 void RenderPrintedPage(const PrintedPage& page, |
60 PrintingContext* context) const; | 62 PrintingContext* context) const; |
61 #endif | 63 #endif |
(...skipping 28 matching lines...) Expand all Loading... | |
90 | 92 |
91 // Getters. All these items are immutable hence thread-safe. | 93 // Getters. All these items are immutable hence thread-safe. |
92 const PrintSettings& settings() const { return immutable_.settings_; } | 94 const PrintSettings& settings() const { return immutable_.settings_; } |
93 const base::string16& name() const { return immutable_.name_; } | 95 const base::string16& name() const { return immutable_.name_; } |
94 int cookie() const { return immutable_.cookie_; } | 96 int cookie() const { return immutable_.cookie_; } |
95 | 97 |
96 // Sets a path where to dump printing output files for debugging. If never set | 98 // Sets a path where to dump printing output files for debugging. If never set |
97 // no files are generated. | 99 // no files are generated. |
98 static void set_debug_dump_path(const base::FilePath& debug_dump_path); | 100 static void set_debug_dump_path(const base::FilePath& debug_dump_path); |
99 | 101 |
100 static const base::FilePath& debug_dump_path(); | 102 // Creates debug file name from given |document_name| and |extension|. |
103 // Returns empty |base::FilePath| if debug dumps is not enabled. | |
104 static base::FilePath CreateDebugDumpPath(const base::string16& document_name, | |
105 const std::string& extension); | |
106 | |
107 // Dump data on FILE thread if debug dumps enabled. | |
Lei Zhang
2014/06/11 21:43:42
- Update the comment if you use GetBlockingPool().
Vitaly Buka (NO REVIEWS)
2014/06/11 22:22:58
Done.
| |
108 void DebugDumpData(const base::RefCountedMemory* data, | |
109 const std::string& extension); | |
101 | 110 |
102 private: | 111 private: |
103 friend class base::RefCountedThreadSafe<PrintedDocument>; | 112 friend class base::RefCountedThreadSafe<PrintedDocument>; |
104 | 113 |
105 virtual ~PrintedDocument(); | 114 virtual ~PrintedDocument(); |
106 | 115 |
107 // Array of data for each print previewed page. | 116 // Array of data for each print previewed page. |
108 typedef std::map<int, scoped_refptr<PrintedPage> > PrintedPages; | 117 typedef std::map<int, scoped_refptr<PrintedPage> > PrintedPages; |
109 | 118 |
110 // Contains all the mutable stuff. All this stuff MUST be accessed with the | 119 // Contains all the mutable stuff. All this stuff MUST be accessed with the |
(...skipping 20 matching lines...) Expand all Loading... | |
131 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 140 #if defined(OS_POSIX) && !defined(OS_MACOSX) |
132 // Page number of the first page. | 141 // Page number of the first page. |
133 int first_page; | 142 int first_page; |
134 #endif | 143 #endif |
135 }; | 144 }; |
136 | 145 |
137 // Contains all the immutable stuff. All this stuff can be accessed without | 146 // 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 | 147 // any lock held. This is because it can't be changed after the object's |
139 // construction. | 148 // construction. |
140 struct Immutable { | 149 struct Immutable { |
141 Immutable(const PrintSettings& settings, PrintedPagesSource* source, | 150 Immutable(const PrintSettings& settings, |
142 int cookie); | 151 PrintedPagesSource* source, |
152 int cookie, | |
153 base::TaskRunner* file_runner); | |
143 ~Immutable(); | 154 ~Immutable(); |
144 | 155 |
145 // Print settings used to generate this document. Immutable. | 156 // Print settings used to generate this document. Immutable. |
146 PrintSettings settings_; | 157 PrintSettings settings_; |
147 | 158 |
148 // Native thread for the render source. | 159 // Native thread for file operations. |
149 base::MessageLoop* source_message_loop_; | 160 scoped_refptr<base::TaskRunner> file_runner_; |
150 | 161 |
151 // Document name. Immutable. | 162 // Document name. Immutable. |
152 base::string16 name_; | 163 base::string16 name_; |
153 | 164 |
154 // Cookie to uniquely identify this document. It is used to make sure that a | 165 // Cookie to uniquely identify this document. It is used to make sure that a |
155 // PrintedPage is correctly belonging to the PrintedDocument. Since | 166 // PrintedPage is correctly belonging to the PrintedDocument. Since |
156 // PrintedPage generation is completely asynchronous, it could be easy to | 167 // 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 | 168 // 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 | 169 // simpler hash of PrintSettings since a new document is made each time the |
159 // print settings change. | 170 // print settings change. |
160 int cookie_; | 171 int cookie_; |
161 }; | 172 }; |
162 | 173 |
163 void DebugDump(const PrintedPage& page); | |
164 | |
165 // All writable data member access must be guarded by this lock. Needs to be | 174 // 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. | 175 // mutable since it can be acquired from const member functions. |
167 mutable base::Lock lock_; | 176 mutable base::Lock lock_; |
168 | 177 |
169 // All the mutable members. | 178 // All the mutable members. |
170 Mutable mutable_; | 179 Mutable mutable_; |
171 | 180 |
172 // All the immutable members. | 181 // All the immutable members. |
173 const Immutable immutable_; | 182 const Immutable immutable_; |
174 | 183 |
175 DISALLOW_COPY_AND_ASSIGN(PrintedDocument); | 184 DISALLOW_COPY_AND_ASSIGN(PrintedDocument); |
176 }; | 185 }; |
177 | 186 |
178 } // namespace printing | 187 } // namespace printing |
179 | 188 |
180 #endif // PRINTING_PRINTED_DOCUMENT_H_ | 189 #endif // PRINTING_PRINTED_DOCUMENT_H_ |
OLD | NEW |