[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] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 13 | #include "base/files/file_path.h" |
thestig | 22dfc401 | 2014-09-05 08:29:44 | [diff] [blame] | 14 | #include "base/files/file_util.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())); |
vitalybuka | 5d129058 | 2014-09-12 09:19:59 | [diff] [blame] | 51 | base::FilePath file_path = |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 52 | #if defined(OS_WIN) |
vitalybuka | 5d129058 | 2014-09-12 09:19:59 | [diff] [blame] | 53 | PrintedDocument::CreateDebugDumpPath(filename, FILE_PATH_LITERAL(".emf")); |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 54 | #else // OS_WIN |
vitalybuka | 5d129058 | 2014-09-12 09:19:59 | [diff] [blame] | 55 | PrintedDocument::CreateDebugDumpPath(filename, FILE_PATH_LITERAL(".pdf")); |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 56 | #endif // OS_WIN |
vitalybuka | 5d129058 | 2014-09-12 09:19:59 | [diff] [blame] | 57 | base::File file(file_path, |
| 58 | base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE); |
| 59 | page->metafile()->SaveTo(&file); |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void DebugDumpDataTask(const base::string16& doc_name, |
| 63 | const base::FilePath::StringType& extension, |
| 64 | const base::RefCountedMemory* data) { |
| 65 | base::FilePath path = |
| 66 | PrintedDocument::CreateDebugDumpPath(doc_name, extension); |
| 67 | if (path.empty()) |
| 68 | return; |
| 69 | base::WriteFile(path, |
| 70 | reinterpret_cast<const char*>(data->front()), |
| 71 | base::checked_cast<int>(data->size())); |
| 72 | } |
| 73 | |
| 74 | void DebugDumpSettings(const base::string16& doc_name, |
| 75 | const PrintSettings& settings, |
| 76 | base::TaskRunner* blocking_runner) { |
| 77 | base::DictionaryValue job_settings; |
| 78 | PrintSettingsToJobSettingsDebug(settings, &job_settings); |
| 79 | std::string settings_str; |
| 80 | base::JSONWriter::WriteWithOptions( |
| 81 | &job_settings, base::JSONWriter::OPTIONS_PRETTY_PRINT, &settings_str); |
| 82 | scoped_refptr<base::RefCountedMemory> data = |
| 83 | base::RefCountedString::TakeString(&settings_str); |
| 84 | blocking_runner->PostTask( |
| 85 | FROM_HERE, |
| 86 | base::Bind( |
| 87 | &DebugDumpDataTask, doc_name, FILE_PATH_LITERAL(".json"), data)); |
| 88 | } |
| 89 | |
[email protected] | e1504d8 | 2009-07-03 15:27:15 | [diff] [blame] | 90 | } // namespace |
| 91 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 92 | PrintedDocument::PrintedDocument(const PrintSettings& settings, |
| 93 | PrintedPagesSource* source, |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 94 | int cookie, |
| 95 | base::TaskRunner* blocking_runner) |
| 96 | : mutable_(source), immutable_(settings, source, cookie, blocking_runner) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 97 | // Records the expected page count if a range is setup. |
[email protected] | e5324b5 | 2013-10-29 03:16:37 | [diff] [blame] | 98 | if (!settings.ranges().empty()) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 99 | // If there is a range, set the number of page |
[email protected] | e5324b5 | 2013-10-29 03:16:37 | [diff] [blame] | 100 | for (unsigned i = 0; i < settings.ranges().size(); ++i) { |
| 101 | const PageRange& range = settings.ranges()[i]; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 102 | mutable_.expected_page_count_ += range.to - range.from + 1; |
| 103 | } |
| 104 | } |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 105 | |
| 106 | if (!g_debug_dump_info.Get().empty()) |
| 107 | DebugDumpSettings(name(), settings, blocking_runner); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | PrintedDocument::~PrintedDocument() { |
| 111 | } |
| 112 | |
[email protected] | 0e0fca3 | 2009-07-06 15:25:50 | [diff] [blame] | 113 | void PrintedDocument::SetPage(int page_number, |
vitalybuka | 5d129058 | 2014-09-12 09:19:59 | [diff] [blame] | 114 | scoped_ptr<MetafilePlayer> metafile, |
[email protected] | 9de1347a | 2014-06-12 09:49:08 | [diff] [blame] | 115 | #if defined(OS_WIN) |
pkasting | 46a6291 | 2014-10-09 21:30:00 | [diff] [blame^] | 116 | float shrink, |
[email protected] | 9de1347a | 2014-06-12 09:49:08 | [diff] [blame] | 117 | #endif // OS_WIN |
[email protected] | 40c7cfe | 2010-07-01 07:04:05 | [diff] [blame] | 118 | const gfx::Size& paper_size, |
[email protected] | 3cce538 | 2011-09-23 23:21:21 | [diff] [blame] | 119 | const gfx::Rect& page_rect) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 120 | // Notice the page_number + 1, the reason is that this is the value that will |
| 121 | // be shown. Users dislike 0-based counting. |
| 122 | scoped_refptr<PrintedPage> page( |
vitalybuka | 5d129058 | 2014-09-12 09:19:59 | [diff] [blame] | 123 | new PrintedPage(page_number + 1, metafile.Pass(), paper_size, page_rect)); |
[email protected] | 9de1347a | 2014-06-12 09:49:08 | [diff] [blame] | 124 | #if defined(OS_WIN) |
| 125 | page->set_shrink_factor(shrink); |
| 126 | #endif // OS_WIN |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 127 | { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 128 | base::AutoLock lock(lock_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 129 | mutable_.pages_[page_number] = page; |
[email protected] | da4eefd | 2011-03-03 23:40:27 | [diff] [blame] | 130 | |
| 131 | #if defined(OS_POSIX) && !defined(OS_MACOSX) |
| 132 | if (page_number < mutable_.first_page) |
| 133 | mutable_.first_page = page_number; |
| 134 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 135 | } |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 136 | |
| 137 | if (!g_debug_dump_info.Get().empty()) { |
| 138 | immutable_.blocking_runner_->PostTask( |
| 139 | FROM_HERE, base::Bind(&DebugDumpPageTask, name(), page)); |
| 140 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 141 | } |
| 142 | |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 143 | scoped_refptr<PrintedPage> PrintedDocument::GetPage(int page_number) { |
| 144 | scoped_refptr<PrintedPage> page; |
| 145 | { |
| 146 | base::AutoLock lock(lock_); |
| 147 | PrintedPages::const_iterator itr = mutable_.pages_.find(page_number); |
| 148 | if (itr != mutable_.pages_.end()) |
| 149 | page = itr->second; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 150 | } |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 151 | return page; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 152 | } |
| 153 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 154 | bool PrintedDocument::IsComplete() const { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 155 | base::AutoLock lock(lock_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 156 | if (!mutable_.page_count_) |
| 157 | return false; |
| 158 | PageNumber page(immutable_.settings_, mutable_.page_count_); |
| 159 | if (page == PageNumber::npos()) |
| 160 | return false; |
[email protected] | 5cc4c42 | 2011-02-19 00:09:22 | [diff] [blame] | 161 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 162 | for (; page != PageNumber::npos(); ++page) { |
[email protected] | da4eefd | 2011-03-03 23:40:27 | [diff] [blame] | 163 | #if defined(OS_WIN) || defined(OS_MACOSX) |
| 164 | const bool metafile_must_be_valid = true; |
[email protected] | 5cc4c42 | 2011-02-19 00:09:22 | [diff] [blame] | 165 | #elif defined(OS_POSIX) |
[email protected] | da4eefd | 2011-03-03 23:40:27 | [diff] [blame] | 166 | const bool metafile_must_be_valid = (page.ToInt() == mutable_.first_page); |
[email protected] | 5cc4c42 | 2011-02-19 00:09:22 | [diff] [blame] | 167 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 168 | PrintedPages::const_iterator itr = mutable_.pages_.find(page.ToInt()); |
[email protected] | 5cc4c42 | 2011-02-19 00:09:22 | [diff] [blame] | 169 | if (itr == mutable_.pages_.end() || !itr->second.get()) |
| 170 | return false; |
[email protected] | 7d748990 | 2011-04-11 21:54:06 | [diff] [blame] | 171 | if (metafile_must_be_valid && !itr->second->metafile()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 172 | return false; |
| 173 | } |
| 174 | return true; |
| 175 | } |
| 176 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 177 | void PrintedDocument::DisconnectSource() { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 178 | base::AutoLock lock(lock_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 179 | mutable_.source_ = NULL; |
| 180 | } |
| 181 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 182 | void PrintedDocument::set_page_count(int max_page) { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 183 | base::AutoLock lock(lock_); |
[email protected] | e1504d8 | 2009-07-03 15:27:15 | [diff] [blame] | 184 | DCHECK_EQ(0, mutable_.page_count_); |
| 185 | mutable_.page_count_ = max_page; |
[email protected] | e5324b5 | 2013-10-29 03:16:37 | [diff] [blame] | 186 | if (immutable_.settings_.ranges().empty()) { |
[email protected] | e1504d8 | 2009-07-03 15:27:15 | [diff] [blame] | 187 | mutable_.expected_page_count_ = max_page; |
| 188 | } else { |
| 189 | // If there is a range, don't bother since expected_page_count_ is already |
| 190 | // initialized. |
| 191 | DCHECK_NE(mutable_.expected_page_count_, 0); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 192 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | int PrintedDocument::page_count() const { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 196 | base::AutoLock lock(lock_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 197 | return mutable_.page_count_; |
| 198 | } |
| 199 | |
| 200 | int PrintedDocument::expected_page_count() const { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 201 | base::AutoLock lock(lock_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 202 | return mutable_.expected_page_count_; |
| 203 | } |
| 204 | |
[email protected] | 79f6388 | 2013-02-10 05:15:45 | [diff] [blame] | 205 | void PrintedDocument::set_debug_dump_path( |
| 206 | const base::FilePath& debug_dump_path) { |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 207 | g_debug_dump_info.Get() = debug_dump_path; |
[email protected] | e1504d8 | 2009-07-03 15:27:15 | [diff] [blame] | 208 | } |
| 209 | |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 210 | base::FilePath PrintedDocument::CreateDebugDumpPath( |
| 211 | const base::string16& document_name, |
| 212 | const base::FilePath::StringType& extension) { |
| 213 | if (g_debug_dump_info.Get().empty()) |
| 214 | return base::FilePath(); |
| 215 | // Create a filename. |
| 216 | base::string16 filename; |
| 217 | base::Time now(base::Time::Now()); |
| 218 | filename = base::TimeFormatShortDateAndTime(now); |
| 219 | filename += base::ASCIIToUTF16("_"); |
| 220 | filename += document_name; |
| 221 | base::FilePath::StringType system_filename; |
| 222 | #if defined(OS_WIN) |
| 223 | system_filename = filename; |
| 224 | #else // OS_WIN |
| 225 | system_filename = base::UTF16ToUTF8(filename); |
| 226 | #endif // OS_WIN |
[email protected] | 6bc03de | 2014-08-07 23:59:15 | [diff] [blame] | 227 | base::i18n::ReplaceIllegalCharactersInPath(&system_filename, '_'); |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 228 | return g_debug_dump_info.Get().Append(system_filename).AddExtension( |
| 229 | extension); |
| 230 | } |
| 231 | |
| 232 | void PrintedDocument::DebugDumpData( |
| 233 | const base::RefCountedMemory* data, |
| 234 | const base::FilePath::StringType& extension) { |
| 235 | if (g_debug_dump_info.Get().empty()) |
| 236 | return; |
| 237 | immutable_.blocking_runner_->PostTask( |
| 238 | FROM_HERE, base::Bind(&DebugDumpDataTask, name(), extension, data)); |
[email protected] | e1504d8 | 2009-07-03 15:27:15 | [diff] [blame] | 239 | } |
| 240 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 241 | PrintedDocument::Mutable::Mutable(PrintedPagesSource* source) |
| 242 | : source_(source), |
| 243 | expected_page_count_(0), |
[email protected] | 732b813 | 2012-01-10 23:17:32 | [diff] [blame] | 244 | page_count_(0) { |
[email protected] | da4eefd | 2011-03-03 23:40:27 | [diff] [blame] | 245 | #if defined(OS_POSIX) && !defined(OS_MACOSX) |
| 246 | first_page = INT_MAX; |
| 247 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 248 | } |
| 249 | |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 250 | PrintedDocument::Mutable::~Mutable() { |
| 251 | } |
| 252 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 253 | PrintedDocument::Immutable::Immutable(const PrintSettings& settings, |
| 254 | PrintedPagesSource* source, |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 255 | int cookie, |
| 256 | base::TaskRunner* blocking_runner) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 257 | : settings_(settings), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 258 | name_(source->RenderSourceName()), |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 259 | cookie_(cookie), |
| 260 | blocking_runner_(blocking_runner) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 261 | } |
| 262 | |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 263 | PrintedDocument::Immutable::~Immutable() { |
| 264 | } |
| 265 | |
[email protected] | d53002f4 | 2014-01-14 16:08:56 | [diff] [blame] | 266 | #if defined(OS_CHROMEOS) || defined(OS_ANDROID) |
[email protected] | b25f003 | 2013-08-19 22:26:25 | [diff] [blame] | 267 | // This function is not used on aura linux/chromeos or android. |
[email protected] | 82b72cb8 | 2011-12-06 00:23:44 | [diff] [blame] | 268 | void PrintedDocument::RenderPrintedPage(const PrintedPage& page, |
| 269 | PrintingContext* context) const { |
| 270 | } |
| 271 | #endif |
| 272 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 273 | } // namespace printing |