Chromium Code Reviews| Index: chrome/renderer/printing/print_web_view_helper.cc |
| diff --git a/chrome/renderer/printing/print_web_view_helper.cc b/chrome/renderer/printing/print_web_view_helper.cc |
| index e6265f29b41526534d1792ee567b3a268775ba5b..2d7bc7be61e4ca7722939c27ba1480633ac027bd 100644 |
| --- a/chrome/renderer/printing/print_web_view_helper.cc |
| +++ b/chrome/renderer/printing/print_web_view_helper.cc |
| @@ -501,7 +501,7 @@ class PrepareFrameAndViewForPrint : public WebKit::WebViewClient, |
| return frame_; |
| } |
| - WebKit::WebNode node() const { |
| + const WebKit::WebNode& node() const { |
| return node_to_print_; |
| } |
| @@ -534,6 +534,7 @@ class PrepareFrameAndViewForPrint : public WebKit::WebViewClient, |
| base::Closure on_ready_; |
| bool should_print_backgrounds_; |
| bool should_print_selection_only_; |
| + bool is_printing_started_; |
| DISALLOW_COPY_AND_ASSIGN(PrepareFrameAndViewForPrint); |
| }; |
| @@ -549,7 +550,8 @@ PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( |
| should_close_web_view_(false), |
| expected_pages_count_(0), |
| should_print_backgrounds_(params.should_print_backgrounds), |
| - should_print_selection_only_(params.selection_only) { |
| + should_print_selection_only_(params.selection_only), |
| + is_printing_started_(false) { |
| PrintMsg_Print_Params print_params = params; |
| if (!should_print_selection_only_ || |
| !PrintingNodeOrPdfFrame(frame_, node_to_print_)) { |
| @@ -595,6 +597,7 @@ void PrepareFrameAndViewForPrint::StartPrinting() { |
| // https://bugs.webkit.org/show_bug.cgi?id=107718 is fixed. |
| expected_pages_count_ = frame_->printBegin(web_print_params_, node_to_print_, |
| NULL); |
| + is_printing_started_ = true; |
| } |
| void PrepareFrameAndViewForPrint::CopySelectionIfNeeded( |
| @@ -653,7 +656,8 @@ gfx::Size PrepareFrameAndViewForPrint::GetPrintCanvasSize() const { |
| void PrepareFrameAndViewForPrint::FinishPrinting() { |
| if (frame_) { |
| - frame_->printEnd(); |
| + if (is_printing_started_) |
| + frame_->printEnd(); |
| WebKit::WebView* web_view = frame_->view(); |
| web_view->resize(prev_view_size_); |
| if (WebKit::WebFrame* web_frame = web_view->mainFrame()) |
| @@ -813,13 +817,13 @@ void PrintWebViewHelper::OnPrintPages() { |
| } |
| void PrintWebViewHelper::OnPrintForSystemDialog() { |
| - WebKit::WebFrame* frame = print_preview_context_.frame(); |
| + WebKit::WebFrame* frame = print_preview_context_.source_frame(); |
| if (!frame) { |
| NOTREACHED(); |
| return; |
| } |
| - Print(frame, print_preview_context_.node()); |
| + Print(frame, print_preview_context_.source_node()); |
| } |
| void PrintWebViewHelper::GetPageSizeAndContentAreaFromPageLayout( |
| @@ -868,8 +872,8 @@ WebKit::WebPrintScalingOption PrintWebViewHelper::GetPrintScalingOption( |
| return WebKit::WebPrintScalingOptionNone; |
| bool no_plugin_scaling = |
| - print_preview_context_.frame()->isPrintScalingDisabledForPlugin( |
| - print_preview_context_.node()); |
| + print_preview_context_.source_frame()->isPrintScalingDisabledForPlugin( |
| + print_preview_context_.source_node()); |
| if (params.is_first_request && no_plugin_scaling) |
| return WebKit::WebPrintScalingOptionNone; |
| @@ -881,8 +885,8 @@ void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) { |
| DCHECK(is_preview_enabled_); |
| print_preview_context_.OnPrintPreview(); |
| - if (!UpdatePrintSettings(print_preview_context_.frame(), |
| - print_preview_context_.node(), settings)) { |
| + if (!UpdatePrintSettings(print_preview_context_.source_frame(), |
| + print_preview_context_.source_node(), settings)) { |
| if (print_preview_context_.last_error() != PREVIEW_ERROR_BAD_SETTING) { |
| Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings( |
| routing_id(), print_pages_params_->params.document_cookie)); |
| @@ -916,8 +920,8 @@ void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) { |
| // message to browser. |
| if (print_pages_params_->params.is_first_request && |
| !print_preview_context_.IsModifiable() && |
| - print_preview_context_.frame()->isPrintScalingDisabledForPlugin( |
| - print_preview_context_.node())) { |
| + print_preview_context_.source_frame()->isPrintScalingDisabledForPlugin( |
| + print_preview_context_.source_node())) { |
| Send(new PrintHostMsg_PrintPreviewScalingDisabled(routing_id())); |
| } |
| @@ -935,6 +939,25 @@ void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) { |
| } |
| print_preview_context_.set_generate_draft_pages(generate_draft_pages); |
| + PrepareFrameForPreviewDocument(); |
| +} |
| + |
| +void PrintWebViewHelper::PrepareFrameForPreviewDocument() { |
| + const PrintMsg_Print_Params& print_params = print_pages_params_->params; |
| + const std::vector<int>& pages = print_pages_params_->pages; |
|
Lei Zhang
2013/01/26 08:40:36
not used?
Vitaly Buka (NO REVIEWS)
2013/01/26 10:29:30
Done.
|
| + |
| + prep_frame_view_.reset( |
| + new PrepareFrameAndViewForPrint(print_params, |
| + print_preview_context_.source_frame(), |
| + print_preview_context_.source_node(), |
| + ignore_css_margins_)); |
| + prep_frame_view_->CopySelectionIfNeeded( |
| + render_view()->GetWebkitPreferences(), |
| + base::Bind(&PrintWebViewHelper::OnFramePreparedForPreviewDocument, |
| + base::Unretained(this))); |
| +} |
| + |
| +void PrintWebViewHelper::OnFramePreparedForPreviewDocument() { |
| if (CreatePreviewDocument()) { |
| DidFinishPrinting(OK); |
| } else { |
| @@ -945,22 +968,26 @@ void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) { |
| } |
| bool PrintWebViewHelper::CreatePreviewDocument() { |
| + if (CheckForCancel()) |
| + return false; |
| + |
| const PrintMsg_Print_Params& print_params = print_pages_params_->params; |
| const std::vector<int>& pages = print_pages_params_->pages; |
| - if (!print_preview_context_.CreatePreviewDocument(print_params, pages, |
| - ignore_css_margins_)) { |
| + |
| + if (!print_preview_context_.CreatePreviewDocument(prep_frame_view_.release(), |
| + pages)) { |
| return false; |
| } |
| PageSizeMargins default_page_layout; |
| - ComputePageLayoutInPointsForCss(print_preview_context_.frame(), 0, |
| + ComputePageLayoutInPointsForCss(print_preview_context_.prepared_frame(), 0, |
| print_params, ignore_css_margins_, NULL, |
| &default_page_layout); |
| if (!old_print_pages_params_.get() || |
| !PageLayoutIsEqual(*old_print_pages_params_, *print_pages_params_)) { |
| bool has_page_size_style = PrintingFrameHasPageSizeStyle( |
| - print_preview_context_.frame(), |
| + print_preview_context_.prepared_frame(), |
| print_preview_context_.total_page_count()); |
| int dpi = GetDPI(&print_params); |
| @@ -1611,8 +1638,7 @@ bool PrintWebViewHelper::PreviewPageRendered(int page_number, |
| } |
| PrintWebViewHelper::PrintPreviewContext::PrintPreviewContext() |
| - : frame_(NULL), |
| - total_page_count_(0), |
| + : source_frame_(NULL), |
| current_page_index_(0), |
| generate_draft_pages_(true), |
| print_ready_metafile_page_count_(0), |
| @@ -1628,8 +1654,8 @@ void PrintWebViewHelper::PrintPreviewContext::InitWithFrame( |
| DCHECK(web_frame); |
| DCHECK(!IsRendering()); |
| state_ = INITIALIZED; |
| - frame_ = web_frame; |
| - node_.reset(); |
| + source_frame_ = web_frame; |
| + source_node_.reset(); |
| } |
| void PrintWebViewHelper::PrintPreviewContext::InitWithNode( |
| @@ -1638,8 +1664,8 @@ void PrintWebViewHelper::PrintPreviewContext::InitWithNode( |
| DCHECK(web_node.document().frame()); |
| DCHECK(!IsRendering()); |
| state_ = INITIALIZED; |
| - frame_ = web_node.document().frame(); |
| - node_ = web_node; |
| + source_frame_ = web_node.document().frame(); |
| + source_node_ = web_node; |
| } |
| void PrintWebViewHelper::PrintPreviewContext::OnPrintPreview() { |
| @@ -1648,45 +1674,42 @@ void PrintWebViewHelper::PrintPreviewContext::OnPrintPreview() { |
| } |
| bool PrintWebViewHelper::PrintPreviewContext::CreatePreviewDocument( |
| - const PrintMsg_Print_Params& print_params, |
| - const std::vector<int>& pages, |
| - bool ignore_css_margins) { |
| + PrepareFrameAndViewForPrint* prepared_frame, |
| + const std::vector<int>& pages) { |
| DCHECK_EQ(INITIALIZED, state_); |
| state_ = RENDERING; |
| - metafile_.reset(new PreviewMetafile); |
| - if (!metafile_->Init()) { |
| - set_error(PREVIEW_ERROR_METAFILE_INIT_FAILED); |
| - LOG(ERROR) << "PreviewMetafile Init failed"; |
| - return false; |
| - } |
| - |
| // Need to make sure old object gets destroyed first. |
| - prep_frame_view_.reset( |
| - new PrepareFrameAndViewForPrint(print_params, frame(), node(), |
| - ignore_css_margins)); |
| + prep_frame_view_.reset(prepared_frame); |
| prep_frame_view_->StartPrinting(); |
| - total_page_count_ = prep_frame_view_->GetExpectedPageCount(); |
| - if (total_page_count_ == 0) { |
| + int total_page_count = prep_frame_view_->GetExpectedPageCount(); |
| + if (total_page_count == 0) { |
| LOG(ERROR) << "CreatePreviewDocument got 0 page count"; |
| set_error(PREVIEW_ERROR_ZERO_PAGES); |
| return false; |
| } |
| + metafile_.reset(new PreviewMetafile); |
| + if (!metafile_->Init()) { |
| + set_error(PREVIEW_ERROR_METAFILE_INIT_FAILED); |
| + LOG(ERROR) << "PreviewMetafile Init failed"; |
| + return false; |
| + } |
| + |
| int selected_page_count = pages.size(); |
| current_page_index_ = 0; |
| print_ready_metafile_page_count_ = selected_page_count; |
| pages_to_render_ = pages; |
| if (selected_page_count == 0) { |
| - print_ready_metafile_page_count_ = total_page_count_; |
| + print_ready_metafile_page_count_ = total_page_count; |
| // Render all pages. |
| - for (int i = 0; i < total_page_count_; ++i) |
| + for (int i = 0; i < total_page_count; ++i) |
| pages_to_render_.push_back(i); |
| } else if (generate_draft_pages_) { |
| int pages_index = 0; |
| - for (int i = 0; i < total_page_count_; ++i) { |
| + for (int i = 0; i < total_page_count; ++i) { |
| if (pages_index < selected_page_count && i == pages[pages_index]) { |
| pages_index++; |
| continue; |
| @@ -1765,7 +1788,13 @@ bool PrintWebViewHelper::PrintPreviewContext::IsRendering() const { |
| bool PrintWebViewHelper::PrintPreviewContext::IsModifiable() const { |
| // The only kind of node we can print right now is a PDF node. |
| - return !PrintingNodeOrPdfFrame(frame_, node_); |
| + return !PrintingNodeOrPdfFrame(source_frame_, source_node_); |
| +} |
| + |
| +bool PrintWebViewHelper::PrintPreviewContext::HasSelection() const { |
|
Lei Zhang
2013/01/26 08:40:36
Is this going to be used in a future CL?
Vitaly Buka (NO REVIEWS)
2013/01/26 10:29:30
I'll submit this with that changes
|
| + return IsModifiable() && source_frame_->hasSelection(); |
| + // The only kind of node we can print right now is a PDF node. |
|
Lei Zhang
2013/01/26 08:40:36
copy + paste error?
Vitaly Buka (NO REVIEWS)
2013/01/26 10:29:30
or bad merge
On 2013/01/26 08:40:36, Lei Zhang wro
|
| + return !PrintingNodeOrPdfFrame(source_frame_, source_node_); |
| } |
| bool PrintWebViewHelper::PrintPreviewContext::IsLastPageOfPrintReadyMetafile() |
| @@ -1790,21 +1819,35 @@ void PrintWebViewHelper::PrintPreviewContext::set_error( |
| error_ = error; |
| } |
| -WebKit::WebFrame* PrintWebViewHelper::PrintPreviewContext::frame() { |
| +WebKit::WebFrame* PrintWebViewHelper::PrintPreviewContext::source_frame() { |
| + // TODO(thestig) turn this back into a DCHECK when http://crbug.com/118303 is |
| + // resolved. |
| + CHECK(state_ != UNINITIALIZED); |
| + return source_frame_; |
| +} |
| + |
| +const WebKit::WebNode& |
| + PrintWebViewHelper::PrintPreviewContext::source_node() const { |
| + DCHECK(state_ != UNINITIALIZED); |
| + return source_node_; |
| +} |
| + |
| +WebKit::WebFrame* PrintWebViewHelper::PrintPreviewContext::prepared_frame() { |
| // TODO(thestig) turn this back into a DCHECK when http://crbug.com/118303 is |
| // resolved. |
| CHECK(state_ != UNINITIALIZED); |
| - return frame_; |
| + return prep_frame_view_->frame(); |
| } |
| -const WebKit::WebNode& PrintWebViewHelper::PrintPreviewContext::node() const { |
| +const WebKit::WebNode& |
| + PrintWebViewHelper::PrintPreviewContext::prepared_node() const { |
| DCHECK(state_ != UNINITIALIZED); |
| - return node_; |
| + return prep_frame_view_->node(); |
| } |
| int PrintWebViewHelper::PrintPreviewContext::total_page_count() const { |
| DCHECK(state_ != UNINITIALIZED); |
| - return total_page_count_; |
| + return prep_frame_view_->GetExpectedPageCount(); |
|
Lei Zhang
2013/01/26 08:40:36
Not sure if this works. In PrintWebViewHelper::OnP
Vitaly Buka (NO REVIEWS)
2013/01/26 10:29:30
You are correct, bat that's confusing.
Changed to
|
| } |
| bool PrintWebViewHelper::PrintPreviewContext::generate_draft_pages() const { |