diff options
-rw-r--r-- | chrome/common/common_param_traits_unittest.cc | 2 | ||||
-rw-r--r-- | chrome/renderer/print_web_view_helper_win.cc | 8 | ||||
-rw-r--r-- | chrome/service/cloud_print/print_system_win.cc | 6 | ||||
-rw-r--r-- | chrome/service/service_utility_process_host.cc | 15 | ||||
-rw-r--r-- | chrome/service/service_utility_process_host.h | 6 | ||||
-rw-r--r-- | chrome/utility/utility_thread.cc | 20 | ||||
-rw-r--r-- | content/plugin/webplugin_delegate_stub.cc | 2 | ||||
-rw-r--r-- | printing/emf_win.cc | 42 | ||||
-rw-r--r-- | printing/emf_win.h | 29 | ||||
-rw-r--r-- | printing/emf_win_unittest.cc | 101 | ||||
-rw-r--r-- | printing/native_metafile.h | 23 |
11 files changed, 105 insertions, 149 deletions
diff --git a/chrome/common/common_param_traits_unittest.cc b/chrome/common/common_param_traits_unittest.cc index c227921..f62bb6f 100644 --- a/chrome/common/common_param_traits_unittest.cc +++ b/chrome/common/common_param_traits_unittest.cc @@ -201,7 +201,7 @@ TEST(IPCMessageTest, Metafile) { printing::NativeMetafileFactory::Create()); RECT test_rect = {0, 0, 100, 100}; // Create a metafile using the screen DC as a reference. - metafile->CreateDc(NULL, NULL); + metafile->Init(); metafile->FinishDocument(); IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL); diff --git a/chrome/renderer/print_web_view_helper_win.cc b/chrome/renderer/print_web_view_helper_win.cc index 2e87939..941aac7 100644 --- a/chrome/renderer/print_web_view_helper_win.cc +++ b/chrome/renderer/print_web_view_helper_win.cc @@ -74,7 +74,7 @@ void PrintWebViewHelper::PrintPage(const ViewMsg_PrintPage_Params& params, // Each metafile contains a single page. scoped_ptr<printing::NativeMetafile> metafile( printing::NativeMetafileFactory::Create()); - metafile->CreateDc(NULL, NULL); + metafile->Init(); DCHECK(metafile->context()); skia::PlatformDevice::InitializeDC(metafile->context()); @@ -111,7 +111,6 @@ void PrintWebViewHelper::PrintPage(const ViewMsg_PrintPage_Params& params, &(page_params.metafile_data_handle))) { page_params.data_size = 0; } - metafile->CloseEmf(); if (!Send(new ViewHostMsg_DuplicateSection( routing_id(), page_params.metafile_data_handle, @@ -142,7 +141,7 @@ void PrintWebViewHelper::CreatePreviewDocument( // http://code.google.com/p/chromium/issues/detail?id=62889 scoped_ptr<printing::NativeMetafile> metafile( printing::NativeMetafileFactory::Create()); - metafile->CreateDc(NULL, NULL); + metafile->Init(); DCHECK(metafile->context()); skia::PlatformDevice::InitializeDC(metafile->context()); @@ -184,7 +183,6 @@ void PrintWebViewHelper::CreatePreviewDocument( preview_params.data_size = 0; preview_params.expected_pages_count = 0; } - metafile->CloseEmf(); if (!Send(new ViewHostMsg_DuplicateSection( routing_id(), preview_params.metafile_data_handle, @@ -263,7 +261,7 @@ void PrintWebViewHelper::RenderPage( HBRUSH whiteBrush = static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH)); FillRect(bitmap_dc, &rect, whiteBrush); - metafile2->CreateDc(NULL, NULL); + metafile2->Init(); HDC hdc = metafile2->context(); DCHECK(hdc); skia::PlatformDevice::InitializeDC(hdc); diff --git a/chrome/service/cloud_print/print_system_win.cc b/chrome/service/cloud_print/print_system_win.cc index 01caaa8..e963403 100644 --- a/chrome/service/cloud_print/print_system_win.cc +++ b/chrome/service/cloud_print/print_system_win.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 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. @@ -26,7 +26,7 @@ #include "printing/backend/print_backend.h" #include "printing/backend/print_backend_consts.h" #include "printing/backend/win_helper.h" -#include "printing/native_metafile.h" +#include "printing/emf_win.h" #include "printing/page_range.h" #include "ui/base/l10n/l10n_util.h" #include "ui/gfx/rect.h" @@ -448,7 +448,7 @@ class PrintSystemWin : public PrintSystem { // ServiceUtilityProcessHost::Client implementation. virtual void OnRenderPDFPagesToMetafileSucceeded( - const printing::NativeMetafile& metafile, + const printing::Emf& metafile, int highest_rendered_page_number) { metafile.SafePlayback(printer_dc_.Get()); bool done_printing = (highest_rendered_page_number != diff --git a/chrome/service/service_utility_process_host.cc b/chrome/service/service_utility_process_host.cc index 31f8b0e..0e8aa38 100644 --- a/chrome/service/service_utility_process_host.cc +++ b/chrome/service/service_utility_process_host.cc @@ -19,8 +19,7 @@ #if defined(OS_WIN) #include "base/scoped_ptr.h" #include "base/win/scoped_handle.h" -#include "printing/native_metafile_factory.h" -#include "printing/native_metafile.h" +#include "printing/emf_win.h" #endif ServiceUtilityProcessHost::ServiceUtilityProcessHost( @@ -203,15 +202,15 @@ void ServiceUtilityProcessHost::Client::MetafileAvailable( if (!scratch_metafile_dir.Set(metafile_path.DirName())) LOG(WARNING) << "Unable to set scratch metafile directory"; #if defined(OS_WIN) - scoped_ptr<printing::NativeMetafile> metafile( - printing::NativeMetafileFactory::Create()); - if (!metafile->CreateFromFile(metafile_path)) { + // It's important that metafile is declared after scratch_metafile_dir so + // that the metafile destructor closes the file before the ScopedTempDir + // destructor tries to remove the directory. + printing::Emf metafile; + if (!metafile.InitFromFile(metafile_path)) { OnRenderPDFPagesToMetafileFailed(); } else { - OnRenderPDFPagesToMetafileSucceeded(*metafile, + OnRenderPDFPagesToMetafileSucceeded(metafile, highest_rendered_page_number); - // Close it so that ScopedTempDir can delete the folder. - metafile->CloseEmf(); } #endif // defined(OS_WIN) } diff --git a/chrome/service/service_utility_process_host.h b/chrome/service/service_utility_process_host.h index b770e1a..ad7eb3e 100644 --- a/chrome/service/service_utility_process_host.h +++ b/chrome/service/service_utility_process_host.h @@ -1,4 +1,4 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 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. @@ -21,7 +21,6 @@ #include "base/task.h" #include "ipc/ipc_channel.h" #include "chrome/service/service_child_process_host.h" -#include "printing/native_metafile.h" class CommandLine; class ScopedTempDir; @@ -35,6 +34,7 @@ class Rect; } // namespace gfx namespace printing { +class Emf; struct PageRange; struct PrinterCapsAndDefaults; } // namespace printing @@ -57,7 +57,7 @@ class ServiceUtilityProcessHost : public ServiceChildProcessHost { // Called when at least one page in the specified PDF has been rendered // successfully into |metafile|. virtual void OnRenderPDFPagesToMetafileSucceeded( - const printing::NativeMetafile& metafile, + const printing::Emf& metafile, int highest_rendered_page_number) {} // Called when no page in the passed in PDF could be rendered. virtual void OnRenderPDFPagesToMetafileFailed() {} diff --git a/chrome/utility/utility_thread.cc b/chrome/utility/utility_thread.cc index cca4b34..bf312f7 100644 --- a/chrome/utility/utility_thread.cc +++ b/chrome/utility/utility_thread.cc @@ -32,8 +32,7 @@ #include "app/win/iat_patch_function.h" #include "base/scoped_ptr.h" #include "base/win/scoped_handle.h" -#include "printing/native_metafile_factory.h" -#include "printing/native_metafile.h" +#include "printing/emf_win.h" #endif namespace { @@ -259,18 +258,17 @@ bool UtilityThread::RenderPDFToWinMetafile( if (!get_info_proc(&buffer.front(), buffer.size(), &total_page_count, NULL)) return false; - scoped_ptr<printing::NativeMetafile> metafile( - printing::NativeMetafileFactory::Create()); - metafile->CreateFileBackedDc(NULL, NULL, metafile_path); + printing::Emf metafile; + metafile.InitToFile(metafile_path); // Since we created the metafile using the screen DPI (but we actually want // the PDF DLL to print using the passed in render_dpi, we apply the following // transformation. - SetGraphicsMode(metafile->context(), GM_ADVANCED); + SetGraphicsMode(metafile.context(), GM_ADVANCED); XFORM xform = {0}; int screen_dpi = GetDeviceCaps(GetDC(NULL), LOGPIXELSX); xform.eM11 = xform.eM22 = static_cast<float>(screen_dpi) / static_cast<float>(render_dpi); - ModifyWorldTransform(metafile->context(), &xform, MWT_LEFTMULTIPLY); + ModifyWorldTransform(metafile.context(), &xform, MWT_LEFTMULTIPLY); bool ret = false; std::vector<printing::PageRange>::const_iterator iter; @@ -280,18 +278,18 @@ bool UtilityThread::RenderPDFToWinMetafile( break; // The underlying metafile is of type Emf and ignores the arguments passed // to StartPage. - metafile->StartPage(gfx::Size(), gfx::Point(), 1); + metafile.StartPage(gfx::Size(), gfx::Point(), 1); if (render_proc(&buffer.front(), buffer.size(), page_number, - metafile->context(), render_dpi, render_dpi, + metafile.context(), render_dpi, render_dpi, render_area.x(), render_area.y(), render_area.width(), render_area.height(), true, false, true, true)) if (*highest_rendered_page_number < page_number) *highest_rendered_page_number = page_number; ret = true; - metafile->FinishPage(); + metafile.FinishPage(); } } - metafile->FinishDocument(); + metafile.FinishDocument(); return ret; } #endif // defined(OS_WIN) diff --git a/content/plugin/webplugin_delegate_stub.cc b/content/plugin/webplugin_delegate_stub.cc index 2de6e42..76b4c23 100644 --- a/content/plugin/webplugin_delegate_stub.cc +++ b/content/plugin/webplugin_delegate_stub.cc @@ -273,7 +273,7 @@ void WebPluginDelegateStub::OnPrint(base::SharedMemoryHandle* shared_memory, #if defined(OS_WIN) scoped_ptr<printing::NativeMetafile> metafile( printing::NativeMetafileFactory::Create()); - if (!metafile->CreateDc(NULL, NULL)) { + if (!metafile->Init()) { NOTREACHED(); return; } diff --git a/printing/emf_win.cc b/printing/emf_win.cc index 8609c34..c411a40 100644 --- a/printing/emf_win.cc +++ b/printing/emf_win.cc @@ -53,41 +53,39 @@ Emf::Emf() : emf_(NULL), hdc_(NULL) { } Emf::~Emf() { - CloseEmf(); - DCHECK(!emf_ && !hdc_); + DCHECK(!hdc_); + if (emf_) + DeleteEnhMetaFile(emf_); } -bool Emf::InitFromData(const void* src_buffer, uint32 src_buffer_size) { +bool Emf::InitToFile(const FilePath& metafile_path) { DCHECK(!emf_ && !hdc_); - emf_ = SetEnhMetaFileBits(src_buffer_size, - reinterpret_cast<const BYTE*>(src_buffer)); - return emf_ != NULL; + hdc_ = CreateEnhMetaFile(NULL, metafile_path.value().c_str(), NULL, NULL); + DCHECK(hdc_); + return hdc_ != NULL; } -bool Emf::CreateDc(HDC sibling, const RECT* rect) { +bool Emf::InitFromFile(const FilePath& metafile_path) { DCHECK(!emf_ && !hdc_); - hdc_ = CreateEnhMetaFile(sibling, NULL, rect, NULL); - DCHECK(hdc_); - return hdc_ != NULL; + emf_ = GetEnhMetaFile(metafile_path.value().c_str()); + DCHECK(emf_); + return emf_ != NULL; } -bool Emf::CreateFileBackedDc(HDC sibling, const RECT* rect, - const FilePath& path) { +bool Emf::Init() { DCHECK(!emf_ && !hdc_); - DCHECK(!path.empty()); - hdc_ = CreateEnhMetaFile(sibling, path.value().c_str(), rect, NULL); + hdc_ = CreateEnhMetaFile(NULL, NULL, NULL, NULL); DCHECK(hdc_); return hdc_ != NULL; } -bool Emf::CreateFromFile(const FilePath& metafile_path) { +bool Emf::InitFromData(const void* src_buffer, uint32 src_buffer_size) { DCHECK(!emf_ && !hdc_); - emf_ = GetEnhMetaFile(metafile_path.value().c_str()); - DCHECK(emf_); + emf_ = SetEnhMetaFileBits(src_buffer_size, + reinterpret_cast<const BYTE*>(src_buffer)); return emf_ != NULL; } - bool Emf::FinishDocument() { DCHECK(!emf_ && hdc_); emf_ = CloseEnhMetaFile(hdc_); @@ -96,14 +94,6 @@ bool Emf::FinishDocument() { return emf_ != NULL; } -void Emf::CloseEmf() { - DCHECK(!hdc_); - if (emf_) { - DeleteEnhMetaFile(emf_); - emf_ = NULL; - } -} - bool Emf::Playback(HDC hdc, const RECT* rect) const { DCHECK(emf_ && !hdc_); RECT bounds; diff --git a/printing/emf_win.h b/printing/emf_win.h index f2ccffd..f4ee30f 100644 --- a/printing/emf_win.h +++ b/printing/emf_win.h @@ -29,10 +29,20 @@ class Emf : public NativeMetafile { class Enumerator; struct EnumerationContext; + // Generates a virtual HDC that will record every GDI commands and compile + // it in a EMF data stream. + Emf(); virtual ~Emf(); + // Generates a new metafile that will record every GDI command, and will + // be saved to |metafile_path|. + virtual bool InitToFile(const FilePath& metafile_path); + + // Initializes the Emf with the data in |metafile_path|. + virtual bool InitFromFile(const FilePath& metafile_path); + // NativeMetafile methods. - virtual bool Init() { return true; } + virtual bool Init(); virtual bool InitFromData(const void* src_buffer, uint32 src_buffer_size); virtual skia::PlatformDevice* StartPageForVectorCanvas( @@ -68,14 +78,6 @@ class Emf : public NativeMetafile { return hdc_; } - virtual bool CreateDc(HDC sibling, const RECT* rect); - virtual bool CreateFileBackedDc(HDC sibling, - const RECT* rect, - const FilePath& path); - virtual bool CreateFromFile(const FilePath& file_path); - - virtual void CloseEmf(); - virtual bool Playback(HDC hdc, const RECT* rect) const; virtual bool SafePlayback(HDC hdc) const; @@ -85,16 +87,7 @@ class Emf : public NativeMetafile { return emf_; } - protected: - Emf(); - private: - friend class NativeMetafileFactory; - FRIEND_TEST_ALL_PREFIXES(EmfTest, DC); - FRIEND_TEST_ALL_PREFIXES(EmfTest, FileBackedDC); - FRIEND_TEST_ALL_PREFIXES(EmfPrintingTest, Enumerate); - FRIEND_TEST_ALL_PREFIXES(EmfPrintingTest, PageBreak); - // 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 9f2899b..2492104 100644 --- a/printing/emf_win_unittest.cc +++ b/printing/emf_win_unittest.cc @@ -45,26 +45,26 @@ namespace printing { TEST(EmfTest, DC) { // Simplest use case. - printing::Emf emf; - RECT rect = {100, 100, 200, 200}; - HDC hdc = CreateCompatibleDC(NULL); - EXPECT_TRUE(hdc != NULL); - EXPECT_TRUE(emf.CreateDc(hdc, &rect)); - EXPECT_TRUE(emf.context() != NULL); - // In theory, you'd use the HDC with GDI functions here. - EXPECT_TRUE(emf.FinishDocument()); - uint32 size = emf.GetDataSize(); - EXPECT_EQ(size, EMF_HEADER_SIZE); + uint32 size; std::vector<BYTE> data; - EXPECT_TRUE(emf.GetData(&data)); - EXPECT_EQ(data.size(), size); - emf.CloseEmf(); - EXPECT_TRUE(DeleteDC(hdc)); + { + printing::Emf emf; + EXPECT_TRUE(emf.Init()); + EXPECT_TRUE(emf.context() != NULL); + // An empty EMF is invalid, so we put at least a rectangle in it. + ::Rectangle(emf.context(), 10, 10, 190, 190); + EXPECT_TRUE(emf.FinishDocument()); + size = emf.GetDataSize(); + EXPECT_GT(size, EMF_HEADER_SIZE); + EXPECT_TRUE(emf.GetData(&data)); + EXPECT_EQ(data.size(), size); + } // Playback the data. - hdc = CreateCompatibleDC(NULL); - EXPECT_TRUE(hdc); + printing::Emf emf; EXPECT_TRUE(emf.InitFromData(&data.front(), size)); + HDC hdc = CreateCompatibleDC(NULL); + EXPECT_TRUE(hdc); RECT output_rect = {0, 0, 10, 10}; EXPECT_TRUE(emf.Playback(hdc, &output_rect)); EXPECT_TRUE(DeleteDC(hdc)); @@ -128,28 +128,31 @@ TEST_F(EmfPrintingTest, PageBreak) { CreateDC(L"WINSPOOL", L"UnitTest Printer", NULL, NULL)); if (!dc.Get()) return; - printing::Emf emf; - EXPECT_TRUE(emf.CreateDc(dc.Get(), NULL)); - EXPECT_TRUE(emf.context() != NULL); - int pages = 3; - while (pages) { - EXPECT_TRUE(emf.StartPage(gfx::Size(), gfx::Point(), 1)); - ::Rectangle(emf.context(), 10, 10, 190, 190); - EXPECT_TRUE(emf.FinishPage()); - --pages; - } - EXPECT_TRUE(emf.FinishDocument()); - uint32 size = emf.GetDataSize(); + uint32 size; std::vector<BYTE> data; - EXPECT_TRUE(emf.GetData(&data)); - EXPECT_EQ(data.size(), size); - emf.CloseEmf(); + { + printing::Emf emf; + EXPECT_TRUE(emf.Init()); + EXPECT_TRUE(emf.context() != NULL); + int pages = 3; + while (pages) { + EXPECT_TRUE(emf.StartPage(gfx::Size(), gfx::Point(), 1)); + ::Rectangle(emf.context(), 10, 10, 190, 190); + EXPECT_TRUE(emf.FinishPage()); + --pages; + } + EXPECT_TRUE(emf.FinishDocument()); + size = emf.GetDataSize(); + EXPECT_TRUE(emf.GetData(&data)); + EXPECT_EQ(data.size(), size); + } // Playback the data. DOCINFO di = {0}; di.cbSize = sizeof(DOCINFO); di.lpszDocName = L"Test Job"; int job_id = ::StartDoc(dc.Get(), &di); + printing::Emf emf; EXPECT_TRUE(emf.InitFromData(&data.front(), size)); EXPECT_TRUE(emf.SafePlayback(dc.Get())); ::EndDoc(dc.Get()); @@ -162,41 +165,39 @@ TEST_F(EmfPrintingTest, PageBreak) { } } -TEST(EmfTest, FileBackedDC) { +TEST(EmfTest, FileBackedEmf) { // Simplest use case. - printing::Emf emf; - RECT rect = {100, 100, 200, 200}; - HDC hdc = CreateCompatibleDC(NULL); - EXPECT_TRUE(hdc != NULL); ScopedTempDir scratch_metafile_dir; ASSERT_TRUE(scratch_metafile_dir.CreateUniqueTempDir()); FilePath metafile_path; EXPECT_TRUE(file_util::CreateTemporaryFileInDir(scratch_metafile_dir.path(), &metafile_path)); - EXPECT_TRUE(emf.CreateFileBackedDc(hdc, &rect, metafile_path)); - EXPECT_TRUE(emf.context() != NULL); - // In theory, you'd use the HDC with GDI functions here. - EXPECT_TRUE(emf.FinishDocument()); - - uint32 size = emf.GetDataSize(); - EXPECT_EQ(size, EMF_HEADER_SIZE); + uint32 size; std::vector<BYTE> data; - EXPECT_TRUE(emf.GetData(&data)); - EXPECT_EQ(data.size(), size); - emf.CloseEmf(); + { + printing::Emf emf; + EXPECT_TRUE(emf.InitToFile(metafile_path)); + EXPECT_TRUE(emf.context() != NULL); + // An empty EMF is invalid, so we put at least a rectangle in it. + ::Rectangle(emf.context(), 10, 10, 190, 190); + EXPECT_TRUE(emf.FinishDocument()); + size = emf.GetDataSize(); + EXPECT_GT(size, EMF_HEADER_SIZE); + EXPECT_TRUE(emf.GetData(&data)); + EXPECT_EQ(data.size(), size); + } int64 file_size = 0; file_util::GetFileSize(metafile_path, &file_size); EXPECT_EQ(size, file_size); - EXPECT_TRUE(DeleteDC(hdc)); // Playback the data. - hdc = CreateCompatibleDC(NULL); + HDC hdc = CreateCompatibleDC(NULL); EXPECT_TRUE(hdc); - EXPECT_TRUE(emf.CreateFromFile(metafile_path)); + printing::Emf emf; + EXPECT_TRUE(emf.InitFromFile(metafile_path)); RECT output_rect = {0, 0, 10, 10}; EXPECT_TRUE(emf.Playback(hdc, &output_rect)); EXPECT_TRUE(DeleteDC(hdc)); - emf.CloseEmf(); } } // namespace printing diff --git a/printing/native_metafile.h b/printing/native_metafile.h index 742ee27..6623343 100644 --- a/printing/native_metafile.h +++ b/printing/native_metafile.h @@ -98,29 +98,6 @@ class NativeMetafile { virtual gfx::NativeDrawingContext context() const = 0; #if defined(OS_WIN) - // Generates a virtual HDC that will record every GDI commands and compile it - // in a EMF data stream. - // hdc is used to setup the default DPI and color settings. hdc is optional. - // rect specifies the dimensions (in .01-millimeter units) of the EMF. rect is - // optional. - virtual bool CreateDc(gfx::NativeDrawingContext sibling, - const RECT* rect) = 0; - - // Similar to the above method but the metafile is backed by a file. - virtual bool CreateFileBackedDc(gfx::NativeDrawingContext sibling, - const RECT* rect, - const FilePath& path) = 0; - - // TODO(maruel): CreateFromFile(). If ever used. Maybe users would like to - // have the ability to save web pages to an EMF file? Afterward, it is easy to - // convert to PDF or PS. - // Load a metafile fromdisk. - virtual bool CreateFromFile(const FilePath& metafile_path) = 0; - - // Closes the HDC created by CreateDc() and generates the compiled EMF - // data. - virtual void CloseEmf() = 0; - // "Plays" the EMF buffer in a HDC. It is the same effect as calling the // original GDI function that were called when recording the EMF. |rect| is in // "logical units" and is optional. If |rect| is NULL, the natural EMF bounds |