OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/printing/print_web_view_helper.h" | 5 #include "chrome/renderer/printing/print_web_view_helper.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/auto_reset.h" | 9 #include "base/auto_reset.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 1232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1243 if (store_print_pages_params) { | 1243 if (store_print_pages_params) { |
1244 old_print_pages_params_.reset(print_pages_params_.release()); | 1244 old_print_pages_params_.reset(print_pages_params_.release()); |
1245 } else { | 1245 } else { |
1246 print_pages_params_.reset(); | 1246 print_pages_params_.reset(); |
1247 old_print_pages_params_.reset(); | 1247 old_print_pages_params_.reset(); |
1248 } | 1248 } |
1249 | 1249 |
1250 notify_browser_of_print_failure_ = true; | 1250 notify_browser_of_print_failure_ = true; |
1251 } | 1251 } |
1252 | 1252 |
| 1253 void PrintWebViewHelper::OnFramePreparedForPrintPages() { |
| 1254 PrintPages(); |
| 1255 FinishFramePrinting(); |
| 1256 } |
| 1257 |
1253 void PrintWebViewHelper::PrintPages() { | 1258 void PrintWebViewHelper::PrintPages() { |
1254 // Delete prep_frame_view_ on exit. | 1259 if (!prep_frame_view_) // Printing is already canceled or failed. |
1255 scoped_ptr<PrepareFrameAndViewForPrint> prep_frame_view; | |
1256 prep_frame_view.swap(prep_frame_view_); | |
1257 if (!prep_frame_view) // Printing is already canceled or failed. | |
1258 return; | 1260 return; |
| 1261 prep_frame_view_->StartPrinting(); |
1259 | 1262 |
1260 prep_frame_view->StartPrinting(); | 1263 int page_count = prep_frame_view_->GetExpectedPageCount(); |
1261 | |
1262 int page_count = prep_frame_view->GetExpectedPageCount(); | |
1263 if (!page_count) { | 1264 if (!page_count) { |
1264 LOG(ERROR) << "Can't print 0 pages."; | 1265 LOG(ERROR) << "Can't print 0 pages."; |
1265 return DidFinishPrinting(FAIL_PRINT); | 1266 return DidFinishPrinting(FAIL_PRINT); |
1266 } | 1267 } |
1267 | 1268 |
1268 #if !defined(OS_CHROMEOS) | 1269 #if !defined(OS_CHROMEOS) |
1269 const PrintMsg_PrintPages_Params& params = *print_pages_params_; | 1270 const PrintMsg_PrintPages_Params& params = *print_pages_params_; |
1270 const PrintMsg_Print_Params& print_params = params.params; | 1271 const PrintMsg_Print_Params& print_params = params.params; |
1271 | 1272 |
1272 // TODO(vitalybuka): should be page_count or valid pages from params.pages. | 1273 // TODO(vitalybuka): should be page_count or valid pages from params.pages. |
1273 // See http://crbug.com/161576 | 1274 // See http://crbug.com/161576 |
1274 Send(new PrintHostMsg_DidGetPrintedPagesCount(routing_id(), | 1275 Send(new PrintHostMsg_DidGetPrintedPagesCount(routing_id(), |
1275 print_params.document_cookie, | 1276 print_params.document_cookie, |
1276 page_count)); | 1277 page_count)); |
1277 if (print_params.preview_ui_id < 0) { | 1278 if (print_params.preview_ui_id < 0) { |
1278 // Printing for system dialog. | 1279 // Printing for system dialog. |
1279 int printed_count = params.pages.empty() ? page_count : params.pages.size(); | 1280 int printed_count = params.pages.empty() ? page_count : params.pages.size(); |
1280 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.SystemDialog", printed_count); | 1281 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.SystemDialog", printed_count); |
1281 } | 1282 } |
1282 #endif // !defined(OS_CHROMEOS) | 1283 #endif // !defined(OS_CHROMEOS) |
1283 | 1284 |
1284 if (!PrintPagesNative(prep_frame_view->frame(), prep_frame_view->node(), | 1285 if (!PrintPagesNative(prep_frame_view_->frame(), prep_frame_view_->node(), |
1285 page_count, prep_frame_view->GetPrintCanvasSize())) { | 1286 page_count, prep_frame_view_->GetPrintCanvasSize())) { |
1286 LOG(ERROR) << "Printing failed."; | 1287 LOG(ERROR) << "Printing failed."; |
1287 return DidFinishPrinting(FAIL_PRINT); | 1288 return DidFinishPrinting(FAIL_PRINT); |
1288 } | 1289 } |
1289 } | 1290 } |
1290 | 1291 |
| 1292 void PrintWebViewHelper::FinishFramePrinting() { |
| 1293 prep_frame_view_.reset(); |
| 1294 } |
| 1295 |
1291 #if defined(OS_MACOSX) || defined(OS_WIN) | 1296 #if defined(OS_MACOSX) || defined(OS_WIN) |
1292 bool PrintWebViewHelper::PrintPagesNative(WebKit::WebFrame* frame, | 1297 bool PrintWebViewHelper::PrintPagesNative(WebKit::WebFrame* frame, |
1293 const WebKit::WebNode& node, | 1298 const WebKit::WebNode& node, |
1294 int page_count, | 1299 int page_count, |
1295 const gfx::Size& canvas_size) { | 1300 const gfx::Size& canvas_size) { |
1296 const PrintMsg_PrintPages_Params& params = *print_pages_params_; | 1301 const PrintMsg_PrintPages_Params& params = *print_pages_params_; |
1297 const PrintMsg_Print_Params& print_params = params.params; | 1302 const PrintMsg_Print_Params& print_params = params.params; |
1298 | 1303 |
1299 PrintMsg_PrintPage_Params page_params; | 1304 PrintMsg_PrintPage_Params page_params; |
1300 page_params.params = print_params; | 1305 page_params.params = print_params; |
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1529 return false; | 1534 return false; |
1530 const PrintMsg_PrintPages_Params& params = *print_pages_params_; | 1535 const PrintMsg_PrintPages_Params& params = *print_pages_params_; |
1531 const PrintMsg_Print_Params& print_params = params.params; | 1536 const PrintMsg_Print_Params& print_params = params.params; |
1532 prep_frame_view_.reset( | 1537 prep_frame_view_.reset( |
1533 new PrepareFrameAndViewForPrint(print_params, frame, node, | 1538 new PrepareFrameAndViewForPrint(print_params, frame, node, |
1534 ignore_css_margins_)); | 1539 ignore_css_margins_)); |
1535 DCHECK(!print_pages_params_->params.selection_only || | 1540 DCHECK(!print_pages_params_->params.selection_only || |
1536 print_pages_params_->pages.empty()); | 1541 print_pages_params_->pages.empty()); |
1537 prep_frame_view_->CopySelectionIfNeeded( | 1542 prep_frame_view_->CopySelectionIfNeeded( |
1538 render_view()->GetWebkitPreferences(), | 1543 render_view()->GetWebkitPreferences(), |
1539 base::Bind(&PrintWebViewHelper::PrintPages, base::Unretained(this))); | 1544 base::Bind(&PrintWebViewHelper::OnFramePreparedForPrintPages, |
| 1545 base::Unretained(this))); |
1540 return true; | 1546 return true; |
1541 } | 1547 } |
1542 | 1548 |
1543 #if defined(OS_POSIX) | 1549 #if defined(OS_POSIX) |
1544 bool PrintWebViewHelper::CopyMetafileDataToSharedMem( | 1550 bool PrintWebViewHelper::CopyMetafileDataToSharedMem( |
1545 Metafile* metafile, | 1551 Metafile* metafile, |
1546 base::SharedMemoryHandle* shared_mem_handle) { | 1552 base::SharedMemoryHandle* shared_mem_handle) { |
1547 uint32 buf_size = metafile->GetDataSize(); | 1553 uint32 buf_size = metafile->GetDataSize(); |
1548 scoped_ptr<base::SharedMemory> shared_buf( | 1554 scoped_ptr<base::SharedMemory> shared_buf( |
1549 content::RenderThread::Get()->HostAllocateSharedMemoryBuffer( | 1555 content::RenderThread::Get()->HostAllocateSharedMemoryBuffer( |
(...skipping 385 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1935 } | 1941 } |
1936 | 1942 |
1937 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { | 1943 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { |
1938 prep_frame_view_.reset(); | 1944 prep_frame_view_.reset(); |
1939 metafile_.reset(); | 1945 metafile_.reset(); |
1940 pages_to_render_.clear(); | 1946 pages_to_render_.clear(); |
1941 error_ = PREVIEW_ERROR_NONE; | 1947 error_ = PREVIEW_ERROR_NONE; |
1942 } | 1948 } |
1943 | 1949 |
1944 } // namespace printing | 1950 } // namespace printing |
OLD | NEW |