refactor: move printing out of chromium_src (#15023)
* remove printing related things from chromium_src * chore: add printing build flag and patch * fix: include PrintingService on other platforms too * fix: printing_handler is only needed on Windows * fix: format BUILD.gn properly * fix: rename printing build flag to avoid conflict with chromium * fix: place previously missed printing calls behind build flag * fix: accidentally renamed flag in patch file * fix: don't include all printing strings * fix: allow ShowItemInFolder and OpenItem to block, fixing a DCHECK crash * fix: make things compile, some changes got lost while rebasing * fix: remove rogue line from BUILD.gn * chore: update patch description * style: lint fix * chore: use chromium printing buildflag, move node related stuff out of patch * revert: remove ScopedAllowBlockingForTesting call * fix: fix my rebase blooper * fix: re-add header lost during rebase, update patch * fix: add <map> include, tweak the patch a bit * revert: remove rogue diff from patch * fix: clean up after rebase
This commit is contained in:
parent
c806c465fa
commit
a82bcc7e3c
53 changed files with 1398 additions and 7358 deletions
File diff suppressed because it is too large
Load diff
|
@ -1,396 +0,0 @@
|
|||
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef CHROME_RENDERER_PRINTING_PRINT_WEB_VIEW_HELPER_H_
|
||||
#define CHROME_RENDERER_PRINTING_PRINT_WEB_VIEW_HELPER_H_
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "base/callback.h"
|
||||
#include "base/gtest_prod_util.h"
|
||||
#include "base/memory/shared_memory.h"
|
||||
#include "base/memory/weak_ptr.h"
|
||||
#include "base/time/time.h"
|
||||
#include "content/public/renderer/render_frame_observer.h"
|
||||
#include "content/public/renderer/render_frame_observer_tracker.h"
|
||||
#include "printing/pdf_metafile_skia.h"
|
||||
#include "third_party/blink/public/web/web_local_frame.h"
|
||||
#include "third_party/blink/public/web/web_node.h"
|
||||
#include "third_party/blink/public/web/web_print_params.h"
|
||||
#include "ui/gfx/geometry/size.h"
|
||||
|
||||
struct PrintMsg_Print_Params;
|
||||
struct PrintMsg_PrintPage_Params;
|
||||
struct PrintMsg_PrintPages_Params;
|
||||
struct PrintHostMsg_SetOptionsFromDocument_Params;
|
||||
|
||||
namespace base {
|
||||
class DictionaryValue;
|
||||
}
|
||||
|
||||
namespace blink {
|
||||
class WebView;
|
||||
}
|
||||
|
||||
namespace cc {
|
||||
class PaintCanvas;
|
||||
}
|
||||
|
||||
namespace printing {
|
||||
|
||||
struct PageSizeMargins;
|
||||
class PrepareFrameAndViewForPrint;
|
||||
|
||||
// Stores reference to frame using WebVew and unique name.
|
||||
// Workaround to modal dialog issue on Linux. crbug.com/236147.
|
||||
// If WebFrame someday supports WeakPtr, we should use it here.
|
||||
class FrameReference {
|
||||
public:
|
||||
explicit FrameReference(blink::WebLocalFrame* frame);
|
||||
FrameReference();
|
||||
~FrameReference();
|
||||
|
||||
void Reset(blink::WebLocalFrame* frame);
|
||||
|
||||
blink::WebLocalFrame* GetFrame();
|
||||
blink::WebView* view();
|
||||
|
||||
private:
|
||||
blink::WebView* view_;
|
||||
blink::WebLocalFrame* frame_;
|
||||
};
|
||||
|
||||
// PrintWebViewHelper handles most of the printing grunt work for RenderView.
|
||||
// We plan on making print asynchronous and that will require copying the DOM
|
||||
// of the document and creating a new WebView with the contents.
|
||||
class PrintWebViewHelper
|
||||
: public content::RenderFrameObserver,
|
||||
public content::RenderFrameObserverTracker<PrintWebViewHelper> {
|
||||
public:
|
||||
explicit PrintWebViewHelper(content::RenderFrame* render_frame);
|
||||
~PrintWebViewHelper() override;
|
||||
|
||||
void PrintNode(const blink::WebNode& node);
|
||||
|
||||
private:
|
||||
enum PrintingResult {
|
||||
OK,
|
||||
FAIL_PRINT_INIT,
|
||||
FAIL_PRINT,
|
||||
FAIL_PREVIEW,
|
||||
INVALID_SETTINGS,
|
||||
};
|
||||
|
||||
enum PrintPreviewErrorBuckets {
|
||||
PREVIEW_ERROR_NONE, // Always first.
|
||||
PREVIEW_ERROR_BAD_SETTING,
|
||||
PREVIEW_ERROR_METAFILE_COPY_FAILED,
|
||||
PREVIEW_ERROR_METAFILE_INIT_FAILED_DEPRECATED,
|
||||
PREVIEW_ERROR_ZERO_PAGES,
|
||||
PREVIEW_ERROR_MAC_DRAFT_METAFILE_INIT_FAILED_DEPRECATED,
|
||||
PREVIEW_ERROR_PAGE_RENDERED_WITHOUT_METAFILE,
|
||||
PREVIEW_ERROR_INVALID_PRINTER_SETTINGS,
|
||||
PREVIEW_ERROR_LAST_ENUM // Always last.
|
||||
};
|
||||
|
||||
// RenderFrameObserver implementation.
|
||||
bool OnMessageReceived(const IPC::Message& message) override;
|
||||
void OnDestruct() override;
|
||||
void ScriptedPrint(bool user_initiated) override;
|
||||
|
||||
// Message handlers ---------------------------------------------------------
|
||||
#if !defined(DISABLE_BASIC_PRINTING)
|
||||
void OnPrintPages(bool silent,
|
||||
bool print_background,
|
||||
const base::string16& device_name);
|
||||
void OnPrintingDone(bool success);
|
||||
#endif // !DISABLE_BASIC_PRINTING
|
||||
void OnPrintPreview(const base::DictionaryValue& settings);
|
||||
|
||||
// Get |page_size| and |content_area| information from
|
||||
// |page_layout_in_points|.
|
||||
void GetPageSizeAndContentAreaFromPageLayout(
|
||||
const PageSizeMargins& page_layout_in_points,
|
||||
gfx::Size* page_size,
|
||||
gfx::Rect* content_area);
|
||||
|
||||
// Update |ignore_css_margins_| based on settings.
|
||||
void UpdateFrameMarginsCssInfo(const base::DictionaryValue& settings);
|
||||
|
||||
// Prepare frame for creating preview document.
|
||||
void PrepareFrameForPreviewDocument();
|
||||
|
||||
// Continue creating preview document.
|
||||
void OnFramePreparedForPreviewDocument();
|
||||
|
||||
// Finalize the print ready preview document.
|
||||
bool FinalizePrintReadyDocument();
|
||||
|
||||
// Renders a print preview page. |page_number| is 0-based.
|
||||
// Returns true if print preview should continue, false on failure.
|
||||
bool RenderPreviewPage(int page_number,
|
||||
const PrintMsg_Print_Params& print_params);
|
||||
|
||||
// Initialize the print preview document.
|
||||
bool CreatePreviewDocument();
|
||||
|
||||
// Main printing code -------------------------------------------------------
|
||||
|
||||
void Print(blink::WebLocalFrame* frame,
|
||||
const blink::WebNode& node,
|
||||
bool silent = false,
|
||||
bool print_background = false,
|
||||
const base::string16& device_name = base::string16());
|
||||
|
||||
// Notification when printing is done - signal tear-down/free resources.
|
||||
void DidFinishPrinting(PrintingResult result);
|
||||
|
||||
// Print Settings -----------------------------------------------------------
|
||||
|
||||
// Initialize print page settings with default settings.
|
||||
// Used only for native printing workflow.
|
||||
bool InitPrintSettings(bool fit_to_paper_size,
|
||||
const base::string16& device_name = base::string16());
|
||||
|
||||
// Calculate number of pages in source document.
|
||||
bool CalculateNumberOfPages(
|
||||
blink::WebLocalFrame* frame,
|
||||
const blink::WebNode& node,
|
||||
int* number_of_pages,
|
||||
const base::string16& device_name = base::string16());
|
||||
|
||||
// Update the current print settings with new |passed_job_settings|.
|
||||
// |passed_job_settings| dictionary contains print job details such as printer
|
||||
// name, number of copies, page range, etc.
|
||||
bool UpdatePrintSettings(blink::WebLocalFrame* frame,
|
||||
const blink::WebNode& node,
|
||||
const base::DictionaryValue& passed_job_settings);
|
||||
|
||||
// Get final print settings from the user.
|
||||
// Return false if the user cancels or on error.
|
||||
bool GetPrintSettingsFromUser(blink::WebLocalFrame* frame,
|
||||
const blink::WebNode& node,
|
||||
int expected_pages_count);
|
||||
|
||||
// Page Printing / Rendering ------------------------------------------------
|
||||
|
||||
void OnFramePreparedForPrintPages();
|
||||
void PrintPages();
|
||||
bool PrintPagesNative(blink::WebLocalFrame* 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,
|
||||
blink::WebLocalFrame* frame,
|
||||
PdfMetafileSkia* metafile);
|
||||
#elif defined(OS_WIN)
|
||||
void PrintPageInternal(const PrintMsg_PrintPage_Params& params,
|
||||
blink::WebLocalFrame* frame,
|
||||
PdfMetafileSkia* metafile,
|
||||
gfx::Size* page_size_in_dpi,
|
||||
gfx::Rect* content_area_in_dpi);
|
||||
#else
|
||||
void PrintPageInternal(const PrintMsg_PrintPage_Params& params,
|
||||
blink::WebLocalFrame* frame);
|
||||
#endif
|
||||
|
||||
// Render the frame for printing.
|
||||
bool RenderPagesForPrint(blink::WebLocalFrame* frame,
|
||||
const blink::WebNode& node);
|
||||
|
||||
// Platform specific helper function for rendering page(s) to |metafile|.
|
||||
#if defined(OS_MACOSX)
|
||||
void RenderPage(const PrintMsg_Print_Params& params,
|
||||
int page_number,
|
||||
blink::WebLocalFrame* frame,
|
||||
PdfMetafileSkia* metafile,
|
||||
gfx::Size* page_size,
|
||||
gfx::Rect* content_rect);
|
||||
#endif // defined(OS_MACOSX)
|
||||
|
||||
// Renders page contents from |frame| to |content_area| of |canvas|.
|
||||
// |page_number| is zero-based.
|
||||
// When method is called, canvas should be setup to draw to |canvas_area|
|
||||
// with |scale_factor|.
|
||||
static float RenderPageContent(blink::WebLocalFrame* frame,
|
||||
int page_number,
|
||||
const gfx::Rect& canvas_area,
|
||||
const gfx::Rect& content_area,
|
||||
double scale_factor,
|
||||
cc::PaintCanvas* canvas);
|
||||
|
||||
// Helper methods -----------------------------------------------------------
|
||||
|
||||
bool CopyMetafileDataToSharedMem(const PdfMetafileSkia& metafile,
|
||||
base::SharedMemoryHandle* shared_mem_handle);
|
||||
|
||||
// Helper method to get page layout in points and fit to page if needed.
|
||||
static void ComputePageLayoutInPointsForCss(
|
||||
blink::WebLocalFrame* frame,
|
||||
int page_index,
|
||||
const PrintMsg_Print_Params& default_params,
|
||||
bool ignore_css_margins,
|
||||
double* scale_factor,
|
||||
PageSizeMargins* page_layout_in_points);
|
||||
|
||||
// Script Initiated Printing ------------------------------------------------
|
||||
|
||||
// Notifies the browser a print preview page has been rendered.
|
||||
// |page_number| is 0-based.
|
||||
// For a valid |page_number| with modifiable content,
|
||||
// |metafile| is the rendered page. Otherwise |metafile| is NULL.
|
||||
// Returns true if print preview should continue, false on failure.
|
||||
bool PreviewPageRendered(int page_number, PdfMetafileSkia* metafile);
|
||||
|
||||
void SetPrintPagesParams(const PrintMsg_PrintPages_Params& settings);
|
||||
|
||||
// WebView used only to print the selection.
|
||||
std::unique_ptr<PrepareFrameAndViewForPrint> prep_frame_view_;
|
||||
bool reset_prep_frame_view_;
|
||||
|
||||
std::unique_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_;
|
||||
|
||||
// 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();
|
||||
~PrintPreviewContext();
|
||||
|
||||
// Initializes the print preview context. Need to be called to set
|
||||
// the |web_frame| / |web_node| to generate the print preview for.
|
||||
void InitWithFrame(blink::WebLocalFrame* web_frame);
|
||||
void InitWithNode(const blink::WebNode& web_node);
|
||||
|
||||
// Does bookkeeping at the beginning of print preview.
|
||||
void OnPrintPreview();
|
||||
|
||||
// Create the print preview document. |pages| is empty to print all pages.
|
||||
// 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.
|
||||
void RenderedPreviewPage(const base::TimeDelta& page_time);
|
||||
|
||||
// Updates the print preview context when the required pages are rendered.
|
||||
void AllPagesRendered();
|
||||
|
||||
// Finalizes the print ready preview document.
|
||||
void FinalizePrintReadyDocument();
|
||||
|
||||
// Cleanup after print preview finishes.
|
||||
void Finished();
|
||||
|
||||
// Cleanup after print preview fails.
|
||||
void Failed(bool report_error);
|
||||
|
||||
// Helper functions
|
||||
int GetNextPageNumber();
|
||||
bool IsRendering() const;
|
||||
bool IsModifiable();
|
||||
bool HasSelection();
|
||||
bool IsLastPageOfPrintReadyMetafile() const;
|
||||
bool IsFinalPageRendered() const;
|
||||
|
||||
// Setters
|
||||
void set_generate_draft_pages(bool generate_draft_pages);
|
||||
void set_error(enum PrintPreviewErrorBuckets error);
|
||||
|
||||
// Getters
|
||||
// Original frame for which preview was requested.
|
||||
blink::WebLocalFrame* source_frame();
|
||||
// Original node for which preview was requested.
|
||||
const blink::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.
|
||||
blink::WebLocalFrame* 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 blink::WebNode& prepared_node() const;
|
||||
|
||||
int total_page_count() const;
|
||||
bool generate_draft_pages() const;
|
||||
PdfMetafileSkia* metafile();
|
||||
int last_error() const;
|
||||
|
||||
private:
|
||||
enum State {
|
||||
UNINITIALIZED, // Not ready to render.
|
||||
INITIALIZED, // Ready to render.
|
||||
RENDERING, // Rendering.
|
||||
DONE // Finished rendering.
|
||||
};
|
||||
|
||||
// Reset some of the internal rendering context.
|
||||
void ClearContext();
|
||||
|
||||
// Specifies what to render for print preview.
|
||||
FrameReference source_frame_;
|
||||
blink::WebNode source_node_;
|
||||
|
||||
std::unique_ptr<PrepareFrameAndViewForPrint> prep_frame_view_;
|
||||
std::unique_ptr<PdfMetafileSkia> metafile_;
|
||||
|
||||
// Total page count in the renderer.
|
||||
int total_page_count_;
|
||||
|
||||
// The current page to render.
|
||||
int current_page_index_;
|
||||
|
||||
// List of page indices that need to be rendered.
|
||||
std::vector<int> pages_to_render_;
|
||||
|
||||
// True, when draft pages needs to be generated.
|
||||
bool generate_draft_pages_;
|
||||
|
||||
// Specifies the total number of pages in the print ready metafile.
|
||||
int print_ready_metafile_page_count_;
|
||||
|
||||
base::TimeDelta document_render_time_;
|
||||
base::TimeTicks begin_time_;
|
||||
|
||||
enum PrintPreviewErrorBuckets error_;
|
||||
|
||||
State state_;
|
||||
};
|
||||
|
||||
bool print_node_in_progress_;
|
||||
bool is_loading_;
|
||||
bool is_scripted_preview_delayed_;
|
||||
int ipc_nesting_level_;
|
||||
|
||||
PrintPreviewContext print_preview_context_;
|
||||
|
||||
// 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_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelper);
|
||||
};
|
||||
|
||||
} // namespace printing
|
||||
|
||||
#endif // CHROME_RENDERER_PRINTING_PRINT_WEB_VIEW_HELPER_H_
|
|
@ -1,142 +0,0 @@
|
|||
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "chrome/renderer/printing/print_web_view_helper.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "base/logging.h"
|
||||
#include "chrome/common/print_messages.h"
|
||||
#include "content/public/renderer/render_thread.h"
|
||||
#include "printing/metafile_skia_wrapper.h"
|
||||
#include "printing/page_size_margins.h"
|
||||
#include "printing/pdf_metafile_skia.h"
|
||||
#include "third_party/blink/public/web/web_local_frame.h"
|
||||
|
||||
#if !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
|
||||
#include "base/process/process_handle.h"
|
||||
#else
|
||||
#include "base/file_descriptor_posix.h"
|
||||
#endif // !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
|
||||
|
||||
namespace printing {
|
||||
|
||||
using blink::WebLocalFrame;
|
||||
|
||||
bool PrintWebViewHelper::RenderPreviewPage(
|
||||
int page_number,
|
||||
const PrintMsg_Print_Params& print_params) {
|
||||
PrintMsg_PrintPage_Params page_params;
|
||||
page_params.params = print_params;
|
||||
page_params.page_number = page_number;
|
||||
std::unique_ptr<PdfMetafileSkia> draft_metafile;
|
||||
PdfMetafileSkia* initial_render_metafile = print_preview_context_.metafile();
|
||||
if (print_preview_context_.IsModifiable() && is_print_ready_metafile_sent_) {
|
||||
draft_metafile.reset(new PdfMetafileSkia());
|
||||
initial_render_metafile = draft_metafile.get();
|
||||
}
|
||||
|
||||
base::TimeTicks begin_time = base::TimeTicks::Now();
|
||||
PrintPageInternal(page_params, print_preview_context_.prepared_frame(),
|
||||
initial_render_metafile);
|
||||
print_preview_context_.RenderedPreviewPage(base::TimeTicks::Now() -
|
||||
begin_time);
|
||||
if (draft_metafile.get()) {
|
||||
draft_metafile->FinishDocument();
|
||||
} else if (print_preview_context_.IsModifiable() &&
|
||||
print_preview_context_.generate_draft_pages()) {
|
||||
DCHECK(!draft_metafile.get());
|
||||
draft_metafile =
|
||||
print_preview_context_.metafile()->GetMetafileForCurrentPage(
|
||||
SkiaDocumentType::PDF);
|
||||
}
|
||||
return PreviewPageRendered(page_number, draft_metafile.get());
|
||||
}
|
||||
|
||||
bool PrintWebViewHelper::PrintPagesNative(blink::WebLocalFrame* frame,
|
||||
int page_count) {
|
||||
PdfMetafileSkia metafile;
|
||||
if (!metafile.Init())
|
||||
return false;
|
||||
|
||||
const PrintMsg_PrintPages_Params& params = *print_pages_params_;
|
||||
std::vector<int> printed_pages;
|
||||
|
||||
if (params.pages.empty()) {
|
||||
for (int i = 0; i < page_count; ++i) {
|
||||
printed_pages.push_back(i);
|
||||
}
|
||||
} else {
|
||||
// TODO(vitalybuka): redesign to make more code cross platform.
|
||||
for (size_t i = 0; i < params.pages.size(); ++i) {
|
||||
if (params.pages[i] >= 0 && params.pages[i] < page_count) {
|
||||
printed_pages.push_back(params.pages[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (printed_pages.empty())
|
||||
return false;
|
||||
|
||||
PrintMsg_PrintPage_Params page_params;
|
||||
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, frame, &metafile);
|
||||
}
|
||||
|
||||
// blink::printEnd() for PDF should be called before metafile is closed.
|
||||
FinishFramePrinting();
|
||||
|
||||
metafile.FinishDocument();
|
||||
|
||||
PrintHostMsg_DidPrintPage_Params printed_page_params;
|
||||
if (!CopyMetafileDataToSharedMem(metafile,
|
||||
&printed_page_params.metafile_data_handle)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
printed_page_params.data_size = metafile.GetDataSize();
|
||||
printed_page_params.document_cookie = params.params.document_cookie;
|
||||
|
||||
for (size_t i = 0; i < printed_pages.size(); ++i) {
|
||||
printed_page_params.page_number = printed_pages[i];
|
||||
Send(new PrintHostMsg_DidPrintPage(routing_id(), printed_page_params));
|
||||
// Send the rest of the pages with an invalid metafile handle.
|
||||
printed_page_params.metafile_data_handle.Release();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void PrintWebViewHelper::PrintPageInternal(
|
||||
const PrintMsg_PrintPage_Params& params,
|
||||
WebLocalFrame* frame,
|
||||
PdfMetafileSkia* metafile) {
|
||||
PageSizeMargins page_layout_in_points;
|
||||
double scale_factor = 1.0f;
|
||||
ComputePageLayoutInPointsForCss(frame, params.page_number, params.params,
|
||||
ignore_css_margins_, &scale_factor,
|
||||
&page_layout_in_points);
|
||||
gfx::Size page_size;
|
||||
gfx::Rect content_area;
|
||||
GetPageSizeAndContentAreaFromPageLayout(page_layout_in_points, &page_size,
|
||||
&content_area);
|
||||
gfx::Rect canvas_area = content_area;
|
||||
|
||||
cc::PaintCanvas* canvas =
|
||||
metafile->GetVectorCanvasForNewPage(page_size, canvas_area, scale_factor);
|
||||
if (!canvas)
|
||||
return;
|
||||
|
||||
MetafileSkiaWrapper::SetMetafileOnCanvas(canvas, metafile);
|
||||
|
||||
RenderPageContent(frame, params.page_number, canvas_area, content_area,
|
||||
scale_factor, canvas);
|
||||
|
||||
// Done printing. Close the device context to retrieve the compiled metafile.
|
||||
if (!metafile->FinishPage())
|
||||
NOTREACHED() << "metafile failed";
|
||||
}
|
||||
|
||||
} // namespace printing
|
|
@ -1,127 +0,0 @@
|
|||
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "chrome/renderer/printing/print_web_view_helper.h"
|
||||
|
||||
#import <AppKit/AppKit.h>
|
||||
|
||||
#include "base/logging.h"
|
||||
#include "base/mac/scoped_nsautorelease_pool.h"
|
||||
#include "base/metrics/histogram.h"
|
||||
#include "cc/paint/paint_canvas.h"
|
||||
#include "chrome/common/print_messages.h"
|
||||
#include "printing/metafile_skia_wrapper.h"
|
||||
#include "printing/page_size_margins.h"
|
||||
#include "third_party/blink/public/web/web_local_frame.h"
|
||||
|
||||
namespace printing {
|
||||
|
||||
using blink::WebLocalFrame;
|
||||
|
||||
void PrintWebViewHelper::PrintPageInternal(
|
||||
const PrintMsg_PrintPage_Params& params,
|
||||
WebLocalFrame* frame) {
|
||||
PdfMetafileSkia metafile;
|
||||
CHECK(metafile.Init());
|
||||
|
||||
int page_number = params.page_number;
|
||||
gfx::Size page_size_in_dpi;
|
||||
gfx::Rect content_area_in_dpi;
|
||||
RenderPage(print_pages_params_->params, page_number, frame, &metafile,
|
||||
&page_size_in_dpi, &content_area_in_dpi);
|
||||
metafile.FinishDocument();
|
||||
|
||||
PrintHostMsg_DidPrintPage_Params page_params;
|
||||
page_params.data_size = metafile.GetDataSize();
|
||||
page_params.page_number = page_number;
|
||||
page_params.document_cookie = params.params.document_cookie;
|
||||
page_params.page_size = page_size_in_dpi;
|
||||
page_params.content_area = content_area_in_dpi;
|
||||
|
||||
// Ask the browser to create the shared memory for us.
|
||||
if (!CopyMetafileDataToSharedMem(metafile,
|
||||
&(page_params.metafile_data_handle))) {
|
||||
// TODO(thestig): Fail and return false instead.
|
||||
page_params.data_size = 0;
|
||||
}
|
||||
|
||||
Send(new PrintHostMsg_DidPrintPage(routing_id(), page_params));
|
||||
}
|
||||
|
||||
bool PrintWebViewHelper::RenderPreviewPage(
|
||||
int page_number,
|
||||
const PrintMsg_Print_Params& print_params) {
|
||||
PrintMsg_Print_Params printParams = print_params;
|
||||
std::unique_ptr<PdfMetafileSkia> draft_metafile;
|
||||
PdfMetafileSkia* initial_render_metafile = print_preview_context_.metafile();
|
||||
|
||||
bool render_to_draft =
|
||||
print_preview_context_.IsModifiable() && is_print_ready_metafile_sent_;
|
||||
|
||||
if (render_to_draft) {
|
||||
draft_metafile.reset(new PdfMetafileSkia());
|
||||
CHECK(draft_metafile->Init());
|
||||
initial_render_metafile = draft_metafile.get();
|
||||
}
|
||||
|
||||
base::TimeTicks begin_time = base::TimeTicks::Now();
|
||||
gfx::Size page_size;
|
||||
RenderPage(printParams, page_number, print_preview_context_.prepared_frame(),
|
||||
initial_render_metafile, &page_size, nullptr);
|
||||
print_preview_context_.RenderedPreviewPage(base::TimeTicks::Now() -
|
||||
begin_time);
|
||||
|
||||
if (draft_metafile.get()) {
|
||||
draft_metafile->FinishDocument();
|
||||
} else {
|
||||
if (print_preview_context_.IsModifiable() &&
|
||||
print_preview_context_.generate_draft_pages()) {
|
||||
DCHECK(!draft_metafile.get());
|
||||
draft_metafile =
|
||||
print_preview_context_.metafile()->GetMetafileForCurrentPage(
|
||||
SkiaDocumentType::PDF);
|
||||
}
|
||||
}
|
||||
return PreviewPageRendered(page_number, draft_metafile.get());
|
||||
}
|
||||
|
||||
void PrintWebViewHelper::RenderPage(const PrintMsg_Print_Params& params,
|
||||
int page_number,
|
||||
WebLocalFrame* frame,
|
||||
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;
|
||||
gfx::Rect content_area;
|
||||
|
||||
ComputePageLayoutInPointsForCss(frame, page_number, params,
|
||||
ignore_css_margins_, &scale_factor,
|
||||
&page_layout_in_points);
|
||||
GetPageSizeAndContentAreaFromPageLayout(page_layout_in_points, page_size,
|
||||
&content_area);
|
||||
if (content_rect)
|
||||
*content_rect = content_area;
|
||||
|
||||
scale_factor *= webkit_shrink_factor;
|
||||
|
||||
gfx::Rect canvas_area = content_area;
|
||||
|
||||
{
|
||||
cc::PaintCanvas* canvas = metafile->GetVectorCanvasForNewPage(
|
||||
*page_size, canvas_area, scale_factor);
|
||||
if (!canvas)
|
||||
return;
|
||||
|
||||
MetafileSkiaWrapper::SetMetafileOnCanvas(canvas, metafile);
|
||||
RenderPageContent(frame, page_number, canvas_area, content_area,
|
||||
scale_factor, static_cast<cc::PaintCanvas*>(canvas));
|
||||
}
|
||||
|
||||
// Done printing. Close the device context to retrieve the compiled metafile.
|
||||
metafile->FinishPage();
|
||||
}
|
||||
|
||||
} // namespace printing
|
|
@ -1,207 +0,0 @@
|
|||
// Copyright 2014 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "chrome/renderer/printing/print_web_view_helper.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "base/logging.h"
|
||||
#include "base/process/process_handle.h"
|
||||
#include "chrome/common/print_messages.h"
|
||||
#include "content/public/renderer/render_thread.h"
|
||||
#include "printing/metafile_skia_wrapper.h"
|
||||
#include "printing/page_size_margins.h"
|
||||
#include "printing/pdf_metafile_skia.h"
|
||||
#include "printing/units.h"
|
||||
#include "third_party/blink/public/web/web_local_frame.h"
|
||||
|
||||
namespace printing {
|
||||
|
||||
using blink::WebLocalFrame;
|
||||
|
||||
bool PrintWebViewHelper::RenderPreviewPage(
|
||||
int page_number,
|
||||
const PrintMsg_Print_Params& print_params) {
|
||||
PrintMsg_PrintPage_Params page_params;
|
||||
page_params.params = print_params;
|
||||
page_params.page_number = page_number;
|
||||
std::unique_ptr<PdfMetafileSkia> draft_metafile;
|
||||
PdfMetafileSkia* initial_render_metafile = print_preview_context_.metafile();
|
||||
if (print_preview_context_.IsModifiable() && is_print_ready_metafile_sent_) {
|
||||
draft_metafile.reset(new PdfMetafileSkia());
|
||||
initial_render_metafile = draft_metafile.get();
|
||||
}
|
||||
|
||||
base::TimeTicks begin_time = base::TimeTicks::Now();
|
||||
PrintPageInternal(page_params, print_preview_context_.prepared_frame(),
|
||||
initial_render_metafile, NULL, NULL);
|
||||
print_preview_context_.RenderedPreviewPage(base::TimeTicks::Now() -
|
||||
begin_time);
|
||||
if (draft_metafile.get()) {
|
||||
draft_metafile->FinishDocument();
|
||||
} else if (print_preview_context_.IsModifiable() &&
|
||||
print_preview_context_.generate_draft_pages()) {
|
||||
DCHECK(!draft_metafile.get());
|
||||
draft_metafile =
|
||||
print_preview_context_.metafile()->GetMetafileForCurrentPage(
|
||||
SkiaDocumentType::PDF);
|
||||
}
|
||||
return PreviewPageRendered(page_number, draft_metafile.get());
|
||||
}
|
||||
|
||||
bool PrintWebViewHelper::PrintPagesNative(blink::WebLocalFrame* frame,
|
||||
int page_count) {
|
||||
PdfMetafileSkia metafile;
|
||||
if (!metafile.Init())
|
||||
return false;
|
||||
|
||||
const PrintMsg_PrintPages_Params& params = *print_pages_params_;
|
||||
std::vector<int> printed_pages;
|
||||
if (params.pages.empty()) {
|
||||
for (int i = 0; i < page_count; ++i) {
|
||||
printed_pages.push_back(i);
|
||||
}
|
||||
} else {
|
||||
// TODO(vitalybuka): redesign to make more code cross platform.
|
||||
for (size_t i = 0; i < params.pages.size(); ++i) {
|
||||
if (params.pages[i] >= 0 && params.pages[i] < page_count) {
|
||||
printed_pages.push_back(params.pages[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (printed_pages.empty())
|
||||
return false;
|
||||
|
||||
std::vector<gfx::Size> page_size_in_dpi(printed_pages.size());
|
||||
std::vector<gfx::Rect> content_area_in_dpi(printed_pages.size());
|
||||
|
||||
PrintMsg_PrintPage_Params page_params;
|
||||
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, frame, &metafile, &page_size_in_dpi[i],
|
||||
&content_area_in_dpi[i]);
|
||||
}
|
||||
|
||||
// blink::printEnd() for PDF should be called before metafile is closed.
|
||||
FinishFramePrinting();
|
||||
|
||||
metafile.FinishDocument();
|
||||
|
||||
PrintHostMsg_DidPrintPage_Params printed_page_params;
|
||||
if (!CopyMetafileDataToSharedMem(metafile,
|
||||
&printed_page_params.metafile_data_handle)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
printed_page_params.content_area = params.params.printable_area;
|
||||
printed_page_params.data_size = metafile.GetDataSize();
|
||||
printed_page_params.document_cookie = params.params.document_cookie;
|
||||
printed_page_params.page_size = params.params.page_size;
|
||||
|
||||
for (size_t i = 0; i < printed_pages.size(); ++i) {
|
||||
printed_page_params.page_number = printed_pages[i];
|
||||
printed_page_params.page_size = page_size_in_dpi[i];
|
||||
printed_page_params.content_area = content_area_in_dpi[i];
|
||||
Send(new PrintHostMsg_DidPrintPage(routing_id(), printed_page_params));
|
||||
// Send the rest of the pages with an invalid metafile handle.
|
||||
printed_page_params.metafile_data_handle.Close();
|
||||
printed_page_params.metafile_data_handle = base::SharedMemoryHandle();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void PrintWebViewHelper::PrintPageInternal(
|
||||
const PrintMsg_PrintPage_Params& params,
|
||||
WebLocalFrame* frame,
|
||||
PdfMetafileSkia* metafile,
|
||||
gfx::Size* page_size_in_dpi,
|
||||
gfx::Rect* content_area_in_dpi) {
|
||||
PageSizeMargins page_layout_in_points;
|
||||
double css_scale_factor = 1.0f;
|
||||
ComputePageLayoutInPointsForCss(frame, params.page_number, params.params,
|
||||
ignore_css_margins_, &css_scale_factor,
|
||||
&page_layout_in_points);
|
||||
gfx::Size page_size;
|
||||
gfx::Rect content_area;
|
||||
GetPageSizeAndContentAreaFromPageLayout(page_layout_in_points, &page_size,
|
||||
&content_area);
|
||||
int dpi = static_cast<int>(params.params.dpi);
|
||||
// Calculate the actual page size and content area in dpi.
|
||||
if (page_size_in_dpi) {
|
||||
*page_size_in_dpi =
|
||||
gfx::Size(static_cast<int>(ConvertUnitDouble(page_size.width(),
|
||||
kPointsPerInch, dpi)),
|
||||
static_cast<int>(ConvertUnitDouble(page_size.height(),
|
||||
kPointsPerInch, dpi)));
|
||||
}
|
||||
|
||||
if (content_area_in_dpi) {
|
||||
// Output PDF matches paper size and should be printer edge to edge.
|
||||
*content_area_in_dpi =
|
||||
gfx::Rect(0, 0, page_size_in_dpi->width(), page_size_in_dpi->height());
|
||||
}
|
||||
|
||||
gfx::Rect canvas_area = content_area;
|
||||
#if 0
|
||||
params.params.display_header_footer ? gfx::Rect(page_size) : content_area;
|
||||
#endif
|
||||
|
||||
float webkit_page_shrink_factor =
|
||||
frame->GetPrintPageShrink(params.page_number);
|
||||
float scale_factor = css_scale_factor * webkit_page_shrink_factor;
|
||||
|
||||
cc::PaintCanvas* canvas =
|
||||
metafile->GetVectorCanvasForNewPage(page_size, canvas_area, scale_factor);
|
||||
if (!canvas)
|
||||
return;
|
||||
|
||||
MetafileSkiaWrapper::SetMetafileOnCanvas(canvas, metafile);
|
||||
|
||||
#if 0
|
||||
if (params.params.display_header_footer) {
|
||||
// |page_number| is 0-based, so 1 is added.
|
||||
PrintHeaderAndFooter(canvas.get(),
|
||||
params.page_number + 1,
|
||||
print_preview_context_.total_page_count(),
|
||||
*frame,
|
||||
scale_factor,
|
||||
page_layout_in_points,
|
||||
params.params);
|
||||
}
|
||||
#endif
|
||||
|
||||
float webkit_scale_factor =
|
||||
RenderPageContent(frame, params.page_number, canvas_area, content_area,
|
||||
scale_factor, canvas);
|
||||
DCHECK_GT(webkit_scale_factor, 0.0f);
|
||||
// Done printing. Close the device context to retrieve the compiled metafile.
|
||||
if (!metafile->FinishPage())
|
||||
NOTREACHED() << "metafile failed";
|
||||
}
|
||||
|
||||
bool PrintWebViewHelper::CopyMetafileDataToSharedMem(
|
||||
const PdfMetafileSkia& metafile,
|
||||
base::SharedMemoryHandle* shared_mem_handle) {
|
||||
uint32_t buf_size = metafile.GetDataSize();
|
||||
if (buf_size == 0)
|
||||
return false;
|
||||
|
||||
std::unique_ptr<base::SharedMemory> shared_buf(
|
||||
content::RenderThread::Get()->HostAllocateSharedMemoryBuffer(buf_size));
|
||||
if (!shared_buf)
|
||||
return false;
|
||||
|
||||
if (!shared_buf->Map(buf_size))
|
||||
return false;
|
||||
|
||||
if (!metafile.GetData(shared_buf->memory(), buf_size))
|
||||
return false;
|
||||
|
||||
*shared_mem_handle =
|
||||
base::SharedMemory::DuplicateHandle(shared_buf->handle());
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace printing
|
Loading…
Add table
Add a link
Reference in a new issue