diff options
-rw-r--r-- | chrome/renderer/chrome_mock_render_thread.cc | 5 | ||||
-rw-r--r-- | chrome/renderer/mock_printer.cc | 4 | ||||
-rw-r--r-- | chrome/renderer/mock_printer.h | 3 | ||||
-rw-r--r-- | chrome/renderer/printing/print_web_view_helper.cc | 113 | ||||
-rw-r--r-- | chrome/renderer/printing/print_web_view_helper.h | 32 | ||||
-rw-r--r-- | chrome/renderer/printing/print_web_view_helper_browsertest.cc | 73 | ||||
-rw-r--r-- | chrome/renderer/printing/print_web_view_helper_linux.cc | 3 | ||||
-rw-r--r-- | chrome/renderer/printing/print_web_view_helper_mac.mm | 4 | ||||
-rw-r--r-- | chrome/renderer/printing/print_web_view_helper_win.cc | 4 | ||||
-rw-r--r-- | printing/print_job_constants.cc | 3 | ||||
-rw-r--r-- | printing/print_job_constants.h | 1 | ||||
-rw-r--r-- | printing/printing_context.cc | 5 |
12 files changed, 190 insertions, 60 deletions
diff --git a/chrome/renderer/chrome_mock_render_thread.cc b/chrome/renderer/chrome_mock_render_thread.cc index 79b1f73..d80d4c8 100644 --- a/chrome/renderer/chrome_mock_render_thread.cc +++ b/chrome/renderer/chrome_mock_render_thread.cc @@ -184,6 +184,11 @@ void ChromeMockRenderThread::OnUpdatePrintSettings( } std::vector<int> pages(printing::PageRange::GetPages(new_ranges)); printer_->UpdateSettings(document_cookie, params, pages, margins_type); + + job_settings.GetBoolean(printing::kSettingShouldPrintSelectionOnly, + ¶ms->params.selection_only); + job_settings.GetBoolean(printing::kSettingShouldPrintBackgrounds, + ¶ms->params.should_print_backgrounds); } MockPrinter* ChromeMockRenderThread::printer() { diff --git a/chrome/renderer/mock_printer.cc b/chrome/renderer/mock_printer.cc index 881e56b..cf4ec46 100644 --- a/chrome/renderer/mock_printer.cc +++ b/chrome/renderer/mock_printer.cc @@ -57,6 +57,7 @@ MockPrinter::MockPrinter() min_shrink_(1.25), desired_dpi_(printing::kPointsPerInch), selection_only_(false), + should_print_backgrounds_(false), document_cookie_(-1), current_document_cookie_(0), printer_status_(PRINTER_READY), @@ -107,6 +108,7 @@ void MockPrinter::SetDefaultPrintSettings(const PrintMsg_Print_Params& params) { min_shrink_ = params.min_shrink; desired_dpi_ = params.desired_dpi; selection_only_ = params.selection_only; + should_print_backgrounds_ = params.should_print_backgrounds; page_size_ = params.page_size; content_size_ = params.content_size; printable_area_ = params.printable_area; @@ -146,6 +148,7 @@ void MockPrinter::ScriptedPrint(int cookie, settings->params.min_shrink = min_shrink_; settings->params.desired_dpi = desired_dpi_; settings->params.selection_only = selection_only_; + settings->params.should_print_backgrounds = should_print_backgrounds_; settings->params.document_cookie = document_cookie_; settings->params.page_size = page_size_; settings->params.content_size = content_size_; @@ -298,6 +301,7 @@ void MockPrinter::SetPrintParams(PrintMsg_Print_Params* params) { params->min_shrink = min_shrink_; params->desired_dpi = desired_dpi_; params->selection_only = selection_only_; + params->should_print_backgrounds = should_print_backgrounds_; params->document_cookie = document_cookie_; params->page_size = page_size_; params->content_size = content_size_; diff --git a/chrome/renderer/mock_printer.h b/chrome/renderer/mock_printer.h index 234094a..625c140 100644 --- a/chrome/renderer/mock_printer.h +++ b/chrome/renderer/mock_printer.h @@ -127,6 +127,9 @@ class MockPrinter { // Print selection. bool selection_only_; + // Print css backgrounds. + bool should_print_backgrounds_; + // Cookie for the document to ensure correctness. int document_cookie_; int current_document_cookie_; diff --git a/chrome/renderer/printing/print_web_view_helper.cc b/chrome/renderer/printing/print_web_view_helper.cc index e6265f2..19e4fc1 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,24 @@ 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; + + 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 +967,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,7 +1637,7 @@ bool PrintWebViewHelper::PreviewPageRendered(int page_number, } PrintWebViewHelper::PrintPreviewContext::PrintPreviewContext() - : frame_(NULL), + : source_frame_(NULL), total_page_count_(0), current_page_index_(0), generate_draft_pages_(true), @@ -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,23 +1674,13 @@ 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(); @@ -1674,6 +1690,13 @@ bool PrintWebViewHelper::PrintPreviewContext::CreatePreviewDocument( 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; @@ -1765,7 +1788,7 @@ 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::IsLastPageOfPrintReadyMetafile() @@ -1790,16 +1813,30 @@ 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 { diff --git a/chrome/renderer/printing/print_web_view_helper.h b/chrome/renderer/printing/print_web_view_helper.h index f75885c..439e824 100644 --- a/chrome/renderer/printing/print_web_view_helper.h +++ b/chrome/renderer/printing/print_web_view_helper.h @@ -138,6 +138,12 @@ class PrintWebViewHelper // Start the process of generating a print preview using |settings|. void OnPrintPreview(const base::DictionaryValue& settings); + // Prepare frame for creating preview document. + void PrepareFrameForPreviewDocument(); + + // Continue creating preview document. + void OnFramePreparedForPreviewDocument(); + // Initialize the print preview document. bool CreatePreviewDocument(); @@ -338,6 +344,8 @@ class PrintWebViewHelper scoped_ptr<base::DictionaryValue> header_footer_info_; // Keeps track of the state of print preview between messages. + // TODO(vitalybuka): Create PrintPreviewContext when needed and delete after + // use. Now it's interaction with various messages is confusing. class PrintPreviewContext { public: PrintPreviewContext(); @@ -352,9 +360,9 @@ class PrintWebViewHelper void OnPrintPreview(); // Create the print preview document. |pages| is empty to print all pages. - bool CreatePreviewDocument(const PrintMsg_Print_Params& params, - const std::vector<int>& pages, - bool ignore_css_margins); + // Takes ownership of |prepared_frame|. + bool CreatePreviewDocument(PrepareFrameAndViewForPrint* prepared_frame, + const std::vector<int>& pages); // Called after a page gets rendered. |page_time| is how long the // rendering took. @@ -384,8 +392,18 @@ class PrintWebViewHelper void set_error(enum PrintPreviewErrorBuckets error); // Getters - WebKit::WebFrame* frame(); - const WebKit::WebNode& node() const; + // Original frame for which preview was requested. + WebKit::WebFrame* source_frame(); + // Original node for which preview was requested. + const WebKit::WebNode& source_node() const; + + // Frame to be use to render preview. May be the same as source_frame(), or + // generated from it, e.g. copy of selected block. + WebKit::WebFrame* prepared_frame(); + // Node to be use to render preview. May be the same as source_node(), or + // generated from it, e.g. copy of selected block. + const WebKit::WebNode& prepared_node() const; + int total_page_count() const; bool generate_draft_pages() const; PreviewMetafile* metafile(); @@ -404,8 +422,8 @@ class PrintWebViewHelper void ClearContext(); // Specifies what to render for print preview. - WebKit::WebFrame* frame_; - WebKit::WebNode node_; + WebKit::WebFrame* source_frame_; + WebKit::WebNode source_node_; scoped_ptr<PrepareFrameAndViewForPrint> prep_frame_view_; scoped_ptr<PreviewMetafile> metafile_; diff --git a/chrome/renderer/printing/print_web_view_helper_browsertest.cc b/chrome/renderer/printing/print_web_view_helper_browsertest.cc index 07cc657..acd929f 100644 --- a/chrome/renderer/printing/print_web_view_helper_browsertest.cc +++ b/chrome/renderer/printing/print_web_view_helper_browsertest.cc @@ -13,6 +13,7 @@ #include "testing/gtest/include/gtest/gtest.h" #include "third_party/WebKit/Source/Platform/chromium/public/WebString.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" +#include "third_party/WebKit/Source/WebKit/chromium/public/WebRange.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" #if defined(OS_WIN) || defined(OS_MACOSX) @@ -30,6 +31,17 @@ namespace { // A simple web page. const char kHelloWorldHTML[] = "<body><p>Hello World!</p></body>"; +// HTML with 3 pages. +const char kMultipageHTML[] = + "<html><head><style>" + ".break { page-break-after: always; }" + "</style></head>" + "<body>" + "<div class='break'>page1</div>" + "<div class='break'>page2</div>" + "<div>page3</div>" + "</body></html>"; + // A simple web page with print page size css. const char kHTMLWithPageSizeCss[] = "<html><head><style>" @@ -87,6 +99,8 @@ void CreatePrintSettingsDictionary(DictionaryValue* dict) { dict->SetBoolean(kSettingPreviewModifiable, false); dict->SetBoolean(kSettingHeaderFooterEnabled, false); dict->SetBoolean(kSettingGenerateDraftData, true); + dict->SetBoolean(kSettingShouldPrintBackgrounds, false); + dict->SetBoolean(kSettingShouldPrintSelectionOnly, false); } } // namespace @@ -101,7 +115,6 @@ class PrintWebViewHelperTestBase : public ChromeRenderViewTest { // according to the specified settings defined in the mock render thread. // Verify the page count is correct. void VerifyPageCount(int count) { - ProcessPendingMessages(); #if defined(OS_CHROMEOS) // The DidGetPrintedPagesCount message isn't sent on ChromeOS. Right now we // always print all pages, and there are checks to that effect built into @@ -118,9 +131,22 @@ class PrintWebViewHelperTestBase : public ChromeRenderViewTest { #endif // defined(OS_CHROMEOS) } + // The renderer should be done calculating the number of rendered pages + // according to the specified settings defined in the mock render thread. + // Verify the page count is correct. + void VerifyPreviewPageCount(int count) { + const IPC::Message* page_cnt_msg = + render_thread_->sink().GetUniqueMessageMatching( + PrintHostMsg_DidGetPreviewPageCount::ID); + ASSERT_TRUE(page_cnt_msg); + PrintHostMsg_DidGetPreviewPageCount::Param post_page_count_param; + PrintHostMsg_DidGetPreviewPageCount::Read(page_cnt_msg, + &post_page_count_param); + EXPECT_EQ(count, post_page_count_param.a.page_count); + } + // Verifies whether the pages printed or not. void VerifyPagesPrinted(bool printed) { - ProcessPendingMessages(); #if defined(OS_CHROMEOS) bool did_print_msg = (render_thread_->sink().GetUniqueMessageMatching( PrintHostMsg_TempFileForPrintingWritten::ID) != NULL); @@ -138,15 +164,21 @@ class PrintWebViewHelperTestBase : public ChromeRenderViewTest { } #endif // defined(OS_CHROMEOS) } + void OnPrintPages() { + PrintWebViewHelper::Get(view_)->OnPrintPages(); + ProcessPendingMessages(); + } void OnPrintPreview(const DictionaryValue& dict) { PrintWebViewHelper* print_web_view_helper = PrintWebViewHelper::Get(view_); print_web_view_helper->OnInitiatePrintPreview(); print_web_view_helper->OnPrintPreview(dict); + ProcessPendingMessages(); } void OnPrintForPrintPreview(const DictionaryValue& dict) { PrintWebViewHelper::Get(view_)->OnPrintForPrintPreview(dict); + ProcessPendingMessages(); } DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelperTestBase); @@ -169,7 +201,7 @@ class PrintWebViewHelperTest : public PrintWebViewHelperTestBase { // that channel all works. TEST_F(PrintWebViewHelperTest, OnPrintPages) { LoadHTML(kHelloWorldHTML); - PrintWebViewHelper::Get(view_)->OnPrintPages(); + OnPrintPages(); VerifyPageCount(1); VerifyPagesPrinted(true); @@ -241,6 +273,7 @@ TEST_F(PrintWebViewHelperTest, AllowUserOriginatedPrinting) { SendWebMouseEvent(mouse_event); mouse_event.type = WebKit::WebInputEvent::MouseUp; SendWebMouseEvent(mouse_event); + ProcessPendingMessages(); VerifyPageCount(1); VerifyPagesPrinted(true); @@ -285,7 +318,7 @@ TEST_F(PrintWebViewHelperTest, PrintWithIframe) { view_->GetWebView()->mainFrame()); // Initiate printing. - PrintWebViewHelper::Get(view_)->OnPrintPages(); + OnPrintPages(); VerifyPagesPrinted(true); // Verify output through MockPrinter. @@ -350,7 +383,7 @@ TEST_F(PrintWebViewHelperTest, PrintLayoutTest) { for (size_t i = 0; i < arraysize(kTestPages); ++i) { // Load an HTML page and print it. LoadHTML(kTestPages[i].page); - PrintWebViewHelper::Get(view_)->OnPrintPages(); + OnPrintPages(); VerifyPagesPrinted(true); // MockRenderThread::Send() just calls MockRenderThread::OnReceived(). @@ -443,7 +476,6 @@ class PrintWebViewHelperPreviewTest : public PrintWebViewHelperTestBase { } void VerifyPrintFailed(bool did_fail) { - ProcessPendingMessages(); bool print_failed = (render_thread_->sink().GetUniqueMessageMatching( PrintHostMsg_PrintingFailed::ID) != NULL); EXPECT_EQ(did_fail, print_failed); @@ -710,7 +742,7 @@ TEST_F(PrintWebViewHelperPreviewTest, PrintToPDFSelectedHonorOrientationCss) { // Test to verify that complete metafile is generated for a subset of pages // without creating draft pages. TEST_F(PrintWebViewHelperPreviewTest, OnPrintPreviewForSelectedPages) { - LoadHTML(kHelloWorldHTML); + LoadHTML(kMultipageHTML); // Fill in some dummy values. DictionaryValue dict; @@ -720,8 +752,8 @@ TEST_F(PrintWebViewHelperPreviewTest, OnPrintPreviewForSelectedPages) { // metafile with the selected pages. Page numbers used in the dictionary // are 1-based. DictionaryValue* page_range = new DictionaryValue(); - page_range->SetInteger(kSettingPageRangeFrom, 1); - page_range->SetInteger(kSettingPageRangeTo, 1); + page_range->SetInteger(kSettingPageRangeFrom, 2); + page_range->SetInteger(kSettingPageRangeTo, 3); ListValue* page_range_array = new ListValue(); page_range_array->Append(page_range); @@ -731,9 +763,30 @@ TEST_F(PrintWebViewHelperPreviewTest, OnPrintPreviewForSelectedPages) { OnPrintPreview(dict); - // Verify that we did not create the draft metafile for the first page. VerifyDidPreviewPage(false, 0); + VerifyDidPreviewPage(false, 1); + VerifyDidPreviewPage(false, 2); + VerifyPreviewPageCount(3); + VerifyPrintPreviewCancelled(false); + VerifyPrintPreviewFailed(false); + VerifyPrintPreviewGenerated(true); + VerifyPagesPrinted(false); +} + +// Test to verify that preview generated only for one page. +TEST_F(PrintWebViewHelperPreviewTest, OnPrintPreviewForSelectedText) { + LoadHTML(kMultipageHTML); + GetMainFrame()->selectRange( + WebKit::WebRange::fromDocumentRange(GetMainFrame(), 1, 3)); + + // Fill in some dummy values. + DictionaryValue dict; + CreatePrintSettingsDictionary(&dict); + dict.SetBoolean(kSettingShouldPrintSelectionOnly, true); + + OnPrintPreview(dict); + VerifyPreviewPageCount(1); VerifyPrintPreviewCancelled(false); VerifyPrintPreviewFailed(false); VerifyPrintPreviewGenerated(true); diff --git a/chrome/renderer/printing/print_web_view_helper_linux.cc b/chrome/renderer/printing/print_web_view_helper_linux.cc index f97075c..eae3a7b 100644 --- a/chrome/renderer/printing/print_web_view_helper_linux.cc +++ b/chrome/renderer/printing/print_web_view_helper_linux.cc @@ -43,7 +43,8 @@ bool PrintWebViewHelper::RenderPreviewPage( base::TimeTicks begin_time = base::TimeTicks::Now(); PrintPageInternal(page_params, print_preview_context_.GetPrintCanvasSize(), - print_preview_context_.frame(), initial_render_metafile); + print_preview_context_.prepared_frame(), + initial_render_metafile); print_preview_context_.RenderedPreviewPage( base::TimeTicks::Now() - begin_time); if (draft_metafile.get()) { diff --git a/chrome/renderer/printing/print_web_view_helper_mac.mm b/chrome/renderer/printing/print_web_view_helper_mac.mm index 924860f..200ccaa 100644 --- a/chrome/renderer/printing/print_web_view_helper_mac.mm +++ b/chrome/renderer/printing/print_web_view_helper_mac.mm @@ -77,8 +77,8 @@ bool PrintWebViewHelper::RenderPreviewPage( base::TimeTicks begin_time = base::TimeTicks::Now(); gfx::Size page_size; - RenderPage(printParams, page_number, print_preview_context_.frame(), true, - initial_render_metafile, &page_size, NULL); + RenderPage(printParams, page_number, print_preview_context_.prepared_frame(), + true, initial_render_metafile, &page_size, NULL); print_preview_context_.RenderedPreviewPage( base::TimeTicks::Now() - begin_time); diff --git a/chrome/renderer/printing/print_web_view_helper_win.cc b/chrome/renderer/printing/print_web_view_helper_win.cc index f43be00..8767c2d 100644 --- a/chrome/renderer/printing/print_web_view_helper_win.cc +++ b/chrome/renderer/printing/print_web_view_helper_win.cc @@ -103,8 +103,8 @@ bool PrintWebViewHelper::RenderPreviewPage( } base::TimeTicks begin_time = base::TimeTicks::Now(); - RenderPage(print_params, page_number, print_preview_context_.frame(), true, - initial_render_metafile, &actual_shrink, NULL, NULL); + RenderPage(print_params, page_number, print_preview_context_.prepared_frame(), + true, initial_render_metafile, &actual_shrink, NULL, NULL); print_preview_context_.RenderedPreviewPage( base::TimeTicks::Now() - begin_time); diff --git a/printing/print_job_constants.cc b/printing/print_job_constants.cc index 55397b6..cd0aaae 100644 --- a/printing/print_job_constants.cc +++ b/printing/print_job_constants.cc @@ -119,6 +119,9 @@ const char kSettingPrintToPDF[] = "printToPDF"; // Whether to print CSS backgrounds. const char kSettingShouldPrintBackgrounds[] = "shouldPrintBackgrounds"; +// Whether to print selection only. +const char kSettingShouldPrintSelectionOnly[] = "shouldPrintSelectionOnly"; + // Indices used to represent first preview page and complete preview document. const int FIRST_PAGE_INDEX = 0; const int COMPLETE_PREVIEW_DOCUMENT_INDEX = -1; diff --git a/printing/print_job_constants.h b/printing/print_job_constants.h index fcee819..065f3ff 100644 --- a/printing/print_job_constants.h +++ b/printing/print_job_constants.h @@ -48,6 +48,7 @@ PRINTING_EXPORT extern const char kSettingPrintableAreaHeight[]; PRINTING_EXPORT extern const char kSettingPrinterName[]; PRINTING_EXPORT extern const char kSettingPrintToPDF[]; PRINTING_EXPORT extern const char kSettingShouldPrintBackgrounds[]; +PRINTING_EXPORT extern const char kSettingShouldPrintSelectionOnly[]; PRINTING_EXPORT extern const int FIRST_PAGE_INDEX; PRINTING_EXPORT extern const int COMPLETE_PREVIEW_DOCUMENT_INDEX; diff --git a/printing/printing_context.cc b/printing/printing_context.cc index a29b31a..407e3bb 100644 --- a/printing/printing_context.cc +++ b/printing/printing_context.cc @@ -84,6 +84,11 @@ PrintingContext::Result PrintingContext::UpdatePrintSettings( NOTREACHED(); } + if (!job_settings.GetBoolean(kSettingShouldPrintSelectionOnly, + &settings_.selection_only)) { + NOTREACHED(); + } + return result; } |