blob: 59cc38569231936926121f0a247be53fa71c37b0 [file] [log] [blame]
[email protected]5cc4c422011-02-19 00:09:221// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]8ff1d422009-07-07 21:31:395#include "printing/printed_document.h"
initial.commit09911bf2008-07-26 23:55:296
[email protected]7d2d0d0e2010-03-06 22:16:107#include <algorithm>
initial.commit09911bf2008-07-26 23:55:298#include <set>
[email protected]7d2d0d0e2010-03-06 22:16:109#include <string>
10#include <vector>
initial.commit09911bf2008-07-26 23:55:2911
[email protected]e1504d82009-07-03 15:27:1512#include "base/file_util.h"
[email protected]57999812013-02-24 05:40:5213#include "base/files/file_path.h"
[email protected]d0767cb542009-10-08 17:38:3014#include "base/i18n/file_util_icu.h"
[email protected]57999812013-02-24 05:40:5215#include "base/i18n/time_formatting.h"
[email protected]625332e02010-12-14 07:48:4916#include "base/lazy_instance.h"
[email protected]a76295972013-07-18 00:42:3217#include "base/message_loop/message_loop.h"
[email protected]896d161f2013-06-11 22:52:2418#include "base/strings/string_util.h"
19#include "base/strings/stringprintf.h"
[email protected]906265872013-06-07 22:40:4520#include "base/strings/utf_string_conversions.h"
[email protected]8ff1d422009-07-07 21:31:3921#include "printing/page_number.h"
[email protected]8ff1d422009-07-07 21:31:3922#include "printing/printed_page.h"
[email protected]57999812013-02-24 05:40:5223#include "printing/printed_pages_source.h"
[email protected]4ae30d082009-02-20 17:55:5524#include "printing/units.h"
[email protected]c399a8a2008-11-22 19:38:0025#include "skia/ext/platform_device.h"
[email protected]9dd7e3d72011-01-20 18:27:0626#include "ui/base/text/text_elider.h"
[email protected]08397d52011-02-05 01:53:3827#include "ui/gfx/font.h"
initial.commit09911bf2008-07-26 23:55:2928
[email protected]e1504d82009-07-03 15:27:1529namespace {
30
31struct PrintDebugDumpPath {
32 PrintDebugDumpPath()
33 : enabled(false) {
34 }
35
36 bool enabled;
[email protected]79f63882013-02-10 05:15:4537 base::FilePath debug_dump_path;
[email protected]e1504d82009-07-03 15:27:1538};
39
[email protected]da9ccfb2012-01-28 00:34:4040base::LazyInstance<PrintDebugDumpPath> g_debug_dump_info =
[email protected]6de0fd1d2011-11-15 13:31:4941 LAZY_INSTANCE_INITIALIZER;
[email protected]e1504d82009-07-03 15:27:1542
43} // namespace
44
initial.commit09911bf2008-07-26 23:55:2945namespace printing {
46
47PrintedDocument::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
63PrintedDocument::~PrintedDocument() {
64}
65
[email protected]0e0fca32009-07-06 15:25:5066void PrintedDocument::SetPage(int page_number,
[email protected]7d7489902011-04-11 21:54:0667 Metafile* metafile,
[email protected]40c7cfe2010-07-01 07:04:0568 double shrink,
69 const gfx::Size& paper_size,
[email protected]3cce5382011-09-23 23:21:2170 const gfx::Rect& page_rect) {
initial.commit09911bf2008-07-26 23:55:2971 // 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]40c7cfe2010-07-01 07:04:0575 metafile,
76 paper_size,
[email protected]732b8132012-01-10 23:17:3277 page_rect,
78 shrink));
initial.commit09911bf2008-07-26 23:55:2979 {
[email protected]20305ec2011-01-21 04:55:5280 base::AutoLock lock(lock_);
initial.commit09911bf2008-07-26 23:55:2981 mutable_.pages_[page_number] = page;
[email protected]da4eefd2011-03-03 23:40:2782
83#if defined(OS_POSIX) && !defined(OS_MACOSX)
84 if (page_number < mutable_.first_page)
85 mutable_.first_page = page_number;
86#endif
initial.commit09911bf2008-07-26 23:55:2987 }
[email protected]71318802013-06-03 00:20:0588 DebugDump(*page.get());
initial.commit09911bf2008-07-26 23:55:2989}
90
91bool PrintedDocument::GetPage(int page_number,
92 scoped_refptr<PrintedPage>* page) {
[email protected]20305ec2011-01-21 04:55:5293 base::AutoLock lock(lock_);
[email protected]82270452009-06-19 15:58:0194 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.commit09911bf2008-07-26 23:55:2999 }
100 }
initial.commit09911bf2008-07-26 23:55:29101 return false;
102}
103
initial.commit09911bf2008-07-26 23:55:29104bool PrintedDocument::IsComplete() const {
[email protected]20305ec2011-01-21 04:55:52105 base::AutoLock lock(lock_);
initial.commit09911bf2008-07-26 23:55:29106 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]5cc4c422011-02-19 00:09:22111
initial.commit09911bf2008-07-26 23:55:29112 for (; page != PageNumber::npos(); ++page) {
[email protected]da4eefd2011-03-03 23:40:27113#if defined(OS_WIN) || defined(OS_MACOSX)
114 const bool metafile_must_be_valid = true;
[email protected]5cc4c422011-02-19 00:09:22115#elif defined(OS_POSIX)
[email protected]da4eefd2011-03-03 23:40:27116 const bool metafile_must_be_valid = (page.ToInt() == mutable_.first_page);
[email protected]5cc4c422011-02-19 00:09:22117#endif
initial.commit09911bf2008-07-26 23:55:29118 PrintedPages::const_iterator itr = mutable_.pages_.find(page.ToInt());
[email protected]5cc4c422011-02-19 00:09:22119 if (itr == mutable_.pages_.end() || !itr->second.get())
120 return false;
[email protected]7d7489902011-04-11 21:54:06121 if (metafile_must_be_valid && !itr->second->metafile())
initial.commit09911bf2008-07-26 23:55:29122 return false;
123 }
124 return true;
125}
126
initial.commit09911bf2008-07-26 23:55:29127void PrintedDocument::DisconnectSource() {
[email protected]20305ec2011-01-21 04:55:52128 base::AutoLock lock(lock_);
initial.commit09911bf2008-07-26 23:55:29129 mutable_.source_ = NULL;
130}
131
[email protected]b5ab3982010-02-16 23:58:27132uint32 PrintedDocument::MemoryUsage() const {
[email protected]60741b42009-09-14 13:57:10133 std::vector< scoped_refptr<PrintedPage> > pages_copy;
initial.commit09911bf2008-07-26 23:55:29134 {
[email protected]20305ec2011-01-21 04:55:52135 base::AutoLock lock(lock_);
initial.commit09911bf2008-07-26 23:55:29136 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]b5ab3982010-02-16 23:58:27145 uint32 total = 0;
initial.commit09911bf2008-07-26 23:55:29146 for (size_t i = 0; i < pages_copy.size(); ++i) {
[email protected]7d7489902011-04-11 21:54:06147 total += pages_copy[i]->metafile()->GetDataSize();
initial.commit09911bf2008-07-26 23:55:29148 }
149 return total;
150}
151
152void PrintedDocument::set_page_count(int max_page) {
[email protected]20305ec2011-01-21 04:55:52153 base::AutoLock lock(lock_);
[email protected]e1504d82009-07-03 15:27:15154 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.commit09911bf2008-07-26 23:55:29162 }
initial.commit09911bf2008-07-26 23:55:29163}
164
165int PrintedDocument::page_count() const {
[email protected]20305ec2011-01-21 04:55:52166 base::AutoLock lock(lock_);
initial.commit09911bf2008-07-26 23:55:29167 return mutable_.page_count_;
168}
169
170int PrintedDocument::expected_page_count() const {
[email protected]20305ec2011-01-21 04:55:52171 base::AutoLock lock(lock_);
initial.commit09911bf2008-07-26 23:55:29172 return mutable_.expected_page_count_;
173}
174
[email protected]be3b19a2009-07-13 14:58:18175void PrintedDocument::DebugDump(const PrintedPage& page) {
[email protected]625332e02010-12-14 07:48:49176 if (!g_debug_dump_info.Get().enabled)
[email protected]e1504d82009-07-03 15:27:15177 return;
178
[email protected]d9d42992010-09-13 19:39:19179 string16 filename;
[email protected]e1504d82009-07-03 15:27:15180 filename += name();
[email protected]d9d42992010-09-13 19:39:19181 filename += ASCIIToUTF16("_");
[email protected]7d3cbc92013-03-18 22:33:04182 filename += ASCIIToUTF16(base::StringPrintf("%02d", page.page_number()));
[email protected]de2943352009-10-22 23:06:12183#if defined(OS_WIN)
[email protected]8f17cd3e2011-03-16 01:39:42184 filename += ASCIIToUTF16("_.emf");
[email protected]7d7489902011-04-11 21:54:06185 page.metafile()->SaveTo(
[email protected]8f17cd3e2011-03-16 01:39:42186 g_debug_dump_info.Get().debug_dump_path.Append(filename));
[email protected]60741b42009-09-14 13:57:10187#else // OS_WIN
[email protected]8f17cd3e2011-03-16 01:39:42188 filename += ASCIIToUTF16("_.pdf");
[email protected]7d7489902011-04-11 21:54:06189 page.metafile()->SaveTo(
[email protected]8f17cd3e2011-03-16 01:39:42190 g_debug_dump_info.Get().debug_dump_path.Append(UTF16ToUTF8(filename)));
[email protected]60741b42009-09-14 13:57:10191#endif // OS_WIN
[email protected]e1504d82009-07-03 15:27:15192}
193
[email protected]79f63882013-02-10 05:15:45194void PrintedDocument::set_debug_dump_path(
195 const base::FilePath& debug_dump_path) {
[email protected]625332e02010-12-14 07:48:49196 g_debug_dump_info.Get().enabled = !debug_dump_path.empty();
197 g_debug_dump_info.Get().debug_dump_path = debug_dump_path;
[email protected]e1504d82009-07-03 15:27:15198}
199
[email protected]79f63882013-02-10 05:15:45200const base::FilePath& PrintedDocument::debug_dump_path() {
[email protected]625332e02010-12-14 07:48:49201 return g_debug_dump_info.Get().debug_dump_path;
[email protected]e1504d82009-07-03 15:27:15202}
203
initial.commit09911bf2008-07-26 23:55:29204PrintedDocument::Mutable::Mutable(PrintedPagesSource* source)
205 : source_(source),
206 expected_page_count_(0),
[email protected]732b8132012-01-10 23:17:32207 page_count_(0) {
[email protected]da4eefd2011-03-03 23:40:27208#if defined(OS_POSIX) && !defined(OS_MACOSX)
209 first_page = INT_MAX;
210#endif
initial.commit09911bf2008-07-26 23:55:29211}
212
[email protected]20f0487a2010-09-30 20:06:30213PrintedDocument::Mutable::~Mutable() {
214}
215
initial.commit09911bf2008-07-26 23:55:29216PrintedDocument::Immutable::Immutable(const PrintSettings& settings,
217 PrintedPagesSource* source,
218 int cookie)
219 : settings_(settings),
[email protected]f58ef2b2013-05-06 22:43:57220 source_message_loop_(base::MessageLoop::current()),
initial.commit09911bf2008-07-26 23:55:29221 name_(source->RenderSourceName()),
initial.commit09911bf2008-07-26 23:55:29222 cookie_(cookie) {
initial.commit09911bf2008-07-26 23:55:29223}
224
[email protected]20f0487a2010-09-30 20:06:30225PrintedDocument::Immutable::~Immutable() {
226}
227
[email protected]b25f0032013-08-19 22:26:25228#if (defined(OS_POSIX) && defined(USE_AURA)) || defined(OS_ANDROID)
229// This function is not used on aura linux/chromeos or android.
[email protected]82b72cb82011-12-06 00:23:44230void PrintedDocument::RenderPrintedPage(const PrintedPage& page,
231 PrintingContext* context) const {
232}
233#endif
234
initial.commit09911bf2008-07-26 23:55:29235} // namespace printing