| 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/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/gfx/size.h" | 9 #include "base/gfx/size.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/units.h" | 13 #include "printing/units.h" |
| 14 #include "webkit/api/public/WebScreenInfo.h" | 14 #include "webkit/api/public/WebScreenInfo.h" |
| 15 #include "webkit/api/public/WebSize.h" | 15 #include "webkit/api/public/WebSize.h" |
| 16 #include "webkit/api/public/WebURL.h" |
| 17 #include "webkit/api/public/WebURLRequest.h" |
| 16 #include "webkit/glue/webframe.h" | 18 #include "webkit/glue/webframe.h" |
| 17 | 19 |
| 18 #if defined(OS_WIN) | 20 #if defined(OS_WIN) |
| 19 #include "chrome/common/gfx/emf.h" | 21 #include "chrome/common/gfx/emf.h" |
| 20 #include "skia/ext/vector_canvas.h" | 22 #include "skia/ext/vector_canvas.h" |
| 21 #endif | 23 #endif |
| 22 | 24 |
| 23 namespace { | 25 namespace { |
| 24 | 26 |
| 25 // Class that calls the Begin and End print functions on the frame and changes | 27 // Class that calls the Begin and End print functions on the frame and changes |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 gfx::Size prev_view_size_; | 81 gfx::Size prev_view_size_; |
| 80 int expected_pages_count_; | 82 int expected_pages_count_; |
| 81 | 83 |
| 82 DISALLOW_COPY_AND_ASSIGN(PrepareFrameAndViewForPrint); | 84 DISALLOW_COPY_AND_ASSIGN(PrepareFrameAndViewForPrint); |
| 83 }; | 85 }; |
| 84 | 86 |
| 85 } // namespace | 87 } // namespace |
| 86 | 88 |
| 87 void PrintWebViewHelper::SyncPrint(WebFrame* frame) { | 89 void PrintWebViewHelper::SyncPrint(WebFrame* frame) { |
| 88 #if defined(OS_WIN) | 90 #if defined(OS_WIN) |
| 91 |
| 92 // If still not finished with earlier print request simply ignore. |
| 93 if (IsPrinting()) |
| 94 return; |
| 95 |
| 89 // Retrieve the default print settings to calculate the expected number of | 96 // Retrieve the default print settings to calculate the expected number of |
| 90 // pages. | 97 // pages. |
| 91 ViewMsg_Print_Params default_settings; | 98 ViewMsg_Print_Params default_settings; |
| 92 | 99 |
| 93 IPC::SyncMessage* msg = | 100 IPC::SyncMessage* msg = |
| 94 new ViewHostMsg_GetDefaultPrintSettings(routing_id(), &default_settings); | 101 new ViewHostMsg_GetDefaultPrintSettings(routing_id(), &default_settings); |
| 95 if (Send(msg)) { | 102 if (Send(msg)) { |
| 96 msg = NULL; | 103 msg = NULL; |
| 97 // Check if the printer returned any settings, if the settings is empty, we | 104 // Check if the printer returned any settings, if the settings is empty, we |
| 98 // can safely assume there are no printer drivers configured. So we safely | 105 // can safely assume there are no printer drivers configured. So we safely |
| (...skipping 20 matching lines...) Expand all Loading... |
| 119 | 126 |
| 120 // Ask the browser to show UI to retrieve the final print settings. | 127 // Ask the browser to show UI to retrieve the final print settings. |
| 121 ViewMsg_PrintPages_Params print_settings; | 128 ViewMsg_PrintPages_Params print_settings; |
| 122 // host_window_ may be NULL at this point if the current window is a popup | 129 // host_window_ may be NULL at this point if the current window is a popup |
| 123 // and the print() command has been issued from the parent. The receiver | 130 // and the print() command has been issued from the parent. The receiver |
| 124 // of this message has to deal with this. | 131 // of this message has to deal with this. |
| 125 msg = new ViewHostMsg_ScriptedPrint(routing_id(), | 132 msg = new ViewHostMsg_ScriptedPrint(routing_id(), |
| 126 render_view_->host_window(), | 133 render_view_->host_window(), |
| 127 default_settings.document_cookie, | 134 default_settings.document_cookie, |
| 128 expected_pages_count, | 135 expected_pages_count, |
| 129 false, // has_selection | 136 frame->HasSelection(), |
| 130 &print_settings); | 137 &print_settings); |
| 131 if (Send(msg)) { | 138 if (Send(msg)) { |
| 132 msg = NULL; | 139 msg = NULL; |
| 133 | 140 |
| 134 // Printing selection only not supported yet. | |
| 135 if (print_settings.params.selection_only) { | |
| 136 NOTIMPLEMENTED(); | |
| 137 } | |
| 138 | |
| 139 // If the settings are invalid, early quit. | 141 // If the settings are invalid, early quit. |
| 140 if (print_settings.params.dpi && | 142 if (print_settings.params.dpi && |
| 141 print_settings.params.document_cookie) { | 143 print_settings.params.document_cookie) { |
| 142 // Render the printed pages. It will implicitly revert the document to | 144 if (print_settings.params.selection_only) { |
| 143 // display CSS media type. | 145 CopyAndPrint(print_settings, frame); |
| 144 PrintPages(print_settings, frame); | 146 } else { |
| 145 // All went well. | 147 // TODO: Always copy before printing. |
| 146 return; | 148 PrintPages(print_settings, frame); |
| 149 } |
| 150 return; // All went well. |
| 147 } else { | 151 } else { |
| 148 // The user cancelled. | 152 // The user cancelled. |
| 149 } | 153 } |
| 150 } else { | 154 } else { |
| 151 // Send() failed. | 155 // Send() failed. |
| 152 NOTREACHED(); | 156 NOTREACHED(); |
| 153 } | 157 } |
| 154 } else { | 158 } else { |
| 155 // The user cancelled. | 159 // The user cancelled. |
| 156 } | 160 } |
| 157 } else { | 161 } else { |
| 158 // Send() failed. | 162 // Send() failed. |
| 159 NOTREACHED(); | 163 NOTREACHED(); |
| 160 } | 164 } |
| 161 // TODO(maruel): bug 1123882 Alert the user that printing failed. | 165 DidFinishPrinting(false); |
| 162 #else // defined(OS_WIN) | 166 #else // defined(OS_WIN) |
| 163 // TODO(port): print not implemented | 167 // TODO(port): print not implemented |
| 164 NOTIMPLEMENTED(); | 168 NOTIMPLEMENTED(); |
| 165 #endif | 169 #endif |
| 166 } | 170 } |
| 167 | 171 |
| 172 void PrintWebViewHelper::DidFinishPrinting(bool success) { |
| 173 if (print_web_view_.get()) { |
| 174 print_web_view_->Close(); |
| 175 print_web_view_.release(); // Close deletes object. |
| 176 print_pages_params_.reset(); |
| 177 } |
| 178 |
| 179 if (!success) { |
| 180 // TODO(sverrir): http://crbug.com/6833 Show some kind of notification. |
| 181 } |
| 182 } |
| 183 |
| 184 bool PrintWebViewHelper::CopyAndPrint(const ViewMsg_PrintPages_Params& params, |
| 185 WebFrame* web_frame) { |
| 186 // Create a new WebView with the same settings as the current display one. |
| 187 // Except that we disable javascript (don't want any active content running |
| 188 // on the page). |
| 189 WebPreferences prefs = web_frame->GetView()->GetPreferences(); |
| 190 prefs.javascript_enabled = false; |
| 191 prefs.java_enabled = false; |
| 192 print_web_view_.reset(WebView::Create(this, prefs)); |
| 193 |
| 194 print_pages_params_.reset(new ViewMsg_PrintPages_Params(params)); |
| 195 print_pages_params_->pages.clear(); // Print all pages of selection. |
| 196 |
| 197 std::string html = web_frame->GetSelection(true); |
| 198 std::string url_str = "data:text/html;charset=utf-8,"; |
| 199 url_str.append(html); |
| 200 GURL url(url_str); |
| 201 |
| 202 // When loading is done this will call DidStopLoading that will do the |
| 203 // actual printing. |
| 204 print_web_view_->GetMainFrame()->LoadRequest(WebKit::WebURLRequest(url)); |
| 205 |
| 206 return true; |
| 207 } |
| 208 |
| 168 void PrintWebViewHelper::PrintPages(const ViewMsg_PrintPages_Params& params, | 209 void PrintWebViewHelper::PrintPages(const ViewMsg_PrintPages_Params& params, |
| 169 WebFrame* frame) { | 210 WebFrame* frame) { |
| 170 PrepareFrameAndViewForPrint prep_frame_view(params.params, | 211 PrepareFrameAndViewForPrint prep_frame_view(params.params, |
| 171 frame, | 212 frame, |
| 172 frame->GetView()); | 213 frame->GetView()); |
| 173 int page_count = prep_frame_view.GetExpectedPageCount(); | 214 int page_count = prep_frame_view.GetExpectedPageCount(); |
| 174 | 215 |
| 175 Send(new ViewHostMsg_DidGetPrintedPagesCount(routing_id(), | 216 Send(new ViewHostMsg_DidGetPrintedPagesCount(routing_id(), |
| 176 params.params.document_cookie, | 217 params.params.document_cookie, |
| 177 page_count)); | 218 page_count)); |
| 178 if (page_count) { | 219 if (page_count) { |
| 179 ViewMsg_PrintPage_Params page_params; | 220 ViewMsg_PrintPage_Params page_params; |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 307 } | 348 } |
| 308 | 349 |
| 309 bool PrintWebViewHelper::Send(IPC::Message* msg) { | 350 bool PrintWebViewHelper::Send(IPC::Message* msg) { |
| 310 return render_view_->Send(msg); | 351 return render_view_->Send(msg); |
| 311 } | 352 } |
| 312 | 353 |
| 313 int32 PrintWebViewHelper::routing_id() { | 354 int32 PrintWebViewHelper::routing_id() { |
| 314 return render_view_->routing_id(); | 355 return render_view_->routing_id(); |
| 315 } | 356 } |
| 316 | 357 |
| 358 void PrintWebViewHelper::DidStopLoading(WebView* webview) { |
| 359 DCHECK(print_pages_params_.get() != NULL); |
| 360 DCHECK_EQ(webview, print_web_view_.get()); |
| 361 PrintPages(*print_pages_params_.get(), print_web_view_->GetMainFrame()); |
| 362 } |
| 363 |
| 317 void PrintWebViewHelper::GetWindowRect(WebWidget* webwidget, | 364 void PrintWebViewHelper::GetWindowRect(WebWidget* webwidget, |
| 318 WebKit::WebRect* rect) { | 365 WebKit::WebRect* rect) { |
| 319 NOTREACHED(); | 366 NOTREACHED(); |
| 320 } | 367 } |
| 321 | 368 |
| 322 void PrintWebViewHelper::DidStopLoading(WebView* webview) { | |
| 323 NOTREACHED(); | |
| 324 } | |
| 325 | |
| 326 WebKit::WebScreenInfo PrintWebViewHelper::GetScreenInfo(WebWidget* webwidget) { | 369 WebKit::WebScreenInfo PrintWebViewHelper::GetScreenInfo(WebWidget* webwidget) { |
| 327 WebKit::WebScreenInfo info; | 370 WebKit::WebScreenInfo info; |
| 328 NOTREACHED(); | 371 NOTREACHED(); |
| 329 return info; | 372 return info; |
| 330 } | 373 } |
| 331 | 374 |
| 332 gfx::NativeViewId PrintWebViewHelper::GetContainingView(WebWidget* webwidget) { | 375 gfx::NativeViewId PrintWebViewHelper::GetContainingView(WebWidget* webwidget) { |
| 333 NOTREACHED(); | 376 NOTREACHED(); |
| 334 return gfx::NativeViewId(); | 377 return gfx::NativeViewId(); |
| 335 } | 378 } |
| 336 | 379 |
| 337 bool PrintWebViewHelper::IsHidden(WebWidget* webwidget) { | 380 bool PrintWebViewHelper::IsHidden(WebWidget* webwidget) { |
| 338 NOTREACHED(); | 381 NOTREACHED(); |
| 339 return true; | 382 return true; |
| 340 } | 383 } |
| OLD | NEW |