PasswordManager::Autofill logs in chrome://password-manager-internals/
[chromium-blink-merge.git] / printing / pdf_metafile_skia.cc
blobc63e308cc54abbfb2b526a02c09aef63fe99beb3
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "printing/pdf_metafile_skia.h"
7 #include "base/containers/hash_tables.h"
8 #include "base/files/file_util.h"
9 #include "base/metrics/histogram.h"
10 #include "base/numerics/safe_conversions.h"
11 #include "base/posix/eintr_wrapper.h"
12 #include "skia/ext/refptr.h"
13 #include "skia/ext/vector_canvas.h"
14 #include "third_party/skia/include/core/SkData.h"
15 #include "third_party/skia/include/core/SkRefCnt.h"
16 #include "third_party/skia/include/core/SkScalar.h"
17 #include "third_party/skia/include/core/SkStream.h"
18 #include "third_party/skia/include/core/SkTypeface.h"
19 #include "third_party/skia/include/pdf/SkPDFDevice.h"
20 #include "third_party/skia/include/pdf/SkPDFDocument.h"
21 #include "ui/gfx/point.h"
22 #include "ui/gfx/rect.h"
23 #include "ui/gfx/size.h"
25 #if defined(OS_MACOSX)
26 #include "printing/pdf_metafile_cg_mac.h"
27 #endif
29 #if defined(OS_POSIX)
30 #include "base/file_descriptor_posix.h"
31 #endif
33 namespace printing {
35 struct PdfMetafileSkiaData {
36 skia::RefPtr<SkPDFDevice> current_page_;
37 skia::RefPtr<SkCanvas> current_page_canvas_;
38 SkPDFDocument pdf_doc_;
39 SkDynamicMemoryWStream pdf_stream_;
40 #if defined(OS_MACOSX)
41 PdfMetafileCg pdf_cg_;
42 #endif
45 PdfMetafileSkia::~PdfMetafileSkia() {}
47 bool PdfMetafileSkia::Init() {
48 return true;
50 bool PdfMetafileSkia::InitFromData(const void* src_buffer,
51 uint32 src_buffer_size) {
52 return data_->pdf_stream_.write(src_buffer, src_buffer_size);
55 bool PdfMetafileSkia::StartPage(const gfx::Size& page_size,
56 const gfx::Rect& content_area,
57 const float& scale_factor) {
58 DCHECK(!data_->current_page_canvas_);
60 // Adjust for the margins and apply the scale factor.
61 SkMatrix transform;
62 transform.setTranslate(SkIntToScalar(content_area.x()),
63 SkIntToScalar(content_area.y()));
64 transform.preScale(SkFloatToScalar(scale_factor),
65 SkFloatToScalar(scale_factor));
67 SkISize pdf_page_size = SkISize::Make(page_size.width(), page_size.height());
68 SkISize pdf_content_size =
69 SkISize::Make(content_area.width(), content_area.height());
71 data_->current_page_ = skia::AdoptRef(
72 new SkPDFDevice(pdf_page_size, pdf_content_size, transform));
73 data_->current_page_canvas_ =
74 skia::AdoptRef(new SkCanvas(data_->current_page_.get()));
75 return true;
78 skia::VectorCanvas* PdfMetafileSkia::GetVectorCanvasForNewPage(
79 const gfx::Size& page_size,
80 const gfx::Rect& content_area,
81 const float& scale_factor) {
82 if (!StartPage(page_size, content_area, scale_factor))
83 return nullptr;
84 return data_->current_page_canvas_.get();
87 bool PdfMetafileSkia::FinishPage() {
88 DCHECK(data_->current_page_canvas_);
89 DCHECK(data_->current_page_);
91 data_->current_page_canvas_.clear(); // Unref SkCanvas.
92 data_->pdf_doc_.appendPage(data_->current_page_.get());
93 return true;
96 bool PdfMetafileSkia::FinishDocument() {
97 // Don't do anything if we've already set the data in InitFromData.
98 if (data_->pdf_stream_.getOffset())
99 return true;
101 if (data_->current_page_canvas_)
102 FinishPage();
104 data_->current_page_.clear();
106 int font_counts[SkAdvancedTypefaceMetrics::kOther_Font + 2];
107 data_->pdf_doc_.getCountOfFontTypes(font_counts);
108 for (int type = 0;
109 type <= SkAdvancedTypefaceMetrics::kOther_Font + 1;
110 type++) {
111 for (int count = 0; count < font_counts[type]; count++) {
112 UMA_HISTOGRAM_ENUMERATION(
113 "PrintPreview.FontType", type,
114 SkAdvancedTypefaceMetrics::kOther_Font + 2);
118 return data_->pdf_doc_.emitPDF(&data_->pdf_stream_);
121 uint32 PdfMetafileSkia::GetDataSize() const {
122 return base::checked_cast<uint32>(data_->pdf_stream_.getOffset());
125 bool PdfMetafileSkia::GetData(void* dst_buffer,
126 uint32 dst_buffer_size) const {
127 if (dst_buffer_size < GetDataSize())
128 return false;
130 SkAutoDataUnref data(data_->pdf_stream_.copyToData());
131 memcpy(dst_buffer, data->bytes(), dst_buffer_size);
132 return true;
135 gfx::Rect PdfMetafileSkia::GetPageBounds(unsigned int page_number) const {
136 // TODO(vandebo) add a method to get the page size for a given page to
137 // SkPDFDocument.
138 NOTIMPLEMENTED();
139 return gfx::Rect();
142 unsigned int PdfMetafileSkia::GetPageCount() const {
143 // TODO(vandebo) add a method to get the number of pages to SkPDFDocument.
144 NOTIMPLEMENTED();
145 return 0;
148 gfx::NativeDrawingContext PdfMetafileSkia::context() const {
149 NOTREACHED();
150 return NULL;
153 #if defined(OS_WIN)
154 bool PdfMetafileSkia::Playback(gfx::NativeDrawingContext hdc,
155 const RECT* rect) const {
156 NOTREACHED();
157 return false;
160 bool PdfMetafileSkia::SafePlayback(gfx::NativeDrawingContext hdc) const {
161 NOTREACHED();
162 return false;
165 #elif defined(OS_MACOSX)
166 /* TODO(caryclark): The set up of PluginInstance::PrintPDFOutput may result in
167 rasterized output. Even if that flow uses PdfMetafileCg::RenderPage,
168 the drawing of the PDF into the canvas may result in a rasterized output.
169 PDFMetafileSkia::RenderPage should be not implemented as shown and instead
170 should do something like the following CL in PluginInstance::PrintPDFOutput:
171 http://codereview.chromium.org/7200040/diff/1/webkit/plugins/ppapi/ppapi_plugin_instance.cc
173 bool PdfMetafileSkia::RenderPage(unsigned int page_number,
174 CGContextRef context,
175 const CGRect rect,
176 const MacRenderPageParams& params) const {
177 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U);
178 if (data_->pdf_cg_.GetDataSize() == 0) {
179 SkAutoDataUnref data(data_->pdf_stream_.copyToData());
180 data_->pdf_cg_.InitFromData(data->bytes(), data->size());
182 return data_->pdf_cg_.RenderPage(page_number, context, rect, params);
184 #endif
186 bool PdfMetafileSkia::SaveTo(base::File* file) const {
187 if (GetDataSize() == 0U)
188 return false;
189 SkAutoDataUnref data(data_->pdf_stream_.copyToData());
190 // TODO(halcanary): rewrite this function without extra data copy
191 // using SkStreamAsset.
192 const char* ptr = reinterpret_cast<const char*>(data->data());
193 int size = base::checked_cast<int>(data->size());
194 return file->WriteAtCurrentPos(ptr, size) == size;
197 #if defined(OS_CHROMEOS) || defined(OS_ANDROID)
198 bool PdfMetafileSkia::SaveToFD(const base::FileDescriptor& fd) const {
199 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U);
201 if (fd.fd < 0) {
202 DLOG(ERROR) << "Invalid file descriptor!";
203 return false;
205 base::File file(fd.fd);
206 bool result = SaveTo(&file);
207 DLOG_IF(ERROR, !result) << "Failed to save file with fd " << fd.fd;
209 if (!fd.auto_close)
210 file.TakePlatformFile();
211 return result;
213 #endif
215 PdfMetafileSkia::PdfMetafileSkia() : data_(new PdfMetafileSkiaData) {
218 scoped_ptr<PdfMetafileSkia> PdfMetafileSkia::GetMetafileForCurrentPage() {
219 scoped_ptr<PdfMetafileSkia> metafile;
220 SkPDFDocument pdf_doc(SkPDFDocument::kDraftMode_Flags);
221 if (!pdf_doc.appendPage(data_->current_page_.get()))
222 return metafile.Pass();
224 SkDynamicMemoryWStream pdf_stream;
225 if (!pdf_doc.emitPDF(&pdf_stream))
226 return metafile.Pass();
228 SkAutoDataUnref data_copy(pdf_stream.copyToData());
229 if (data_copy->size() == 0)
230 return scoped_ptr<PdfMetafileSkia>();
232 metafile.reset(new PdfMetafileSkia);
233 if (!metafile->InitFromData(data_copy->bytes(),
234 base::checked_cast<uint32>(data_copy->size()))) {
235 metafile.reset();
237 return metafile.Pass();
240 } // namespace printing