[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] | e1504d8 | 2009-07-03 15:27:15 | [diff] [blame] | 12 | #include "base/file_util.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 13 | #include "base/files/file_path.h" |
[email protected] | d0767cb54 | 2009-10-08 17:38:30 | [diff] [blame] | 14 | #include "base/i18n/file_util_icu.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 15 | #include "base/i18n/time_formatting.h" |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 16 | #include "base/lazy_instance.h" |
[email protected] | a7629597 | 2013-07-18 00:42:32 | [diff] [blame] | 17 | #include "base/message_loop/message_loop.h" |
[email protected] | 896d161f | 2013-06-11 22:52:24 | [diff] [blame] | 18 | #include "base/strings/string_util.h" |
| 19 | #include "base/strings/stringprintf.h" |
[email protected] | 90626587 | 2013-06-07 22:40:45 | [diff] [blame] | 20 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 21 | #include "printing/page_number.h" |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 22 | #include "printing/printed_page.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 23 | #include "printing/printed_pages_source.h" |
[email protected] | 4ae30d08 | 2009-02-20 17:55:55 | [diff] [blame] | 24 | #include "printing/units.h" |
[email protected] | c399a8a | 2008-11-22 19:38:00 | [diff] [blame] | 25 | #include "skia/ext/platform_device.h" |
[email protected] | 9dd7e3d7 | 2011-01-20 18:27:06 | [diff] [blame] | 26 | #include "ui/base/text/text_elider.h" |
[email protected] | 08397d5 | 2011-02-05 01:53:38 | [diff] [blame] | 27 | #include "ui/gfx/font.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 28 | |
[email protected] | e1504d8 | 2009-07-03 15:27:15 | [diff] [blame] | 29 | namespace { |
| 30 | |
| 31 | struct PrintDebugDumpPath { |
| 32 | PrintDebugDumpPath() |
| 33 | : enabled(false) { |
| 34 | } |
| 35 | |
| 36 | bool enabled; |
[email protected] | 79f6388 | 2013-02-10 05:15:45 | [diff] [blame] | 37 | base::FilePath debug_dump_path; |
[email protected] | e1504d8 | 2009-07-03 15:27:15 | [diff] [blame] | 38 | }; |
| 39 | |
[email protected] | da9ccfb | 2012-01-28 00:34:40 | [diff] [blame] | 40 | base::LazyInstance<PrintDebugDumpPath> 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 | |
| 43 | } // namespace |
| 44 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 45 | namespace printing { |
| 46 | |
| 47 | PrintedDocument::PrintedDocument(const PrintSettings& settings, |
| 48 | PrintedPagesSource* source, |
| 49 | int cookie) |
| 50 | : mutable_(source), |
| 51 | immutable_(settings, source, cookie) { |
| 52 | |
| 53 | // Records the expected page count if a range is setup. |
| 54 | if (!settings.ranges.empty()) { |
| 55 | // If there is a range, set the number of page |
| 56 | for (unsigned i = 0; i < settings.ranges.size(); ++i) { |
| 57 | const PageRange& range = settings.ranges[i]; |
| 58 | mutable_.expected_page_count_ += range.to - range.from + 1; |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | PrintedDocument::~PrintedDocument() { |
| 64 | } |
| 65 | |
[email protected] | 0e0fca3 | 2009-07-06 15:25:50 | [diff] [blame] | 66 | void PrintedDocument::SetPage(int page_number, |
[email protected] | 7d748990 | 2011-04-11 21:54:06 | [diff] [blame] | 67 | Metafile* metafile, |
[email protected] | 40c7cfe | 2010-07-01 07:04:05 | [diff] [blame] | 68 | double shrink, |
| 69 | const gfx::Size& paper_size, |
[email protected] | 3cce538 | 2011-09-23 23:21:21 | [diff] [blame] | 70 | const gfx::Rect& page_rect) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 71 | // Notice the page_number + 1, the reason is that this is the value that will |
| 72 | // be shown. Users dislike 0-based counting. |
| 73 | scoped_refptr<PrintedPage> page( |
| 74 | new PrintedPage(page_number + 1, |
[email protected] | 40c7cfe | 2010-07-01 07:04:05 | [diff] [blame] | 75 | metafile, |
| 76 | paper_size, |
[email protected] | 732b813 | 2012-01-10 23:17:32 | [diff] [blame] | 77 | page_rect, |
| 78 | shrink)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 79 | { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 80 | base::AutoLock lock(lock_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 81 | mutable_.pages_[page_number] = page; |
[email protected] | da4eefd | 2011-03-03 23:40:27 | [diff] [blame] | 82 | |
| 83 | #if defined(OS_POSIX) && !defined(OS_MACOSX) |
| 84 | if (page_number < mutable_.first_page) |
| 85 | mutable_.first_page = page_number; |
| 86 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 87 | } |
[email protected] | 7131880 | 2013-06-03 00:20:05 | [diff] [blame] | 88 | DebugDump(*page.get()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | bool PrintedDocument::GetPage(int page_number, |
| 92 | scoped_refptr<PrintedPage>* page) { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 93 | base::AutoLock lock(lock_); |
[email protected] | 8227045 | 2009-06-19 15:58:01 | [diff] [blame] | 94 | PrintedPages::const_iterator itr = mutable_.pages_.find(page_number); |
| 95 | if (itr != mutable_.pages_.end()) { |
| 96 | if (itr->second.get()) { |
| 97 | *page = itr->second; |
| 98 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 99 | } |
| 100 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 101 | return false; |
| 102 | } |
| 103 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 104 | bool PrintedDocument::IsComplete() const { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 105 | base::AutoLock lock(lock_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 106 | if (!mutable_.page_count_) |
| 107 | return false; |
| 108 | PageNumber page(immutable_.settings_, mutable_.page_count_); |
| 109 | if (page == PageNumber::npos()) |
| 110 | return false; |
[email protected] | 5cc4c42 | 2011-02-19 00:09:22 | [diff] [blame] | 111 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 112 | for (; page != PageNumber::npos(); ++page) { |
[email protected] | da4eefd | 2011-03-03 23:40:27 | [diff] [blame] | 113 | #if defined(OS_WIN) || defined(OS_MACOSX) |
| 114 | const bool metafile_must_be_valid = true; |
[email protected] | 5cc4c42 | 2011-02-19 00:09:22 | [diff] [blame] | 115 | #elif defined(OS_POSIX) |
[email protected] | da4eefd | 2011-03-03 23:40:27 | [diff] [blame] | 116 | const bool metafile_must_be_valid = (page.ToInt() == mutable_.first_page); |
[email protected] | 5cc4c42 | 2011-02-19 00:09:22 | [diff] [blame] | 117 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 118 | PrintedPages::const_iterator itr = mutable_.pages_.find(page.ToInt()); |
[email protected] | 5cc4c42 | 2011-02-19 00:09:22 | [diff] [blame] | 119 | if (itr == mutable_.pages_.end() || !itr->second.get()) |
| 120 | return false; |
[email protected] | 7d748990 | 2011-04-11 21:54:06 | [diff] [blame] | 121 | if (metafile_must_be_valid && !itr->second->metafile()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 122 | return false; |
| 123 | } |
| 124 | return true; |
| 125 | } |
| 126 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 127 | void PrintedDocument::DisconnectSource() { |
[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_.source_ = NULL; |
| 130 | } |
| 131 | |
[email protected] | b5ab398 | 2010-02-16 23:58:27 | [diff] [blame] | 132 | uint32 PrintedDocument::MemoryUsage() const { |
[email protected] | 60741b4 | 2009-09-14 13:57:10 | [diff] [blame] | 133 | std::vector< scoped_refptr<PrintedPage> > pages_copy; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 134 | { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 135 | base::AutoLock lock(lock_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 136 | pages_copy.reserve(mutable_.pages_.size()); |
| 137 | PrintedPages::const_iterator end = mutable_.pages_.end(); |
| 138 | for (PrintedPages::const_iterator itr = mutable_.pages_.begin(); |
| 139 | itr != end; ++itr) { |
| 140 | if (itr->second.get()) { |
| 141 | pages_copy.push_back(itr->second); |
| 142 | } |
| 143 | } |
| 144 | } |
[email protected] | b5ab398 | 2010-02-16 23:58:27 | [diff] [blame] | 145 | uint32 total = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 146 | for (size_t i = 0; i < pages_copy.size(); ++i) { |
[email protected] | 7d748990 | 2011-04-11 21:54:06 | [diff] [blame] | 147 | total += pages_copy[i]->metafile()->GetDataSize(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 148 | } |
| 149 | return total; |
| 150 | } |
| 151 | |
| 152 | void PrintedDocument::set_page_count(int max_page) { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 153 | base::AutoLock lock(lock_); |
[email protected] | e1504d8 | 2009-07-03 15:27:15 | [diff] [blame] | 154 | DCHECK_EQ(0, mutable_.page_count_); |
| 155 | mutable_.page_count_ = max_page; |
| 156 | if (immutable_.settings_.ranges.empty()) { |
| 157 | mutable_.expected_page_count_ = max_page; |
| 158 | } else { |
| 159 | // If there is a range, don't bother since expected_page_count_ is already |
| 160 | // initialized. |
| 161 | DCHECK_NE(mutable_.expected_page_count_, 0); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 162 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | int PrintedDocument::page_count() const { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 166 | base::AutoLock lock(lock_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 167 | return mutable_.page_count_; |
| 168 | } |
| 169 | |
| 170 | int PrintedDocument::expected_page_count() const { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 171 | base::AutoLock lock(lock_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 172 | return mutable_.expected_page_count_; |
| 173 | } |
| 174 | |
[email protected] | be3b19a | 2009-07-13 14:58:18 | [diff] [blame] | 175 | void PrintedDocument::DebugDump(const PrintedPage& page) { |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 176 | if (!g_debug_dump_info.Get().enabled) |
[email protected] | e1504d8 | 2009-07-03 15:27:15 | [diff] [blame] | 177 | return; |
| 178 | |
[email protected] | d9d4299 | 2010-09-13 19:39:19 | [diff] [blame] | 179 | string16 filename; |
[email protected] | e1504d8 | 2009-07-03 15:27:15 | [diff] [blame] | 180 | filename += name(); |
[email protected] | d9d4299 | 2010-09-13 19:39:19 | [diff] [blame] | 181 | filename += ASCIIToUTF16("_"); |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 182 | filename += ASCIIToUTF16(base::StringPrintf("%02d", page.page_number())); |
[email protected] | de294335 | 2009-10-22 23:06:12 | [diff] [blame] | 183 | #if defined(OS_WIN) |
[email protected] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 184 | filename += ASCIIToUTF16("_.emf"); |
[email protected] | 7d748990 | 2011-04-11 21:54:06 | [diff] [blame] | 185 | page.metafile()->SaveTo( |
[email protected] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 186 | g_debug_dump_info.Get().debug_dump_path.Append(filename)); |
[email protected] | 60741b4 | 2009-09-14 13:57:10 | [diff] [blame] | 187 | #else // OS_WIN |
[email protected] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 188 | filename += ASCIIToUTF16("_.pdf"); |
[email protected] | 7d748990 | 2011-04-11 21:54:06 | [diff] [blame] | 189 | page.metafile()->SaveTo( |
[email protected] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 190 | g_debug_dump_info.Get().debug_dump_path.Append(UTF16ToUTF8(filename))); |
[email protected] | 60741b4 | 2009-09-14 13:57:10 | [diff] [blame] | 191 | #endif // OS_WIN |
[email protected] | e1504d8 | 2009-07-03 15:27:15 | [diff] [blame] | 192 | } |
| 193 | |
[email protected] | 79f6388 | 2013-02-10 05:15:45 | [diff] [blame] | 194 | void PrintedDocument::set_debug_dump_path( |
| 195 | const base::FilePath& debug_dump_path) { |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 196 | g_debug_dump_info.Get().enabled = !debug_dump_path.empty(); |
| 197 | g_debug_dump_info.Get().debug_dump_path = debug_dump_path; |
[email protected] | e1504d8 | 2009-07-03 15:27:15 | [diff] [blame] | 198 | } |
| 199 | |
[email protected] | 79f6388 | 2013-02-10 05:15:45 | [diff] [blame] | 200 | const base::FilePath& PrintedDocument::debug_dump_path() { |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 201 | return g_debug_dump_info.Get().debug_dump_path; |
[email protected] | e1504d8 | 2009-07-03 15:27:15 | [diff] [blame] | 202 | } |
| 203 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 204 | PrintedDocument::Mutable::Mutable(PrintedPagesSource* source) |
| 205 | : source_(source), |
| 206 | expected_page_count_(0), |
[email protected] | 732b813 | 2012-01-10 23:17:32 | [diff] [blame] | 207 | page_count_(0) { |
[email protected] | da4eefd | 2011-03-03 23:40:27 | [diff] [blame] | 208 | #if defined(OS_POSIX) && !defined(OS_MACOSX) |
| 209 | first_page = INT_MAX; |
| 210 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 211 | } |
| 212 | |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 213 | PrintedDocument::Mutable::~Mutable() { |
| 214 | } |
| 215 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 216 | PrintedDocument::Immutable::Immutable(const PrintSettings& settings, |
| 217 | PrintedPagesSource* source, |
| 218 | int cookie) |
| 219 | : settings_(settings), |
[email protected] | f58ef2b | 2013-05-06 22:43:57 | [diff] [blame] | 220 | source_message_loop_(base::MessageLoop::current()), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 221 | name_(source->RenderSourceName()), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 222 | cookie_(cookie) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 223 | } |
| 224 | |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 225 | PrintedDocument::Immutable::~Immutable() { |
| 226 | } |
| 227 | |
[email protected] | b25f003 | 2013-08-19 22:26:25 | [diff] [blame^] | 228 | #if (defined(OS_POSIX) && defined(USE_AURA)) || defined(OS_ANDROID) |
| 229 | // This function is not used on aura linux/chromeos or android. |
[email protected] | 82b72cb8 | 2011-12-06 00:23:44 | [diff] [blame] | 230 | void PrintedDocument::RenderPrintedPage(const PrintedPage& page, |
| 231 | PrintingContext* context) const { |
| 232 | } |
| 233 | #endif |
| 234 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 235 | } // namespace printing |