diff options
author | thestig@chromium.org <thestig@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-02-22 20:34:15 +0000 |
---|---|---|
committer | thestig@chromium.org <thestig@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-02-22 20:34:15 +0000 |
commit | 8eb26a975613aab90f2eed7730d341fa6f6fe588 (patch) | |
tree | 8c3c5b6d3c9560c60cd0376c0b49f6163d1a5f10 /chrome/renderer | |
parent | bca14bc4a3d8d82e817f01d0bb105aaa6e3c081a (diff) | |
download | chromium_src-8eb26a975613aab90f2eed7730d341fa6f6fe588.zip chromium_src-8eb26a975613aab90f2eed7730d341fa6f6fe588.tar.gz chromium_src-8eb26a975613aab90f2eed7730d341fa6f6fe588.tar.bz2 |
Print Preview: Generate previews on Linux.
BUG=none
TEST=Run with --enable-print-preview on Linux.
Review URL: http://codereview.chromium.org/6546033
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@75627 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/renderer')
-rw-r--r-- | chrome/renderer/print_web_view_helper.cc | 15 | ||||
-rw-r--r-- | chrome/renderer/print_web_view_helper.h | 29 | ||||
-rw-r--r-- | chrome/renderer/print_web_view_helper_linux.cc | 133 |
3 files changed, 105 insertions, 72 deletions
diff --git a/chrome/renderer/print_web_view_helper.cc b/chrome/renderer/print_web_view_helper.cc index 4c059af..518529d 100644 --- a/chrome/renderer/print_web_view_helper.cc +++ b/chrome/renderer/print_web_view_helper.cc @@ -572,18 +572,7 @@ void PrintWebViewHelper::RenderPagesForPreview(WebFrame *frame) { CreatePreviewDocument(print_settings, frame); } -#if defined(OS_LINUX) -void PrintWebViewHelper::CreatePreviewDocument( - const ViewMsg_PrintPages_Params& params, WebFrame* frame) { - ViewHostMsg_DidPreviewDocument_Params preview_params; - // TODO(kmadhusu): Implement this function for linux. - preview_params.document_cookie = params.params.document_cookie; - render_view()->Send(new ViewHostMsg_PagesReadyForPreview( - render_view()->routing_id(), preview_params)); -} -#endif - -#if defined(OS_MACOSX) +#if defined(OS_POSIX) bool PrintWebViewHelper::CopyMetafileDataToSharedMem( printing::NativeMetafile* metafile, base::SharedMemoryHandle* shared_mem_handle) { @@ -604,4 +593,4 @@ bool PrintWebViewHelper::CopyMetafileDataToSharedMem( NOTREACHED(); return false; } -#endif +#endif // defined(OS_POSIX) diff --git a/chrome/renderer/print_web_view_helper.h b/chrome/renderer/print_web_view_helper.h index 5682879..5c7afcc 100644 --- a/chrome/renderer/print_web_view_helper.h +++ b/chrome/renderer/print_web_view_helper.h @@ -7,6 +7,7 @@ #pragma once #include "base/scoped_ptr.h" +#include "base/shared_memory.h" #include "base/time.h" #include "chrome/renderer/render_view_observer.h" #include "printing/native_metafile.h" @@ -14,10 +15,6 @@ #include "third_party/WebKit/Source/WebKit/chromium/public/WebViewClient.h" #include "ui/gfx/size.h" -#if defined(OS_MACOSX) || defined(OS_WIN) -#include "base/shared_memory.h" -#endif // defined(OS_MACOSX) || defined(OS_WIN) - namespace gfx { class Size; } @@ -178,20 +175,26 @@ class PrintWebViewHelper : public RenderViewObserver , // valid metafile data handle. void CreatePreviewDocument(const ViewMsg_PrintPages_Params& params, WebKit::WebFrame* frame); -#if 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); -#elif defined(OS_WIN) + + // Platform specific helper function for rendering page(s) to |metafile|. +#if defined(OS_WIN) void RenderPage(const ViewMsg_Print_Params& params, float* scale_factor, int page_number, WebKit::WebFrame* frame, scoped_ptr<printing::NativeMetafile>* metafile); -#endif +#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); +#elif defined(OS_POSIX) + bool RenderPages(const ViewMsg_PrintPages_Params& params, + WebKit::WebFrame* frame, + bool send_expected_page_count, + int* page_count, + printing::NativeMetafile* metafile); +#endif // defined(OS_WIN) -#if defined(OS_MACOSX) || defined(OS_WIN) bool CopyMetafileDataToSharedMem(printing::NativeMetafile* metafile, - base::SharedMemoryHandle* shared_mem_handle); -#endif + base::SharedMemoryHandle* shared_mem_handle); WebKit::WebView* print_web_view_; scoped_ptr<ViewMsg_PrintPages_Params> print_pages_params_; diff --git a/chrome/renderer/print_web_view_helper_linux.cc b/chrome/renderer/print_web_view_helper_linux.cc index c7c9ce7..0a4b5d5 100644 --- a/chrome/renderer/print_web_view_helper_linux.cc +++ b/chrome/renderer/print_web_view_helper_linux.cc @@ -11,69 +11,57 @@ #include "printing/native_metafile.h" #include "skia/ext/vector_canvas.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" -#include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h" #if !defined(OS_CHROMEOS) #include "base/process_util.h" #endif // !defined(OS_CHROMEOS) -using printing::NativeMetafile; using WebKit::WebFrame; using WebKit::WebNode; -using WebKit::WebSize; -void PrintWebViewHelper::PrintPages(const ViewMsg_PrintPages_Params& params, - WebFrame* frame, - WebNode* node) { +void PrintWebViewHelper::CreatePreviewDocument( + const ViewMsg_PrintPages_Params& params, WebFrame* frame) { // We only can use PDF in the renderer because Cairo needs to create a // temporary file for a PostScript surface. printing::NativeMetafile metafile(printing::NativeMetafile::PDF); - int page_count; - skia::VectorCanvas* canvas = NULL; + int page_count = 0; - { - // Hack - when |prep_frame_view| goes out of scope, PrintEnd() gets called. - // Doing this before closing |metafile| below ensures - // webkit::ppapi::PluginInstance::PrintEnd() has a valid canvas/metafile to - // save the final output to. See pepper_plugin_instance.cc for the whole - // story. - ViewMsg_Print_Params printParams = params.params; - PrepareFrameAndViewForPrint prep_frame_view(printParams, - frame, - node, - frame->view()); - page_count = prep_frame_view.GetExpectedPageCount(); -#if !defined(OS_CHROMEOS) - Send(new ViewHostMsg_DidGetPrintedPagesCount(routing_id(), - printParams.document_cookie, - page_count)); -#endif // !defined(OS_CHROMEOS) + if (!RenderPages(params, frame, false, &page_count, &metafile)) + return; - if (!page_count) - return; + // Get the size of the resulting metafile. + uint32 buf_size = metafile.GetDataSize(); + DCHECK_GT(buf_size, 0u); - metafile.Init(); + ViewHostMsg_DidPreviewDocument_Params preview_params; + preview_params.data_size = buf_size; + preview_params.document_cookie = params.params.document_cookie; - ViewMsg_PrintPage_Params page_params; - page_params.params = printParams; - const gfx::Size& canvas_size = prep_frame_view.GetPrintCanvasSize(); - if (params.pages.empty()) { - for (int i = 0; i < page_count; ++i) { - page_params.page_number = i; - delete canvas; - PrintPage(page_params, canvas_size, frame, &metafile, &canvas); - } - } else { - for (size_t i = 0; i < params.pages.size(); ++i) { - page_params.page_number = params.pages[i]; - delete canvas; - PrintPage(page_params, canvas_size, frame, &metafile, &canvas); - } - } + if (!CopyMetafileDataToSharedMem(&metafile, + &(preview_params.metafile_data_handle))) { + preview_params.data_size = 0; } + Send(new ViewHostMsg_PagesReadyForPreview(routing_id(), preview_params)); +} - delete canvas; - metafile.Close(); +void PrintWebViewHelper::PrintPages(const ViewMsg_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. + printing::NativeMetafile metafile(printing::NativeMetafile::PDF); + int page_count = 0; + bool send_expected_page_count = +#if defined(OS_CHROMEOS) + false; +#else + true; +#endif // defined(OS_CHROMEOS) + + if (!RenderPages(params, frame, send_expected_page_count, &page_count, + &metafile)) { + return; + } // Get the size of the resulting metafile. uint32 buf_size = metafile.GetDataSize(); @@ -141,6 +129,59 @@ void PrintWebViewHelper::PrintPages(const ViewMsg_PrintPages_Params& params, #endif // defined(OS_CHROMEOS) } +bool PrintWebViewHelper::RenderPages(const ViewMsg_PrintPages_Params& params, + WebFrame* frame, + bool send_expected_page_count, + int* page_count, + printing::NativeMetafile* metafile) { + ViewMsg_Print_Params printParams = params.params; + skia::VectorCanvas* canvas = NULL; + + { + // Hack - when |prep_frame_view| goes out of scope, PrintEnd() gets called. + // Doing this before closing |metafile| below ensures + // webkit::ppapi::PluginInstance::PrintEnd() has a valid canvas/metafile to + // save the final output to. See pepper_plugin_instance.cc for the whole + // story. + PrepareFrameAndViewForPrint prep_frame_view(printParams, + frame, + NULL, + frame->view()); + *page_count = prep_frame_view.GetExpectedPageCount(); + if (send_expected_page_count) { + Send(new ViewHostMsg_DidGetPrintedPagesCount(routing_id(), + printParams.document_cookie, + *page_count)); + } + if (!*page_count) + return false; + + metafile->Init(); + + ViewMsg_PrintPage_Params page_params; + page_params.params = printParams; + const gfx::Size& canvas_size = prep_frame_view.GetPrintCanvasSize(); + if (params.pages.empty()) { + for (int i = 0; i < *page_count; ++i) { + page_params.page_number = i; + delete canvas; + PrintPage(page_params, canvas_size, frame, metafile, &canvas); + } + } else { + for (size_t i = 0; i < params.pages.size(); ++i) { + page_params.page_number = params.pages[i]; + delete canvas; + PrintPage(page_params, canvas_size, frame, metafile, &canvas); + } + } + } + + delete canvas; + metafile->Close(); + + return true; +} + void PrintWebViewHelper::PrintPage(const ViewMsg_PrintPage_Params& params, const gfx::Size& canvas_size, WebFrame* frame, |