[email protected] | 5cc4c42 | 2011-02-19 00:09:22 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 5 | #include "printing/printed_document.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
[email protected] | 7d2d0d0e | 2010-03-06 22:16:10 | [diff] [blame] | 7 | #include <algorithm> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 8 | #include <set> |
[email protected] | 7d2d0d0e | 2010-03-06 22:16:10 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <vector> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 11 | |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 12 | #include "base/bind.h" |
[email protected] | e1504d8 | 2009-07-03 15:27:15 | [diff] [blame] | 13 | #include "base/file_util.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 14 | #include "base/files/file_path.h" |
[email protected] | d0767cb54 | 2009-10-08 17:38:30 | [diff] [blame] | 15 | #include "base/i18n/file_util_icu.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 16 | #include "base/i18n/time_formatting.h" |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 17 | #include "base/json/json_writer.h" |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 18 | #include "base/lazy_instance.h" |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 19 | #include "base/memory/ref_counted_memory.h" |
[email protected] | a7629597 | 2013-07-18 00:42:32 | [diff] [blame] | 20 | #include "base/message_loop/message_loop.h" |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 21 | #include "base/numerics/safe_conversions.h" |
[email protected] | 896d161f | 2013-06-11 22:52:24 | [diff] [blame] | 22 | #include "base/strings/string_util.h" |
| 23 | #include "base/strings/stringprintf.h" |
[email protected] | 90626587 | 2013-06-07 22:40:45 | [diff] [blame] | 24 | #include "base/strings/utf_string_conversions.h" |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 25 | #include "base/time/time.h" |
| 26 | #include "base/values.h" |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 27 | #include "printing/page_number.h" |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 28 | #include "printing/print_settings_conversion.h" |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 29 | #include "printing/printed_page.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 30 | #include "printing/printed_pages_source.h" |
[email protected] | 4ae30d08 | 2009-02-20 17:55:55 | [diff] [blame] | 31 | #include "printing/units.h" |
[email protected] | c399a8a | 2008-11-22 19:38:00 | [diff] [blame] | 32 | #include "skia/ext/platform_device.h" |
[email protected] | 08397d5 | 2011-02-05 01:53:38 | [diff] [blame] | 33 | #include "ui/gfx/font.h" |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 34 | #include "ui/gfx/text_elider.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 35 | |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 36 | namespace printing { |
| 37 | |
[email protected] | e1504d8 | 2009-07-03 15:27:15 | [diff] [blame] | 38 | namespace { |
| 39 | |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 40 | base::LazyInstance<base::FilePath> g_debug_dump_info = |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 41 | LAZY_INSTANCE_INITIALIZER; |
[email protected] | e1504d8 | 2009-07-03 15:27:15 | [diff] [blame] | 42 | |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 43 | void DebugDumpPageTask(const base::string16& doc_name, |
| 44 | const PrintedPage* page) { |
| 45 | if (g_debug_dump_info.Get().empty()) |
| 46 | return; |
| 47 | |
| 48 | base::string16 filename = doc_name; |
| 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 | } |
| 87 | |
[email protected] | e1504d8 | 2009-07-03 15:27:15 | [diff] [blame] | 88 | } // namespace |
| 89 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 90 | PrintedDocument::PrintedDocument(const PrintSettings& settings, |
| 91 | PrintedPagesSource* source, |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 92 | int cookie, |
| 93 | base::TaskRunner* blocking_runner) |
| 94 | : mutable_(source), immutable_(settings, source, cookie, blocking_runner) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 95 | // Records the expected page count if a range is setup. |
[email protected] | e5324b5 | 2013-10-29 03:16:37 | [diff] [blame] | 96 | if (!settings.ranges().empty()) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 97 | // If there is a range, set the number of page |
[email protected] | e5324b5 | 2013-10-29 03:16:37 | [diff] [blame] | 98 | for (unsigned i = 0; i < settings.ranges().size(); ++i) { |
| 99 | const PageRange& range = settings.ranges()[i]; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 100 | mutable_.expected_page_count_ += range.to - range.from + 1; |
| 101 | } |
| 102 | } |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 103 | |
| 104 | if (!g_debug_dump_info.Get().empty()) |
| 105 | DebugDumpSettings(name(), settings, blocking_runner); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | PrintedDocument::~PrintedDocument() { |
| 109 | } |
| 110 | |
[email protected] | 0e0fca3 | 2009-07-06 15:25:50 | [diff] [blame] | 111 | void PrintedDocument::SetPage(int page_number, |
[email protected] | 7d748990 | 2011-04-11 21:54:06 | [diff] [blame] | 112 | Metafile* metafile, |
[email protected] | 9de1347a | 2014-06-12 09:49:08 | [diff] [blame] | 113 | #if defined(OS_WIN) |
[email protected] | 40c7cfe | 2010-07-01 07:04:05 | [diff] [blame] | 114 | double shrink, |
[email protected] | 9de1347a | 2014-06-12 09:49:08 | [diff] [blame] | 115 | #endif // OS_WIN |
[email protected] | 40c7cfe | 2010-07-01 07:04:05 | [diff] [blame] | 116 | const gfx::Size& paper_size, |
[email protected] | 3cce538 | 2011-09-23 23:21:21 | [diff] [blame] | 117 | const gfx::Rect& page_rect) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 118 | // Notice the page_number + 1, the reason is that this is the value that will |
| 119 | // be shown. Users dislike 0-based counting. |
| 120 | scoped_refptr<PrintedPage> page( |
[email protected] | 9de1347a | 2014-06-12 09:49:08 | [diff] [blame] | 121 | new PrintedPage(page_number + 1, metafile, paper_size, page_rect)); |
| 122 | #if defined(OS_WIN) |
| 123 | page->set_shrink_factor(shrink); |
| 124 | #endif // OS_WIN |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 125 | { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 126 | base::AutoLock lock(lock_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 127 | mutable_.pages_[page_number] = page; |
[email protected] | da4eefd | 2011-03-03 23:40:27 | [diff] [blame] | 128 | |
| 129 | #if defined(OS_POSIX) && !defined(OS_MACOSX) |
| 130 | if (page_number < mutable_.first_page) |
| 131 | mutable_.first_page = page_number; |
| 132 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 133 | } |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 134 | |
| 135 | if (!g_debug_dump_info.Get().empty()) { |
| 136 | immutable_.blocking_runner_->PostTask( |
| 137 | FROM_HERE, base::Bind(&DebugDumpPageTask, name(), page)); |
| 138 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 139 | } |
| 140 | |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 141 | scoped_refptr<PrintedPage> PrintedDocument::GetPage(int page_number) { |
| 142 | scoped_refptr<PrintedPage> page; |
| 143 | { |
| 144 | base::AutoLock lock(lock_); |
| 145 | PrintedPages::const_iterator itr = mutable_.pages_.find(page_number); |
| 146 | if (itr != mutable_.pages_.end()) |
| 147 | page = itr->second; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 148 | } |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 149 | return page; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 150 | } |
| 151 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 152 | bool PrintedDocument::IsComplete() const { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 153 | base::AutoLock lock(lock_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 154 | if (!mutable_.page_count_) |
| 155 | return false; |
| 156 | PageNumber page(immutable_.settings_, mutable_.page_count_); |
| 157 | if (page == PageNumber::npos()) |
| 158 | return false; |
[email protected] | 5cc4c42 | 2011-02-19 00:09:22 | [diff] [blame] | 159 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 160 | for (; page != PageNumber::npos(); ++page) { |
[email protected] | da4eefd | 2011-03-03 23:40:27 | [diff] [blame] | 161 | #if defined(OS_WIN) || defined(OS_MACOSX) |
| 162 | const bool metafile_must_be_valid = true; |
[email protected] | 5cc4c42 | 2011-02-19 00:09:22 | [diff] [blame] | 163 | #elif defined(OS_POSIX) |
[email protected] | da4eefd | 2011-03-03 23:40:27 | [diff] [blame] | 164 | const bool metafile_must_be_valid = (page.ToInt() == mutable_.first_page); |
[email protected] | 5cc4c42 | 2011-02-19 00:09:22 | [diff] [blame] | 165 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 166 | PrintedPages::const_iterator itr = mutable_.pages_.find(page.ToInt()); |
[email protected] | 5cc4c42 | 2011-02-19 00:09:22 | [diff] [blame] | 167 | if (itr == mutable_.pages_.end() || !itr->second.get()) |
| 168 | return false; |
[email protected] | 7d748990 | 2011-04-11 21:54:06 | [diff] [blame] | 169 | if (metafile_must_be_valid && !itr->second->metafile()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 170 | return false; |
| 171 | } |
| 172 | return true; |
| 173 | } |
| 174 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 175 | void PrintedDocument::DisconnectSource() { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 176 | base::AutoLock lock(lock_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 177 | mutable_.source_ = NULL; |
| 178 | } |
| 179 | |
[email protected] | b5ab398 | 2010-02-16 23:58:27 | [diff] [blame] | 180 | uint32 PrintedDocument::MemoryUsage() const { |
[email protected] | 60741b4 | 2009-09-14 13:57:10 | [diff] [blame] | 181 | std::vector< scoped_refptr<PrintedPage> > pages_copy; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 182 | { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 183 | base::AutoLock lock(lock_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 184 | pages_copy.reserve(mutable_.pages_.size()); |
| 185 | PrintedPages::const_iterator end = mutable_.pages_.end(); |
| 186 | for (PrintedPages::const_iterator itr = mutable_.pages_.begin(); |
| 187 | itr != end; ++itr) { |
| 188 | if (itr->second.get()) { |
| 189 | pages_copy.push_back(itr->second); |
| 190 | } |
| 191 | } |
| 192 | } |
[email protected] | b5ab398 | 2010-02-16 23:58:27 | [diff] [blame] | 193 | uint32 total = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 194 | for (size_t i = 0; i < pages_copy.size(); ++i) { |
[email protected] | 7d748990 | 2011-04-11 21:54:06 | [diff] [blame] | 195 | total += pages_copy[i]->metafile()->GetDataSize(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 196 | } |
| 197 | return total; |
| 198 | } |
| 199 | |
| 200 | void PrintedDocument::set_page_count(int max_page) { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 201 | base::AutoLock lock(lock_); |
[email protected] | e1504d8 | 2009-07-03 15:27:15 | [diff] [blame] | 202 | DCHECK_EQ(0, mutable_.page_count_); |
| 203 | mutable_.page_count_ = max_page; |
[email protected] | e5324b5 | 2013-10-29 03:16:37 | [diff] [blame] | 204 | if (immutable_.settings_.ranges().empty()) { |
[email protected] | e1504d8 | 2009-07-03 15:27:15 | [diff] [blame] | 205 | mutable_.expected_page_count_ = max_page; |
| 206 | } else { |
| 207 | // If there is a range, don't bother since expected_page_count_ is already |
| 208 | // initialized. |
| 209 | DCHECK_NE(mutable_.expected_page_count_, 0); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 210 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | int PrintedDocument::page_count() const { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 214 | base::AutoLock lock(lock_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 215 | return mutable_.page_count_; |
| 216 | } |
| 217 | |
| 218 | int PrintedDocument::expected_page_count() const { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 219 | base::AutoLock lock(lock_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 220 | return mutable_.expected_page_count_; |
| 221 | } |
| 222 | |
[email protected] | 79f6388 | 2013-02-10 05:15:45 | [diff] [blame] | 223 | void PrintedDocument::set_debug_dump_path( |
| 224 | const base::FilePath& debug_dump_path) { |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 225 | g_debug_dump_info.Get() = debug_dump_path; |
[email protected] | e1504d8 | 2009-07-03 15:27:15 | [diff] [blame] | 226 | } |
| 227 | |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 228 | base::FilePath PrintedDocument::CreateDebugDumpPath( |
| 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 |
[email protected] | 6bc03de | 2014-08-07 23:59:15 | [diff] [blame] | 245 | base::i18n::ReplaceIllegalCharactersInPath(&system_filename, '_'); |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 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)); |
[email protected] | e1504d8 | 2009-07-03 15:27:15 | [diff] [blame] | 257 | } |
| 258 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 259 | PrintedDocument::Mutable::Mutable(PrintedPagesSource* source) |
| 260 | : source_(source), |
| 261 | expected_page_count_(0), |
[email protected] | 732b813 | 2012-01-10 23:17:32 | [diff] [blame] | 262 | page_count_(0) { |
[email protected] | da4eefd | 2011-03-03 23:40:27 | [diff] [blame] | 263 | #if defined(OS_POSIX) && !defined(OS_MACOSX) |
| 264 | first_page = INT_MAX; |
| 265 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 266 | } |
| 267 | |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 268 | PrintedDocument::Mutable::~Mutable() { |
| 269 | } |
| 270 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 271 | PrintedDocument::Immutable::Immutable(const PrintSettings& settings, |
| 272 | PrintedPagesSource* source, |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 273 | int cookie, |
| 274 | base::TaskRunner* blocking_runner) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 275 | : settings_(settings), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 276 | name_(source->RenderSourceName()), |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 277 | cookie_(cookie), |
| 278 | blocking_runner_(blocking_runner) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 279 | } |
| 280 | |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 281 | PrintedDocument::Immutable::~Immutable() { |
| 282 | } |
| 283 | |
[email protected] | d53002f4 | 2014-01-14 16:08:56 | [diff] [blame] | 284 | #if defined(OS_CHROMEOS) || defined(OS_ANDROID) |
[email protected] | b25f003 | 2013-08-19 22:26:25 | [diff] [blame] | 285 | // This function is not used on aura linux/chromeos or android. |
[email protected] | 82b72cb8 | 2011-12-06 00:23:44 | [diff] [blame] | 286 | void PrintedDocument::RenderPrintedPage(const PrintedPage& page, |
| 287 | PrintingContext* context) const { |
| 288 | } |
| 289 | #endif |
| 290 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 291 | } // namespace printing |