| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/renderer/print_web_view_helper.h" | 5 #include "chrome/renderer/print_web_view_helper.h" |
| 6 | 6 |
| 7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
| 8 #include "base/gfx/size.h" | 8 #include "base/gfx/size.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "chrome/common/render_messages.h" | 10 #include "chrome/common/render_messages.h" |
| 11 #include "chrome/renderer/render_view.h" | 11 #include "chrome/renderer/render_view.h" |
| 12 #include "grit/generated_resources.h" | 12 #include "grit/generated_resources.h" |
| 13 #include "printing/native_metafile.h" | 13 #include "printing/native_metafile.h" |
| 14 #include "webkit/api/public/WebConsoleMessage.h" | 14 #include "webkit/api/public/WebConsoleMessage.h" |
| 15 #include "webkit/glue/webframe.h" | 15 #include "webkit/api/public/WebFrame.h" |
| 16 | 16 |
| 17 using WebKit::WebConsoleMessage; | 17 using WebKit::WebConsoleMessage; |
| 18 using WebKit::WebFrame; |
| 18 using WebKit::WebString; | 19 using WebKit::WebString; |
| 19 | 20 |
| 20 #include "skia/ext/vector_canvas.h" | 21 #include "skia/ext/vector_canvas.h" |
| 21 | 22 |
| 22 void PrintWebViewHelper::Print(WebFrame* frame, bool script_initiated) { | 23 void PrintWebViewHelper::Print(WebFrame* frame, bool script_initiated) { |
| 23 const int kMinSecondsToIgnoreJavascriptInitiatedPrint = 2; | 24 const int kMinSecondsToIgnoreJavascriptInitiatedPrint = 2; |
| 24 const int kMaxSecondsToIgnoreJavascriptInitiatedPrint = 2 * 60; // 2 Minutes. | 25 const int kMaxSecondsToIgnoreJavascriptInitiatedPrint = 2 * 60; // 2 Minutes. |
| 25 | 26 |
| 26 // If still not finished with earlier print request simply ignore. | 27 // If still not finished with earlier print request simply ignore. |
| 27 if (IsPrinting()) | 28 if (IsPrinting()) |
| 28 return; | 29 return; |
| 29 | 30 |
| 30 // TODO(maruel): Move this out of platform specific code. | 31 // TODO(maruel): Move this out of platform specific code. |
| 31 // Check if there is script repeatedly trying to print and ignore it if too | 32 // Check if there is script repeatedly trying to print and ignore it if too |
| 32 // frequent. We use exponential wait time so for a page that calls print() in | 33 // frequent. We use exponential wait time so for a page that calls print() in |
| 33 // a loop the user will need to cancel the print dialog after 2 seconds, 4 | 34 // a loop the user will need to cancel the print dialog after 2 seconds, 4 |
| 34 // seconds, 8, ... up to the maximum of 2 minutes. | 35 // seconds, 8, ... up to the maximum of 2 minutes. |
| 35 // This gives the user time to navigate from the page. | 36 // This gives the user time to navigate from the page. |
| 36 if (script_initiated && (user_cancelled_scripted_print_count_ > 0)) { | 37 if (script_initiated && (user_cancelled_scripted_print_count_ > 0)) { |
| 37 base::TimeDelta diff = base::Time::Now() - last_cancelled_script_print_; | 38 base::TimeDelta diff = base::Time::Now() - last_cancelled_script_print_; |
| 38 int min_wait_seconds = std::min( | 39 int min_wait_seconds = std::min( |
| 39 kMinSecondsToIgnoreJavascriptInitiatedPrint << | 40 kMinSecondsToIgnoreJavascriptInitiatedPrint << |
| 40 (user_cancelled_scripted_print_count_ - 1), | 41 (user_cancelled_scripted_print_count_ - 1), |
| 41 kMaxSecondsToIgnoreJavascriptInitiatedPrint); | 42 kMaxSecondsToIgnoreJavascriptInitiatedPrint); |
| 42 if (diff.InSeconds() < min_wait_seconds) { | 43 if (diff.InSeconds() < min_wait_seconds) { |
| 43 WebString message(WebString::fromUTF8( | 44 WebString message(WebString::fromUTF8( |
| 44 "Ignoring too frequent calls to print().")); | 45 "Ignoring too frequent calls to print().")); |
| 45 frame->AddMessageToConsole(WebConsoleMessage( | 46 frame->addMessageToConsole(WebConsoleMessage( |
| 46 WebConsoleMessage::LevelWarning, | 47 WebConsoleMessage::LevelWarning, |
| 47 message)); | 48 message)); |
| 48 return; | 49 return; |
| 49 } | 50 } |
| 50 } | 51 } |
| 51 | 52 |
| 52 // Retrieve the default print settings to calculate the expected number of | 53 // Retrieve the default print settings to calculate the expected number of |
| 53 // pages. | 54 // pages. |
| 54 ViewMsg_Print_Params default_settings; | 55 ViewMsg_Print_Params default_settings; |
| 55 bool user_cancelled_print = false; | 56 bool user_cancelled_print = false; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 70 | 71 |
| 71 // Continue only if the settings are valid. | 72 // Continue only if the settings are valid. |
| 72 if (default_settings.dpi && default_settings.document_cookie) { | 73 if (default_settings.dpi && default_settings.document_cookie) { |
| 73 int expected_pages_count = 0; | 74 int expected_pages_count = 0; |
| 74 | 75 |
| 75 // Prepare once to calculate the estimated page count. This must be in | 76 // Prepare once to calculate the estimated page count. This must be in |
| 76 // a scope for itself (see comments on PrepareFrameAndViewForPrint). | 77 // a scope for itself (see comments on PrepareFrameAndViewForPrint). |
| 77 { | 78 { |
| 78 PrepareFrameAndViewForPrint prep_frame_view(default_settings, | 79 PrepareFrameAndViewForPrint prep_frame_view(default_settings, |
| 79 frame, | 80 frame, |
| 80 frame->GetView()); | 81 frame->view()); |
| 81 expected_pages_count = prep_frame_view.GetExpectedPageCount(); | 82 expected_pages_count = prep_frame_view.GetExpectedPageCount(); |
| 82 DCHECK(expected_pages_count); | 83 DCHECK(expected_pages_count); |
| 83 } | 84 } |
| 84 | 85 |
| 85 // Ask the browser to show UI to retrieve the final print settings. | 86 // Ask the browser to show UI to retrieve the final print settings. |
| 86 ViewMsg_PrintPages_Params print_settings; | 87 ViewMsg_PrintPages_Params print_settings; |
| 87 | 88 |
| 88 ViewHostMsg_ScriptedPrint_Params params; | 89 ViewHostMsg_ScriptedPrint_Params params; |
| 89 | 90 |
| 90 // The routing id is sent across as it is needed to look up the | 91 // The routing id is sent across as it is needed to look up the |
| 91 // corresponding RenderViewHost instance to signal and reset the | 92 // corresponding RenderViewHost instance to signal and reset the |
| 92 // pump messages event. | 93 // pump messages event. |
| 93 params.routing_id = routing_id(); | 94 params.routing_id = routing_id(); |
| 94 // host_window_ may be NULL at this point if the current window is a popup | 95 // host_window_ may be NULL at this point if the current window is a popup |
| 95 // and the print() command has been issued from the parent. The receiver | 96 // and the print() command has been issued from the parent. The receiver |
| 96 // of this message has to deal with this. | 97 // of this message has to deal with this. |
| 97 params.host_window_id = render_view_->host_window(); | 98 params.host_window_id = render_view_->host_window(); |
| 98 params.cookie = default_settings.document_cookie; | 99 params.cookie = default_settings.document_cookie; |
| 99 params.has_selection = frame->HasSelection(); | 100 params.has_selection = frame->hasSelection(); |
| 100 params.expected_pages_count = expected_pages_count; | 101 params.expected_pages_count = expected_pages_count; |
| 101 | 102 |
| 102 msg = new ViewHostMsg_ScriptedPrint(params, | 103 msg = new ViewHostMsg_ScriptedPrint(params, |
| 103 &print_settings); | 104 &print_settings); |
| 104 msg->set_pump_messages_event(render_view_->modal_dialog_event()); | 105 msg->set_pump_messages_event(render_view_->modal_dialog_event()); |
| 105 | 106 |
| 106 if (Send(msg)) { | 107 if (Send(msg)) { |
| 107 msg = NULL; | 108 msg = NULL; |
| 108 | 109 |
| 109 // If the settings are invalid, early quit. | 110 // If the settings are invalid, early quit. |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 0, 0, | 189 0, 0, |
| 189 size_x, size_y, | 190 size_x, size_y, |
| 190 src_bmp.getPixels(), | 191 src_bmp.getPixels(), |
| 191 reinterpret_cast<BITMAPINFO*>(&bitmap_header), | 192 reinterpret_cast<BITMAPINFO*>(&bitmap_header), |
| 192 DIB_RGB_COLORS, | 193 DIB_RGB_COLORS, |
| 193 SRCCOPY); | 194 SRCCOPY); |
| 194 DCHECK(retval != GDI_ERROR); | 195 DCHECK(retval != GDI_ERROR); |
| 195 #else | 196 #else |
| 196 // 100% GDI based. | 197 // 100% GDI based. |
| 197 skia::VectorCanvas canvas(hdc, size_x, size_y); | 198 skia::VectorCanvas canvas(hdc, size_x, size_y); |
| 198 float webkit_shrink = frame->PrintPage(params.page_number, &canvas); | 199 float webkit_shrink = frame->printPage(params.page_number, &canvas); |
| 199 if (shrink <= 0) { | 200 if (shrink <= 0) { |
| 200 NOTREACHED() << "Printing page " << params.page_number << " failed."; | 201 NOTREACHED() << "Printing page " << params.page_number << " failed."; |
| 201 } else { | 202 } else { |
| 202 // Update the dpi adjustment with the "page shrink" calculated in webkit. | 203 // Update the dpi adjustment with the "page shrink" calculated in webkit. |
| 203 shrink /= webkit_shrink; | 204 shrink /= webkit_shrink; |
| 204 } | 205 } |
| 205 #endif | 206 #endif |
| 206 | 207 |
| 207 // Done printing. Close the device context to retrieve the compiled metafile. | 208 // Done printing. Close the device context to retrieve the compiled metafile. |
| 208 if (!metafile.CloseDc()) { | 209 if (!metafile.CloseDc()) { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 } | 244 } |
| 244 metafile.CloseEmf(); | 245 metafile.CloseEmf(); |
| 245 if (Send(new ViewHostMsg_DuplicateSection( | 246 if (Send(new ViewHostMsg_DuplicateSection( |
| 246 routing_id(), | 247 routing_id(), |
| 247 page_params.metafile_data_handle, | 248 page_params.metafile_data_handle, |
| 248 &page_params.metafile_data_handle))) { | 249 &page_params.metafile_data_handle))) { |
| 249 Send(new ViewHostMsg_DidPrintPage(routing_id(), page_params)); | 250 Send(new ViewHostMsg_DidPrintPage(routing_id(), page_params)); |
| 250 } | 251 } |
| 251 } | 252 } |
| 252 | 253 |
| OLD | NEW |