Update printing code
This commit is contained in:
parent
6d32db32ef
commit
d309fd5a27
23 changed files with 665 additions and 485 deletions
|
@ -22,8 +22,7 @@
|
|||
#include "content/public/renderer/render_thread.h"
|
||||
#include "content/public/renderer/render_view.h"
|
||||
#include "net/base/escape.h"
|
||||
#include "printing/metafile.h"
|
||||
#include "printing/metafile_impl.h"
|
||||
#include "printing/pdf_metafile_skia.h"
|
||||
#include "printing/units.h"
|
||||
#include "skia/ext/vector_platform_device_skia.h"
|
||||
#include "third_party/WebKit/public/platform/WebSize.h"
|
||||
|
@ -65,7 +64,8 @@ bool PrintMsg_Print_Params_IsValid(const PrintMsg_Print_Params& params) {
|
|||
return !params.content_size.IsEmpty() && !params.page_size.IsEmpty() &&
|
||||
!params.printable_area.IsEmpty() && params.document_cookie &&
|
||||
params.desired_dpi && params.max_shrink && params.min_shrink &&
|
||||
params.dpi && (params.margin_top >= 0) && (params.margin_left >= 0);
|
||||
params.dpi && (params.margin_top >= 0) && (params.margin_left >= 0) &&
|
||||
params.dpi > kMinDpi && params.document_cookie != 0;
|
||||
}
|
||||
|
||||
PrintMsg_Print_Params GetCssPrintParams(
|
||||
|
@ -409,8 +409,6 @@ class PrepareFrameAndViewForPrint : public blink::WebViewClient,
|
|||
return expected_pages_count_;
|
||||
}
|
||||
|
||||
gfx::Size GetPrintCanvasSize() const;
|
||||
|
||||
void FinishPrinting();
|
||||
|
||||
bool IsLoadingSelection() {
|
||||
|
@ -590,12 +588,6 @@ void PrepareFrameAndViewForPrint::CallOnReady() {
|
|||
return on_ready_.Run(); // Can delete |this|.
|
||||
}
|
||||
|
||||
gfx::Size PrepareFrameAndViewForPrint::GetPrintCanvasSize() const {
|
||||
DCHECK(is_printing_started_);
|
||||
return gfx::Size(web_print_params_.printContentArea.width,
|
||||
web_print_params_.printContentArea.height);
|
||||
}
|
||||
|
||||
void PrepareFrameAndViewForPrint::RestoreSize() {
|
||||
if (frame()) {
|
||||
blink::WebView* web_view = frame_.GetFrame()->view();
|
||||
|
@ -606,7 +598,7 @@ void PrepareFrameAndViewForPrint::RestoreSize() {
|
|||
}
|
||||
|
||||
void PrepareFrameAndViewForPrint::FinishPrinting() {
|
||||
blink::WebFrame* frame = frame_.GetFrame();
|
||||
blink::WebLocalFrame* frame = frame_.GetFrame();
|
||||
if (frame) {
|
||||
blink::WebView* web_view = frame->view();
|
||||
if (is_printing_started_) {
|
||||
|
@ -630,10 +622,15 @@ void PrepareFrameAndViewForPrint::FinishPrinting() {
|
|||
PrintWebViewHelper::PrintWebViewHelper(content::RenderView* render_view)
|
||||
: content::RenderViewObserver(render_view),
|
||||
content::RenderViewObserverTracker<PrintWebViewHelper>(render_view),
|
||||
reset_prep_frame_view_(false),
|
||||
is_print_ready_metafile_sent_(false),
|
||||
ignore_css_margins_(false),
|
||||
is_scripted_printing_blocked_(false),
|
||||
notify_browser_of_print_failure_(true),
|
||||
print_for_preview_(false),
|
||||
print_node_in_progress_(false),
|
||||
is_loading_(false),
|
||||
is_scripted_preview_delayed_(false),
|
||||
weak_ptr_factory_(this) {
|
||||
}
|
||||
|
||||
|
@ -673,11 +670,13 @@ bool PrintWebViewHelper::GetPrintFrame(blink::WebLocalFrame** frame) {
|
|||
return true;
|
||||
}
|
||||
|
||||
#if !defined(DISABLE_BASIC_PRINTING)
|
||||
void PrintWebViewHelper::OnPrintPages(bool silent, bool print_background) {
|
||||
blink::WebLocalFrame* frame;
|
||||
if (GetPrintFrame(&frame))
|
||||
Print(frame, blink::WebNode(), silent, print_background);
|
||||
}
|
||||
#endif // !DISABLE_BASIC_PRINTING
|
||||
|
||||
void PrintWebViewHelper::GetPageSizeAndContentAreaFromPageLayout(
|
||||
const PageSizeMargins& page_layout_in_points,
|
||||
|
@ -726,9 +725,6 @@ void PrintWebViewHelper::PrintNode(const blink::WebNode& node) {
|
|||
}
|
||||
|
||||
print_node_in_progress_ = true;
|
||||
|
||||
// Make a copy of the node, in case RenderView::OnContextMenuClosed resets
|
||||
// its |context_menu_node_|.
|
||||
blink::WebNode duplicate_node(node);
|
||||
Print(duplicate_node.document().frame(), duplicate_node);
|
||||
|
||||
|
@ -783,7 +779,7 @@ void PrintWebViewHelper::DidFinishPrinting(PrintingResult result) {
|
|||
break;
|
||||
|
||||
case FAIL_PRINT:
|
||||
if (notify_browser_of_print_failure_ && print_pages_params_.get()) {
|
||||
if (notify_browser_of_print_failure_ && print_pages_params_) {
|
||||
int cookie = print_pages_params_->params.document_cookie;
|
||||
Send(new PrintHostMsg_PrintingFailed(routing_id(), cookie));
|
||||
}
|
||||
|
@ -821,8 +817,7 @@ void PrintWebViewHelper::PrintPages() {
|
|||
page_count));
|
||||
#endif // !defined(OS_CHROMEOS)
|
||||
|
||||
if (!PrintPagesNative(prep_frame_view_->frame(), page_count,
|
||||
prep_frame_view_->GetPrintCanvasSize())) {
|
||||
if (!PrintPagesNative(prep_frame_view_->frame(), page_count)) {
|
||||
LOG(ERROR) << "Printing failed.";
|
||||
return DidFinishPrinting(FAIL_PRINT);
|
||||
}
|
||||
|
@ -832,10 +827,9 @@ void PrintWebViewHelper::FinishFramePrinting() {
|
|||
prep_frame_view_.reset();
|
||||
}
|
||||
|
||||
#if defined(OS_MACOSX) || defined(OS_WIN)
|
||||
#if defined(OS_MACOSX)
|
||||
bool PrintWebViewHelper::PrintPagesNative(blink::WebFrame* frame,
|
||||
int page_count,
|
||||
const gfx::Size& canvas_size) {
|
||||
int page_count) {
|
||||
const PrintMsg_PrintPages_Params& params = *print_pages_params_;
|
||||
const PrintMsg_Print_Params& print_params = params.params;
|
||||
|
||||
|
@ -844,20 +838,20 @@ bool PrintWebViewHelper::PrintPagesNative(blink::WebFrame* frame,
|
|||
if (params.pages.empty()) {
|
||||
for (int i = 0; i < page_count; ++i) {
|
||||
page_params.page_number = i;
|
||||
PrintPageInternal(page_params, canvas_size, frame);
|
||||
PrintPageInternal(page_params, frame);
|
||||
}
|
||||
} else {
|
||||
for (size_t i = 0; i < params.pages.size(); ++i) {
|
||||
if (params.pages[i] >= page_count)
|
||||
break;
|
||||
page_params.page_number = params.pages[i];
|
||||
PrintPageInternal(page_params, canvas_size, frame);
|
||||
PrintPageInternal(page_params, frame);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif // OS_MACOSX || OS_WIN
|
||||
#endif // OS_MACOSX
|
||||
|
||||
// static - Not anonymous so that platform implementations can use it.
|
||||
void PrintWebViewHelper::ComputePageLayoutInPointsForCss(
|
||||
|
@ -886,13 +880,6 @@ bool PrintWebViewHelper::InitPrintSettings(bool fit_to_paper_size) {
|
|||
if (!PrintMsg_Print_Params_IsValid(settings.params))
|
||||
result = false;
|
||||
|
||||
if (result &&
|
||||
(settings.params.dpi < kMinDpi || settings.params.document_cookie == 0)) {
|
||||
// Invalid print page settings.
|
||||
NOTREACHED();
|
||||
result = false;
|
||||
}
|
||||
|
||||
// Reset to default values.
|
||||
ignore_css_margins_ = false;
|
||||
settings.pages.clear();
|
||||
|
@ -904,7 +891,7 @@ bool PrintWebViewHelper::InitPrintSettings(bool fit_to_paper_size) {
|
|||
blink::WebPrintScalingOptionFitToPrintableArea;
|
||||
}
|
||||
|
||||
print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings));
|
||||
SetPrintPagesParams(settings);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -923,8 +910,6 @@ bool PrintWebViewHelper::CalculateNumberOfPages(blink::WebLocalFrame* frame,
|
|||
PrepareFrameAndViewForPrint prepare(params, frame, node, ignore_css_margins_);
|
||||
prepare.StartPrinting();
|
||||
|
||||
Send(new PrintHostMsg_DidGetDocumentCookie(routing_id(),
|
||||
params.document_cookie));
|
||||
*number_of_pages = prepare.GetExpectedPageCount();
|
||||
return true;
|
||||
}
|
||||
|
@ -953,9 +938,8 @@ bool PrintWebViewHelper::GetPrintSettingsFromUser(blink::WebFrame* frame,
|
|||
new PrintHostMsg_ScriptedPrint(routing_id(), params, &print_settings);
|
||||
msg->EnableMessagePumping();
|
||||
Send(msg);
|
||||
print_pages_params_.reset(new PrintMsg_PrintPages_Params(print_settings));
|
||||
|
||||
print_pages_params_->params.print_scaling_option = scaling_option;
|
||||
print_settings.params.print_scaling_option = scaling_option;
|
||||
SetPrintPagesParams(print_settings);
|
||||
return (print_settings.params.dpi && print_settings.params.document_cookie);
|
||||
}
|
||||
|
||||
|
@ -965,9 +949,8 @@ bool PrintWebViewHelper::RenderPagesForPrint(blink::WebLocalFrame* frame,
|
|||
return false;
|
||||
const PrintMsg_PrintPages_Params& params = *print_pages_params_;
|
||||
const PrintMsg_Print_Params& print_params = params.params;
|
||||
prep_frame_view_.reset(
|
||||
new PrepareFrameAndViewForPrint(print_params, frame, node,
|
||||
ignore_css_margins_));
|
||||
prep_frame_view_.reset(new PrepareFrameAndViewForPrint(
|
||||
print_params, frame, node, ignore_css_margins_));
|
||||
DCHECK(!print_pages_params_->params.selection_only ||
|
||||
print_pages_params_->pages.empty());
|
||||
prep_frame_view_->CopySelectionIfNeeded(
|
||||
|
@ -979,24 +962,27 @@ bool PrintWebViewHelper::RenderPagesForPrint(blink::WebLocalFrame* frame,
|
|||
|
||||
#if defined(OS_POSIX)
|
||||
bool PrintWebViewHelper::CopyMetafileDataToSharedMem(
|
||||
Metafile* metafile,
|
||||
PdfMetafileSkia* metafile,
|
||||
base::SharedMemoryHandle* shared_mem_handle) {
|
||||
uint32 buf_size = metafile->GetDataSize();
|
||||
scoped_ptr<base::SharedMemory> shared_buf(
|
||||
content::RenderThread::Get()->HostAllocateSharedMemoryBuffer(
|
||||
buf_size).release());
|
||||
|
||||
if (shared_buf.get()) {
|
||||
if (shared_buf) {
|
||||
if (shared_buf->Map(buf_size)) {
|
||||
metafile->GetData(shared_buf->memory(), buf_size);
|
||||
shared_buf->GiveToProcess(base::GetCurrentProcessHandle(),
|
||||
shared_mem_handle);
|
||||
return true;
|
||||
return shared_buf->GiveToProcess(base::GetCurrentProcessHandle(),
|
||||
shared_mem_handle);
|
||||
}
|
||||
}
|
||||
NOTREACHED();
|
||||
return false;
|
||||
}
|
||||
#endif // defined(OS_POSIX)
|
||||
|
||||
void PrintWebViewHelper::SetPrintPagesParams(
|
||||
const PrintMsg_PrintPages_Params& settings) {
|
||||
print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings));
|
||||
}
|
||||
|
||||
} // namespace printing
|
||||
|
|
|
@ -7,13 +7,15 @@
|
|||
|
||||
#include <vector>
|
||||
|
||||
#include "base/callback.h"
|
||||
#include "base/gtest_prod_util.h"
|
||||
#include "base/memory/scoped_ptr.h"
|
||||
#include "base/memory/shared_memory.h"
|
||||
#include "base/memory/weak_ptr.h"
|
||||
#include "base/time/time.h"
|
||||
#include "content/public/renderer/render_view_observer.h"
|
||||
#include "content/public/renderer/render_view_observer_tracker.h"
|
||||
#include "printing/metafile_impl.h"
|
||||
#include "printing/pdf_metafile_skia.h"
|
||||
#include "third_party/WebKit/public/platform/WebCanvas.h"
|
||||
#include "third_party/WebKit/public/web/WebNode.h"
|
||||
#include "third_party/WebKit/public/web/WebPrintParams.h"
|
||||
|
@ -22,6 +24,7 @@
|
|||
struct PrintMsg_Print_Params;
|
||||
struct PrintMsg_PrintPage_Params;
|
||||
struct PrintMsg_PrintPages_Params;
|
||||
struct PrintHostMsg_SetOptionsFromDocument_Params;
|
||||
|
||||
namespace base {
|
||||
class DictionaryValue;
|
||||
|
@ -81,8 +84,10 @@ class PrintWebViewHelper
|
|||
bool user_initiated) OVERRIDE;
|
||||
|
||||
// Message handlers ---------------------------------------------------------
|
||||
#if !defined(DISABLE_BASIC_PRINTING)
|
||||
void OnPrintPages(bool silent, bool print_background);
|
||||
void OnPrintingDone(bool success);
|
||||
#endif // !DISABLE_BASIC_PRINTING
|
||||
|
||||
// Get |page_size| and |content_area| information from
|
||||
// |page_layout_in_points|.
|
||||
|
@ -125,20 +130,22 @@ class PrintWebViewHelper
|
|||
|
||||
void OnFramePreparedForPrintPages();
|
||||
void PrintPages();
|
||||
bool PrintPagesNative(blink::WebFrame* frame,
|
||||
int page_count,
|
||||
const gfx::Size& canvas_size);
|
||||
bool PrintPagesNative(blink::WebFrame* frame, int page_count);
|
||||
void FinishFramePrinting();
|
||||
|
||||
// Prints the page listed in |params|.
|
||||
#if defined(OS_LINUX) || defined(OS_ANDROID)
|
||||
void PrintPageInternal(const PrintMsg_PrintPage_Params& params,
|
||||
const gfx::Size& canvas_size,
|
||||
blink::WebFrame* frame,
|
||||
Metafile* metafile);
|
||||
PdfMetafileSkia* metafile);
|
||||
#elif defined(OS_WIN)
|
||||
void PrintPageInternal(const PrintMsg_PrintPage_Params& params,
|
||||
blink::WebFrame* frame,
|
||||
PdfMetafileSkia* metafile,
|
||||
gfx::Size* page_size_in_dpi,
|
||||
gfx::Rect* content_area_in_dpi);
|
||||
#else
|
||||
void PrintPageInternal(const PrintMsg_PrintPage_Params& params,
|
||||
const gfx::Size& canvas_size,
|
||||
blink::WebFrame* frame);
|
||||
#endif
|
||||
|
||||
|
@ -147,24 +154,15 @@ class PrintWebViewHelper
|
|||
const blink::WebNode& node);
|
||||
|
||||
// Platform specific helper function for rendering page(s) to |metafile|.
|
||||
#if defined(OS_WIN)
|
||||
#if defined(OS_MACOSX)
|
||||
void RenderPage(const PrintMsg_Print_Params& params,
|
||||
int page_number,
|
||||
blink::WebFrame* frame,
|
||||
bool is_preview,
|
||||
Metafile* metafile,
|
||||
double* scale_factor,
|
||||
gfx::Size* page_size_in_dpi,
|
||||
gfx::Rect* content_area_in_dpi);
|
||||
#elif defined(OS_MACOSX)
|
||||
void RenderPage(const PrintMsg_Print_Params& params,
|
||||
int page_number,
|
||||
blink::WebFrame* frame,
|
||||
bool is_preview,
|
||||
Metafile* metafile,
|
||||
PdfMetafileSkia* metafile,
|
||||
gfx::Size* page_size,
|
||||
gfx::Rect* content_rect);
|
||||
#endif // defined(OS_WIN)
|
||||
#endif // defined(OS_MACOSX)
|
||||
|
||||
// Renders page contents from |frame| to |content_area| of |canvas|.
|
||||
// |page_number| is zero-based.
|
||||
|
@ -179,7 +177,7 @@ class PrintWebViewHelper
|
|||
|
||||
// Helper methods -----------------------------------------------------------
|
||||
|
||||
bool CopyMetafileDataToSharedMem(Metafile* metafile,
|
||||
bool CopyMetafileDataToSharedMem(PdfMetafileSkia* metafile,
|
||||
base::SharedMemoryHandle* shared_mem_handle);
|
||||
|
||||
// Helper method to get page layout in points and fit to page if needed.
|
||||
|
@ -195,18 +193,35 @@ class PrintWebViewHelper
|
|||
|
||||
// Script Initiated Printing ------------------------------------------------
|
||||
|
||||
void SetPrintPagesParams(const PrintMsg_PrintPages_Params& settings);
|
||||
|
||||
// WebView used only to print the selection.
|
||||
scoped_ptr<PrepareFrameAndViewForPrint> prep_frame_view_;
|
||||
bool reset_prep_frame_view_;
|
||||
|
||||
scoped_ptr<PrintMsg_PrintPages_Params> print_pages_params_;
|
||||
bool is_print_ready_metafile_sent_;
|
||||
bool ignore_css_margins_;
|
||||
|
||||
// Used for scripted initiated printing blocking.
|
||||
bool is_scripted_printing_blocked_;
|
||||
|
||||
// Let the browser process know of a printing failure. Only set to false when
|
||||
// the failure came from the browser in the first place.
|
||||
bool notify_browser_of_print_failure_;
|
||||
|
||||
// True, when printing from print preview.
|
||||
bool print_for_preview_;
|
||||
|
||||
bool print_node_in_progress_;
|
||||
bool is_loading_;
|
||||
bool is_scripted_preview_delayed_;
|
||||
|
||||
// Used to fix a race condition where the source is a PDF and print preview
|
||||
// hangs because RequestPrintPreview is called before DidStopLoading() is
|
||||
// called. This is a store for the RequestPrintPreview() call and its
|
||||
// parameters so that it can be invoked after DidStopLoading.
|
||||
base::Closure on_stop_loading_closure_;
|
||||
|
||||
base::WeakPtrFactory<PrintWebViewHelper> weak_ptr_factory_;
|
||||
|
||||
|
|
|
@ -8,10 +8,9 @@
|
|||
#include "base/memory/scoped_ptr.h"
|
||||
#include "chrome/common/print_messages.h"
|
||||
#include "content/public/renderer/render_thread.h"
|
||||
#include "printing/metafile.h"
|
||||
#include "printing/metafile_impl.h"
|
||||
#include "printing/metafile_skia_wrapper.h"
|
||||
#include "printing/page_size_margins.h"
|
||||
#include "printing/pdf_metafile_skia.h"
|
||||
#include "skia/ext/platform_device.h"
|
||||
#include "skia/ext/vector_canvas.h"
|
||||
#include "third_party/WebKit/public/web/WebLocalFrame.h"
|
||||
|
@ -27,9 +26,8 @@ namespace printing {
|
|||
using blink::WebFrame;
|
||||
|
||||
bool PrintWebViewHelper::PrintPagesNative(blink::WebFrame* frame,
|
||||
int page_count,
|
||||
const gfx::Size& canvas_size) {
|
||||
NativeMetafile metafile;
|
||||
int page_count) {
|
||||
PdfMetafileSkia metafile;
|
||||
if (!metafile.Init())
|
||||
return false;
|
||||
|
||||
|
@ -56,7 +54,7 @@ bool PrintWebViewHelper::PrintPagesNative(blink::WebFrame* frame,
|
|||
page_params.params = params.params;
|
||||
for (size_t i = 0; i < printed_pages.size(); ++i) {
|
||||
page_params.page_number = printed_pages[i];
|
||||
PrintPageInternal(page_params, canvas_size, frame, &metafile);
|
||||
PrintPageInternal(page_params, frame, &metafile);
|
||||
}
|
||||
|
||||
// blink::printEnd() for PDF should be called before metafile is closed.
|
||||
|
@ -119,9 +117,8 @@ bool PrintWebViewHelper::PrintPagesNative(blink::WebFrame* frame,
|
|||
|
||||
void PrintWebViewHelper::PrintPageInternal(
|
||||
const PrintMsg_PrintPage_Params& params,
|
||||
const gfx::Size& canvas_size,
|
||||
WebFrame* frame,
|
||||
Metafile* metafile) {
|
||||
PdfMetafileSkia* metafile) {
|
||||
PageSizeMargins page_layout_in_points;
|
||||
double scale_factor = 1.0f;
|
||||
ComputePageLayoutInPointsForCss(frame, params.page_number, params.params,
|
||||
|
|
|
@ -10,8 +10,6 @@
|
|||
#include "base/mac/scoped_nsautorelease_pool.h"
|
||||
#include "base/metrics/histogram.h"
|
||||
#include "chrome/common/print_messages.h"
|
||||
#include "printing/metafile.h"
|
||||
#include "printing/metafile_impl.h"
|
||||
#include "printing/metafile_skia_wrapper.h"
|
||||
#include "printing/page_size_margins.h"
|
||||
#include "skia/ext/platform_device.h"
|
||||
|
@ -25,9 +23,8 @@ using blink::WebFrame;
|
|||
|
||||
void PrintWebViewHelper::PrintPageInternal(
|
||||
const PrintMsg_PrintPage_Params& params,
|
||||
const gfx::Size& canvas_size,
|
||||
WebFrame* frame) {
|
||||
NativeMetafile metafile;
|
||||
PdfMetafileSkia metafile;
|
||||
if (!metafile.Init())
|
||||
return;
|
||||
|
||||
|
@ -54,10 +51,13 @@ void PrintWebViewHelper::PrintPageInternal(
|
|||
Send(new PrintHostMsg_DidPrintPage(routing_id(), page_params));
|
||||
}
|
||||
|
||||
void PrintWebViewHelper::RenderPage(
|
||||
const PrintMsg_Print_Params& params, int page_number, WebFrame* frame,
|
||||
bool is_preview, Metafile* metafile, gfx::Size* page_size,
|
||||
gfx::Rect* content_rect) {
|
||||
void PrintWebViewHelper::RenderPage(const PrintMsg_Print_Params& params,
|
||||
int page_number,
|
||||
WebFrame* frame,
|
||||
bool is_preview,
|
||||
PdfMetafileSkia* metafile,
|
||||
gfx::Size* page_size,
|
||||
gfx::Rect* content_rect) {
|
||||
double scale_factor = 1.0f;
|
||||
double webkit_shrink_factor = frame->getPrintPageShrink(page_number);
|
||||
PageSizeMargins page_layout_in_points;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue