Merge pull request #1835 from hokein/pdf-api

Add `BrowserWindow.printToPDF` API Implementation
This commit is contained in:
Cheng Zhao 2015-06-17 12:22:56 +08:00
Родитель b3e9d35667 82b1607c1e
Коммит a751f4c689
23 изменённых файлов: 1149 добавлений и 63 удалений

Просмотреть файл

@ -18,6 +18,8 @@
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "brightray/browser/inspectable_web_contents.h"
#include "chrome/browser/printing/print_view_manager_basic.h"
#include "chrome/browser/printing/print_preview_message_handler.h"
#include "content/public/browser/favicon_status.h"
#include "content/public/browser/guest_host.h"
#include "content/public/browser/navigation_details.h"
@ -40,6 +42,15 @@
#include "atom/common/node_includes.h"
namespace {
struct PrintSettings {
bool silent;
bool print_background;
};
} // namespace
namespace mate {
template<>
@ -64,6 +75,19 @@ struct Converter<atom::api::SetSizeParams> {
}
};
template<>
struct Converter<PrintSettings> {
static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val,
PrintSettings* out) {
mate::Dictionary dict;
if (!ConvertFromV8(isolate, val, &dict))
return false;
dict.Get("silent", &(out->silent));
dict.Get("printBackground", &(out->print_background));
return true;
}
};
} // namespace mate
@ -588,6 +612,23 @@ bool WebContents::IsAudioMuted() {
return web_contents()->IsAudioMuted();
}
void WebContents::Print(mate::Arguments* args) {
PrintSettings settings = { false, false };
if (args->Length() == 1 && !args->GetNext(&settings)) {
args->ThrowError();
return;
}
printing::PrintViewManagerBasic::FromWebContents(web_contents())->
PrintNow(settings.silent, settings.print_background);
}
void WebContents::PrintToPDF(const base::DictionaryValue& setting,
const PrintToPDFCallback& callback) {
printing::PrintPreviewMessageHandler::FromWebContents(web_contents())->
PrintToPDF(setting, callback);
}
void WebContents::Undo() {
web_contents()->Undo();
}
@ -760,6 +801,8 @@ mate::ObjectTemplateBuilder WebContents::GetObjectTemplateBuilder(
.SetMethod("unregisterServiceWorker",
&WebContents::UnregisterServiceWorker)
.SetMethod("inspectServiceWorker", &WebContents::InspectServiceWorker)
.SetMethod("print", &WebContents::Print)
.SetMethod("_printToPDF", &WebContents::PrintToPDF)
.Build());
return mate::ObjectTemplateBuilder(

Просмотреть файл

@ -52,6 +52,10 @@ class WebContents : public mate::EventEmitter,
public content::WebContentsObserver,
public content::GpuDataManagerObserver {
public:
// For node.js callback function type: function(error, buffer)
typedef base::Callback<void(v8::Local<v8::Value>, v8::Local<v8::Value>)>
PrintToPDFCallback;
// Create from an existing WebContents.
static mate::Handle<WebContents> CreateFrom(
v8::Isolate* isolate, brightray::InspectableWebContents* web_contents);
@ -89,6 +93,11 @@ class WebContents : public mate::EventEmitter,
void UnregisterServiceWorker(const base::Callback<void(bool)>&);
void SetAudioMuted(bool muted);
bool IsAudioMuted();
void Print(mate::Arguments* args);
// Print current page as PDF.
void PrintToPDF(const base::DictionaryValue& setting,
const PrintToPDFCallback& callback);
// Editing commands.
void Undo();

Просмотреть файл

@ -20,32 +20,6 @@
#include "atom/common/node_includes.h"
namespace {
struct PrintSettings {
bool silent;
bool print_background;
};
} // namespace
namespace mate {
template<>
struct Converter<PrintSettings> {
static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val,
PrintSettings* out) {
mate::Dictionary dict;
if (!ConvertFromV8(isolate, val, &dict))
return false;
dict.Get("silent", &(out->silent));
dict.Get("printBackground", &(out->print_background));
return true;
}
};
} // namespace mate
namespace atom {
namespace api {
@ -420,16 +394,6 @@ void Window::CapturePage(mate::Arguments* args) {
rect, base::Bind(&OnCapturePageDone, args->isolate(), callback));
}
void Window::Print(mate::Arguments* args) {
PrintSettings settings = { false, false };;
if (args->Length() == 1 && !args->GetNext(&settings)) {
args->ThrowError();
return;
}
window_->Print(settings.silent, settings.print_background);
}
void Window::SetProgressBar(double progress) {
window_->SetProgressBar(progress);
}
@ -541,7 +505,6 @@ void Window::BuildPrototype(v8::Isolate* isolate,
.SetMethod("blurWebView", &Window::BlurWebView)
.SetMethod("isWebViewFocused", &Window::IsWebViewFocused)
.SetMethod("capturePage", &Window::CapturePage)
.SetMethod("print", &Window::Print)
.SetMethod("setProgressBar", &Window::SetProgressBar)
.SetMethod("setOverlayIcon", &Window::SetOverlayIcon)
.SetMethod("_setMenu", &Window::SetMenu)

Просмотреть файл

@ -131,7 +131,6 @@ class Window : public mate::EventEmitter,
void SetDocumentEdited(bool edited);
bool IsDocumentEdited();
void CapturePage(mate::Arguments* args);
void Print(mate::Arguments* args);
void SetProgressBar(double progress);
void SetOverlayIcon(const gfx::Image& overlay,
const std::string& description);

Просмотреть файл

@ -83,5 +83,7 @@ BrowserWindow::isDevToolsOpened = -> @webContents.isDevToolsOpened()
BrowserWindow::toggleDevTools = -> @webContents.toggleDevTools()
BrowserWindow::inspectElement = -> @webContents.inspectElement.apply @webContents, arguments
BrowserWindow::inspectServiceWorker = -> @webContents.inspectServiceWorker()
BrowserWindow::print = -> @webContents.print.apply @webContents, arguments
BrowserWindow::printToPDF = -> @webContents.printToPDF.apply @webContents, arguments
module.exports = BrowserWindow

Просмотреть файл

@ -3,6 +3,9 @@ NavigationController = require './navigation-controller'
binding = process.atomBinding 'web_contents'
ipc = require 'ipc'
nextId = 0
getNextId = -> ++nextId
wrapWebContents = (webContents) ->
# webContents is an EventEmitter.
webContents.__proto__ = EventEmitter.prototype
@ -58,6 +61,46 @@ wrapWebContents = (webContents) ->
Object.defineProperty event, 'sender', value: webContents
ipc.emit channel, event, args...
webContents.printToPDF = (options, callback) ->
printingSetting =
pageRage:[],
mediaSize:
height_microns:297000,
is_default:true,
name:"ISO_A4",
width_microns:210000,
custom_display_name:"A4",
landscape:false,
color:2,
headerFooterEnabled:false,
marginsType:0,
isFirstRequest:false,
requestID: getNextId(),
previewModifiable:true,
printToPDF:true,
printWithCloudPrint:false,
printWithPrivet:false,
printWithExtension:false,
deviceName:"Save as PDF",
generateDraftData:true,
fitToPageEnabled:false,
duplex:0,
copies:1,
collate:true,
shouldPrintBackgrounds:false,
shouldPrintSelectionOnly:false
if options.landscape
printingSetting.landscape = options.landscape
if options.marginsType
printingSetting.marginsType = options.marginsType
if options.printSelectionOnly
printingSetting.shouldPrintSelectionOnly = options.printSelectionOnly
if options.printBackgrounds
printingSetting.shouldPrintBackgrounds = options.printBackground
webContents._printToPDF printingSetting, callback
webContents
binding._setWrapWebContents wrapWebContents

Просмотреть файл

@ -12,6 +12,8 @@
#include "atom/browser/ui/file_dialog.h"
#include "atom/browser/web_dialog_helper.h"
#include "base/files/file_util.h"
#include "chrome/browser/printing/print_preview_message_handler.h"
#include "chrome/browser/printing/print_view_manager_basic.h"
#include "chrome/browser/ui/browser_dialogs.h"
#include "content/public/browser/child_process_security_policy.h"
#include "content/public/browser/render_process_host.h"
@ -102,6 +104,9 @@ void CommonWebContentsDelegate::InitWithWebContents(
owner_window_ = owner_window;
web_contents->SetDelegate(this);
printing::PrintViewManagerBasic::CreateForWebContents(web_contents);
printing::PrintPreviewMessageHandler::CreateForWebContents(web_contents);
// Create InspectableWebContents.
web_contents_.reset(brightray::InspectableWebContents::Create(web_contents));
web_contents_->SetDelegate(this);

Просмотреть файл

@ -27,7 +27,6 @@
#include "base/strings/utf_string_conversions.h"
#include "brightray/browser/inspectable_web_contents.h"
#include "brightray/browser/inspectable_web_contents_view.h"
#include "chrome/browser/printing/print_view_manager_basic.h"
#include "content/browser/renderer_host/render_widget_host_impl.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/notification_details.h"
@ -96,8 +95,6 @@ NativeWindow::NativeWindow(content::WebContents* web_contents,
has_dialog_attached_(false),
zoom_factor_(1.0),
weak_factory_(this) {
printing::PrintViewManagerBasic::CreateForWebContents(web_contents);
InitWithWebContents(web_contents, this);
options.Get(switches::kFrame, &has_frame_);
@ -257,11 +254,6 @@ bool NativeWindow::IsDocumentEdited() {
void NativeWindow::SetMenu(ui::MenuModel* menu) {
}
void NativeWindow::Print(bool silent, bool print_background) {
printing::PrintViewManagerBasic::FromWebContents(GetWebContents())->
PrintNow(silent, print_background);
}
void NativeWindow::ShowDefinitionForSelection() {
NOTIMPLEMENTED();
}

Просмотреть файл

@ -154,9 +154,6 @@ class NativeWindow : public CommonWebContentsDelegate,
virtual void CapturePage(const gfx::Rect& rect,
const CapturePageCallback& callback);
// Print current page.
virtual void Print(bool silent, bool print_background);
// Show popup dictionary.
virtual void ShowDefinitionForSelection();

Просмотреть файл

@ -291,6 +291,8 @@ registerWebViewElement = ->
"send"
"getId"
"inspectServiceWorker"
"print"
"printToPDF"
]
# Forward proto.foo* method calls to WebViewImpl.foo*.

Просмотреть файл

@ -0,0 +1,140 @@
// 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/browser/printing/print_preview_message_handler.h"
#include "base/bind.h"
#include "base/memory/shared_memory.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/printing/print_job_manager.h"
#include "chrome/browser/printing/printer_query.h"
#include "chrome/common/print_messages.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "printing/page_size_margins.h"
#include "printing/print_job_constants.h"
#include "printing/pdf_metafile_skia.h"
#include "atom/common/node_includes.h"
using content::BrowserThread;
using content::WebContents;
DEFINE_WEB_CONTENTS_USER_DATA_KEY(printing::PrintPreviewMessageHandler);
namespace {
void StopWorker(int document_cookie) {
if (document_cookie <= 0)
return;
scoped_refptr<printing::PrintQueriesQueue> queue =
g_browser_process->print_job_manager()->queue();
scoped_refptr<printing::PrinterQuery> printer_query =
queue->PopPrinterQuery(document_cookie);
if (printer_query.get()) {
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
base::Bind(&printing::PrinterQuery::StopWorker,
printer_query));
}
}
char* CopyPDFDataOnIOThread(
const PrintHostMsg_DidPreviewDocument_Params& params) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
scoped_ptr<base::SharedMemory> shared_buf(
new base::SharedMemory(params.metafile_data_handle, true));
if (!shared_buf->Map(params.data_size))
return nullptr;
char* memory_pdf_data = static_cast<char*>(shared_buf->memory());
char* pdf_data = new char[params.data_size];
memcpy(pdf_data, memory_pdf_data, params.data_size);
return pdf_data;
}
} // namespace
namespace printing {
PrintPreviewMessageHandler::PrintPreviewMessageHandler(
WebContents* web_contents)
: content::WebContentsObserver(web_contents) {
DCHECK(web_contents);
}
PrintPreviewMessageHandler::~PrintPreviewMessageHandler() {
}
void PrintPreviewMessageHandler::OnMetafileReadyForPrinting(
const PrintHostMsg_DidPreviewDocument_Params& params) {
// Always try to stop the worker.
StopWorker(params.document_cookie);
if (params.expected_pages_count <= 0) {
NOTREACHED();
return;
}
BrowserThread::PostTaskAndReplyWithResult(
BrowserThread::IO,
FROM_HERE,
base::Bind(&CopyPDFDataOnIOThread, params),
base::Bind(&PrintPreviewMessageHandler::RunPrintToPDFCallback,
base::Unretained(this),
params.preview_request_id,
params.data_size));
}
void PrintPreviewMessageHandler::OnPrintPreviewFailed(int document_cookie,
int request_id) {
StopWorker(document_cookie);
RunPrintToPDFCallback(request_id, 0, nullptr);
}
bool PrintPreviewMessageHandler::OnMessageReceived(
const IPC::Message& message) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(PrintPreviewMessageHandler, message)
IPC_MESSAGE_HANDLER(PrintHostMsg_MetafileReadyForPrinting,
OnMetafileReadyForPrinting)
IPC_MESSAGE_HANDLER(PrintHostMsg_PrintPreviewFailed,
OnPrintPreviewFailed)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
}
void PrintPreviewMessageHandler::PrintToPDF(
const base::DictionaryValue& options,
const atom::api::WebContents::PrintToPDFCallback& callback) {
int request_id;
options.GetInteger(printing::kPreviewRequestID, &request_id);
print_to_pdf_callback_map_[request_id] = callback;
content::RenderViewHost* rvh = web_contents()->GetRenderViewHost();
rvh->Send(new PrintMsg_PrintPreview(rvh->GetRoutingID(), options));
}
void PrintPreviewMessageHandler::RunPrintToPDFCallback(
int request_id, uint32 data_size, char* data) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::Locker locker(isolate);
v8::HandleScope handle_scope(isolate);
if (data) {
v8::Local<v8::Value> buffer = node::Buffer::Use(isolate,
data, static_cast<size_t>(data_size));
print_to_pdf_callback_map_[request_id].Run(v8::Null(isolate), buffer);
} else {
v8::Local<v8::String> error_message = v8::String::NewFromUtf8(isolate,
"Fail to generate PDF");
print_to_pdf_callback_map_[request_id].Run(
v8::Exception::Error(error_message), v8::Null(isolate));
}
print_to_pdf_callback_map_.erase(request_id);
}
} // namespace printing

Просмотреть файл

@ -0,0 +1,59 @@
// 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_BROWSER_PRINTING_PRINT_PREVIEW_MESSAGE_HANDLER_H_
#define CHROME_BROWSER_PRINTING_PRINT_PREVIEW_MESSAGE_HANDLER_H_
#include <map>
#include "atom/browser/api/atom_api_web_contents.h"
#include "base/compiler_specific.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_contents_user_data.h"
struct PrintHostMsg_DidPreviewDocument_Params;
namespace content {
class WebContents;
}
namespace printing {
struct PageSizeMargins;
// Manages the print preview handling for a WebContents.
class PrintPreviewMessageHandler
: public content::WebContentsObserver,
public content::WebContentsUserData<PrintPreviewMessageHandler> {
public:
~PrintPreviewMessageHandler() override;
// content::WebContentsObserver implementation.
bool OnMessageReceived(const IPC::Message& message) override;
void PrintToPDF(const base::DictionaryValue& options,
const atom::api::WebContents::PrintToPDFCallback& callback);
private:
typedef std::map<int, atom::api::WebContents::PrintToPDFCallback>
PrintToPDFCallbackMap;
explicit PrintPreviewMessageHandler(content::WebContents* web_contents);
friend class content::WebContentsUserData<PrintPreviewMessageHandler>;
// Message handlers.
void OnMetafileReadyForPrinting(
const PrintHostMsg_DidPreviewDocument_Params& params);
void OnPrintPreviewFailed(int document_cookie, int request_id);
void RunPrintToPDFCallback(int request_id, uint32 data_size, char* data);
PrintToPDFCallbackMap print_to_pdf_callback_map_;
DISALLOW_COPY_AND_ASSIGN(PrintPreviewMessageHandler);
};
} // namespace printing
#endif // CHROME_BROWSER_PRINTING_PRINT_PREVIEW_MESSAGE_HANDLER_H_

Просмотреть файл

@ -128,6 +128,8 @@ bool PrintingMessageFilter::OnMessageReceived(const IPC::Message& message) {
IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_GetDefaultPrintSettings,
OnGetDefaultPrintSettings)
IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_ScriptedPrint, OnScriptedPrint)
IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_UpdatePrintSettings,
OnUpdatePrintSettings)
#if defined(ENABLE_FULL_PRINTING)
IPC_MESSAGE_HANDLER(PrintHostMsg_CheckForCancel, OnCheckForCancel)
#endif
@ -372,4 +374,57 @@ void PrintingMessageFilter::UpdateFileDescriptor(int render_view_id, int fd) {
}
#endif
void PrintingMessageFilter::OnUpdatePrintSettings(
int document_cookie, const base::DictionaryValue& job_settings,
IPC::Message* reply_msg) {
scoped_ptr<base::DictionaryValue> new_settings(job_settings.DeepCopy());
scoped_refptr<PrinterQuery> printer_query;
printer_query = queue_->PopPrinterQuery(document_cookie);
if (!printer_query.get()) {
int host_id = render_process_id_;
int routing_id = reply_msg->routing_id();
if (!new_settings->GetInteger(printing::kPreviewInitiatorHostId,
&host_id) ||
!new_settings->GetInteger(printing::kPreviewInitiatorRoutingId,
&routing_id)) {
host_id = content::ChildProcessHost::kInvalidUniqueID;
routing_id = content::ChildProcessHost::kInvalidUniqueID;
}
printer_query = queue_->CreatePrinterQuery(host_id, routing_id);
}
printer_query->SetSettings(
new_settings.Pass(),
base::Bind(&PrintingMessageFilter::OnUpdatePrintSettingsReply, this,
printer_query, reply_msg));
}
void PrintingMessageFilter::OnUpdatePrintSettingsReply(
scoped_refptr<PrinterQuery> printer_query,
IPC::Message* reply_msg) {
PrintMsg_PrintPages_Params params;
if (!printer_query.get() ||
printer_query->last_status() != PrintingContext::OK) {
params.Reset();
} else {
RenderParamsFromPrintSettings(printer_query->settings(), &params.params);
params.params.document_cookie = printer_query->cookie();
params.pages = PageRange::GetPages(printer_query->settings().ranges());
}
PrintHostMsg_UpdatePrintSettings::WriteReplyParams(
reply_msg,
params,
printer_query.get() &&
(printer_query->last_status() == printing::PrintingContext::CANCEL));
Send(reply_msg);
// If user hasn't cancelled.
if (printer_query.get()) {
if (printer_query->cookie() && printer_query->settings().dpi()) {
queue_->QueuePrinterQuery(printer_query.get());
} else {
printer_query->StopWorker();
}
}
}
} // namespace printing

Просмотреть файл

@ -96,6 +96,15 @@ class PrintingMessageFilter : public content::BrowserMessageFilter {
void OnScriptedPrintReply(scoped_refptr<PrinterQuery> printer_query,
IPC::Message* reply_msg);
// Modify the current print settings based on |job_settings|. The task is
// handled by the print worker thread and the UI thread. The reply occurs on
// the IO thread.
void OnUpdatePrintSettings(int document_cookie,
const base::DictionaryValue& job_settings,
IPC::Message* reply_msg);
void OnUpdatePrintSettingsReply(scoped_refptr<PrinterQuery> printer_query,
IPC::Message* reply_msg);
#if defined(ENABLE_FULL_PRINTING)
// Check to see if print preview has been cancelled.
void OnCheckForCancel(int32 preview_ui_id,

Просмотреть файл

@ -46,7 +46,9 @@ struct PrintMsg_Print_Params {
int document_cookie;
bool selection_only;
bool supports_alpha_blend;
int preview_request_id;
blink::WebPrintScalingOption print_scaling_option;
bool print_to_pdf;
base::string16 title;
base::string16 url;
bool should_print_backgrounds;
@ -185,6 +187,27 @@ IPC_STRUCT_BEGIN(PrintHostMsg_ScriptedPrint_Params)
IPC_STRUCT_MEMBER(printing::MarginType, margin_type)
IPC_STRUCT_END()
// Parameters to describe a rendered document.
IPC_STRUCT_BEGIN(PrintHostMsg_DidPreviewDocument_Params)
// A shared memory handle to metafile data.
IPC_STRUCT_MEMBER(base::SharedMemoryHandle, metafile_data_handle)
// Size of metafile data.
IPC_STRUCT_MEMBER(uint32, data_size)
// Cookie for the document to ensure correctness.
IPC_STRUCT_MEMBER(int, document_cookie)
// Store the expected pages count.
IPC_STRUCT_MEMBER(int, expected_pages_count)
// Whether the preview can be modified.
IPC_STRUCT_MEMBER(bool, modifiable)
// The id of the preview request.
IPC_STRUCT_MEMBER(int, preview_request_id)
IPC_STRUCT_END()
// Messages sent from the browser to the renderer.
@ -198,6 +221,12 @@ IPC_MESSAGE_ROUTED2(PrintMsg_PrintPages,
IPC_MESSAGE_ROUTED1(PrintMsg_PrintingDone,
bool /* success */)
// Tells the render view to switch the CSS to print media type, renders every
// requested pages for print preview using the given |settings|. This gets
// called multiple times as the user updates settings.
IPC_MESSAGE_ROUTED1(PrintMsg_PrintPreview,
base::DictionaryValue /* settings */)
// Messages sent from the renderer to the browser.
#if defined(OS_WIN)
@ -231,6 +260,14 @@ IPC_MESSAGE_ROUTED1(PrintHostMsg_DidPrintPage,
IPC_SYNC_MESSAGE_ROUTED0_1(PrintHostMsg_GetDefaultPrintSettings,
PrintMsg_Print_Params /* default_settings */)
// The renderer wants to update the current print settings with new
// |job_settings|.
IPC_SYNC_MESSAGE_ROUTED2_2(PrintHostMsg_UpdatePrintSettings,
int /* document_cookie */,
base::DictionaryValue /* job_settings */,
PrintMsg_PrintPages_Params /* current_settings */,
bool /* canceled */)
// It's the renderer that controls the printing process when it is generated
// by javascript. This step is about showing UI to the user to select the
// final print settings. The output parameter is the same as
@ -247,6 +284,15 @@ IPC_MESSAGE_ROUTED0(PrintHostMsg_ShowInvalidPrinterSettingsError)
IPC_MESSAGE_ROUTED1(PrintHostMsg_PrintingFailed,
int /* document cookie */)
// Sends back to the browser the complete rendered document (non-draft mode,
// used for printing) that was requested by a PrintMsg_PrintPreview message.
// The memory handle in this message is already valid in the browser process.
IPC_MESSAGE_ROUTED1(PrintHostMsg_MetafileReadyForPrinting,
PrintHostMsg_DidPreviewDocument_Params /* params */)
IPC_MESSAGE_ROUTED2(PrintHostMsg_PrintPreviewFailed,
int /* document cookie */,
int /* request_id */);
#if defined(OS_WIN)
// Tell the utility process to start rendering the given PDF into a metafile.

Просмотреть файл

@ -650,6 +650,7 @@ bool PrintWebViewHelper::OnMessageReceived(const IPC::Message& message) {
IPC_BEGIN_MESSAGE_MAP(PrintWebViewHelper, message)
IPC_MESSAGE_HANDLER(PrintMsg_PrintPages, OnPrintPages)
IPC_MESSAGE_HANDLER(PrintMsg_PrintingDone, OnPrintingDone)
IPC_MESSAGE_HANDLER(PrintMsg_PrintPreview, OnPrintPreview)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
@ -712,6 +713,128 @@ void PrintWebViewHelper::OnPrintingDone(bool success) {
DidFinishPrinting(success ? OK : FAIL_PRINT);
}
void PrintWebViewHelper::OnPrintPreview(const base::DictionaryValue& settings) {
blink::WebLocalFrame* frame;
if (GetPrintFrame(&frame)) {
print_preview_context_.InitWithFrame(frame);
if (!print_preview_context_.source_frame()) {
DidFinishPrinting(FAIL_PREVIEW);
return;
}
if (!UpdatePrintSettings(print_preview_context_.source_frame(),
print_preview_context_.source_node(), settings)) {
DidFinishPrinting(FAIL_PREVIEW);
return;
}
is_print_ready_metafile_sent_ = false;
PrepareFrameForPreviewDocument();
}
}
void PrintWebViewHelper::PrepareFrameForPreviewDocument() {
reset_prep_frame_view_ = false;
if (!print_pages_params_) {
DidFinishPrinting(FAIL_PREVIEW);
return;
}
// Don't reset loading frame or WebKit will fail assert. Just retry when
// current selection is loaded.
if (prep_frame_view_ && prep_frame_view_->IsLoadingSelection()) {
reset_prep_frame_view_ = true;
return;
}
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 (reset_prep_frame_view_) {
PrepareFrameForPreviewDocument();
return;
}
DidFinishPrinting(CreatePreviewDocument() ? OK : FAIL_PREVIEW);
}
bool PrintWebViewHelper::CreatePreviewDocument() {
if (!print_pages_params_)
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(prep_frame_view_.release(),
pages)) {
return false;
}
while (!print_preview_context_.IsFinalPageRendered()) {
int page_number = print_preview_context_.GetNextPageNumber();
DCHECK_GE(page_number, 0);
if (!RenderPreviewPage(page_number, print_params))
return false;
// We must call PrepareFrameAndViewForPrint::FinishPrinting() (by way of
// print_preview_context_.AllPagesRendered()) before calling
// FinalizePrintReadyDocument() when printing a PDF because the plugin
// code does not generate output until we call FinishPrinting(). We do not
// generate draft pages for PDFs, so IsFinalPageRendered() and
// IsLastPageOfPrintReadyMetafile() will be true in the same iteration of
// the loop.
if (print_preview_context_.IsFinalPageRendered())
print_preview_context_.AllPagesRendered();
if (print_preview_context_.IsLastPageOfPrintReadyMetafile()) {
DCHECK(print_preview_context_.IsModifiable() ||
print_preview_context_.IsFinalPageRendered());
if (!FinalizePrintReadyDocument())
return false;
}
}
print_preview_context_.Finished();
return true;
}
bool PrintWebViewHelper::FinalizePrintReadyDocument() {
DCHECK(!is_print_ready_metafile_sent_);
print_preview_context_.FinalizePrintReadyDocument();
// Get the size of the resulting metafile.
PdfMetafileSkia* metafile = print_preview_context_.metafile();
uint32 buf_size = metafile->GetDataSize();
DCHECK_GT(buf_size, 0u);
PrintHostMsg_DidPreviewDocument_Params preview_params;
preview_params.data_size = buf_size;
preview_params.document_cookie = print_pages_params_->params.document_cookie;
preview_params.expected_pages_count =
print_preview_context_.total_page_count();
preview_params.modifiable = print_preview_context_.IsModifiable();
preview_params.preview_request_id =
print_pages_params_->params.preview_request_id;
// Ask the browser to create the shared memory for us.
if (!CopyMetafileDataToSharedMem(metafile,
&(preview_params.metafile_data_handle))) {
LOG(ERROR) << "CopyMetafileDataToSharedMem failed";
print_preview_context_.set_error(PREVIEW_ERROR_METAFILE_COPY_FAILED);
return false;
}
is_print_ready_metafile_sent_ = true;
Send(new PrintHostMsg_MetafileReadyForPrinting(routing_id(), preview_params));
return true;
}
void PrintWebViewHelper::PrintNode(const blink::WebNode& node) {
if (node.isNull() || !node.document().frame()) {
// This can occur when the context menu refers to an invalid WebNode.
@ -786,6 +909,15 @@ void PrintWebViewHelper::DidFinishPrinting(PrintingResult result) {
Send(new PrintHostMsg_PrintingFailed(routing_id(), cookie));
}
break;
case FAIL_PREVIEW:
LOG(ERROR) << "PREVIEW FAILED.";
if (print_pages_params_) {
Send(new PrintHostMsg_PrintPreviewFailed(routing_id(),
print_pages_params_->params.document_cookie,
print_pages_params_->params.preview_request_id));
}
break;
}
prep_frame_view_.reset();
print_pages_params_.reset();
@ -916,6 +1048,68 @@ bool PrintWebViewHelper::CalculateNumberOfPages(blink::WebLocalFrame* frame,
return true;
}
bool PrintWebViewHelper::UpdatePrintSettings(
blink::WebLocalFrame* frame,
const blink::WebNode& node,
const base::DictionaryValue& passed_job_settings) {
const base::DictionaryValue* job_settings = &passed_job_settings;
base::DictionaryValue modified_job_settings;
if (job_settings->empty()) {
if (!print_for_preview_)
print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING);
return false;
}
bool source_is_html = true;
if (print_for_preview_) {
if (!job_settings->GetBoolean(kSettingPreviewModifiable, &source_is_html)) {
NOTREACHED();
}
} else {
source_is_html = !PrintingNodeOrPdfFrame(frame, node);
}
if (print_for_preview_ || !source_is_html) {
modified_job_settings.MergeDictionary(job_settings);
modified_job_settings.SetBoolean(kSettingHeaderFooterEnabled, false);
modified_job_settings.SetInteger(kSettingMarginsType, NO_MARGINS);
job_settings = &modified_job_settings;
}
// Send the cookie so that UpdatePrintSettings can reuse PrinterQuery when
// possible.
int cookie =
print_pages_params_ ? print_pages_params_->params.document_cookie : 0;
PrintMsg_PrintPages_Params settings;
bool canceled = false;
Send(new PrintHostMsg_UpdatePrintSettings(routing_id(), cookie, *job_settings,
&settings, &canceled));
if (canceled) {
notify_browser_of_print_failure_ = false;
return false;
}
if (!print_for_preview_) {
job_settings->GetInteger(kPreviewRequestID,
&settings.params.preview_request_id);
settings.params.print_to_pdf = true;
UpdateFrameMarginsCssInfo(*job_settings);
settings.params.print_scaling_option =
blink::WebPrintScalingOptionSourceSize;
}
SetPrintPagesParams(settings);
if (!PrintMsg_Print_Params_IsValid(settings.params)) {
if (!print_for_preview_)
print_preview_context_.set_error(PREVIEW_ERROR_INVALID_PRINTER_SETTINGS);
return false;
}
return true;
}
bool PrintWebViewHelper::GetPrintSettingsFromUser(blink::WebFrame* frame,
const blink::WebNode& node,
int expected_pages_count) {
@ -987,4 +1181,264 @@ void PrintWebViewHelper::SetPrintPagesParams(
print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings));
}
bool PrintWebViewHelper::PreviewPageRendered(int page_number,
PdfMetafileSkia* metafile) {
DCHECK_GE(page_number, FIRST_PAGE_INDEX);
// For non-modifiable files, |metafile| should be NULL, so do not bother
// sending a message. If we don't generate draft metafiles, |metafile| is
// NULL.
if (!print_preview_context_.IsModifiable() ||
!print_preview_context_.generate_draft_pages()) {
DCHECK(!metafile);
return true;
}
if (!metafile) {
NOTREACHED();
print_preview_context_.set_error(
PREVIEW_ERROR_PAGE_RENDERED_WITHOUT_METAFILE);
return false;
}
return true;
}
PrintWebViewHelper::PrintPreviewContext::PrintPreviewContext()
: total_page_count_(0),
current_page_index_(0),
generate_draft_pages_(true),
print_ready_metafile_page_count_(0),
error_(PREVIEW_ERROR_NONE),
state_(UNINITIALIZED) {
}
PrintWebViewHelper::PrintPreviewContext::~PrintPreviewContext() {
}
void PrintWebViewHelper::PrintPreviewContext::InitWithFrame(
blink::WebLocalFrame* web_frame) {
DCHECK(web_frame);
DCHECK(!IsRendering());
state_ = INITIALIZED;
source_frame_.Reset(web_frame);
source_node_.reset();
}
void PrintWebViewHelper::PrintPreviewContext::InitWithNode(
const blink::WebNode& web_node) {
DCHECK(!web_node.isNull());
DCHECK(web_node.document().frame());
DCHECK(!IsRendering());
state_ = INITIALIZED;
source_frame_.Reset(web_node.document().frame());
source_node_ = web_node;
}
void PrintWebViewHelper::PrintPreviewContext::OnPrintPreview() {
DCHECK_EQ(INITIALIZED, state_);
ClearContext();
}
bool PrintWebViewHelper::PrintPreviewContext::CreatePreviewDocument(
PrepareFrameAndViewForPrint* prepared_frame,
const std::vector<int>& pages) {
DCHECK_EQ(INITIALIZED, state_);
state_ = RENDERING;
// Need to make sure old object gets destroyed first.
prep_frame_view_.reset(prepared_frame);
prep_frame_view_->StartPrinting();
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 PdfMetafileSkia);
if (!metafile_->Init()) {
set_error(PREVIEW_ERROR_METAFILE_INIT_FAILED);
LOG(ERROR) << "PdfMetafileSkia Init failed";
return false;
}
current_page_index_ = 0;
pages_to_render_ = pages;
// Sort and make unique.
std::sort(pages_to_render_.begin(), pages_to_render_.end());
pages_to_render_.resize(
std::unique(pages_to_render_.begin(), pages_to_render_.end()) -
pages_to_render_.begin());
// Remove invalid pages.
pages_to_render_.resize(std::lower_bound(pages_to_render_.begin(),
pages_to_render_.end(),
total_page_count_) -
pages_to_render_.begin());
print_ready_metafile_page_count_ = pages_to_render_.size();
if (pages_to_render_.empty()) {
print_ready_metafile_page_count_ = total_page_count_;
// Render all pages.
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) {
if (pages_index < print_ready_metafile_page_count_ &&
i == pages_to_render_[pages_index]) {
pages_index++;
continue;
}
pages_to_render_.push_back(i);
}
}
document_render_time_ = base::TimeDelta();
begin_time_ = base::TimeTicks::Now();
return true;
}
void PrintWebViewHelper::PrintPreviewContext::RenderedPreviewPage(
const base::TimeDelta& page_time) {
DCHECK_EQ(RENDERING, state_);
document_render_time_ += page_time;
UMA_HISTOGRAM_TIMES("PrintPreview.RenderPDFPageTime", page_time);
}
void PrintWebViewHelper::PrintPreviewContext::AllPagesRendered() {
DCHECK_EQ(RENDERING, state_);
state_ = DONE;
prep_frame_view_->FinishPrinting();
}
void PrintWebViewHelper::PrintPreviewContext::FinalizePrintReadyDocument() {
DCHECK(IsRendering());
base::TimeTicks begin_time = base::TimeTicks::Now();
metafile_->FinishDocument();
if (print_ready_metafile_page_count_ <= 0) {
NOTREACHED();
return;
}
UMA_HISTOGRAM_MEDIUM_TIMES("PrintPreview.RenderToPDFTime",
document_render_time_);
base::TimeDelta total_time =
(base::TimeTicks::Now() - begin_time) + document_render_time_;
UMA_HISTOGRAM_MEDIUM_TIMES("PrintPreview.RenderAndGeneratePDFTime",
total_time);
UMA_HISTOGRAM_MEDIUM_TIMES("PrintPreview.RenderAndGeneratePDFTimeAvgPerPage",
total_time / pages_to_render_.size());
}
void PrintWebViewHelper::PrintPreviewContext::Finished() {
DCHECK_EQ(DONE, state_);
state_ = INITIALIZED;
ClearContext();
}
void PrintWebViewHelper::PrintPreviewContext::Failed(bool report_error) {
DCHECK(state_ == INITIALIZED || state_ == RENDERING);
state_ = INITIALIZED;
if (report_error) {
DCHECK_NE(PREVIEW_ERROR_NONE, error_);
UMA_HISTOGRAM_ENUMERATION("PrintPreview.RendererError", error_,
PREVIEW_ERROR_LAST_ENUM);
}
ClearContext();
}
int PrintWebViewHelper::PrintPreviewContext::GetNextPageNumber() {
DCHECK_EQ(RENDERING, state_);
if (IsFinalPageRendered())
return -1;
return pages_to_render_[current_page_index_++];
}
bool PrintWebViewHelper::PrintPreviewContext::IsRendering() const {
return state_ == RENDERING || state_ == DONE;
}
bool PrintWebViewHelper::PrintPreviewContext::IsModifiable() {
// The only kind of node we can print right now is a PDF node.
return !PrintingNodeOrPdfFrame(source_frame(), source_node_);
}
bool PrintWebViewHelper::PrintPreviewContext::HasSelection() {
return IsModifiable() && source_frame()->hasSelection();
}
bool PrintWebViewHelper::PrintPreviewContext::IsLastPageOfPrintReadyMetafile()
const {
DCHECK(IsRendering());
return current_page_index_ == print_ready_metafile_page_count_;
}
bool PrintWebViewHelper::PrintPreviewContext::IsFinalPageRendered() const {
DCHECK(IsRendering());
return static_cast<size_t>(current_page_index_) == pages_to_render_.size();
}
void PrintWebViewHelper::PrintPreviewContext::set_generate_draft_pages(
bool generate_draft_pages) {
DCHECK_EQ(INITIALIZED, state_);
generate_draft_pages_ = generate_draft_pages;
}
void PrintWebViewHelper::PrintPreviewContext::set_error(
enum PrintPreviewErrorBuckets error) {
error_ = error;
}
blink::WebLocalFrame* PrintWebViewHelper::PrintPreviewContext::source_frame() {
DCHECK(state_ != UNINITIALIZED);
return source_frame_.GetFrame();
}
const blink::WebNode&
PrintWebViewHelper::PrintPreviewContext::source_node() const {
DCHECK(state_ != UNINITIALIZED);
return source_node_;
}
blink::WebLocalFrame*
PrintWebViewHelper::PrintPreviewContext::prepared_frame() {
DCHECK(state_ != UNINITIALIZED);
return prep_frame_view_->frame();
}
const blink::WebNode&
PrintWebViewHelper::PrintPreviewContext::prepared_node() const {
DCHECK(state_ != UNINITIALIZED);
return prep_frame_view_->node();
}
int PrintWebViewHelper::PrintPreviewContext::total_page_count() const {
DCHECK(state_ != UNINITIALIZED);
return total_page_count_;
}
bool PrintWebViewHelper::PrintPreviewContext::generate_draft_pages() const {
return generate_draft_pages_;
}
PdfMetafileSkia* PrintWebViewHelper::PrintPreviewContext::metafile() {
DCHECK(IsRendering());
return metafile_.get();
}
int PrintWebViewHelper::PrintPreviewContext::last_error() const {
return error_;
}
void PrintWebViewHelper::PrintPreviewContext::ClearContext() {
prep_frame_view_.reset();
metafile_.reset();
pages_to_render_.clear();
error_ = PREVIEW_ERROR_NONE;
}
} // namespace printing

Просмотреть файл

@ -76,6 +76,19 @@ class PrintWebViewHelper
OK,
FAIL_PRINT_INIT,
FAIL_PRINT,
FAIL_PREVIEW,
};
enum PrintPreviewErrorBuckets {
PREVIEW_ERROR_NONE, // Always first.
PREVIEW_ERROR_BAD_SETTING,
PREVIEW_ERROR_METAFILE_COPY_FAILED,
PREVIEW_ERROR_METAFILE_INIT_FAILED,
PREVIEW_ERROR_ZERO_PAGES,
PREVIEW_ERROR_MAC_DRAFT_METAFILE_INIT_FAILED,
PREVIEW_ERROR_PAGE_RENDERED_WITHOUT_METAFILE,
PREVIEW_ERROR_INVALID_PRINTER_SETTINGS,
PREVIEW_ERROR_LAST_ENUM // Always last.
};
// RenderViewObserver implementation.
@ -88,6 +101,8 @@ class PrintWebViewHelper
void OnPrintPages(bool silent, bool print_background);
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|.
@ -99,6 +114,24 @@ class PrintWebViewHelper
// 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,
@ -120,6 +153,14 @@ class PrintWebViewHelper
const blink::WebNode& node,
int* number_of_pages);
// 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::WebFrame* frame,
@ -193,6 +234,13 @@ class PrintWebViewHelper
// 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.
@ -213,10 +261,121 @@ class PrintWebViewHelper
// 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_;
scoped_ptr<PrepareFrameAndViewForPrint> prep_frame_view_;
scoped_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_;
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

Просмотреть файл

@ -24,6 +24,36 @@ namespace printing {
using blink::WebFrame;
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;
scoped_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();
}
return PreviewPageRendered(page_number, draft_metafile.get());
}
bool PrintWebViewHelper::PrintPagesNative(blink::WebFrame* frame,
int page_count) {
PdfMetafileSkia metafile;

Просмотреть файл

@ -50,6 +50,47 @@ void PrintWebViewHelper::PrintPageInternal(
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;
scoped_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());
if (!draft_metafile->Init()) {
print_preview_context_.set_error(
PREVIEW_ERROR_MAC_DRAFT_METAFILE_INIT_FAILED);
LOG(ERROR) << "Draft PdfMetafileSkia Init failed";
return false;
}
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(),
true, initial_render_metafile, &page_size, 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();
}
}
return PreviewPageRendered(page_number, draft_metafile.get());
}
void PrintWebViewHelper::RenderPage(const PrintMsg_Print_Params& params,
int page_number,
WebFrame* frame,

Просмотреть файл

@ -21,7 +21,6 @@ namespace printing {
using blink::WebFrame;
#if 0
bool PrintWebViewHelper::RenderPreviewPage(
int page_number,
const PrintMsg_Print_Params& print_params) {
@ -53,7 +52,6 @@ bool PrintWebViewHelper::RenderPreviewPage(
}
return PreviewPageRendered(page_number, draft_metafile.get());
}
#endif
bool PrintWebViewHelper::PrintPagesNative(blink::WebFrame* frame,
int page_count) {

Просмотреть файл

@ -556,20 +556,11 @@ process.
### BrowserWindow.print([options])
* `options` Object
* `silent` Boolean - Don't ask user for print settings, defaults to `false`
* `printBackground` Boolean - Also prints the background color and image of
the web page, defaults to `false`.
Same with `webContents.print([options])`
Prints window's web page. When `silent` is set to `false`, Electron will pick
up system's default printer and default settings for printing.
### BrowserWindow.printToPDF(options, callback)
Calling `window.print()` in web page is equivalent to call
`BrowserWindow.print({silent: false, printBackground: false})`.
**Note:** On Windows, the print API relies on `pdf.dll`. If your application
doesn't need print feature, you can safely remove `pdf.dll` in saving binary
size.
Same with `webContents.printToPDF(options, callback)`
### BrowserWindow.loadUrl(url, [options])
@ -950,6 +941,45 @@ Unregisters any serviceworker if present and returns boolean as
response to `callback` when the JS promise is fullfilled or false
when the JS promise is rejected.
### WebContents.print([options])
* `options` Object
* `silent` Boolean - Don't ask user for print settings, defaults to `false`
* `printBackground` Boolean - Also prints the background color and image of
the web page, defaults to `false`.
Prints window's web page. When `silent` is set to `false`, Electron will pick
up system's default printer and default settings for printing.
Calling `window.print()` in web page is equivalent to call
`WebContents.print({silent: false, printBackground: false})`.
**Note:** On Windows, the print API relies on `pdf.dll`. If your application
doesn't need print feature, you can safely remove `pdf.dll` in saving binary
size.
### WebContents.printToPDF(options, callback)
* `options` Object
* `marginsType` Integer - Specify the type of margins to use
* 0 - default
* 1 - none
* 2 - minimum
* `printBackground` Boolean - Whether to print CSS backgrounds.
* `printSelectionOnly` Boolean - Whether to print selection only.
* `landscape` Boolean - `true` for landscape, `false` for portrait.
* `callback` Function - `function(error, data) {}`
* `error` Error
* `data` Buffer - PDF file content
Prints windows' web page as PDF with Chromium's preview printing custom
settings.
By default, the options will be
`{marginsType:0, printBackgrounds:false, printSelectionOnly:false,
landscape:false}`.
### WebContents.send(channel[, args...])
* `channel` String

Просмотреть файл

@ -308,6 +308,14 @@ Executes editing command `replace` in page.
Executes editing command `replaceMisspelling` in page.
### `<webview>.print([options])`
Prints webview's web page. Same with `webContents.print([options])`.
### `<webview>.printToPDF(options, callback)`
Prints webview's web page as PDF, Same with `webContents.printToPDF(options, callback)`
### `<webview>`.send(channel[, args...])
* `channel` String

Просмотреть файл

@ -316,6 +316,8 @@
'chromium_src/chrome/browser/printing/printer_query.h',
'chromium_src/chrome/browser/printing/printing_message_filter.cc',
'chromium_src/chrome/browser/printing/printing_message_filter.h',
'chromium_src/chrome/browser/printing/print_preview_message_handler.cc',
'chromium_src/chrome/browser/printing/print_preview_message_handler.h',
'chromium_src/chrome/browser/renderer_host/pepper/chrome_browser_pepper_host_factory.cc',
'chromium_src/chrome/browser/renderer_host/pepper/chrome_browser_pepper_host_factory.h',
'chromium_src/chrome/browser/renderer_host/pepper/pepper_broker_message_filter.cc',