| OLD | NEW |
| 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 DebugDumpPageOnFileThread(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( |
| 53 PrintedDocument::CreateDebugDumpPath(filename, "emf")); |
| 54 #else // OS_WIN |
| 55 page->metafile()->SaveTo( |
| 56 PrintedDocument::CreateDebugDumpPath(filename, "pdf")); |
| 57 #endif // OS_WIN |
| 58 } |
| 59 |
| 60 void DebugDumpDataOnFileThread(const base::string16& doc_name, |
| 61 const std::string& 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* file_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 file_runner->PostTask( |
| 83 FROM_HERE, |
| 84 base::Bind(&DebugDumpDataOnFileThread, doc_name, "json", data)); |
| 85 } |
| 42 | 86 |
| 43 } // namespace | 87 } // namespace |
| 44 | 88 |
| 45 namespace printing { | |
| 46 | |
| 47 PrintedDocument::PrintedDocument(const PrintSettings& settings, | 89 PrintedDocument::PrintedDocument(const PrintSettings& settings, |
| 48 PrintedPagesSource* source, | 90 PrintedPagesSource* source, |
| 49 int cookie) | 91 int cookie, |
| 50 : mutable_(source), | 92 base::TaskRunner* file_runner) |
| 51 immutable_(settings, source, cookie) { | 93 : mutable_(source), immutable_(settings, source, cookie, file_runner) { |
| 52 | |
| 53 // Records the expected page count if a range is setup. | 94 // Records the expected page count if a range is setup. |
| 54 if (!settings.ranges().empty()) { | 95 if (!settings.ranges().empty()) { |
| 55 // If there is a range, set the number of page | 96 // If there is a range, set the number of page |
| 56 for (unsigned i = 0; i < settings.ranges().size(); ++i) { | 97 for (unsigned i = 0; i < settings.ranges().size(); ++i) { |
| 57 const PageRange& range = settings.ranges()[i]; | 98 const PageRange& range = settings.ranges()[i]; |
| 58 mutable_.expected_page_count_ += range.to - range.from + 1; | 99 mutable_.expected_page_count_ += range.to - range.from + 1; |
| 59 } | 100 } |
| 60 } | 101 } |
| 102 |
| 103 if (!g_debug_dump_info.Get().empty()) |
| 104 DebugDumpSettings(name(), settings, file_runner); |
| 61 } | 105 } |
| 62 | 106 |
| 63 PrintedDocument::~PrintedDocument() { | 107 PrintedDocument::~PrintedDocument() { |
| 64 } | 108 } |
| 65 | 109 |
| 66 void PrintedDocument::SetPage(int page_number, | 110 void PrintedDocument::SetPage(int page_number, |
| 67 Metafile* metafile, | 111 Metafile* metafile, |
| 68 double shrink, | 112 double shrink, |
| 69 const gfx::Size& paper_size, | 113 const gfx::Size& paper_size, |
| 70 const gfx::Rect& page_rect) { | 114 const gfx::Rect& page_rect) { |
| 71 // Notice the page_number + 1, the reason is that this is the value that will | 115 // Notice the page_number + 1, the reason is that this is the value that will |
| 72 // be shown. Users dislike 0-based counting. | 116 // be shown. Users dislike 0-based counting. |
| 73 scoped_refptr<PrintedPage> page( | 117 scoped_refptr<PrintedPage> page( |
| 74 new PrintedPage(page_number + 1, | 118 new PrintedPage(page_number + 1, |
| 75 metafile, | 119 metafile, |
| 76 paper_size, | 120 paper_size, |
| 77 page_rect, | 121 page_rect, |
| 78 shrink)); | 122 shrink)); |
| 79 { | 123 { |
| 80 base::AutoLock lock(lock_); | 124 base::AutoLock lock(lock_); |
| 81 mutable_.pages_[page_number] = page; | 125 mutable_.pages_[page_number] = page; |
| 82 | 126 |
| 83 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 127 #if defined(OS_POSIX) && !defined(OS_MACOSX) |
| 84 if (page_number < mutable_.first_page) | 128 if (page_number < mutable_.first_page) |
| 85 mutable_.first_page = page_number; | 129 mutable_.first_page = page_number; |
| 86 #endif | 130 #endif |
| 87 } | 131 } |
| 88 DebugDump(*page.get()); | 132 |
| 133 if (!g_debug_dump_info.Get().empty()) { |
| 134 immutable_.file_runner_->PostTask( |
| 135 FROM_HERE, base::Bind(&DebugDumpPageOnFileThread, name(), page)); |
| 136 } |
| 89 } | 137 } |
| 90 | 138 |
| 91 bool PrintedDocument::GetPage(int page_number, | 139 scoped_refptr<PrintedPage> PrintedDocument::GetPage(int page_number) { |
| 92 scoped_refptr<PrintedPage>* page) { | 140 scoped_refptr<PrintedPage> page; |
| 93 base::AutoLock lock(lock_); | 141 { |
| 94 PrintedPages::const_iterator itr = mutable_.pages_.find(page_number); | 142 base::AutoLock lock(lock_); |
| 95 if (itr != mutable_.pages_.end()) { | 143 PrintedPages::const_iterator itr = mutable_.pages_.find(page_number); |
| 96 if (itr->second.get()) { | 144 if (itr != mutable_.pages_.end()) |
| 97 *page = itr->second; | 145 page = itr->second; |
| 98 return true; | |
| 99 } | |
| 100 } | 146 } |
| 101 return false; | 147 return page; |
| 102 } | 148 } |
| 103 | 149 |
| 104 bool PrintedDocument::IsComplete() const { | 150 bool PrintedDocument::IsComplete() const { |
| 105 base::AutoLock lock(lock_); | 151 base::AutoLock lock(lock_); |
| 106 if (!mutable_.page_count_) | 152 if (!mutable_.page_count_) |
| 107 return false; | 153 return false; |
| 108 PageNumber page(immutable_.settings_, mutable_.page_count_); | 154 PageNumber page(immutable_.settings_, mutable_.page_count_); |
| 109 if (page == PageNumber::npos()) | 155 if (page == PageNumber::npos()) |
| 110 return false; | 156 return false; |
| 111 | 157 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 int PrintedDocument::page_count() const { | 211 int PrintedDocument::page_count() const { |
| 166 base::AutoLock lock(lock_); | 212 base::AutoLock lock(lock_); |
| 167 return mutable_.page_count_; | 213 return mutable_.page_count_; |
| 168 } | 214 } |
| 169 | 215 |
| 170 int PrintedDocument::expected_page_count() const { | 216 int PrintedDocument::expected_page_count() const { |
| 171 base::AutoLock lock(lock_); | 217 base::AutoLock lock(lock_); |
| 172 return mutable_.expected_page_count_; | 218 return mutable_.expected_page_count_; |
| 173 } | 219 } |
| 174 | 220 |
| 175 void PrintedDocument::DebugDump(const PrintedPage& page) { | |
| 176 if (!g_debug_dump_info.Get().enabled) | |
| 177 return; | |
| 178 | |
| 179 base::string16 filename; | |
| 180 filename += name(); | |
| 181 filename += base::ASCIIToUTF16("_"); | |
| 182 filename += base::ASCIIToUTF16( | |
| 183 base::StringPrintf("%02d", page.page_number())); | |
| 184 #if defined(OS_WIN) | |
| 185 filename += base::ASCIIToUTF16("_.emf"); | |
| 186 page.metafile()->SaveTo( | |
| 187 g_debug_dump_info.Get().debug_dump_path.Append(filename)); | |
| 188 #else // OS_WIN | |
| 189 filename += base::ASCIIToUTF16("_.pdf"); | |
| 190 page.metafile()->SaveTo( | |
| 191 g_debug_dump_info.Get().debug_dump_path.Append( | |
| 192 base::UTF16ToUTF8(filename))); | |
| 193 #endif // OS_WIN | |
| 194 } | |
| 195 | |
| 196 void PrintedDocument::set_debug_dump_path( | 221 void PrintedDocument::set_debug_dump_path( |
| 197 const base::FilePath& debug_dump_path) { | 222 const base::FilePath& debug_dump_path) { |
| 198 g_debug_dump_info.Get().enabled = !debug_dump_path.empty(); | 223 g_debug_dump_info.Get() = debug_dump_path; |
| 199 g_debug_dump_info.Get().debug_dump_path = debug_dump_path; | |
| 200 } | 224 } |
| 201 | 225 |
| 202 const base::FilePath& PrintedDocument::debug_dump_path() { | 226 base::FilePath PrintedDocument::CreateDebugDumpPath( |
| 203 return g_debug_dump_info.Get().debug_dump_path; | 227 const base::string16& document_name, |
| 228 const std::string& extension) { |
| 229 if (g_debug_dump_info.Get().empty()) |
| 230 return base::FilePath(); |
| 231 // Create a filename. |
| 232 base::string16 filename; |
| 233 base::Time now(base::Time::Now()); |
| 234 filename = base::TimeFormatShortDateAndTime(now); |
| 235 filename += base::ASCIIToUTF16("_"); |
| 236 filename += document_name; |
| 237 filename += base::ASCIIToUTF16("."); |
| 238 filename += base::ASCIIToUTF16(extension); |
| 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); |
| 247 } |
| 248 |
| 249 void PrintedDocument::DebugDumpData(const base::RefCountedMemory* data, |
| 250 const std::string& extension) { |
| 251 if (g_debug_dump_info.Get().empty()) |
| 252 return; |
| 253 immutable_.file_runner_->PostTask( |
| 254 FROM_HERE, |
| 255 base::Bind(&DebugDumpDataOnFileThread, name(), extension, data)); |
| 204 } | 256 } |
| 205 | 257 |
| 206 PrintedDocument::Mutable::Mutable(PrintedPagesSource* source) | 258 PrintedDocument::Mutable::Mutable(PrintedPagesSource* source) |
| 207 : source_(source), | 259 : source_(source), |
| 208 expected_page_count_(0), | 260 expected_page_count_(0), |
| 209 page_count_(0) { | 261 page_count_(0) { |
| 210 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 262 #if defined(OS_POSIX) && !defined(OS_MACOSX) |
| 211 first_page = INT_MAX; | 263 first_page = INT_MAX; |
| 212 #endif | 264 #endif |
| 213 } | 265 } |
| 214 | 266 |
| 215 PrintedDocument::Mutable::~Mutable() { | 267 PrintedDocument::Mutable::~Mutable() { |
| 216 } | 268 } |
| 217 | 269 |
| 218 PrintedDocument::Immutable::Immutable(const PrintSettings& settings, | 270 PrintedDocument::Immutable::Immutable(const PrintSettings& settings, |
| 219 PrintedPagesSource* source, | 271 PrintedPagesSource* source, |
| 220 int cookie) | 272 int cookie, |
| 273 base::TaskRunner* file_runner) |
| 221 : settings_(settings), | 274 : settings_(settings), |
| 222 source_message_loop_(base::MessageLoop::current()), | |
| 223 name_(source->RenderSourceName()), | 275 name_(source->RenderSourceName()), |
| 224 cookie_(cookie) { | 276 cookie_(cookie), |
| 277 file_runner_(file_runner) { |
| 225 } | 278 } |
| 226 | 279 |
| 227 PrintedDocument::Immutable::~Immutable() { | 280 PrintedDocument::Immutable::~Immutable() { |
| 228 } | 281 } |
| 229 | 282 |
| 230 #if defined(OS_CHROMEOS) || defined(OS_ANDROID) | 283 #if defined(OS_CHROMEOS) || defined(OS_ANDROID) |
| 231 // This function is not used on aura linux/chromeos or android. | 284 // This function is not used on aura linux/chromeos or android. |
| 232 void PrintedDocument::RenderPrintedPage(const PrintedPage& page, | 285 void PrintedDocument::RenderPrintedPage(const PrintedPage& page, |
| 233 PrintingContext* context) const { | 286 PrintingContext* context) const { |
| 234 } | 287 } |
| 235 #endif | 288 #endif |
| 236 | 289 |
| 237 } // namespace printing | 290 } // namespace printing |
| OLD | NEW |