Skip to content

Commit

Permalink
Cleanup NativeMetafile (win) interface and EMF class.
Browse files Browse the repository at this point in the history
- Rename CreateDc to Init() and remove unused argument (all non-test calls were CreateDc(NULL, NULL).  [This matches cross platform interface.]
- Remove CreateFileBackedDc from the NativeMetafile interface and make InitToFile() in the EMF class.
- Remove CreateFromFile from the NativeMetafile interface and make it InitFromFile() in the EMF class.
- Move the CloseEmf method into the destructor, making the Emf class a use once class (matches actual use).

BUG=NONE
TEST=NONE

Review URL: http://codereview.chromium.org/6695013

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@78666 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
vandebo@chromium.org committed Mar 18, 2011
1 parent 55f2c51 commit acc1348
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 150 deletions.
4 changes: 2 additions & 2 deletions chrome/common/common_param_traits_unittest.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2006-2008 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.

Expand Down Expand Up @@ -241,7 +241,7 @@ TEST(IPCMessageTest, Metafile) {
printing::NativeMetafileFactory::CreateMetafile());
RECT test_rect = {0, 0, 100, 100};
// Create a metafile using the screen DC as a reference.
metafile->CreateDc(NULL, NULL);
metafile->Init();
metafile->Close();

IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL);
Expand Down
8 changes: 3 additions & 5 deletions chrome/renderer/print_web_view_helper_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void PrintWebViewHelper::PrintPage(const ViewMsg_PrintPage_Params& params,
// Each metafile contains a single page.
scoped_ptr<printing::NativeMetafile> metafile(
printing::NativeMetafileFactory::CreateMetafile());
metafile->CreateDc(NULL, NULL);
metafile->Init();
DCHECK(metafile->context());
skia::PlatformDevice::InitializeDC(metafile->context());

Expand Down Expand Up @@ -108,7 +108,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,
Expand Down Expand Up @@ -139,7 +138,7 @@ void PrintWebViewHelper::CreatePreviewDocument(
// http://code.google.com/p/chromium/issues/detail?id=62889
scoped_ptr<printing::NativeMetafile> metafile(
printing::NativeMetafileFactory::CreateMetafile());
metafile->CreateDc(NULL, NULL);
metafile->Init();
DCHECK(metafile->context());
skia::PlatformDevice::InitializeDC(metafile->context());

Expand Down Expand Up @@ -181,7 +180,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,
Expand Down Expand Up @@ -289,7 +287,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);
Expand Down
6 changes: 3 additions & 3 deletions chrome/service/cloud_print/print_system_win.cc
Original file line number Diff line number Diff line change
@@ -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.

Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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 !=
Expand Down
15 changes: 7 additions & 8 deletions chrome/service/service_utility_process_host.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,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(
Expand Down Expand Up @@ -204,15 +203,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::CreateMetafile());
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)
}
Expand Down
6 changes: 3 additions & 3 deletions chrome/service/service_utility_process_host.h
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -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;
Expand All @@ -35,6 +34,7 @@ class Rect;
} // namespace gfx

namespace printing {
class Emf;
struct PageRange;
struct PrinterCapsAndDefaults;
} // namespace printing
Expand All @@ -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() {}
Expand Down
20 changes: 9 additions & 11 deletions chrome/utility/utility_thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,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 {
Expand Down Expand Up @@ -257,37 +256,36 @@ bool UtilityThread::RenderPDFToWinMetafile(
if (!get_info_proc(&buffer.front(), buffer.size(), &total_page_count, NULL))
return false;

scoped_ptr<printing::NativeMetafile> metafile(
printing::NativeMetafileFactory::CreateMetafile());
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;
for (iter = page_ranges.begin(); iter != page_ranges.end(); ++iter) {
for (int page_number = iter->from; page_number <= iter->to; ++page_number) {
if (page_number >= total_page_count)
break;
metafile->StartPage();
metafile.StartPage();
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->Close();
metafile.Close();
return ret;
}
#endif // defined(OS_WIN)
Expand Down
2 changes: 1 addition & 1 deletion content/plugin/webplugin_delegate_stub.cc
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ void WebPluginDelegateStub::OnPrint(base::SharedMemoryHandle* shared_memory,
#if defined(OS_WIN)
scoped_ptr<printing::NativeMetafile> metafile(
printing::NativeMetafileFactory::CreateMetafile());
if (!metafile->CreateDc(NULL, NULL)) {
if (!metafile->Init()) {
NOTREACHED();
return;
}
Expand Down
42 changes: 16 additions & 26 deletions printing/emf_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,41 +50,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::Close() {
DCHECK(!emf_ && hdc_);
emf_ = CloseEnhMetaFile(hdc_);
Expand All @@ -93,14 +91,6 @@ bool Emf::Close() {
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;
Expand Down
29 changes: 11 additions & 18 deletions printing/emf_win.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,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 bool StartPage();
Expand All @@ -57,14 +67,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;

Expand All @@ -74,16 +76,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,
Expand Down
Loading

0 comments on commit acc1348

Please sign in to comment.