diff options
author | stuartmorgan@chromium.org <stuartmorgan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-05-10 18:10:33 +0000 |
---|---|---|
committer | stuartmorgan@chromium.org <stuartmorgan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-05-10 18:10:33 +0000 |
commit | 35b9aa07e104d4a228778cbffc9577087ba80b07 (patch) | |
tree | aae602e493c4a903bbda6ceb42851675a39d9cfe /chrome/renderer | |
parent | e6771600ddcb67a80854ff8c268d599b267236f3 (diff) | |
download | chromium_src-35b9aa07e104d4a228778cbffc9577087ba80b07.zip chromium_src-35b9aa07e104d4a228778cbffc9577087ba80b07.tar.gz chromium_src-35b9aa07e104d4a228778cbffc9577087ba80b07.tar.bz2 |
Unfork most of the PrintWebViewHelper code between Mac and Windows.
I had originally planned to wait until Linux moved to the same printing workflow to unfork everything at once, but there has started to be some unnecessary divergence between the Mac and Windows code so I'm doing those two platforms now, and hopefully Linux can adopt the methods later.
BUG=24809
TEST=Printing still works on Mac and Windows.
Review URL: http://codereview.chromium.org/1748013
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@46830 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/renderer')
-rw-r--r-- | chrome/renderer/print_web_view_helper.cc | 170 | ||||
-rw-r--r-- | chrome/renderer/print_web_view_helper_linux.cc | 3 | ||||
-rw-r--r-- | chrome/renderer/print_web_view_helper_mac.mm | 161 | ||||
-rw-r--r-- | chrome/renderer/print_web_view_helper_win.cc | 163 |
4 files changed, 172 insertions, 325 deletions
diff --git a/chrome/renderer/print_web_view_helper.cc b/chrome/renderer/print_web_view_helper.cc index 50c1797..d0ad4db 100644 --- a/chrome/renderer/print_web_view_helper.cc +++ b/chrome/renderer/print_web_view_helper.cc @@ -11,6 +11,7 @@ #include "gfx/codec/jpeg_codec.h" #include "grit/generated_resources.h" #include "printing/units.h" +#include "third_party/WebKit/WebKit/chromium/public/WebConsoleMessage.h" #include "third_party/WebKit/WebKit/chromium/public/WebFrame.h" #include "third_party/WebKit/WebKit/chromium/public/WebRect.h" #include "third_party/WebKit/WebKit/chromium/public/WebScreenInfo.h" @@ -18,6 +19,7 @@ #include "third_party/WebKit/WebKit/chromium/public/WebURLRequest.h" #include "webkit/glue/webkit_glue.h" +using WebKit::WebConsoleMessage; using WebKit::WebFrame; using WebKit::WebRect; using WebKit::WebScreenInfo; @@ -72,6 +74,141 @@ PrintWebViewHelper::PrintWebViewHelper(RenderView* render_view) PrintWebViewHelper::~PrintWebViewHelper() {} +#if defined(OS_MACOSX) || defined(OS_WIN) +void PrintWebViewHelper::Print(WebFrame* frame, bool script_initiated) { + const int kMinSecondsToIgnoreJavascriptInitiatedPrint = 2; + const int kMaxSecondsToIgnoreJavascriptInitiatedPrint = 2 * 60; // 2 Minutes. + + // 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_->runModalAlertDialog(frame, + l10n_util::GetStringUTF16(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; + bool use_browser_overlays = true; + + // 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->view()); + expected_pages_count = prep_frame_view.GetExpectedPageCount(); + DCHECK(expected_pages_count); + use_browser_overlays = prep_frame_view.ShouldUseBrowserOverlays(); + } + + // Ask the browser to show UI to retrieve the final print settings. + ViewMsg_PrintPages_Params print_settings; + + ViewHostMsg_ScriptedPrint_Params params; + + // 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; + // TODO(maruel): Reenable once http://crbug.com/22937 is fixed. + // Print selection is broken because DidStopLoading is never called. + // params.has_selection = frame->hasSelection(); + params.has_selection = false; + params.expected_pages_count = expected_pages_count; + params.use_overlays = use_browser_overlays; + + msg = new ViewHostMsg_ScriptedPrint(routing_id(), params, + &print_settings); + msg->EnableMessagePumping(); + 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); + } + + // 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(); + } + // When |user_cancelled_print| is true, we treat it as success so that + // DidFinishPrinting() won't show any error alert. + // If |user_cancelled_print| is false and we reach here, there must be + // something wrong and hence is not success, DidFinishPrinting() should show + // an error alert. + // In both cases, we have to call DidFinishPrinting() here to release + // printing resources, since we don't need them anymore. + DidFinishPrinting(user_cancelled_print); +} +#endif // OS_MACOSX || OS_WIN + void PrintWebViewHelper::DidFinishPrinting(bool success) { if (!success) { WebView* web_view = print_web_view_; @@ -155,6 +292,39 @@ void PrintWebViewHelper::PrintPageAsJPEG( DCHECK(encoded); } +#if defined(OS_MACOSX) || defined(OS_WIN) +void PrintWebViewHelper::PrintPages(const ViewMsg_PrintPages_Params& params, + WebFrame* frame) { + PrepareFrameAndViewForPrint prep_frame_view(params.params, + frame, + frame->view()); + int page_count = prep_frame_view.GetExpectedPageCount(); + + Send(new ViewHostMsg_DidGetPrintedPagesCount(routing_id(), + params.params.document_cookie, + page_count)); + if (!page_count) + return; + + const gfx::Size& canvas_size = prep_frame_view.GetPrintCanvasSize(); + ViewMsg_PrintPage_Params page_params; + page_params.params = params.params; + if (params.pages.empty()) { + for (int i = 0; i < page_count; ++i) { + page_params.page_number = i; + PrintPage(page_params, canvas_size, frame); + } + } else { + for (size_t i = 0; i < params.pages.size(); ++i) { + if (params.pages[i] >= page_count) + break; + page_params.page_number = params.pages[i]; + PrintPage(page_params, canvas_size, frame); + } + } +} +#endif // OS_MACOSX || OS_WIN + bool PrintWebViewHelper::Send(IPC::Message* msg) { return render_view_->Send(msg); } diff --git a/chrome/renderer/print_web_view_helper_linux.cc b/chrome/renderer/print_web_view_helper_linux.cc index ed977c3..2a0e333 100644 --- a/chrome/renderer/print_web_view_helper_linux.cc +++ b/chrome/renderer/print_web_view_helper_linux.cc @@ -18,7 +18,8 @@ void PrintWebViewHelper::Print(WebFrame* frame, bool script_initiated) { if (IsPrinting()) return; - // TODO(myhuang): Get printing parameters via IPC. + // TODO(myhuang): Get printing parameters via IPC + // using the print_web_view_helper.cc version of Print. // For testing purpose, we hard-coded printing parameters here. // The paper size is US Letter (8.5 in. by 11 in.). diff --git a/chrome/renderer/print_web_view_helper_mac.mm b/chrome/renderer/print_web_view_helper_mac.mm index 96113fb..d6d91b4 100644 --- a/chrome/renderer/print_web_view_helper_mac.mm +++ b/chrome/renderer/print_web_view_helper_mac.mm @@ -16,170 +16,9 @@ #include "printing/native_metafile.h" #include "third_party/WebKit/WebKit/chromium/public/WebFrame.h" #include "third_party/WebKit/WebKit/chromium/public/WebCanvas.h" -#include "third_party/WebKit/WebKit/chromium/public/WebConsoleMessage.h" using WebKit::WebFrame; using WebKit::WebCanvas; -using WebKit::WebConsoleMessage; -using WebKit::WebString; - -// TODO(stuartmorgan): There's a fair amount of code here that is duplicated -// from _win that should instead be shared. Once Linux has a real print settings -// implementation, it's likely that this whole method can just be moved to the -// cross-platform file, and the slight divergences resolved/ifdef'd. -void PrintWebViewHelper::Print(WebFrame* frame, bool script_initiated) { - const int kMinSecondsToIgnoreJavascriptInitiatedPrint = 2; - const int kMaxSecondsToIgnoreJavascriptInitiatedPrint = 2 * 60; // 2 Minutes. - - // If still not finished with earlier print request simply ignore. - if (IsPrinting()) - return; - - // TODO(maruel): Move this out of platform specific code. - // 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; - if (default_settings.IsEmpty()) { - NOTREACHED() << "Couldn't get default print settings"; - 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->view()); - expected_pages_count = prep_frame_view.GetExpectedPageCount(); - DCHECK(expected_pages_count); - } - - // Ask the browser to show UI to retrieve the final print settings. - ViewMsg_PrintPages_Params print_settings; - - ViewHostMsg_ScriptedPrint_Params params; - - // 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; - - msg = new ViewHostMsg_ScriptedPrint(routing_id(), params, - &print_settings); - msg->EnableMessagePumping(); - 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); - } - - // 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(); - } - // When |user_cancelled_print| is true, we treat it as success so that - // DidFinishPrinting() won't show any error alert. - // If |user_cancelled_print| is false and we reach here, there must be - // something wrong and hence is not success, DidFinishPrinting() should show - // an error alert. - // In both cases, we have to call DidFinishPrinting() here to release - // printing resources, since we don't need them anymore. - DidFinishPrinting(user_cancelled_print); -} - -void PrintWebViewHelper::PrintPages(const ViewMsg_PrintPages_Params& params, - WebKit::WebFrame* frame) { - PrepareFrameAndViewForPrint prep_frame_view(params.params, - frame, - frame->view()); - int page_count = prep_frame_view.GetExpectedPageCount(); - - Send(new ViewHostMsg_DidGetPrintedPagesCount(routing_id(), - params.params.document_cookie, - page_count)); - if (!page_count) - return; - - const gfx::Size& canvas_size = prep_frame_view.GetPrintCanvasSize(); - ViewMsg_PrintPage_Params page_params; - page_params.params = params.params; - if (params.pages.empty()) { - for (int i = 0; i < page_count; ++i) { - page_params.page_number = i; - PrintPage(page_params, canvas_size, frame); - } - } else { - for (size_t i = 0; i < params.pages.size(); ++i) { - if (params.pages[i] >= page_count) - break; - page_params.page_number = params.pages[i]; - PrintPage(page_params, canvas_size, frame); - } - } -} void PrintWebViewHelper::PrintPage(const ViewMsg_PrintPage_Params& params, const gfx::Size& canvas_size, diff --git a/chrome/renderer/print_web_view_helper_win.cc b/chrome/renderer/print_web_view_helper_win.cc index 8c7f5c4..396edb5 100644 --- a/chrome/renderer/print_web_view_helper_win.cc +++ b/chrome/renderer/print_web_view_helper_win.cc @@ -12,174 +12,11 @@ #include "grit/generated_resources.h" #include "printing/native_metafile.h" #include "skia/ext/vector_canvas.h" -#include "third_party/WebKit/WebKit/chromium/public/WebConsoleMessage.h" #include "third_party/WebKit/WebKit/chromium/public/WebFrame.h" -using WebKit::WebConsoleMessage; using WebKit::WebFrame; using WebKit::WebString; -void PrintWebViewHelper::Print(WebFrame* frame, bool script_initiated) { - const int kMinSecondsToIgnoreJavascriptInitiatedPrint = 2; - const int kMaxSecondsToIgnoreJavascriptInitiatedPrint = 2 * 60; // 2 Minutes. - - // If still not finished with earlier print request simply ignore. - if (IsPrinting()) - return; - - // TODO(maruel): Move this out of platform specific code. - // 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_->runModalAlertDialog(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; - bool use_browser_overlays = true; - - // 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->view()); - expected_pages_count = prep_frame_view.GetExpectedPageCount(); - DCHECK(expected_pages_count); - use_browser_overlays = prep_frame_view.ShouldUseBrowserOverlays(); - } - - // Ask the browser to show UI to retrieve the final print settings. - ViewMsg_PrintPages_Params print_settings; - - ViewHostMsg_ScriptedPrint_Params params; - - // 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; - // TODO(maruel): Reenable once http://crbug.com/22937 is fixed. - // Print selection is broken because DidStopLoading is never called. - // params.has_selection = frame->hasSelection(); - params.has_selection = false; - params.expected_pages_count = expected_pages_count; - params.use_overlays = use_browser_overlays; - - msg = new ViewHostMsg_ScriptedPrint(routing_id(), params, - &print_settings); - msg->EnableMessagePumping(); - 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); - } - - // 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(); - } - // When |user_cancelled_print| is true, we treat it as success so that - // DidFinishPrinting() won't show any error alert. - // If |user_cancelled_print| is false and we reach here, there must be - // something wrong and hence is not success, DidFinishPrinting() should show - // an error alert. - // In both cases, we have to call DidFinishPrinting() here to release - // printing resources, since we do need them anymore. - DidFinishPrinting(user_cancelled_print); -} - -void PrintWebViewHelper::PrintPages(const ViewMsg_PrintPages_Params& params, - WebFrame* frame) { - PrepareFrameAndViewForPrint prep_frame_view(params.params, - frame, - frame->view()); - int page_count = prep_frame_view.GetExpectedPageCount(); - - Send(new ViewHostMsg_DidGetPrintedPagesCount(routing_id(), - params.params.document_cookie, - page_count)); - if (page_count) { - ViewMsg_PrintPage_Params page_params; - page_params.params = params.params; - if (params.pages.empty()) { - for (int i = 0; i < page_count; ++i) { - page_params.page_number = i; - PrintPage(page_params, prep_frame_view.GetPrintCanvasSize(), frame); - } - } else { - for (size_t i = 0; i < params.pages.size(); ++i) { - page_params.page_number = params.pages[i]; - PrintPage(page_params, prep_frame_view.GetPrintCanvasSize(), frame); - } - } - } -} - void PrintWebViewHelper::PrintPage(const ViewMsg_PrintPage_Params& params, const gfx::Size& canvas_size, WebFrame* frame) { |