Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(809)

Side by Side Diff: printing/printed_document.cc

Issue 329683002: Improvements in --debug-print switch implementation. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Thu 06/12/2014 2:53:06.07 Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « printing/printed_document.h ('k') | printing/printing.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 #include "printing/printed_document.h" 5 #include "printing/printed_document.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <set> 8 #include <set>
9 #include <string> 9 #include <string>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/bind.h"
12 #include "base/file_util.h" 13 #include "base/file_util.h"
13 #include "base/files/file_path.h" 14 #include "base/files/file_path.h"
14 #include "base/i18n/file_util_icu.h" 15 #include "base/i18n/file_util_icu.h"
15 #include "base/i18n/time_formatting.h" 16 #include "base/i18n/time_formatting.h"
17 #include "base/json/json_writer.h"
16 #include "base/lazy_instance.h" 18 #include "base/lazy_instance.h"
19 #include "base/memory/ref_counted_memory.h"
17 #include "base/message_loop/message_loop.h" 20 #include "base/message_loop/message_loop.h"
21 #include "base/numerics/safe_conversions.h"
18 #include "base/strings/string_util.h" 22 #include "base/strings/string_util.h"
19 #include "base/strings/stringprintf.h" 23 #include "base/strings/stringprintf.h"
20 #include "base/strings/utf_string_conversions.h" 24 #include "base/strings/utf_string_conversions.h"
25 #include "base/time/time.h"
26 #include "base/values.h"
21 #include "printing/page_number.h" 27 #include "printing/page_number.h"
28 #include "printing/print_settings_conversion.h"
22 #include "printing/printed_page.h" 29 #include "printing/printed_page.h"
23 #include "printing/printed_pages_source.h" 30 #include "printing/printed_pages_source.h"
24 #include "printing/units.h" 31 #include "printing/units.h"
25 #include "skia/ext/platform_device.h" 32 #include "skia/ext/platform_device.h"
26 #include "ui/gfx/font.h" 33 #include "ui/gfx/font.h"
27 #include "ui/gfx/text_elider.h" 34 #include "ui/gfx/text_elider.h"
28 35
36 namespace printing {
37
29 namespace { 38 namespace {
30 39
31 struct PrintDebugDumpPath { 40 base::LazyInstance<base::FilePath> g_debug_dump_info =
32 PrintDebugDumpPath() 41 LAZY_INSTANCE_INITIALIZER;
33 : enabled(false) {
34 }
35 42
36 bool enabled; 43 void DebugDumpPageTask(const base::string16& doc_name,
37 base::FilePath debug_dump_path; 44 const PrintedPage* page) {
38 }; 45 if (g_debug_dump_info.Get().empty())
46 return;
39 47
40 base::LazyInstance<PrintDebugDumpPath> g_debug_dump_info = 48 base::string16 filename = doc_name;
41 LAZY_INSTANCE_INITIALIZER; 49 filename +=
50 base::ASCIIToUTF16(base::StringPrintf("_%04d", page->page_number()));
51 #if defined(OS_WIN)
52 page->metafile()->SaveTo(PrintedDocument::CreateDebugDumpPath(
53 filename, FILE_PATH_LITERAL(".emf")));
54 #else // OS_WIN
55 page->metafile()->SaveTo(PrintedDocument::CreateDebugDumpPath(
56 filename, FILE_PATH_LITERAL(".pdf")));
57 #endif // OS_WIN
58 }
59
60 void DebugDumpDataTask(const base::string16& doc_name,
61 const base::FilePath::StringType& extension,
62 const base::RefCountedMemory* data) {
63 base::FilePath path =
64 PrintedDocument::CreateDebugDumpPath(doc_name, extension);
65 if (path.empty())
66 return;
67 base::WriteFile(path,
68 reinterpret_cast<const char*>(data->front()),
69 base::checked_cast<int>(data->size()));
70 }
71
72 void DebugDumpSettings(const base::string16& doc_name,
73 const PrintSettings& settings,
74 base::TaskRunner* blocking_runner) {
75 base::DictionaryValue job_settings;
76 PrintSettingsToJobSettingsDebug(settings, &job_settings);
77 std::string settings_str;
78 base::JSONWriter::WriteWithOptions(
79 &job_settings, base::JSONWriter::OPTIONS_PRETTY_PRINT, &settings_str);
80 scoped_refptr<base::RefCountedMemory> data =
81 base::RefCountedString::TakeString(&settings_str);
82 blocking_runner->PostTask(
83 FROM_HERE,
84 base::Bind(
85 &DebugDumpDataTask, doc_name, FILE_PATH_LITERAL(".json"), data));
86 }
42 87
43 } // namespace 88 } // namespace
44 89
45 namespace printing {
46
47 PrintedDocument::PrintedDocument(const PrintSettings& settings, 90 PrintedDocument::PrintedDocument(const PrintSettings& settings,
48 PrintedPagesSource* source, 91 PrintedPagesSource* source,
49 int cookie) 92 int cookie,
50 : mutable_(source), 93 base::TaskRunner* blocking_runner)
51 immutable_(settings, source, cookie) { 94 : mutable_(source), immutable_(settings, source, cookie, blocking_runner) {
52
53 // Records the expected page count if a range is setup. 95 // Records the expected page count if a range is setup.
54 if (!settings.ranges().empty()) { 96 if (!settings.ranges().empty()) {
55 // If there is a range, set the number of page 97 // If there is a range, set the number of page
56 for (unsigned i = 0; i < settings.ranges().size(); ++i) { 98 for (unsigned i = 0; i < settings.ranges().size(); ++i) {
57 const PageRange& range = settings.ranges()[i]; 99 const PageRange& range = settings.ranges()[i];
58 mutable_.expected_page_count_ += range.to - range.from + 1; 100 mutable_.expected_page_count_ += range.to - range.from + 1;
59 } 101 }
60 } 102 }
103
104 if (!g_debug_dump_info.Get().empty())
105 DebugDumpSettings(name(), settings, blocking_runner);
61 } 106 }
62 107
63 PrintedDocument::~PrintedDocument() { 108 PrintedDocument::~PrintedDocument() {
64 } 109 }
65 110
66 void PrintedDocument::SetPage(int page_number, 111 void PrintedDocument::SetPage(int page_number,
67 Metafile* metafile, 112 Metafile* metafile,
68 #if defined(OS_WIN) 113 #if defined(OS_WIN)
69 double shrink, 114 double shrink,
70 #endif // OS_WIN 115 #endif // OS_WIN
71 const gfx::Size& paper_size, 116 const gfx::Size& paper_size,
72 const gfx::Rect& page_rect) { 117 const gfx::Rect& page_rect) {
73 // Notice the page_number + 1, the reason is that this is the value that will 118 // Notice the page_number + 1, the reason is that this is the value that will
74 // be shown. Users dislike 0-based counting. 119 // be shown. Users dislike 0-based counting.
75 scoped_refptr<PrintedPage> page( 120 scoped_refptr<PrintedPage> page(
76 new PrintedPage(page_number + 1, metafile, paper_size, page_rect)); 121 new PrintedPage(page_number + 1, metafile, paper_size, page_rect));
77 #if defined(OS_WIN) 122 #if defined(OS_WIN)
78 page->set_shrink_factor(shrink); 123 page->set_shrink_factor(shrink);
79 #endif // OS_WIN 124 #endif // OS_WIN
80 { 125 {
81 base::AutoLock lock(lock_); 126 base::AutoLock lock(lock_);
82 mutable_.pages_[page_number] = page; 127 mutable_.pages_[page_number] = page;
83 128
84 #if defined(OS_POSIX) && !defined(OS_MACOSX) 129 #if defined(OS_POSIX) && !defined(OS_MACOSX)
85 if (page_number < mutable_.first_page) 130 if (page_number < mutable_.first_page)
86 mutable_.first_page = page_number; 131 mutable_.first_page = page_number;
87 #endif 132 #endif
88 } 133 }
89 DebugDump(*page.get()); 134
135 if (!g_debug_dump_info.Get().empty()) {
136 immutable_.blocking_runner_->PostTask(
137 FROM_HERE, base::Bind(&DebugDumpPageTask, name(), page));
138 }
90 } 139 }
91 140
92 bool PrintedDocument::GetPage(int page_number, 141 scoped_refptr<PrintedPage> PrintedDocument::GetPage(int page_number) {
93 scoped_refptr<PrintedPage>* page) { 142 scoped_refptr<PrintedPage> page;
94 base::AutoLock lock(lock_); 143 {
95 PrintedPages::const_iterator itr = mutable_.pages_.find(page_number); 144 base::AutoLock lock(lock_);
96 if (itr != mutable_.pages_.end()) { 145 PrintedPages::const_iterator itr = mutable_.pages_.find(page_number);
97 if (itr->second.get()) { 146 if (itr != mutable_.pages_.end())
98 *page = itr->second; 147 page = itr->second;
99 return true;
100 }
101 } 148 }
102 return false; 149 return page;
103 } 150 }
104 151
105 bool PrintedDocument::IsComplete() const { 152 bool PrintedDocument::IsComplete() const {
106 base::AutoLock lock(lock_); 153 base::AutoLock lock(lock_);
107 if (!mutable_.page_count_) 154 if (!mutable_.page_count_)
108 return false; 155 return false;
109 PageNumber page(immutable_.settings_, mutable_.page_count_); 156 PageNumber page(immutable_.settings_, mutable_.page_count_);
110 if (page == PageNumber::npos()) 157 if (page == PageNumber::npos())
111 return false; 158 return false;
112 159
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 int PrintedDocument::page_count() const { 213 int PrintedDocument::page_count() const {
167 base::AutoLock lock(lock_); 214 base::AutoLock lock(lock_);
168 return mutable_.page_count_; 215 return mutable_.page_count_;
169 } 216 }
170 217
171 int PrintedDocument::expected_page_count() const { 218 int PrintedDocument::expected_page_count() const {
172 base::AutoLock lock(lock_); 219 base::AutoLock lock(lock_);
173 return mutable_.expected_page_count_; 220 return mutable_.expected_page_count_;
174 } 221 }
175 222
176 void PrintedDocument::DebugDump(const PrintedPage& page) {
177 if (!g_debug_dump_info.Get().enabled)
178 return;
179
180 base::string16 filename;
181 filename += name();
182 filename += base::ASCIIToUTF16("_");
183 filename += base::ASCIIToUTF16(
184 base::StringPrintf("%02d", page.page_number()));
185 #if defined(OS_WIN)
186 filename += base::ASCIIToUTF16("_.emf");
187 page.metafile()->SaveTo(
188 g_debug_dump_info.Get().debug_dump_path.Append(filename));
189 #else // OS_WIN
190 filename += base::ASCIIToUTF16("_.pdf");
191 page.metafile()->SaveTo(
192 g_debug_dump_info.Get().debug_dump_path.Append(
193 base::UTF16ToUTF8(filename)));
194 #endif // OS_WIN
195 }
196
197 void PrintedDocument::set_debug_dump_path( 223 void PrintedDocument::set_debug_dump_path(
198 const base::FilePath& debug_dump_path) { 224 const base::FilePath& debug_dump_path) {
199 g_debug_dump_info.Get().enabled = !debug_dump_path.empty(); 225 g_debug_dump_info.Get() = debug_dump_path;
200 g_debug_dump_info.Get().debug_dump_path = debug_dump_path;
201 } 226 }
202 227
203 const base::FilePath& PrintedDocument::debug_dump_path() { 228 base::FilePath PrintedDocument::CreateDebugDumpPath(
204 return g_debug_dump_info.Get().debug_dump_path; 229 const base::string16& document_name,
230 const base::FilePath::StringType& extension) {
231 if (g_debug_dump_info.Get().empty())
232 return base::FilePath();
233 // Create a filename.
234 base::string16 filename;
235 base::Time now(base::Time::Now());
236 filename = base::TimeFormatShortDateAndTime(now);
237 filename += base::ASCIIToUTF16("_");
238 filename += document_name;
239 base::FilePath::StringType system_filename;
240 #if defined(OS_WIN)
241 system_filename = filename;
242 #else // OS_WIN
243 system_filename = base::UTF16ToUTF8(filename);
244 #endif // OS_WIN
245 file_util::ReplaceIllegalCharactersInPath(&system_filename, '_');
246 return g_debug_dump_info.Get().Append(system_filename).AddExtension(
247 extension);
248 }
249
250 void PrintedDocument::DebugDumpData(
251 const base::RefCountedMemory* data,
252 const base::FilePath::StringType& extension) {
253 if (g_debug_dump_info.Get().empty())
254 return;
255 immutable_.blocking_runner_->PostTask(
256 FROM_HERE, base::Bind(&DebugDumpDataTask, name(), extension, data));
205 } 257 }
206 258
207 PrintedDocument::Mutable::Mutable(PrintedPagesSource* source) 259 PrintedDocument::Mutable::Mutable(PrintedPagesSource* source)
208 : source_(source), 260 : source_(source),
209 expected_page_count_(0), 261 expected_page_count_(0),
210 page_count_(0) { 262 page_count_(0) {
211 #if defined(OS_POSIX) && !defined(OS_MACOSX) 263 #if defined(OS_POSIX) && !defined(OS_MACOSX)
212 first_page = INT_MAX; 264 first_page = INT_MAX;
213 #endif 265 #endif
214 } 266 }
215 267
216 PrintedDocument::Mutable::~Mutable() { 268 PrintedDocument::Mutable::~Mutable() {
217 } 269 }
218 270
219 PrintedDocument::Immutable::Immutable(const PrintSettings& settings, 271 PrintedDocument::Immutable::Immutable(const PrintSettings& settings,
220 PrintedPagesSource* source, 272 PrintedPagesSource* source,
221 int cookie) 273 int cookie,
274 base::TaskRunner* blocking_runner)
222 : settings_(settings), 275 : settings_(settings),
223 source_message_loop_(base::MessageLoop::current()),
224 name_(source->RenderSourceName()), 276 name_(source->RenderSourceName()),
225 cookie_(cookie) { 277 cookie_(cookie),
278 blocking_runner_(blocking_runner) {
226 } 279 }
227 280
228 PrintedDocument::Immutable::~Immutable() { 281 PrintedDocument::Immutable::~Immutable() {
229 } 282 }
230 283
231 #if defined(OS_CHROMEOS) || defined(OS_ANDROID) 284 #if defined(OS_CHROMEOS) || defined(OS_ANDROID)
232 // This function is not used on aura linux/chromeos or android. 285 // This function is not used on aura linux/chromeos or android.
233 void PrintedDocument::RenderPrintedPage(const PrintedPage& page, 286 void PrintedDocument::RenderPrintedPage(const PrintedPage& page,
234 PrintingContext* context) const { 287 PrintingContext* context) const {
235 } 288 }
236 #endif 289 #endif
237 290
238 } // namespace printing 291 } // namespace printing
OLDNEW
« no previous file with comments | « printing/printed_document.h ('k') | printing/printing.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698