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

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: Wed 06/11/2014 10:41:42.83 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
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/file_util.h" 12 #include "base/file_util.h"
13 #include "base/files/file_path.h" 13 #include "base/files/file_path.h"
14 #include "base/i18n/file_util_icu.h" 14 #include "base/i18n/file_util_icu.h"
15 #include "base/i18n/time_formatting.h" 15 #include "base/i18n/time_formatting.h"
16 #include "base/json/json_writer.h"
16 #include "base/lazy_instance.h" 17 #include "base/lazy_instance.h"
17 #include "base/message_loop/message_loop.h" 18 #include "base/message_loop/message_loop.h"
19 #include "base/numerics/safe_conversions.h"
18 #include "base/strings/string_util.h" 20 #include "base/strings/string_util.h"
19 #include "base/strings/stringprintf.h" 21 #include "base/strings/stringprintf.h"
20 #include "base/strings/utf_string_conversions.h" 22 #include "base/strings/utf_string_conversions.h"
23 #include "base/time/time.h"
24 #include "base/values.h"
21 #include "printing/page_number.h" 25 #include "printing/page_number.h"
26 #include "printing/print_settings_conversion.h"
22 #include "printing/printed_page.h" 27 #include "printing/printed_page.h"
23 #include "printing/printed_pages_source.h" 28 #include "printing/printed_pages_source.h"
24 #include "printing/units.h" 29 #include "printing/units.h"
25 #include "skia/ext/platform_device.h" 30 #include "skia/ext/platform_device.h"
26 #include "ui/gfx/font.h" 31 #include "ui/gfx/font.h"
27 #include "ui/gfx/text_elider.h" 32 #include "ui/gfx/text_elider.h"
28 33
34 namespace printing {
35
29 namespace { 36 namespace {
30 37
31 struct PrintDebugDumpPath { 38 base::LazyInstance<base::FilePath> g_debug_dump_info =
32 PrintDebugDumpPath()
33 : enabled(false) {
34 }
35
36 bool enabled;
37 base::FilePath debug_dump_path;
38 };
39
40 base::LazyInstance<PrintDebugDumpPath> g_debug_dump_info =
41 LAZY_INSTANCE_INITIALIZER; 39 LAZY_INSTANCE_INITIALIZER;
42 40
43 } // namespace 41 } // namespace
44 42
45 namespace printing {
46
47 PrintedDocument::PrintedDocument(const PrintSettings& settings, 43 PrintedDocument::PrintedDocument(const PrintSettings& settings,
48 PrintedPagesSource* source, 44 PrintedPagesSource* source,
49 int cookie) 45 int cookie)
50 : mutable_(source), 46 : mutable_(source),
51 immutable_(settings, source, cookie) { 47 immutable_(settings, source, cookie) {
52 48
53 // Records the expected page count if a range is setup. 49 // Records the expected page count if a range is setup.
54 if (!settings.ranges().empty()) { 50 if (!settings.ranges().empty()) {
55 // If there is a range, set the number of page 51 // If there is a range, set the number of page
56 for (unsigned i = 0; i < settings.ranges().size(); ++i) { 52 for (unsigned i = 0; i < settings.ranges().size(); ++i) {
(...skipping 21 matching lines...) Expand all
78 shrink)); 74 shrink));
79 { 75 {
80 base::AutoLock lock(lock_); 76 base::AutoLock lock(lock_);
81 mutable_.pages_[page_number] = page; 77 mutable_.pages_[page_number] = page;
82 78
83 #if defined(OS_POSIX) && !defined(OS_MACOSX) 79 #if defined(OS_POSIX) && !defined(OS_MACOSX)
84 if (page_number < mutable_.first_page) 80 if (page_number < mutable_.first_page)
85 mutable_.first_page = page_number; 81 mutable_.first_page = page_number;
86 #endif 82 #endif
87 } 83 }
88 DebugDump(*page.get());
89 } 84 }
90 85
91 bool PrintedDocument::GetPage(int page_number, 86 scoped_refptr<PrintedPage> PrintedDocument::GetPage(int page_number) {
92 scoped_refptr<PrintedPage>* page) { 87 scoped_refptr<PrintedPage> page;
93 base::AutoLock lock(lock_); 88 {
94 PrintedPages::const_iterator itr = mutable_.pages_.find(page_number); 89 base::AutoLock lock(lock_);
95 if (itr != mutable_.pages_.end()) { 90 PrintedPages::const_iterator itr = mutable_.pages_.find(page_number);
96 if (itr->second.get()) { 91 if (itr != mutable_.pages_.end())
97 *page = itr->second; 92 page = itr->second;
98 return true;
99 }
100 } 93 }
101 return false; 94 if (page)
95 DebugDump(*page);
96 return page;
102 } 97 }
103 98
104 bool PrintedDocument::IsComplete() const { 99 bool PrintedDocument::IsComplete() const {
105 base::AutoLock lock(lock_); 100 base::AutoLock lock(lock_);
106 if (!mutable_.page_count_) 101 if (!mutable_.page_count_)
107 return false; 102 return false;
108 PageNumber page(immutable_.settings_, mutable_.page_count_); 103 PageNumber page(immutable_.settings_, mutable_.page_count_);
109 if (page == PageNumber::npos()) 104 if (page == PageNumber::npos())
110 return false; 105 return false;
111 106
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 base::AutoLock lock(lock_); 161 base::AutoLock lock(lock_);
167 return mutable_.page_count_; 162 return mutable_.page_count_;
168 } 163 }
169 164
170 int PrintedDocument::expected_page_count() const { 165 int PrintedDocument::expected_page_count() const {
171 base::AutoLock lock(lock_); 166 base::AutoLock lock(lock_);
172 return mutable_.expected_page_count_; 167 return mutable_.expected_page_count_;
173 } 168 }
174 169
175 void PrintedDocument::DebugDump(const PrintedPage& page) { 170 void PrintedDocument::DebugDump(const PrintedPage& page) {
176 if (!g_debug_dump_info.Get().enabled) 171 if (g_debug_dump_info.Get().empty())
177 return; 172 return;
178 173
179 base::string16 filename; 174 base::string16 filename = name();
180 filename += name();
181 filename += base::ASCIIToUTF16("_");
182 filename += base::ASCIIToUTF16( 175 filename += base::ASCIIToUTF16(
183 base::StringPrintf("%02d", page.page_number())); 176 base::StringPrintf("_%04d", page.page_number()));
184 #if defined(OS_WIN) 177 #if defined(OS_WIN)
185 filename += base::ASCIIToUTF16("_.emf"); 178 page.metafile()->SaveTo(CreateDebugDumpPath(filename, "emf"));
186 page.metafile()->SaveTo(
187 g_debug_dump_info.Get().debug_dump_path.Append(filename));
188 #else // OS_WIN 179 #else // OS_WIN
189 filename += base::ASCIIToUTF16("_.pdf"); 180 page.metafile()->SaveTo(CreateDebugDumpPath(filename, "pdf"));
190 page.metafile()->SaveTo(
191 g_debug_dump_info.Get().debug_dump_path.Append(
192 base::UTF16ToUTF8(filename)));
193 #endif // OS_WIN 181 #endif // OS_WIN
182
183 base::DictionaryValue job_settings;
184 PrintSettingsToJobSettingsDebug(settings(), &job_settings);
185 std::string settings;
186 base::JSONWriter::WriteWithOptions(
187 &job_settings, base::JSONWriter::OPTIONS_PRETTY_PRINT, &settings);
188 base::WriteFile(CreateDebugDumpPath(filename, "json"),
189 settings.data(),
190 base::checked_cast<int>(settings.size()));
194 } 191 }
195 192
196 void PrintedDocument::set_debug_dump_path( 193 void PrintedDocument::set_debug_dump_path(
197 const base::FilePath& debug_dump_path) { 194 const base::FilePath& debug_dump_path) {
198 g_debug_dump_info.Get().enabled = !debug_dump_path.empty(); 195 g_debug_dump_info.Get() = debug_dump_path;
199 g_debug_dump_info.Get().debug_dump_path = debug_dump_path;
200 } 196 }
201 197
202 const base::FilePath& PrintedDocument::debug_dump_path() { 198 base::FilePath PrintedDocument::CreateDebugDumpPath(
203 return g_debug_dump_info.Get().debug_dump_path; 199 const base::string16& document_name,
200 const std::string& extension) {
201 if (g_debug_dump_info.Get().empty())
202 return base::FilePath();
203 // Create a filename.
204 base::string16 filename;
205 base::Time now(base::Time::Now());
206 filename = base::TimeFormatShortDateAndTime(now);
Lei Zhang 2014/06/11 20:03:06 Might be clear to StringPrintf() this.
Vitaly Buka (NO REVIEWS) 2014/06/11 21:32:57 You mean document name? We have not StringPrintf f
207 filename += base::ASCIIToUTF16("_");
208 filename += document_name;
209 filename += base::ASCIIToUTF16(".");
210 filename += base::ASCIIToUTF16(extension);
211 base::FilePath::StringType system_filename;
212 #if defined(OS_WIN)
213 system_filename = filename;
214 #else // OS_WIN
215 system_filename = base::UTF16ToUTF8(filename);
216 #endif // OS_WIN
217 file_util::ReplaceIllegalCharactersInPath(&system_filename, '_');
218 return g_debug_dump_info.Get().Append(system_filename);
204 } 219 }
205 220
206 PrintedDocument::Mutable::Mutable(PrintedPagesSource* source) 221 PrintedDocument::Mutable::Mutable(PrintedPagesSource* source)
207 : source_(source), 222 : source_(source),
208 expected_page_count_(0), 223 expected_page_count_(0),
209 page_count_(0) { 224 page_count_(0) {
210 #if defined(OS_POSIX) && !defined(OS_MACOSX) 225 #if defined(OS_POSIX) && !defined(OS_MACOSX)
211 first_page = INT_MAX; 226 first_page = INT_MAX;
212 #endif 227 #endif
213 } 228 }
(...skipping 14 matching lines...) Expand all
228 } 243 }
229 244
230 #if defined(OS_CHROMEOS) || defined(OS_ANDROID) 245 #if defined(OS_CHROMEOS) || defined(OS_ANDROID)
231 // This function is not used on aura linux/chromeos or android. 246 // This function is not used on aura linux/chromeos or android.
232 void PrintedDocument::RenderPrintedPage(const PrintedPage& page, 247 void PrintedDocument::RenderPrintedPage(const PrintedPage& page,
233 PrintingContext* context) const { 248 PrintingContext* context) const {
234 } 249 }
235 #endif 250 #endif
236 251
237 } // namespace printing 252 } // namespace printing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698