diff options
author | tc@google.com <tc@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-08-05 20:26:41 +0000 |
---|---|---|
committer | tc@google.com <tc@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-08-05 20:26:41 +0000 |
commit | 6862ac6cbec1c9a3d7e332b3e8c696c9a3d172d6 (patch) | |
tree | 7b810a3eb33cb63e404c8f4d67e6f3676168b13b /chrome/renderer/print_web_view_helper.cc | |
parent | e7ef7150c893a1ff2fe4197ce9d76ccb21001caa (diff) | |
download | chromium_src-6862ac6cbec1c9a3d7e332b3e8c696c9a3d172d6.zip chromium_src-6862ac6cbec1c9a3d7e332b3e8c696c9a3d172d6.tar.gz chromium_src-6862ac6cbec1c9a3d7e332b3e8c696c9a3d172d6.tar.bz2 |
Original change by Min-Yu Huang <minyu.huang@gmail.com> in
http://codereview.chromium.org/160347
This is the very preliminary implementation to support printing on Linux and it
has not been finished yet. For each page to be printed, we convert rendering
actions on canvas into cairo APIs and generate a PS/PDF file.
chrome/chrome.gyp:
Include our newly added and renamed files.
chrome/browser/browser.h:
chrome/browser/browser.cc:
Allow the user print the web page by hitting ctrl-p.
chrome/browser/gtk/standard_menus.cc:
Show "Print" in the menu.
chrome/renderer/print_web_view_helper.cc:
chrome/renderer/print_web_view_helper.h:
chrome/renderer/print_web_view_helper_mac.cc
chrome/renderer/print_web_view_helper_win.cc
Move the class PrepareFrameAndViewForPrint to the header file and move
platform dependent parts to their corresponding files.
chrome/renderer/print_web_view_helper_linux.cc:
Hard-coded parameters for printing. Only print the first page now.
skia/ext/vector_canvas.cc:
skia/ext/vector_canvas.h:
skia/ext/vector_canvas_linux.cc:
skia/ext/vector_canvas_win.cc:
Move platform dependent parts to their corresponding files.
skia/ext/vector_platform_device.h:
skia/ext/vector_platform_device_linux.cc:
skia/ext/vector_platform_device_linux.h
We translate skia APIs into Cairo APIs here. A PDF file is also created and
saved to the disk at this moment for testing purpose (you have to run chrome
without the sandbox to save the file). There are still lots of bugs.
skia/skia.gyp:
Include our newly added files when compiling skia package on Linux.
BUG=9847
Review URL: http://codereview.chromium.org/160673
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@22522 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/renderer/print_web_view_helper.cc')
-rw-r--r-- | chrome/renderer/print_web_view_helper.cc | 337 |
1 files changed, 33 insertions, 304 deletions
diff --git a/chrome/renderer/print_web_view_helper.cc b/chrome/renderer/print_web_view_helper.cc index 1e9a0fe..a0803ba 100644 --- a/chrome/renderer/print_web_view_helper.cc +++ b/chrome/renderer/print_web_view_helper.cc @@ -6,220 +6,64 @@ #include "app/l10n_util.h" #include "base/logging.h" -#include "base/gfx/size.h" #include "chrome/common/render_messages.h" #include "chrome/renderer/render_view.h" #include "grit/generated_resources.h" -#include "printing/native_metafile.h" #include "printing/units.h" -#include "webkit/api/public/WebConsoleMessage.h" #include "webkit/api/public/WebRect.h" #include "webkit/api/public/WebScreenInfo.h" #include "webkit/api/public/WebSize.h" -#include "webkit/api/public/WebURL.h" #include "webkit/api/public/WebURLRequest.h" #include "webkit/glue/webframe.h" -#if defined(OS_WIN) -#include "skia/ext/vector_canvas.h" -#endif - -using WebKit::WebConsoleMessage; using WebKit::WebRect; using WebKit::WebScreenInfo; using WebKit::WebString; using WebKit::WebURLRequest; -namespace { - -const int kMinSecondsToIgnoreJavascriptInitiatedPrint = 2; -const int kMaxSecondsToIgnoreJavascriptInitiatedPrint = 2 * 60; // 2 Minutes. - -// Class that calls the Begin and End print functions on the frame and changes -// the size of the view temporarily to support full page printing.. -// Do not serve any events in the time between construction and destruction of -// this class because it will cause flicker. -class PrepareFrameAndViewForPrint { - public: - PrepareFrameAndViewForPrint(const ViewMsg_Print_Params& print_params, - WebFrame* frame, - WebView* web_view) - : frame_(frame), - web_view_(web_view), - expected_pages_count_(0) { - print_canvas_size_.set_width( - printing::ConvertUnit(print_params.printable_size.width(), - static_cast<int>(print_params.dpi), - print_params.desired_dpi)); - print_canvas_size_.set_height( - printing::ConvertUnit(print_params.printable_size.height(), - static_cast<int>(print_params.dpi), - print_params.desired_dpi)); - - // Layout page according to printer page size. Since WebKit shrinks the - // size of the page automatically (from 125% to 200%) we trick it to - // think the page is 125% larger so the size of the page is correct for - // minimum (default) scaling. - // This is important for sites that try to fill the page. - gfx::Size print_layout_size(print_canvas_size_); - print_layout_size.set_height(static_cast<int>( - static_cast<double>(print_layout_size.height()) * 1.25)); +PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( + const ViewMsg_Print_Params& print_params, + WebFrame* frame, + WebView* web_view) + : frame_(frame), web_view_(web_view), expected_pages_count_(0) { - prev_view_size_ = web_view->size(); - - web_view->resize(print_layout_size); - - expected_pages_count_ = frame->PrintBegin(print_canvas_size_); - } - - int GetExpectedPageCount() const { - return expected_pages_count_; - } - - gfx::Size GetPrintCanvasSize() const { - return print_canvas_size_; - } - - ~PrepareFrameAndViewForPrint() { - frame_->PrintEnd(); - web_view_->resize(prev_view_size_); - } + print_canvas_size_.set_width( + printing::ConvertUnit(print_params.printable_size.width(), + static_cast<int>(print_params.dpi), + print_params.desired_dpi)); - private: - WebFrame* frame_; - WebView* web_view_; - gfx::Size print_canvas_size_; - gfx::Size prev_view_size_; - int expected_pages_count_; + print_canvas_size_.set_height( + printing::ConvertUnit(print_params.printable_size.height(), + static_cast<int>(print_params.dpi), + print_params.desired_dpi)); - DISALLOW_COPY_AND_ASSIGN(PrepareFrameAndViewForPrint); -}; - -} // namespace - -void PrintWebViewHelper::Print(WebFrame* frame, bool script_initiated) { -#if defined(OS_WIN) - - // If still not finished with earlier print request simply ignore. - if (IsPrinting()) - return; - - // Check if there is script repeatedly trying to print and ignore it if too - // frequent. We use exponential wait time so for a page that calls print() in - // a loop the user will need to cancel the print dialog after 2 seconds, 4 - // seconds, 8, ... up to the maximum of 2 minutes. - // This gives the user time to navigate from the page. - if (script_initiated && (user_cancelled_scripted_print_count_ > 0)) { - base::TimeDelta diff = base::Time::Now() - last_cancelled_script_print_; - int min_wait_seconds = std::min( - kMinSecondsToIgnoreJavascriptInitiatedPrint << - (user_cancelled_scripted_print_count_ - 1), - kMaxSecondsToIgnoreJavascriptInitiatedPrint); - if (diff.InSeconds() < min_wait_seconds) { - WebString message(WebString::fromUTF8( - "Ignoring too frequent calls to print().")); - frame->AddMessageToConsole(WebConsoleMessage( - WebConsoleMessage::LevelWarning, - message)); - return; - } - } - - // Retrieve the default print settings to calculate the expected number of - // pages. - ViewMsg_Print_Params default_settings; - bool user_cancelled_print = false; - - IPC::SyncMessage* msg = - new ViewHostMsg_GetDefaultPrintSettings(routing_id(), &default_settings); - if (Send(msg)) { - msg = NULL; - // Check if the printer returned any settings, if the settings is empty, we - // can safely assume there are no printer drivers configured. So we safely - // terminate. - if (default_settings.IsEmpty()) { - // TODO: Create an async alert (http://crbug.com/14918). - render_view_->RunJavaScriptAlert(frame, - l10n_util::GetString(IDS_DEFAULT_PRINTER_NOT_FOUND_WARNING)); - return; - } - - // Continue only if the settings are valid. - if (default_settings.dpi && default_settings.document_cookie) { - int expected_pages_count = 0; - - // Prepare once to calculate the estimated page count. This must be in - // a scope for itself (see comments on PrepareFrameAndViewForPrint). - { - PrepareFrameAndViewForPrint prep_frame_view(default_settings, - frame, - frame->GetView()); - expected_pages_count = prep_frame_view.GetExpectedPageCount(); - DCHECK(expected_pages_count); - } + // Layout page according to printer page size. Since WebKit shrinks the + // size of the page automatically (from 125% to 200%) we trick it to + // think the page is 125% larger so the size of the page is correct for + // minimum (default) scaling. + // This is important for sites that try to fill the page. + gfx::Size print_layout_size(print_canvas_size_); + print_layout_size.set_height(static_cast<int>( + static_cast<double>(print_layout_size.height()) * 1.25)); - // Ask the browser to show UI to retrieve the final print settings. - ViewMsg_PrintPages_Params print_settings; + prev_view_size_ = web_view->size(); - ViewHostMsg_ScriptedPrint_Params params; + web_view->resize(print_layout_size); - // The routing id is sent across as it is needed to look up the - // corresponding RenderViewHost instance to signal and reset the - // pump messages event. - params.routing_id = routing_id(); - // host_window_ may be NULL at this point if the current window is a popup - // and the print() command has been issued from the parent. The receiver - // of this message has to deal with this. - params.host_window_id = render_view_->host_window(); - params.cookie = default_settings.document_cookie; - params.has_selection = frame->HasSelection(); - params.expected_pages_count = expected_pages_count; + expected_pages_count_ = frame->PrintBegin(print_canvas_size_); +} - msg = new ViewHostMsg_ScriptedPrint(params, - &print_settings); - msg->set_pump_messages_event(render_view_->modal_dialog_event()); +PrepareFrameAndViewForPrint::~PrepareFrameAndViewForPrint() { + frame_->PrintEnd(); + web_view_->resize(prev_view_size_); +} - if (Send(msg)) { - msg = NULL; - // If the settings are invalid, early quit. - if (print_settings.params.dpi && - print_settings.params.document_cookie) { - if (print_settings.params.selection_only) { - CopyAndPrint(print_settings, frame); - } else { - // TODO: Always copy before printing. - PrintPages(print_settings, frame); - } +PrintWebViewHelper::PrintWebViewHelper(RenderView* render_view) + : render_view_(render_view), + user_cancelled_scripted_print_count_(0) {} - // Reset cancel counter on first successful print. - user_cancelled_scripted_print_count_ = 0; - return; // All went well. - } else { - user_cancelled_print = true; - } - } else { - // Send() failed. - NOTREACHED(); - } - } else { - // Failed to get default settings. - NOTREACHED(); - } - } else { - // Send() failed. - NOTREACHED(); - } - if (script_initiated && user_cancelled_print) { - ++user_cancelled_scripted_print_count_; - last_cancelled_script_print_ = base::Time::Now(); - } - DidFinishPrinting(user_cancelled_print); -#else // defined(OS_WIN) - // TODO(port): print not implemented - NOTIMPLEMENTED(); -#endif -} +PrintWebViewHelper::~PrintWebViewHelper() {} void PrintWebViewHelper::DidFinishPrinting(bool success) { if (!success) { @@ -237,7 +81,6 @@ void PrintWebViewHelper::DidFinishPrinting(bool success) { print_web_view_.release(); // Close deletes object. print_pages_params_.reset(); } - } bool PrintWebViewHelper::CopyAndPrint(const ViewMsg_PrintPages_Params& params, @@ -292,120 +135,6 @@ void PrintWebViewHelper::PrintPages(const ViewMsg_PrintPages_Params& params, } } -void PrintWebViewHelper::PrintPage(const ViewMsg_PrintPage_Params& params, - const gfx::Size& canvas_size, - WebFrame* frame) { -#if defined(OS_WIN) - // Generate a memory-based metafile. It will use the current screen's DPI. - printing::NativeMetafile metafile; - - metafile.CreateDc(NULL, NULL); - HDC hdc = metafile.hdc(); - DCHECK(hdc); - skia::PlatformDevice::InitializeDC(hdc); - // Since WebKit extends the page width depending on the magical shrink - // factor we make sure the canvas covers the worst case scenario - // (x2.0 currently). PrintContext will then set the correct clipping region. - int size_x = static_cast<int>(canvas_size.width() * params.params.max_shrink); - int size_y = static_cast<int>(canvas_size.height() * - params.params.max_shrink); - // Calculate the dpi adjustment. - float shrink = static_cast<float>(canvas_size.width()) / - params.params.printable_size.width(); -#if 0 - // TODO(maruel): This code is kept for testing until the 100% GDI drawing - // code is stable. maruels use this code's output as a reference when the - // GDI drawing code fails. - - // Mix of Skia and GDI based. - skia::PlatformCanvas canvas(size_x, size_y, true); - canvas.drawARGB(255, 255, 255, 255, SkPorterDuff::kSrc_Mode); - float webkit_shrink = frame->PrintPage(params.page_number, &canvas); - if (shrink <= 0) { - NOTREACHED() << "Printing page " << params.page_number << " failed."; - } else { - // Update the dpi adjustment with the "page shrink" calculated in webkit. - shrink /= webkit_shrink; - } - - // Create a BMP v4 header that we can serialize. - BITMAPV4HEADER bitmap_header; - gfx::CreateBitmapV4Header(size_x, size_y, &bitmap_header); - const SkBitmap& src_bmp = canvas.getDevice()->accessBitmap(true); - SkAutoLockPixels src_lock(src_bmp); - int retval = StretchDIBits(hdc, - 0, - 0, - size_x, size_y, - 0, 0, - size_x, size_y, - src_bmp.getPixels(), - reinterpret_cast<BITMAPINFO*>(&bitmap_header), - DIB_RGB_COLORS, - SRCCOPY); - DCHECK(retval != GDI_ERROR); -#else - // 100% GDI based. - skia::VectorCanvas canvas(hdc, size_x, size_y); - float webkit_shrink = frame->PrintPage(params.page_number, &canvas); - if (shrink <= 0) { - NOTREACHED() << "Printing page " << params.page_number << " failed."; - } else { - // Update the dpi adjustment with the "page shrink" calculated in webkit. - shrink /= webkit_shrink; - } -#endif - - // Done printing. Close the device context to retrieve the compiled metafile. - if (!metafile.CloseDc()) { - NOTREACHED() << "metafile failed"; - } - - // Get the size of the compiled metafile. - unsigned buf_size = metafile.GetDataSize(); - DCHECK_GT(buf_size, 128u); - ViewHostMsg_DidPrintPage_Params page_params; - page_params.data_size = 0; - page_params.metafile_data_handle = NULL; - page_params.page_number = params.page_number; - page_params.document_cookie = params.params.document_cookie; - page_params.actual_shrink = shrink; - base::SharedMemory shared_buf; - - // http://msdn2.microsoft.com/en-us/library/ms535522.aspx - // Windows 2000/XP: When a page in a spooled file exceeds approximately 350 - // MB, it can fail to print and not send an error message. - if (buf_size < 350*1024*1024) { - // Allocate a shared memory buffer to hold the generated metafile data. - if (shared_buf.Create(L"", false, false, buf_size) && - shared_buf.Map(buf_size)) { - // Copy the bits into shared memory. - if (metafile.GetData(shared_buf.memory(), buf_size)) { - page_params.metafile_data_handle = shared_buf.handle(); - page_params.data_size = buf_size; - } else { - NOTREACHED() << "GetData() failed"; - } - shared_buf.Unmap(); - } else { - NOTREACHED() << "Buffer allocation failed"; - } - } else { - NOTREACHED() << "Buffer too large: " << buf_size; - } - metafile.CloseEmf(); - if (Send(new ViewHostMsg_DuplicateSection( - routing_id(), - page_params.metafile_data_handle, - &page_params.metafile_data_handle))) { - Send(new ViewHostMsg_DidPrintPage(routing_id(), page_params)); - } -#else // defined(OS_WIN) - // TODO(port) implement printing - NOTIMPLEMENTED(); -#endif -} - bool PrintWebViewHelper::Send(IPC::Message* msg) { return render_view_->Send(msg); } |