Skip to content

Commit

Permalink
Connect the right metafiles for print preview on Linux and Windows.
Browse files Browse the repository at this point in the history
+ Remove the NativeMetafileFactory since we can't just use preview flag.
+ Update each Metafile constructor site to use PreviewMetafile or NativeMetafileImpl.
+ Fix misc. problems blocking pdf generation on Windows.
+ Rename the metafile interface.

BUG=NONE
TEST=NONE

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@81161 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
vandebo@chromium.org committed Apr 11, 2011
1 parent 03e204f commit 7d74899
Show file tree
Hide file tree
Showing 43 changed files with 308 additions and 416 deletions.
5 changes: 3 additions & 2 deletions chrome/browser/printing/print_dialog_gtk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "base/utf_string_conversions.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_window.h"
#include "printing/metafile.h"
#include "printing/print_settings_initializer_gtk.h"

// static
Expand Down Expand Up @@ -85,7 +86,7 @@ void PrintDialogGtk::ShowDialog(
gtk_widget_show(dialog_);
}

void PrintDialogGtk::PrintDocument(const NativeMetafile* metafile,
void PrintDialogGtk::PrintDocument(const printing::Metafile* metafile,
const string16& document_name) {
// This runs on the print worker thread, does not block the UI thread.
DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI));
Expand Down Expand Up @@ -164,7 +165,7 @@ void PrintDialogGtk::OnResponse(GtkWidget* dialog, int response_id) {
}
}

void PrintDialogGtk::SaveDocumentToDisk(const NativeMetafile* metafile,
void PrintDialogGtk::SaveDocumentToDisk(const printing::Metafile* metafile,
const string16& document_name) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));

Expand Down
10 changes: 6 additions & 4 deletions chrome/browser/printing/print_dialog_gtk.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "content/browser/browser_thread.h"
#include "printing/native_metafile.h"
#include "printing/print_dialog_gtk_interface.h"
#include "printing/printing_context_cairo.h"
#include "ui/base/gtk/gtk_signal.h"
Expand All @@ -22,7 +21,10 @@ namespace base {
class WaitableEvent;
}

using printing::NativeMetafile;
namespace printing {
class Metafile;
}

using printing::PrintingContextCairo;

// Needs to be freed on the UI thread to clean up its GTK members variables.
Expand All @@ -38,7 +40,7 @@ class PrintDialogGtk
// printing::PrintDialogGtkInterface implementation.
virtual void ShowDialog(
PrintingContextCairo::PrintSettingsCallback* callback);
virtual void PrintDocument(const NativeMetafile* metafile,
virtual void PrintDocument(const printing::Metafile* metafile,
const string16& document_name);
virtual void AddRefToDialog();
virtual void ReleaseDialog();
Expand All @@ -54,7 +56,7 @@ class PrintDialogGtk
CHROMEGTK_CALLBACK_1(PrintDialogGtk, void, OnResponse, int);

// Saves data in |metafile| to disk for document named |document_name|.
void SaveDocumentToDisk(const NativeMetafile* metafile,
void SaveDocumentToDisk(const printing::Metafile* metafile,
const string16& document_name);

// Prints document named |document_name|.
Expand Down
10 changes: 4 additions & 6 deletions chrome/browser/printing/print_view_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
#include "content/common/notification_details.h"
#include "content/common/notification_source.h"
#include "grit/generated_resources.h"
#include "printing/native_metafile.h"
#include "printing/native_metafile_factory.h"
#include "printing/metafile.h"
#include "printing/metafile_impl.h"
#include "printing/printed_document.h"
#include "ui/base/l10n/l10n_util.h"

Expand Down Expand Up @@ -146,11 +146,9 @@ void PrintViewManager::OnDidPrintPage(
}
}

scoped_ptr<NativeMetafile> metafile;
scoped_ptr<Metafile> metafile(new NativeMetafile);
if (metafile_must_be_valid) {
metafile.reset(NativeMetafileFactory::CreateFromData(shared_buf.memory(),
params.data_size));
if (!metafile.get()) {
if (!metafile->InitFromData(shared_buf.memory(), params.data_size)) {
NOTREACHED() << "Invalid metafile header";
tab_contents()->Stop();
return;
Expand Down
18 changes: 6 additions & 12 deletions chrome/browser/ui/webui/print_preview_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
#include "content/browser/renderer_host/render_view_host.h"
#include "content/browser/tab_contents/tab_contents.h"
#include "printing/backend/print_backend.h"
#include "printing/native_metafile_factory.h"
#include "printing/native_metafile.h"
#include "printing/metafile.h"
#include "printing/metafile_impl.h"
#include "printing/print_job_constants.h"

namespace {
Expand Down Expand Up @@ -121,7 +121,7 @@ class EnumeratePrintersTaskProxy
class PrintToPdfTask : public Task {
public:
// Takes ownership of |metafile|.
PrintToPdfTask(printing::NativeMetafile* metafile, const FilePath& path)
PrintToPdfTask(printing::Metafile* metafile, const FilePath& path)
: metafile_(metafile), path_(path) {
}

Expand All @@ -134,7 +134,7 @@ class PrintToPdfTask : public Task {

private:
// The metafile holding the PDF data.
scoped_ptr<printing::NativeMetafile> metafile_;
scoped_ptr<printing::Metafile> metafile_;

// The absolute path where the file will be saved.
FilePath path_;
Expand Down Expand Up @@ -262,21 +262,15 @@ void PrintPreviewHandler::SelectFile() {

void PrintPreviewHandler::FileSelected(const FilePath& path,
int index, void* params) {
#if defined(OS_POSIX)
PrintPreviewUIHTMLSource::PrintPreviewData data;
PrintPreviewUI* pp_ui = reinterpret_cast<PrintPreviewUI*>(web_ui_);
pp_ui->html_source()->GetPrintPreviewData(&data);
DCHECK(data.first != NULL);
DCHECK(data.second > 0);

printing::NativeMetafile* metafile =
printing::NativeMetafileFactory::CreateFromData(data.first->memory(),
data.second);
metafile->FinishDocument();
printing::PreviewMetafile* metafile = new printing::PreviewMetafile;
metafile->InitFromData(data.first->memory(), data.second);

PrintToPdfTask* task = new PrintToPdfTask(metafile, path);
BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, task);
#else
NOTIMPLEMENTED();
#endif
}
5 changes: 0 additions & 5 deletions chrome/common/common_param_traits_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/rect.h"

#if defined(OS_WIN)
#include "printing/native_metafile_factory.h"
#include "printing/native_metafile.h"
#endif

// Tests that serialize/deserialize correctly understand each other
TEST(IPCMessageTest, Serialize) {
const char* serialize_cases[] = {
Expand Down
11 changes: 4 additions & 7 deletions chrome/renderer/mock_printer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
#include "base/shared_memory.h"
#include "chrome/common/print_messages.h"
#include "ipc/ipc_message_utils.h"
#include "printing/native_metafile_factory.h"
#include "printing/native_metafile.h"
#include "printing/metafile_impl.h"
#include "printing/units.h"
#include "testing/gtest/include/gtest/gtest.h"

Expand Down Expand Up @@ -137,11 +136,9 @@ void MockPrinter::PrintPage(const PrintHostMsg_DidPrintPage_Params& params) {
base::SharedMemory metafile_data(params.metafile_data_handle, true);
#endif
metafile_data.Map(params.data_size);
scoped_ptr<printing::NativeMetafile> metafile(
printing::NativeMetafileFactory::CreateFromData(metafile_data.memory(),
params.data_size));
CHECK(metafile.get());
printing::Image image(*metafile);
printing::NativeMetafile metafile;
metafile.InitFromData(metafile_data.memory(), params.data_size);
printing::Image image(metafile);
MockPrinterPage* page_data = new MockPrinterPage(metafile_data.memory(),
params.data_size,
image);
Expand Down
3 changes: 2 additions & 1 deletion chrome/renderer/print_web_view_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "content/common/view_messages.h"
#include "content/renderer/render_view.h"
#include "grit/generated_resources.h"
#include "printing/metafile.h"
#include "printing/units.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebConsoleMessage.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
Expand Down Expand Up @@ -636,7 +637,7 @@ void PrintWebViewHelper::RenderPagesForPreview(WebKit::WebFrame* frame,

#if defined(OS_POSIX)
bool PrintWebViewHelper::CopyMetafileDataToSharedMem(
printing::NativeMetafile* metafile,
printing::Metafile* metafile,
base::SharedMemoryHandle* shared_mem_handle) {
uint32 buf_size = metafile->GetDataSize();
base::SharedMemoryHandle mem_handle;
Expand Down
12 changes: 6 additions & 6 deletions chrome/renderer/print_web_view_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "base/time.h"
#include "content/renderer/render_view_observer.h"
#include "content/renderer/render_view_observer_tracker.h"
#include "printing/native_metafile.h"
#include "printing/metafile.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrameClient.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebViewClient.h"
#include "ui/gfx/size.h"
Expand Down Expand Up @@ -152,7 +152,7 @@ class PrintWebViewHelper : public RenderViewObserver ,
void PrintPageInternal(const PrintMsg_PrintPage_Params& params,
const gfx::Size& canvas_size,
WebKit::WebFrame* frame,
printing::NativeMetafile* metafile,
printing::Metafile* metafile,
scoped_ptr<skia::VectorCanvas>* canvas);
#else
void PrintPageInternal(const PrintMsg_PrintPage_Params& params,
Expand All @@ -177,25 +177,25 @@ class PrintWebViewHelper : public RenderViewObserver ,
#if defined(OS_WIN)
void RenderPage(const PrintMsg_Print_Params& params, float* scale_factor,
int page_number, WebKit::WebFrame* frame,
scoped_ptr<printing::NativeMetafile>* metafile);
scoped_ptr<printing::Metafile>* metafile);
#elif defined(OS_MACOSX)
void RenderPage(const gfx::Size& page_size, const gfx::Point& content_origin,
const float& scale_factor, int page_number,
WebKit::WebFrame* frame, printing::NativeMetafile* metafile);
WebKit::WebFrame* frame, printing::Metafile* metafile);
#elif defined(OS_POSIX)
bool RenderPages(const PrintMsg_PrintPages_Params& params,
WebKit::WebFrame* frame,
WebKit::WebNode* node,
bool send_expected_page_count,
int* page_count,
printing::NativeMetafile* metafile);
printing::Metafile* metafile);
#endif // defined(OS_WIN)

// Helper methods -----------------------------------------------------------

bool CopyAndPrint(WebKit::WebFrame* web_frame);

bool CopyMetafileDataToSharedMem(printing::NativeMetafile* metafile,
bool CopyMetafileDataToSharedMem(printing::Metafile* metafile,
base::SharedMemoryHandle* shared_mem_handle);

static void GetPageSizeAndMarginsInPoints(
Expand Down
42 changes: 20 additions & 22 deletions chrome/renderer/print_web_view_helper_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
#include "base/memory/scoped_ptr.h"
#include "chrome/common/print_messages.h"
#include "content/common/view_messages.h"
#include "printing/native_metafile_factory.h"
#include "printing/native_metafile_skia_wrapper.h"
#include "printing/native_metafile.h"
#include "printing/metafile.h"
#include "printing/metafile_impl.h"
#include "printing/metafile_skia_wrapper.h"
#include "skia/ext/vector_canvas.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
#include "ui/gfx/point.h"
Expand All @@ -26,25 +26,24 @@ using WebKit::WebNode;
void PrintWebViewHelper::CreatePreviewDocument(
const PrintMsg_PrintPages_Params& params, WebKit::WebFrame* frame,
WebKit::WebNode* node) {
// We only can use PDF in the renderer because Cairo needs to create a
// temporary file for a PostScript surface.
scoped_ptr<printing::NativeMetafile> metafile(
printing::NativeMetafileFactory::Create());
int page_count = 0;
printing::PreviewMetafile metafile;
if (!metafile.Init())
return;

if (!RenderPages(params, frame, node, false, &page_count, metafile.get()))
if (!RenderPages(params, frame, node, false, &page_count, &metafile))
return;

// Get the size of the resulting metafile.
uint32 buf_size = metafile->GetDataSize();
uint32 buf_size = metafile.GetDataSize();
DCHECK_GT(buf_size, 0u);

PrintHostMsg_DidPreviewDocument_Params preview_params;
preview_params.document_cookie = params.params.document_cookie;
preview_params.expected_pages_count = page_count;
preview_params.data_size = buf_size;

if (!CopyMetafileDataToSharedMem(metafile.get(),
if (!CopyMetafileDataToSharedMem(&metafile,
&(preview_params.metafile_data_handle))) {
preview_params.expected_pages_count = 0;
preview_params.data_size = 0;
Expand All @@ -55,10 +54,6 @@ void PrintWebViewHelper::CreatePreviewDocument(
void PrintWebViewHelper::PrintPages(const PrintMsg_PrintPages_Params& params,
WebFrame* frame,
WebNode* node) {
// We only can use PDF in the renderer because Cairo needs to create a
// temporary file for a PostScript surface.
scoped_ptr<printing::NativeMetafile> metafile(
printing::NativeMetafileFactory::Create());
int page_count = 0;
bool send_expected_page_count =
#if defined(OS_CHROMEOS)
Expand All @@ -67,13 +62,17 @@ void PrintWebViewHelper::PrintPages(const PrintMsg_PrintPages_Params& params,
true;
#endif // defined(OS_CHROMEOS)

printing::NativeMetafile metafile;
if (!metafile.Init())
return;

if (!RenderPages(params, frame, node, send_expected_page_count, &page_count,
metafile.get())) {
&metafile)) {
return;
}

// Get the size of the resulting metafile.
uint32 buf_size = metafile->GetDataSize();
uint32 buf_size = metafile.GetDataSize();
DCHECK_GT(buf_size, 0u);

#if defined(OS_CHROMEOS)
Expand All @@ -85,7 +84,7 @@ void PrintWebViewHelper::PrintPages(const PrintMsg_PrintPages_Params& params,
&sequence_number))) {
return;
}
if (!metafile->SaveToFD(fd))
if (!metafile.SaveToFD(fd))
return;

// Tell the browser we've finished writing the file.
Expand All @@ -112,7 +111,7 @@ void PrintWebViewHelper::PrintPages(const PrintMsg_PrintPages_Params& params,
NOTREACHED() << "Map failed";
return;
}
metafile->GetData(shared_buf.memory(), buf_size);
metafile.GetData(shared_buf.memory(), buf_size);
printed_page_params.data_size = buf_size;
shared_buf.GiveToProcess(base::GetCurrentProcessHandle(),
&(printed_page_params.metafile_data_handle));
Expand Down Expand Up @@ -149,7 +148,7 @@ bool PrintWebViewHelper::RenderPages(const PrintMsg_PrintPages_Params& params,
WebKit::WebNode* node,
bool send_expected_page_count,
int* page_count,
printing::NativeMetafile* metafile) {
printing::Metafile* metafile) {
PrintMsg_Print_Params printParams = params.params;
scoped_ptr<skia::VectorCanvas> canvas;

Expand Down Expand Up @@ -197,7 +196,7 @@ void PrintWebViewHelper::PrintPageInternal(
const PrintMsg_PrintPage_Params& params,
const gfx::Size& canvas_size,
WebFrame* frame,
printing::NativeMetafile* metafile,
printing::Metafile* metafile,
scoped_ptr<skia::VectorCanvas>* canvas) {
double content_width_in_points;
double content_height_in_points;
Expand Down Expand Up @@ -228,8 +227,7 @@ void PrintWebViewHelper::PrintPageInternal(
return;

canvas->reset(new skia::VectorCanvas(device));
printing::NativeMetafileSkiaWrapper::SetMetafileOnCanvas(canvas->get(),
metafile);
printing::MetafileSkiaWrapper::SetMetafileOnCanvas(canvas->get(), metafile);
frame->printPage(params.page_number, canvas->get());

// TODO(myhuang): We should handle transformation for paper margins.
Expand Down
Loading

0 comments on commit 7d74899

Please sign in to comment.