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/files/file_path.h" |
10 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
11 #include "base/strings/string16.h" | 12 #include "base/strings/string16.h" |
12 #include "base/synchronization/lock.h" | 13 #include "base/synchronization/lock.h" |
13 #include "printing/print_settings.h" | 14 #include "printing/print_settings.h" |
14 #include "ui/gfx/native_widget_types.h" | 15 #include "ui/gfx/native_widget_types.h" |
15 | 16 |
16 namespace base { | 17 namespace base { |
17 class FilePath; | 18 class RefCountedMemory; |
18 class MessageLoop; | 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* blocking_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, | 47 void SetPage(int page_number, |
46 Metafile* metafile, | 48 Metafile* metafile, |
47 #if defined(OS_WIN) | 49 #if defined(OS_WIN) |
48 double shrink, | 50 double shrink, |
49 #endif // OS_WIN | 51 #endif // OS_WIN |
50 const gfx::Size& paper_size, | 52 const gfx::Size& paper_size, |
51 const gfx::Rect& page_rect); | 53 const gfx::Rect& page_rect); |
52 | 54 |
53 // Retrieves a page. If the page is not available right now, it | 55 // Retrieves a page. If the page is not available right now, it |
54 // requests to have this page be rendered and returns false. | 56 // requests to have this page be rendered and returns NULL. |
55 // Note: locks for a short amount of time. | 57 // Note: locks for a short amount of time. |
56 bool GetPage(int page_number, scoped_refptr<PrintedPage>* page); | 58 scoped_refptr<PrintedPage> GetPage(int page_number); |
57 | 59 |
58 // Draws the page in the context. | 60 // Draws the page in the context. |
59 // Note: locks for a short amount of time in debug only. | 61 // Note: locks for a short amount of time in debug only. |
60 #if defined(OS_WIN) || defined(OS_MACOSX) && !defined(USE_AURA) | 62 #if defined(OS_WIN) || defined(OS_MACOSX) && !defined(USE_AURA) |
61 void RenderPrintedPage(const PrintedPage& page, | 63 void RenderPrintedPage(const PrintedPage& page, |
62 gfx::NativeDrawingContext context) const; | 64 gfx::NativeDrawingContext context) const; |
63 #elif defined(OS_POSIX) | 65 #elif defined(OS_POSIX) |
64 void RenderPrintedPage(const PrintedPage& page, | 66 void RenderPrintedPage(const PrintedPage& page, |
65 PrintingContext* context) const; | 67 PrintingContext* context) const; |
66 #endif | 68 #endif |
(...skipping 28 matching lines...) Expand all Loading... |
95 | 97 |
96 // Getters. All these items are immutable hence thread-safe. | 98 // Getters. All these items are immutable hence thread-safe. |
97 const PrintSettings& settings() const { return immutable_.settings_; } | 99 const PrintSettings& settings() const { return immutable_.settings_; } |
98 const base::string16& name() const { return immutable_.name_; } | 100 const base::string16& name() const { return immutable_.name_; } |
99 int cookie() const { return immutable_.cookie_; } | 101 int cookie() const { return immutable_.cookie_; } |
100 | 102 |
101 // Sets a path where to dump printing output files for debugging. If never set | 103 // Sets a path where to dump printing output files for debugging. If never set |
102 // no files are generated. | 104 // no files are generated. |
103 static void set_debug_dump_path(const base::FilePath& debug_dump_path); | 105 static void set_debug_dump_path(const base::FilePath& debug_dump_path); |
104 | 106 |
105 static const base::FilePath& debug_dump_path(); | 107 // Creates debug file name from given |document_name| and |extension|. |
| 108 // |extension| should include '.', example ".pdf" |
| 109 // Returns empty |base::FilePath| if debug dumps is not enabled. |
| 110 static base::FilePath CreateDebugDumpPath( |
| 111 const base::string16& document_name, |
| 112 const base::FilePath::StringType& extension); |
| 113 |
| 114 // Dump data on blocking task runner if debug dumps enabled. |
| 115 void DebugDumpData(const base::RefCountedMemory* data, |
| 116 const base::FilePath::StringType& extension); |
106 | 117 |
107 private: | 118 private: |
108 friend class base::RefCountedThreadSafe<PrintedDocument>; | 119 friend class base::RefCountedThreadSafe<PrintedDocument>; |
109 | 120 |
110 virtual ~PrintedDocument(); | 121 virtual ~PrintedDocument(); |
111 | 122 |
112 // Array of data for each print previewed page. | 123 // Array of data for each print previewed page. |
113 typedef std::map<int, scoped_refptr<PrintedPage> > PrintedPages; | 124 typedef std::map<int, scoped_refptr<PrintedPage> > PrintedPages; |
114 | 125 |
115 // Contains all the mutable stuff. All this stuff MUST be accessed with the | 126 // Contains all the mutable stuff. All this stuff MUST be accessed with the |
(...skipping 20 matching lines...) Expand all Loading... |
136 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 147 #if defined(OS_POSIX) && !defined(OS_MACOSX) |
137 // Page number of the first page. | 148 // Page number of the first page. |
138 int first_page; | 149 int first_page; |
139 #endif | 150 #endif |
140 }; | 151 }; |
141 | 152 |
142 // Contains all the immutable stuff. All this stuff can be accessed without | 153 // Contains all the immutable stuff. All this stuff can be accessed without |
143 // any lock held. This is because it can't be changed after the object's | 154 // any lock held. This is because it can't be changed after the object's |
144 // construction. | 155 // construction. |
145 struct Immutable { | 156 struct Immutable { |
146 Immutable(const PrintSettings& settings, PrintedPagesSource* source, | 157 Immutable(const PrintSettings& settings, |
147 int cookie); | 158 PrintedPagesSource* source, |
| 159 int cookie, |
| 160 base::TaskRunner* blocking_runner); |
148 ~Immutable(); | 161 ~Immutable(); |
149 | 162 |
150 // Print settings used to generate this document. Immutable. | 163 // Print settings used to generate this document. Immutable. |
151 PrintSettings settings_; | 164 PrintSettings settings_; |
152 | 165 |
153 // Native thread for the render source. | |
154 base::MessageLoop* source_message_loop_; | |
155 | |
156 // Document name. Immutable. | 166 // Document name. Immutable. |
157 base::string16 name_; | 167 base::string16 name_; |
158 | 168 |
159 // Cookie to uniquely identify this document. It is used to make sure that a | 169 // Cookie to uniquely identify this document. It is used to make sure that a |
160 // PrintedPage is correctly belonging to the PrintedDocument. Since | 170 // PrintedPage is correctly belonging to the PrintedDocument. Since |
161 // PrintedPage generation is completely asynchronous, it could be easy to | 171 // PrintedPage generation is completely asynchronous, it could be easy to |
162 // mess up and send the page to the wrong document. It can be viewed as a | 172 // mess up and send the page to the wrong document. It can be viewed as a |
163 // simpler hash of PrintSettings since a new document is made each time the | 173 // simpler hash of PrintSettings since a new document is made each time the |
164 // print settings change. | 174 // print settings change. |
165 int cookie_; | 175 int cookie_; |
| 176 |
| 177 // Native thread for blocking operations, like file access. |
| 178 scoped_refptr<base::TaskRunner> blocking_runner_; |
166 }; | 179 }; |
167 | 180 |
168 void DebugDump(const PrintedPage& page); | |
169 | |
170 // All writable data member access must be guarded by this lock. Needs to be | 181 // All writable data member access must be guarded by this lock. Needs to be |
171 // mutable since it can be acquired from const member functions. | 182 // mutable since it can be acquired from const member functions. |
172 mutable base::Lock lock_; | 183 mutable base::Lock lock_; |
173 | 184 |
174 // All the mutable members. | 185 // All the mutable members. |
175 Mutable mutable_; | 186 Mutable mutable_; |
176 | 187 |
177 // All the immutable members. | 188 // All the immutable members. |
178 const Immutable immutable_; | 189 const Immutable immutable_; |
179 | 190 |
180 DISALLOW_COPY_AND_ASSIGN(PrintedDocument); | 191 DISALLOW_COPY_AND_ASSIGN(PrintedDocument); |
181 }; | 192 }; |
182 | 193 |
183 } // namespace printing | 194 } // namespace printing |
184 | 195 |
185 #endif // PRINTING_PRINTED_DOCUMENT_H_ | 196 #endif // PRINTING_PRINTED_DOCUMENT_H_ |
OLD | NEW |