blob: 8265e75fcc4ee59efb630ab6ef5adfa16bb48bc1 [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]c95198b2014-06-12 16:56:5512#include "base/bind.h"
[email protected]e1504d82009-07-03 15:27:1513#include "base/file_util.h"
[email protected]57999812013-02-24 05:40:5214#include "base/files/file_path.h"
[email protected]d0767cb542009-10-08 17:38:3015#include "base/i18n/file_util_icu.h"
[email protected]57999812013-02-24 05:40:5216#include "base/i18n/time_formatting.h"
[email protected]c95198b2014-06-12 16:56:5517#include "base/json/json_writer.h"
[email protected]625332e02010-12-14 07:48:4918#include "base/lazy_instance.h"
[email protected]c95198b2014-06-12 16:56:5519#include "base/memory/ref_counted_memory.h"
[email protected]a76295972013-07-18 00:42:3220#include "base/message_loop/message_loop.h"
[email protected]c95198b2014-06-12 16:56:5521#include "base/numerics/safe_conversions.h"
[email protected]896d161f2013-06-11 22:52:2422#include "base/strings/string_util.h"
23#include "base/strings/stringprintf.h"
[email protected]906265872013-06-07 22:40:4524#include "base/strings/utf_string_conversions.h"
[email protected]c95198b2014-06-12 16:56:5525#include "base/time/time.h"
26#include "base/values.h"
[email protected]8ff1d422009-07-07 21:31:3927#include "printing/page_number.h"
[email protected]c95198b2014-06-12 16:56:5528#include "printing/print_settings_conversion.h"
[email protected]8ff1d422009-07-07 21:31:3929#include "printing/printed_page.h"
[email protected]57999812013-02-24 05:40:5230#include "printing/printed_pages_source.h"
[email protected]4ae30d082009-02-20 17:55:5531#include "printing/units.h"
[email protected]c399a8a2008-11-22 19:38:0032#include "skia/ext/platform_device.h"
[email protected]08397d52011-02-05 01:53:3833#include "ui/gfx/font.h"
[email protected]dbb97ba2013-09-09 22:15:2534#include "ui/gfx/text_elider.h"
initial.commit09911bf2008-07-26 23:55:2935
[email protected]c95198b2014-06-12 16:56:5536namespace printing {
37
[email protected]e1504d82009-07-03 15:27:1538namespace {
39
[email protected]c95198b2014-06-12 16:56:5540base::LazyInstance<base::FilePath> g_debug_dump_info =
[email protected]6de0fd1d2011-11-15 13:31:4941 LAZY_INSTANCE_INITIALIZER;
[email protected]e1504d82009-07-03 15:27:1542
[email protected]c95198b2014-06-12 16:56:5543void 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
60void 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
72void 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]e1504d82009-07-03 15:27:1588} // namespace
89
initial.commit09911bf2008-07-26 23:55:2990PrintedDocument::PrintedDocument(const PrintSettings& settings,
91 PrintedPagesSource* source,
[email protected]c95198b2014-06-12 16:56:5592 int cookie,
93 base::TaskRunner* blocking_runner)
94 : mutable_(source), immutable_(settings, source, cookie, blocking_runner) {
initial.commit09911bf2008-07-26 23:55:2995 // Records the expected page count if a range is setup.
[email protected]e5324b52013-10-29 03:16:3796 if (!settings.ranges().empty()) {
initial.commit09911bf2008-07-26 23:55:2997 // If there is a range, set the number of page
[email protected]e5324b52013-10-29 03:16:3798 for (unsigned i = 0; i < settings.ranges().size(); ++i) {
99 const PageRange& range = settings.ranges()[i];
initial.commit09911bf2008-07-26 23:55:29100 mutable_.expected_page_count_ += range.to - range.from + 1;
101 }
102 }
[email protected]c95198b2014-06-12 16:56:55103
104 if (!g_debug_dump_info.Get().empty())
105 DebugDumpSettings(name(), settings, blocking_runner);
initial.commit09911bf2008-07-26 23:55:29106}
107
108PrintedDocument::~PrintedDocument() {
109}
110
[email protected]0e0fca32009-07-06 15:25:50111void PrintedDocument::SetPage(int page_number,
[email protected]7d7489902011-04-11 21:54:06112 Metafile* metafile,
[email protected]9de1347a2014-06-12 09:49:08113#if defined(OS_WIN)
[email protected]40c7cfe2010-07-01 07:04:05114 double shrink,
[email protected]9de1347a2014-06-12 09:49:08115#endif // OS_WIN
[email protected]40c7cfe2010-07-01 07:04:05116 const gfx::Size& paper_size,
[email protected]3cce5382011-09-23 23:21:21117 const gfx::Rect& page_rect) {
initial.commit09911bf2008-07-26 23:55:29118 // 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]9de1347a2014-06-12 09:49:08121 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.commit09911bf2008-07-26 23:55:29125 {
[email protected]20305ec2011-01-21 04:55:52126 base::AutoLock lock(lock_);
initial.commit09911bf2008-07-26 23:55:29127 mutable_.pages_[page_number] = page;
[email protected]da4eefd2011-03-03 23:40:27128
129#if defined(OS_POSIX) && !defined(OS_MACOSX)
130 if (page_number < mutable_.first_page)
131 mutable_.first_page = page_number;
132#endif
initial.commit09911bf2008-07-26 23:55:29133 }
[email protected]c95198b2014-06-12 16:56:55134
135 if (!g_debug_dump_info.Get().empty()) {
136 immutable_.blocking_runner_->PostTask(
137 FROM_HERE, base::Bind(&DebugDumpPageTask, name(), page));
138 }
initial.commit09911bf2008-07-26 23:55:29139}
140
[email protected]c95198b2014-06-12 16:56:55141scoped_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.commit09911bf2008-07-26 23:55:29148 }
[email protected]c95198b2014-06-12 16:56:55149 return page;
initial.commit09911bf2008-07-26 23:55:29150}
151
initial.commit09911bf2008-07-26 23:55:29152bool PrintedDocument::IsComplete() const {
[email protected]20305ec2011-01-21 04:55:52153 base::AutoLock lock(lock_);
initial.commit09911bf2008-07-26 23:55:29154 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]5cc4c422011-02-19 00:09:22159
initial.commit09911bf2008-07-26 23:55:29160 for (; page != PageNumber::npos(); ++page) {
[email protected]da4eefd2011-03-03 23:40:27161#if defined(OS_WIN) || defined(OS_MACOSX)
162 const bool metafile_must_be_valid = true;
[email protected]5cc4c422011-02-19 00:09:22163#elif defined(OS_POSIX)
[email protected]da4eefd2011-03-03 23:40:27164 const bool metafile_must_be_valid = (page.ToInt() == mutable_.first_page);
[email protected]5cc4c422011-02-19 00:09:22165#endif
initial.commit09911bf2008-07-26 23:55:29166 PrintedPages::const_iterator itr = mutable_.pages_.find(page.ToInt());
[email protected]5cc4c422011-02-19 00:09:22167 if (itr == mutable_.pages_.end() || !itr->second.get())
168 return false;
[email protected]7d7489902011-04-11 21:54:06169 if (metafile_must_be_valid && !itr->second->metafile())
initial.commit09911bf2008-07-26 23:55:29170 return false;
171 }
172 return true;
173}
174
initial.commit09911bf2008-07-26 23:55:29175void PrintedDocument::DisconnectSource() {
[email protected]20305ec2011-01-21 04:55:52176 base::AutoLock lock(lock_);
initial.commit09911bf2008-07-26 23:55:29177 mutable_.source_ = NULL;
178}
179
[email protected]b5ab3982010-02-16 23:58:27180uint32 PrintedDocument::MemoryUsage() const {
[email protected]60741b42009-09-14 13:57:10181 std::vector< scoped_refptr<PrintedPage> > pages_copy;
initial.commit09911bf2008-07-26 23:55:29182 {
[email protected]20305ec2011-01-21 04:55:52183 base::AutoLock lock(lock_);
initial.commit09911bf2008-07-26 23:55:29184 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]b5ab3982010-02-16 23:58:27193 uint32 total = 0;
initial.commit09911bf2008-07-26 23:55:29194 for (size_t i = 0; i < pages_copy.size(); ++i) {
[email protected]7d7489902011-04-11 21:54:06195 total += pages_copy[i]->metafile()->GetDataSize();
initial.commit09911bf2008-07-26 23:55:29196 }
197 return total;
198}
199
200void PrintedDocument::set_page_count(int max_page) {
[email protected]20305ec2011-01-21 04:55:52201 base::AutoLock lock(lock_);
[email protected]e1504d82009-07-03 15:27:15202 DCHECK_EQ(0, mutable_.page_count_);
203 mutable_.page_count_ = max_page;
[email protected]e5324b52013-10-29 03:16:37204 if (immutable_.settings_.ranges().empty()) {
[email protected]e1504d82009-07-03 15:27:15205 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.commit09911bf2008-07-26 23:55:29210 }
initial.commit09911bf2008-07-26 23:55:29211}
212
213int PrintedDocument::page_count() const {
[email protected]20305ec2011-01-21 04:55:52214 base::AutoLock lock(lock_);
initial.commit09911bf2008-07-26 23:55:29215 return mutable_.page_count_;
216}
217
218int PrintedDocument::expected_page_count() const {
[email protected]20305ec2011-01-21 04:55:52219 base::AutoLock lock(lock_);
initial.commit09911bf2008-07-26 23:55:29220 return mutable_.expected_page_count_;
221}
222
[email protected]79f63882013-02-10 05:15:45223void PrintedDocument::set_debug_dump_path(
224 const base::FilePath& debug_dump_path) {
[email protected]c95198b2014-06-12 16:56:55225 g_debug_dump_info.Get() = debug_dump_path;
[email protected]e1504d82009-07-03 15:27:15226}
227
[email protected]c95198b2014-06-12 16:56:55228base::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]6bc03de2014-08-07 23:59:15245 base::i18n::ReplaceIllegalCharactersInPath(&system_filename, '_');
[email protected]c95198b2014-06-12 16:56:55246 return g_debug_dump_info.Get().Append(system_filename).AddExtension(
247 extension);
248}
249
250void 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]e1504d82009-07-03 15:27:15257}
258
initial.commit09911bf2008-07-26 23:55:29259PrintedDocument::Mutable::Mutable(PrintedPagesSource* source)
260 : source_(source),
261 expected_page_count_(0),
[email protected]732b8132012-01-10 23:17:32262 page_count_(0) {
[email protected]da4eefd2011-03-03 23:40:27263#if defined(OS_POSIX) && !defined(OS_MACOSX)
264 first_page = INT_MAX;
265#endif
initial.commit09911bf2008-07-26 23:55:29266}
267
[email protected]20f0487a2010-09-30 20:06:30268PrintedDocument::Mutable::~Mutable() {
269}
270
initial.commit09911bf2008-07-26 23:55:29271PrintedDocument::Immutable::Immutable(const PrintSettings& settings,
272 PrintedPagesSource* source,
[email protected]c95198b2014-06-12 16:56:55273 int cookie,
274 base::TaskRunner* blocking_runner)
initial.commit09911bf2008-07-26 23:55:29275 : settings_(settings),
initial.commit09911bf2008-07-26 23:55:29276 name_(source->RenderSourceName()),
[email protected]c95198b2014-06-12 16:56:55277 cookie_(cookie),
278 blocking_runner_(blocking_runner) {
initial.commit09911bf2008-07-26 23:55:29279}
280
[email protected]20f0487a2010-09-30 20:06:30281PrintedDocument::Immutable::~Immutable() {
282}
283
[email protected]d53002f42014-01-14 16:08:56284#if defined(OS_CHROMEOS) || defined(OS_ANDROID)
[email protected]b25f0032013-08-19 22:26:25285// This function is not used on aura linux/chromeos or android.
[email protected]82b72cb82011-12-06 00:23:44286void PrintedDocument::RenderPrintedPage(const PrintedPage& page,
287 PrintingContext* context) const {
288}
289#endif
290
initial.commit09911bf2008-07-26 23:55:29291} // namespace printing