summaryrefslogtreecommitdiffstats
path: root/printing
diff options
context:
space:
mode:
Diffstat (limited to 'printing')
-rw-r--r--printing/BUILD.gn1
-rw-r--r--printing/emf_win.cc41
-rw-r--r--printing/emf_win.h21
-rw-r--r--printing/emf_win_unittest.cc12
-rw-r--r--printing/metafile.cc49
-rw-r--r--printing/metafile.h88
-rw-r--r--printing/pdf_metafile_cg_mac.cc13
-rw-r--r--printing/pdf_metafile_cg_mac.h3
-rw-r--r--printing/pdf_metafile_skia.cc63
-rw-r--r--printing/pdf_metafile_skia.h14
-rw-r--r--printing/print_dialog_gtk_interface.h4
-rw-r--r--printing/printed_document.cc34
-rw-r--r--printing/printed_document.h9
-rw-r--r--printing/printed_document_linux.cc4
-rw-r--r--printing/printed_document_mac.cc2
-rw-r--r--printing/printed_page.cc6
-rw-r--r--printing/printed_page.h6
-rw-r--r--printing/printed_page_unittest.cc31
-rw-r--r--printing/printing.gyp1
-rw-r--r--printing/printing_context_linux.cc3
-rw-r--r--printing/printing_context_linux.h4
21 files changed, 191 insertions, 218 deletions
diff --git a/printing/BUILD.gn b/printing/BUILD.gn
index 2a0d2b5..a1ef97d 100644
--- a/printing/BUILD.gn
+++ b/printing/BUILD.gn
@@ -28,6 +28,7 @@ component("printing") {
"image_linux.cc",
"image_mac.cc",
"image_win.cc",
+ "metafile.cc",
"metafile.h",
"metafile_skia_wrapper.cc",
"metafile_skia_wrapper.h",
diff --git a/printing/emf_win.cc b/printing/emf_win.cc
index b6c9b4f..5497495a 100644
--- a/printing/emf_win.cc
+++ b/printing/emf_win.cc
@@ -4,6 +4,7 @@
#include "printing/emf_win.h"
+#include "base/files/file.h"
#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
@@ -273,38 +274,6 @@ bool Emf::GetData(void* buffer, uint32 size) const {
return size2 == size && size2 != 0;
}
-bool Emf::GetDataAsVector(std::vector<uint8>* buffer) const {
- uint32 size = GetDataSize();
- if (!size)
- return false;
-
- buffer->resize(size);
- if (!GetData(&buffer->front(), size))
- return false;
- return true;
-}
-
-bool Emf::SaveTo(const base::FilePath& file_path) const {
- HANDLE file = CreateFile(file_path.value().c_str(), GENERIC_WRITE,
- FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
- CREATE_ALWAYS, 0, NULL);
- if (file == INVALID_HANDLE_VALUE)
- return false;
-
- bool success = false;
- std::vector<uint8> buffer;
- if (GetDataAsVector(&buffer)) {
- DWORD written = 0;
- if (WriteFile(file, &*buffer.begin(), static_cast<DWORD>(buffer.size()),
- &written, NULL) &&
- written == buffer.size()) {
- success = true;
- }
- }
- CloseHandle(file);
- return success;
-}
-
int CALLBACK Emf::SafePlaybackProc(HDC hdc,
HANDLETABLE* handle_table,
const ENHMETARECORD* record,
@@ -606,7 +575,7 @@ bool Emf::IsAlphaBlendUsed() const {
return result;
}
-Emf* Emf::RasterizeMetafile(int raster_area_in_pixels) const {
+scoped_ptr<Emf> Emf::RasterizeMetafile(int raster_area_in_pixels) const {
gfx::Rect page_bounds = GetPageBounds(1);
gfx::Size page_size(page_bounds.size());
if (page_size.GetArea() <= 0) {
@@ -648,10 +617,10 @@ Emf* Emf::RasterizeMetafile(int raster_area_in_pixels) const {
result->FinishPage();
result->FinishDocument();
- return result.release();
+ return result.Pass();
}
-Emf* Emf::RasterizeAlphaBlend() const {
+scoped_ptr<Emf> Emf::RasterizeAlphaBlend() const {
gfx::Rect page_bounds = GetPageBounds(1);
if (page_bounds.size().GetArea() <= 0) {
NOTREACHED() << "Metafile is empty";
@@ -676,7 +645,7 @@ Emf* Emf::RasterizeAlphaBlend() const {
result->FinishDocument();
- return result.release();
+ return result.Pass();
}
diff --git a/printing/emf_win.h b/printing/emf_win.h
index e31e204..f759300 100644
--- a/printing/emf_win.h
+++ b/printing/emf_win.h
@@ -12,6 +12,7 @@
#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/gtest_prod_util.h"
+#include "base/memory/scoped_ptr.h"
#include "printing/metafile.h"
namespace base {
@@ -47,10 +48,10 @@ class PRINTING_EXPORT Emf : public Metafile {
// Generates a new metafile that will record every GDI command, and will
// be saved to |metafile_path|.
- virtual bool InitToFile(const base::FilePath& metafile_path);
+ bool InitToFile(const base::FilePath& metafile_path);
// Initializes the Emf with the data in |metafile_path|.
- virtual bool InitFromFile(const base::FilePath& metafile_path);
+ bool InitFromFile(const base::FilePath& metafile_path);
// Metafile methods.
virtual bool Init() OVERRIDE;
@@ -73,11 +74,6 @@ class PRINTING_EXPORT Emf : public Metafile {
virtual uint32 GetDataSize() const OVERRIDE;
virtual bool GetData(void* buffer, uint32 size) const OVERRIDE;
- // Saves the EMF data to a file as-is. It is recommended to use the .emf file
- // extension but it is not enforced. This function synchronously writes to the
- // file. For testing only.
- virtual bool SaveTo(const base::FilePath& file_path) const OVERRIDE;
-
// Should be passed to Playback to keep the exact same size.
virtual gfx::Rect GetPageBounds(unsigned int page_number) const OVERRIDE;
@@ -92,29 +88,24 @@ class PRINTING_EXPORT Emf : public Metafile {
virtual bool Playback(HDC hdc, const RECT* rect) const OVERRIDE;
virtual bool SafePlayback(HDC hdc) const OVERRIDE;
- virtual HENHMETAFILE emf() const OVERRIDE {
- return emf_;
- }
+ HENHMETAFILE emf() const { return emf_; }
// Returns true if metafile contains alpha blend.
bool IsAlphaBlendUsed() const;
// Returns new metafile with only bitmap created by playback of the current
// metafile. Returns NULL if fails.
- Emf* RasterizeMetafile(int raster_area_in_pixels) const;
+ scoped_ptr<Emf> RasterizeMetafile(int raster_area_in_pixels) const;
// Returns new metafile where AlphaBlend replaced by bitmaps. Returns NULL
// if fails.
- Emf* RasterizeAlphaBlend() const;
+ scoped_ptr<Emf> RasterizeAlphaBlend() const;
private:
FRIEND_TEST_ALL_PREFIXES(EmfTest, DC);
FRIEND_TEST_ALL_PREFIXES(EmfPrintingTest, PageBreak);
FRIEND_TEST_ALL_PREFIXES(EmfTest, FileBackedEmf);
- // Retrieves the underlying data stream. It is a helper function.
- bool GetDataAsVector(std::vector<uint8>* buffer) const;
-
// Playbacks safely one EMF record.
static int CALLBACK SafePlaybackProc(HDC hdc,
HANDLETABLE* handle_table,
diff --git a/printing/emf_win_unittest.cc b/printing/emf_win_unittest.cc
index ad4d8c3..451a532 100644
--- a/printing/emf_win_unittest.cc
+++ b/printing/emf_win_unittest.cc
@@ -52,7 +52,7 @@ const uint32 EMF_HEADER_SIZE = 128;
TEST(EmfTest, DC) {
// Simplest use case.
uint32 size;
- std::vector<BYTE> data;
+ std::vector<char> data;
{
Emf emf;
EXPECT_TRUE(emf.Init());
@@ -134,7 +134,7 @@ TEST_F(EmfPrintingTest, PageBreak) {
if (!dc.Get())
return;
uint32 size;
- std::vector<BYTE> data;
+ std::vector<char> data;
{
Emf emf;
EXPECT_TRUE(emf.Init());
@@ -179,7 +179,7 @@ TEST(EmfTest, FileBackedEmf) {
EXPECT_TRUE(base::CreateTemporaryFileInDir(scratch_metafile_dir.path(),
&metafile_path));
uint32 size;
- std::vector<BYTE> data;
+ std::vector<char> data;
{
Emf emf;
EXPECT_TRUE(emf.InitToFile(metafile_path));
@@ -221,12 +221,12 @@ TEST(EmfTest, RasterizeMetafile) {
// Just 1px bitmap but should be stretched to the same bounds.
EXPECT_EQ(emf.GetPageBounds(1), raster->GetPageBounds(1));
- raster.reset(emf.RasterizeMetafile(20));
+ raster = emf.RasterizeMetafile(20);
EXPECT_EQ(emf.GetPageBounds(1), raster->GetPageBounds(1));
- raster.reset(emf.RasterizeMetafile(16*1024*1024));
+ raster = emf.RasterizeMetafile(16 * 1024 * 1024);
// Expected size about 64MB.
- EXPECT_LE(abs(int(raster->GetDataSize()) - 64*1024*1024), 1024*1024);
+ EXPECT_LE(abs(int(raster->GetDataSize()) - 64 * 1024 * 1024), 1024 * 1024);
// Bounds should still be the same.
EXPECT_EQ(emf.GetPageBounds(1), raster->GetPageBounds(1));
}
diff --git a/printing/metafile.cc b/printing/metafile.cc
new file mode 100644
index 0000000..ece61df
--- /dev/null
+++ b/printing/metafile.cc
@@ -0,0 +1,49 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "printing/metafile.h"
+
+#include <vector>
+
+#include "base/files/file.h"
+#include "base/numerics/safe_conversions.h"
+
+namespace printing {
+
+MetafilePlayer::MetafilePlayer() {
+}
+
+MetafilePlayer::~MetafilePlayer() {
+}
+
+Metafile::Metafile() {
+}
+
+Metafile::~Metafile() {
+}
+
+bool Metafile::GetDataAsVector(std::vector<char>* buffer) const {
+ buffer->resize(GetDataSize());
+ if (buffer->empty())
+ return false;
+ return GetData(&buffer->front(), base::checked_cast<uint32>(buffer->size()));
+}
+
+bool Metafile::SaveTo(base::File* file) const {
+ if (!file->IsValid())
+ return false;
+
+ std::vector<char> buffer;
+ if (!GetDataAsVector(&buffer))
+ return false;
+
+ int size = base::checked_cast<int>(buffer.size());
+ if (file->WriteAtCurrentPos(&buffer[0], size) != size) {
+ DLOG(ERROR) << "Failed to save file.";
+ return false;
+ }
+ return true;
+}
+
+} // namespace printing
diff --git a/printing/metafile.h b/printing/metafile.h
index 5f771e5f..af0b2372 100644
--- a/printing/metafile.h
+++ b/printing/metafile.h
@@ -5,6 +5,8 @@
#ifndef PRINTING_METAFILE_H_
#define PRINTING_METAFILE_H_
+#include <vector>
+
#include "base/basictypes.h"
#include "build/build_config.h"
#include "printing/printing_export.h"
@@ -19,7 +21,7 @@
#endif
namespace base {
-class FilePath;
+class File;
}
namespace gfx {
@@ -29,17 +31,10 @@ class Size;
class SkBaseDevice;
-#if defined(OS_CHROMEOS) || defined(OS_ANDROID)
-namespace base {
-struct FileDescriptor;
-}
-#endif
-
namespace printing {
-// This class creates a graphics context that renders into a data stream
-// (usually PDF or EMF).
-class PRINTING_EXPORT Metafile {
+// This class plays metafiles from data stream (usually PDF or EMF).
+class PRINTING_EXPORT MetafilePlayer {
public:
#if defined(OS_MACOSX)
// |shrink_to_fit| specifies whether the output should be shrunk to fit a
@@ -71,8 +66,40 @@ class PRINTING_EXPORT Metafile {
bool autorotate;
};
#endif // defined(OS_MACOSX)
+ MetafilePlayer();
+ virtual ~MetafilePlayer();
+
+#if defined(OS_WIN)
+ // The slow version of Playback(). It enumerates all the records and play them
+ // back in the HDC. The trick is that it skip over the records known to have
+ // issue with some printers. See Emf::Record::SafePlayback implementation for
+ // details.
+ virtual bool SafePlayback(gfx::NativeDrawingContext hdc) const = 0;
+
+#elif defined(OS_MACOSX)
+ // Renders the given page into |rect| in the given context.
+ // Pages use a 1-based index. The rendering uses the arguments in
+ // |params| to determine scaling, translation, and rotation.
+ virtual bool RenderPage(unsigned int page_number,
+ gfx::NativeDrawingContext context,
+ const CGRect rect,
+ const MacRenderPageParams& params) const = 0;
+#endif // if defined(OS_WIN)
+
+ // Saves the underlying data to the given file. This function should ONLY be
+ // called after the metafile is closed. Returns true if writing succeeded.
+ virtual bool SaveTo(base::File* file) const = 0;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(MetafilePlayer);
+};
- virtual ~Metafile() {}
+// This class creates a graphics context that renders into a data stream
+// (usually PDF or EMF).
+class PRINTING_EXPORT Metafile : public MetafilePlayer {
+ public:
+ Metafile();
+ virtual ~Metafile();
// Initializes a fresh new metafile for rendering. Returns false on failure.
// Note: It should only be called from within the renderer process to allocate
@@ -87,10 +114,9 @@ class PRINTING_EXPORT Metafile {
// This method calls StartPage and then returns an appropriate
// VectorPlatformDevice implementation bound to the context created by
// StartPage or NULL on error.
- virtual SkBaseDevice* StartPageForVectorCanvas(
- const gfx::Size& page_size,
- const gfx::Rect& content_area,
- const float& scale_factor) = 0;
+ virtual SkBaseDevice* StartPageForVectorCanvas(const gfx::Size& page_size,
+ const gfx::Rect& content_area,
+ const float& scale_factor) = 0;
// Prepares a context for rendering a new page with the given |page_size|,
// |content_area| and a |scale_factor| to use for the drawing. The units are
@@ -117,15 +143,9 @@ class PRINTING_EXPORT Metafile {
// Returns true if the copy succeeds.
virtual bool GetData(void* dst_buffer, uint32 dst_buffer_size) const = 0;
- // Saves the underlying data to the given file. This function should ONLY be
- // called after the metafile is closed. Returns true if writing succeeded.
- virtual bool SaveTo(const base::FilePath& file_path) const = 0;
-
- // Returns the bounds of the given page. Pages use a 1-based index.
virtual gfx::Rect GetPageBounds(unsigned int page_number) const = 0;
virtual unsigned int GetPageCount() const = 0;
- // Get the context for rendering to the PDF.
virtual gfx::NativeDrawingContext context() const = 0;
#if defined(OS_WIN)
@@ -139,28 +159,14 @@ class PRINTING_EXPORT Metafile {
// it requires user intervention.
virtual bool Playback(gfx::NativeDrawingContext hdc,
const RECT* rect) const = 0;
+#endif // OS_WIN
- // The slow version of Playback(). It enumerates all the records and play them
- // back in the HDC. The trick is that it skip over the records known to have
- // issue with some printers. See Emf::Record::SafePlayback implementation for
- // details.
- virtual bool SafePlayback(gfx::NativeDrawingContext hdc) const = 0;
+ bool GetDataAsVector(std::vector<char>* buffer) const;
- virtual HENHMETAFILE emf() const = 0;
-#elif defined(OS_MACOSX)
- // Renders the given page into |rect| in the given context.
- // Pages use a 1-based index. The rendering uses the arguments in
- // |params| to determine scaling, translation, and rotation.
- virtual bool RenderPage(unsigned int page_number,
- gfx::NativeDrawingContext context,
- const CGRect rect,
- const MacRenderPageParams& params) const = 0;
-#elif defined(OS_CHROMEOS) || defined(OS_ANDROID)
- // Saves the underlying data to the file associated with fd. This function
- // should ONLY be called after the metafile is closed.
- // Returns true if writing succeeded.
- virtual bool SaveToFD(const base::FileDescriptor& fd) const = 0;
-#endif // if defined(OS_CHROMEOS) || defined(OS_ANDROID)
+ virtual bool SaveTo(base::File* file) const OVERRIDE;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(Metafile);
};
} // namespace printing
diff --git a/printing/pdf_metafile_cg_mac.cc b/printing/pdf_metafile_cg_mac.cc
index 8668738..47915d1 100644
--- a/printing/pdf_metafile_cg_mac.cc
+++ b/printing/pdf_metafile_cg_mac.cc
@@ -286,19 +286,6 @@ bool PdfMetafileCg::GetData(void* dst_buffer, uint32 dst_buffer_size) const {
return true;
}
-bool PdfMetafileCg::SaveTo(const base::FilePath& file_path) const {
- DCHECK(pdf_data_.get());
- DCHECK(!context_.get());
-
- std::string path_string = file_path.value();
- ScopedCFTypeRef<CFURLRef> path_url(CFURLCreateFromFileSystemRepresentation(
- kCFAllocatorDefault, reinterpret_cast<const UInt8*>(path_string.c_str()),
- path_string.length(), false));
- SInt32 error_code;
- CFURLWriteDataAndPropertiesToResource(path_url, pdf_data_, NULL, &error_code);
- return error_code == 0;
-}
-
CGContextRef PdfMetafileCg::context() const {
return context_.get();
}
diff --git a/printing/pdf_metafile_cg_mac.h b/printing/pdf_metafile_cg_mac.h
index 331129a..98c8ea4 100644
--- a/printing/pdf_metafile_cg_mac.h
+++ b/printing/pdf_metafile_cg_mac.h
@@ -49,9 +49,6 @@ class PRINTING_EXPORT PdfMetafileCg : public Metafile {
virtual uint32 GetDataSize() const OVERRIDE;
virtual bool GetData(void* dst_buffer, uint32 dst_buffer_size) const OVERRIDE;
- // For testing purposes only.
- virtual bool SaveTo(const base::FilePath& file_path) const OVERRIDE;
-
virtual gfx::Rect GetPageBounds(unsigned int page_number) const OVERRIDE;
virtual unsigned int GetPageCount() const OVERRIDE;
diff --git a/printing/pdf_metafile_skia.cc b/printing/pdf_metafile_skia.cc
index 899232d..36a42d9 100644
--- a/printing/pdf_metafile_skia.cc
+++ b/printing/pdf_metafile_skia.cc
@@ -128,18 +128,6 @@ bool PdfMetafileSkia::GetData(void* dst_buffer,
return true;
}
-bool PdfMetafileSkia::SaveTo(const base::FilePath& file_path) const {
- DCHECK_GT(data_->pdf_stream_.getOffset(), 0U);
- SkAutoDataUnref data(data_->pdf_stream_.copyToData());
- if (base::WriteFile(file_path,
- reinterpret_cast<const char*>(data->data()),
- GetDataSize()) != static_cast<int>(GetDataSize())) {
- DLOG(ERROR) << "Failed to save file " << file_path.value().c_str();
- return false;
- }
- return true;
-}
-
gfx::Rect PdfMetafileSkia::GetPageBounds(unsigned int page_number) const {
// TODO(vandebo) add a method to get the page size for a given page to
// SkPDFDocument.
@@ -170,10 +158,6 @@ bool PdfMetafileSkia::SafePlayback(gfx::NativeDrawingContext hdc) const {
return false;
}
-HENHMETAFILE PdfMetafileSkia::emf() const {
- NOTREACHED();
- return NULL;
-}
#elif defined(OS_MACOSX)
/* TODO(caryclark): The set up of PluginInstance::PrintPDFOutput may result in
rasterized output. Even if that flow uses PdfMetafileCg::RenderPage,
@@ -203,23 +187,15 @@ bool PdfMetafileSkia::SaveToFD(const base::FileDescriptor& fd) const {
DLOG(ERROR) << "Invalid file descriptor!";
return false;
}
-
- bool result = true;
+ base::File file(fd.fd);
SkAutoDataUnref data(data_->pdf_stream_.copyToData());
- if (base::WriteFileDescriptor(fd.fd,
- reinterpret_cast<const char*>(data->data()),
- GetDataSize()) !=
- static_cast<int>(GetDataSize())) {
- DLOG(ERROR) << "Failed to save file with fd " << fd.fd;
- result = false;
- }
+ bool result =
+ file.WriteAtCurrentPos(reinterpret_cast<const char*>(data->data()),
+ GetDataSize()) == static_cast<int>(GetDataSize());
+ DLOG_IF(ERROR, !result) << "Failed to save file with fd " << fd.fd;
- if (fd.auto_close) {
- if (IGNORE_EINTR(close(fd.fd)) < 0) {
- DPLOG(WARNING) << "close";
- result = false;
- }
- }
+ if (!fd.auto_close)
+ file.TakePlatformFile();
return result;
}
#endif
@@ -229,23 +205,26 @@ PdfMetafileSkia::PdfMetafileSkia()
page_outstanding_(false) {
}
-PdfMetafileSkia* PdfMetafileSkia::GetMetafileForCurrentPage() {
+scoped_ptr<PdfMetafileSkia> PdfMetafileSkia::GetMetafileForCurrentPage() {
+ scoped_ptr<PdfMetafileSkia> metafile;
SkPDFDocument pdf_doc(SkPDFDocument::kDraftMode_Flags);
- SkDynamicMemoryWStream pdf_stream;
if (!pdf_doc.appendPage(data_->current_page_.get()))
- return NULL;
+ return metafile.Pass();
+ SkDynamicMemoryWStream pdf_stream;
if (!pdf_doc.emitPDF(&pdf_stream))
- return NULL;
+ return metafile.Pass();
- SkAutoDataUnref data(pdf_stream.copyToData());
- if (data->size() == 0)
- return NULL;
+ SkAutoDataUnref data_copy(pdf_stream.copyToData());
+ if (data_copy->size() == 0)
+ return scoped_ptr<PdfMetafileSkia>();
- PdfMetafileSkia* metafile = new PdfMetafileSkia;
- metafile->InitFromData(data->bytes(),
- base::checked_cast<uint32>(data->size()));
- return metafile;
+ metafile.reset(new PdfMetafileSkia);
+ if (!metafile->InitFromData(data_copy->bytes(),
+ base::checked_cast<uint32>(data_copy->size()))) {
+ metafile.reset();
+ }
+ return metafile.Pass();
}
} // namespace printing
diff --git a/printing/pdf_metafile_skia.h b/printing/pdf_metafile_skia.h
index b44133f..f46fd3c 100644
--- a/printing/pdf_metafile_skia.h
+++ b/printing/pdf_metafile_skia.h
@@ -15,6 +15,12 @@
#include <windows.h>
#endif
+#if defined(OS_CHROMEOS) || defined(OS_ANDROID)
+namespace base {
+struct FileDescriptor;
+}
+#endif
+
namespace printing {
struct PdfMetafileSkiaData;
@@ -44,8 +50,6 @@ class PRINTING_EXPORT PdfMetafileSkia : public Metafile {
virtual uint32 GetDataSize() const OVERRIDE;
virtual bool GetData(void* dst_buffer, uint32 dst_buffer_size) const OVERRIDE;
- virtual bool SaveTo(const base::FilePath& file_path) const OVERRIDE;
-
virtual gfx::Rect GetPageBounds(unsigned int page_number) const OVERRIDE;
virtual unsigned int GetPageCount() const OVERRIDE;
@@ -55,7 +59,6 @@ class PRINTING_EXPORT PdfMetafileSkia : public Metafile {
virtual bool Playback(gfx::NativeDrawingContext hdc,
const RECT* rect) const OVERRIDE;
virtual bool SafePlayback(gfx::NativeDrawingContext hdc) const OVERRIDE;
- virtual HENHMETAFILE emf() const OVERRIDE;
#elif defined(OS_MACOSX)
virtual bool RenderPage(unsigned int page_number,
gfx::NativeDrawingContext context,
@@ -64,11 +67,12 @@ class PRINTING_EXPORT PdfMetafileSkia : public Metafile {
#endif
#if defined(OS_CHROMEOS) || defined(OS_ANDROID)
- virtual bool SaveToFD(const base::FileDescriptor& fd) const OVERRIDE;
+ // TODO(vitalybuka): replace with SaveTo().
+ bool SaveToFD(const base::FileDescriptor& fd) const;
#endif // if defined(OS_CHROMEOS) || defined(OS_ANDROID)
// Return a new metafile containing just the current page in draft mode.
- PdfMetafileSkia* GetMetafileForCurrentPage();
+ scoped_ptr<PdfMetafileSkia> GetMetafileForCurrentPage();
private:
scoped_ptr<PdfMetafileSkiaData> data_;
diff --git a/printing/print_dialog_gtk_interface.h b/printing/print_dialog_gtk_interface.h
index e06e43e..5c47b28 100644
--- a/printing/print_dialog_gtk_interface.h
+++ b/printing/print_dialog_gtk_interface.h
@@ -11,7 +11,7 @@
namespace printing {
-class Metafile;
+class MetafilePlayer;
class PrintSettings;
// An interface for GTK printing dialogs. Classes that live outside of
@@ -36,7 +36,7 @@ class PrintDialogGtkInterface {
// Prints the document named |document_name| contained in |metafile|.
// Called from the print worker thread. Once called, the
// PrintDialogGtkInterface instance should not be reused.
- virtual void PrintDocument(const Metafile* metafile,
+ virtual void PrintDocument(const MetafilePlayer& metafile,
const base::string16& document_name) = 0;
// Same as AddRef/Release, but with different names since
diff --git a/printing/printed_document.cc b/printing/printed_document.cc
index f1b507d..63fce8f 100644
--- a/printing/printed_document.cc
+++ b/printing/printed_document.cc
@@ -48,13 +48,15 @@ void DebugDumpPageTask(const base::string16& doc_name,
base::string16 filename = doc_name;
filename +=
base::ASCIIToUTF16(base::StringPrintf("_%04d", page->page_number()));
+ base::FilePath file_path =
#if defined(OS_WIN)
- page->metafile()->SaveTo(PrintedDocument::CreateDebugDumpPath(
- filename, FILE_PATH_LITERAL(".emf")));
+ PrintedDocument::CreateDebugDumpPath(filename, FILE_PATH_LITERAL(".emf"));
#else // OS_WIN
- page->metafile()->SaveTo(PrintedDocument::CreateDebugDumpPath(
- filename, FILE_PATH_LITERAL(".pdf")));
+ PrintedDocument::CreateDebugDumpPath(filename, FILE_PATH_LITERAL(".pdf"));
#endif // OS_WIN
+ base::File file(file_path,
+ base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE);
+ page->metafile()->SaveTo(&file);
}
void DebugDumpDataTask(const base::string16& doc_name,
@@ -109,7 +111,7 @@ PrintedDocument::~PrintedDocument() {
}
void PrintedDocument::SetPage(int page_number,
- Metafile* metafile,
+ scoped_ptr<MetafilePlayer> metafile,
#if defined(OS_WIN)
double shrink,
#endif // OS_WIN
@@ -118,7 +120,7 @@ void PrintedDocument::SetPage(int page_number,
// Notice the page_number + 1, the reason is that this is the value that will
// be shown. Users dislike 0-based counting.
scoped_refptr<PrintedPage> page(
- new PrintedPage(page_number + 1, metafile, paper_size, page_rect));
+ new PrintedPage(page_number + 1, metafile.Pass(), paper_size, page_rect));
#if defined(OS_WIN)
page->set_shrink_factor(shrink);
#endif // OS_WIN
@@ -177,26 +179,6 @@ void PrintedDocument::DisconnectSource() {
mutable_.source_ = NULL;
}
-uint32 PrintedDocument::MemoryUsage() const {
- std::vector< scoped_refptr<PrintedPage> > pages_copy;
- {
- base::AutoLock lock(lock_);
- pages_copy.reserve(mutable_.pages_.size());
- PrintedPages::const_iterator end = mutable_.pages_.end();
- for (PrintedPages::const_iterator itr = mutable_.pages_.begin();
- itr != end; ++itr) {
- if (itr->second.get()) {
- pages_copy.push_back(itr->second);
- }
- }
- }
- uint32 total = 0;
- for (size_t i = 0; i < pages_copy.size(); ++i) {
- total += pages_copy[i]->metafile()->GetDataSize();
- }
- return total;
-}
-
void PrintedDocument::set_page_count(int max_page) {
base::AutoLock lock(lock_);
DCHECK_EQ(0, mutable_.page_count_);
diff --git a/printing/printed_document.h b/printing/printed_document.h
index 4d0e41e..47c9ec7 100644
--- a/printing/printed_document.h
+++ b/printing/printed_document.h
@@ -9,6 +9,7 @@
#include "base/files/file_path.h"
#include "base/memory/ref_counted.h"
+#include "base/memory/scoped_ptr.h"
#include "base/strings/string16.h"
#include "base/synchronization/lock.h"
#include "printing/print_settings.h"
@@ -21,7 +22,7 @@ class TaskRunner;
namespace printing {
-class Metafile;
+class MetafilePlayer;
class PrintedPage;
class PrintedPagesSource;
class PrintingContext;
@@ -45,7 +46,7 @@ class PRINTING_EXPORT PrintedDocument
// Sets a page's data. 0-based. Takes metafile ownership.
// Note: locks for a short amount of time.
void SetPage(int page_number,
- Metafile* metafile,
+ scoped_ptr<MetafilePlayer> metafile,
#if defined(OS_WIN)
double shrink,
#endif // OS_WIN
@@ -76,10 +77,6 @@ class PRINTING_EXPORT PrintedDocument
// the source is being destroyed.
void DisconnectSource();
- // Retrieves the current memory usage of the renderer pages.
- // Note: locks for a short amount of time.
- uint32 MemoryUsage() const;
-
// Sets the number of pages in the document to be rendered. Can only be set
// once.
// Note: locks for a short amount of time.
diff --git a/printing/printed_document_linux.cc b/printing/printed_document_linux.cc
index a800307..a104171 100644
--- a/printing/printed_document_linux.cc
+++ b/printing/printed_document_linux.cc
@@ -27,8 +27,8 @@ void PrintedDocument::RenderPrintedPage(
{
base::AutoLock lock(lock_);
if (page.page_number() - 1 == mutable_.first_page) {
- static_cast<PrintingContextLinux*>(context)->PrintDocument(
- page.metafile());
+ static_cast<PrintingContextLinux*>(context)
+ ->PrintDocument(*page.metafile());
}
}
}
diff --git a/printing/printed_document_mac.cc b/printing/printed_document_mac.cc
index cc671e1..3710d62 100644
--- a/printing/printed_document_mac.cc
+++ b/printing/printed_document_mac.cc
@@ -29,7 +29,7 @@ void PrintedDocument::RenderPrintedPage(
gfx::Rect content_area;
page.GetCenteredPageContentRect(page_setup.physical_size(), &content_area);
- const Metafile* metafile = page.metafile();
+ const MetafilePlayer* metafile = page.metafile();
// Each Metafile is a one-page PDF, and pages use 1-based indexing.
const int page_number = 1;
struct Metafile::MacRenderPageParams params;
diff --git a/printing/printed_page.cc b/printing/printed_page.cc
index 19b9fd3..998f8be 100644
--- a/printing/printed_page.cc
+++ b/printing/printed_page.cc
@@ -7,11 +7,11 @@
namespace printing {
PrintedPage::PrintedPage(int page_number,
- Metafile* metafile,
+ scoped_ptr<MetafilePlayer> metafile,
const gfx::Size& page_size,
const gfx::Rect& page_content_rect)
: page_number_(page_number),
- metafile_(metafile),
+ metafile_(metafile.Pass()),
#if defined(OS_WIN)
shrink_factor_(0.0f),
#endif // OS_WIN
@@ -22,7 +22,7 @@ PrintedPage::PrintedPage(int page_number,
PrintedPage::~PrintedPage() {
}
-const Metafile* PrintedPage::metafile() const {
+const MetafilePlayer* PrintedPage::metafile() const {
return metafile_.get();
}
diff --git a/printing/printed_page.h b/printing/printed_page.h
index a1adfa4..0859502 100644
--- a/printing/printed_page.h
+++ b/printing/printed_page.h
@@ -23,13 +23,13 @@ class PRINTING_EXPORT PrintedPage
: public base::RefCountedThreadSafe<PrintedPage> {
public:
PrintedPage(int page_number,
- Metafile* metafile,
+ scoped_ptr<MetafilePlayer> metafile,
const gfx::Size& page_size,
const gfx::Rect& page_content_rect);
// Getters
int page_number() const { return page_number_; }
- const Metafile* metafile() const;
+ const MetafilePlayer* metafile() const;
const gfx::Size& page_size() const { return page_size_; }
const gfx::Rect& page_content_rect() const { return page_content_rect_; }
#if defined(OS_WIN)
@@ -53,7 +53,7 @@ class PRINTING_EXPORT PrintedPage
const int page_number_;
// Actual paint data.
- const scoped_ptr<Metafile> metafile_;
+ const scoped_ptr<MetafilePlayer> metafile_;
#if defined(OS_WIN)
// Shrink done in comparison to desired_dpi.
diff --git a/printing/printed_page_unittest.cc b/printing/printed_page_unittest.cc
index d37d93a..795afe4 100644
--- a/printing/printed_page_unittest.cc
+++ b/printing/printed_page_unittest.cc
@@ -12,8 +12,10 @@ TEST(PrintedPageTest, GetCenteredPageContentRect) {
gfx::Rect page_content;
// No centering.
- page = new PrintedPage(
- 1, NULL, gfx::Size(1200, 1200), gfx::Rect(0, 0, 400, 1100));
+ page = new PrintedPage(1,
+ scoped_ptr<MetafilePlayer>(),
+ gfx::Size(1200, 1200),
+ gfx::Rect(0, 0, 400, 1100));
page->GetCenteredPageContentRect(gfx::Size(1000, 1000), &page_content);
EXPECT_EQ(0, page_content.x());
EXPECT_EQ(0, page_content.y());
@@ -21,8 +23,10 @@ TEST(PrintedPageTest, GetCenteredPageContentRect) {
EXPECT_EQ(1100, page_content.height());
// X centered.
- page = new PrintedPage(
- 1, NULL, gfx::Size(500, 1200), gfx::Rect(0, 0, 400, 1100));
+ page = new PrintedPage(1,
+ scoped_ptr<MetafilePlayer>(),
+ gfx::Size(500, 1200),
+ gfx::Rect(0, 0, 400, 1100));
page->GetCenteredPageContentRect(gfx::Size(1000, 1000), &page_content);
EXPECT_EQ(250, page_content.x());
EXPECT_EQ(0, page_content.y());
@@ -30,8 +34,10 @@ TEST(PrintedPageTest, GetCenteredPageContentRect) {
EXPECT_EQ(1100, page_content.height());
// Y centered.
- page = new PrintedPage(
- 1, NULL, gfx::Size(1200, 500), gfx::Rect(0, 0, 400, 1100));
+ page = new PrintedPage(1,
+ scoped_ptr<MetafilePlayer>(),
+ gfx::Size(1200, 500),
+ gfx::Rect(0, 0, 400, 1100));
page->GetCenteredPageContentRect(gfx::Size(1000, 1000), &page_content);
EXPECT_EQ(0, page_content.x());
EXPECT_EQ(250, page_content.y());
@@ -39,8 +45,10 @@ TEST(PrintedPageTest, GetCenteredPageContentRect) {
EXPECT_EQ(1100, page_content.height());
// Both X and Y centered.
- page =
- new PrintedPage(1, NULL, gfx::Size(500, 500), gfx::Rect(0, 0, 400, 1100));
+ page = new PrintedPage(1,
+ scoped_ptr<MetafilePlayer>(),
+ gfx::Size(500, 500),
+ gfx::Rect(0, 0, 400, 1100));
page->GetCenteredPageContentRect(gfx::Size(1000, 1000), &page_content);
EXPECT_EQ(250, page_content.x());
EXPECT_EQ(250, page_content.y());
@@ -50,8 +58,11 @@ TEST(PrintedPageTest, GetCenteredPageContentRect) {
#if defined(OS_WIN)
TEST(PrintedPageTest, Shrink) {
- scoped_refptr<PrintedPage> page = new PrintedPage(
- 1, NULL, gfx::Size(1200, 1200), gfx::Rect(0, 0, 400, 1100));
+ scoped_refptr<PrintedPage> page =
+ new PrintedPage(1,
+ scoped_ptr<MetafilePlayer>(),
+ gfx::Size(1200, 1200),
+ gfx::Rect(0, 0, 400, 1100));
EXPECT_EQ(0.0f, page->shrink_factor());
page->set_shrink_factor(0.2f);
EXPECT_EQ(0.2f, page->shrink_factor());
diff --git a/printing/printing.gyp b/printing/printing.gyp
index 43f903d..48e289a 100644
--- a/printing/printing.gyp
+++ b/printing/printing.gyp
@@ -44,6 +44,7 @@
'image_linux.cc',
'image_mac.cc',
'image_win.cc',
+ 'metafile.cc',
'metafile.h',
'metafile_skia_wrapper.cc',
'metafile_skia_wrapper.h',
diff --git a/printing/printing_context_linux.cc b/printing/printing_context_linux.cc
index 926d4e2..8e23577 100644
--- a/printing/printing_context_linux.cc
+++ b/printing/printing_context_linux.cc
@@ -59,9 +59,8 @@ void PrintingContextLinux::SetPdfPaperSizeFunction(
get_pdf_paper_size_ = get_pdf_paper_size;
}
-void PrintingContextLinux::PrintDocument(const Metafile* metafile) {
+void PrintingContextLinux::PrintDocument(const MetafilePlayer& metafile) {
DCHECK(print_dialog_);
- DCHECK(metafile);
print_dialog_->PrintDocument(metafile, document_name_);
}
diff --git a/printing/printing_context_linux.h b/printing/printing_context_linux.h
index af59806..ca7085a 100644
--- a/printing/printing_context_linux.h
+++ b/printing/printing_context_linux.h
@@ -15,7 +15,7 @@ class DictionaryValue;
namespace printing {
-class Metafile;
+class MetafilePlayer;
class PrintDialogGtkInterface;
// PrintingContext with optional native UI for print dialog and pdf_paper_size.
@@ -34,7 +34,7 @@ class PRINTING_EXPORT PrintingContextLinux : public PrintingContext {
gfx::Size (*get_pdf_paper_size)(PrintingContextLinux* context));
// Prints the document contained in |metafile|.
- void PrintDocument(const Metafile* metafile);
+ void PrintDocument(const MetafilePlayer& metafile);
// PrintingContext implementation.
virtual void AskUserForSettings(