зеркало из https://github.com/electron/electron.git
Add printToPDF Implementation.
This commit is contained in:
Родитель
b360f7d86a
Коммит
7ffa7042b1
|
@ -28,6 +28,8 @@
|
|||
#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 "chrome/browser/printing/print_preview_message_handler.h"
|
||||
#include "chrome/browser/ui/browser_dialogs.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"
|
||||
|
@ -97,6 +99,7 @@ NativeWindow::NativeWindow(content::WebContents* web_contents,
|
|||
zoom_factor_(1.0),
|
||||
weak_factory_(this) {
|
||||
printing::PrintViewManagerBasic::CreateForWebContents(web_contents);
|
||||
printing::PrintPreviewMessageHandler::CreateForWebContents(web_contents);
|
||||
|
||||
InitWithWebContents(web_contents, this);
|
||||
|
||||
|
@ -263,6 +266,8 @@ void NativeWindow::Print(bool silent, bool print_background) {
|
|||
}
|
||||
|
||||
void NativeWindow::PrintToPDF() {
|
||||
printing::PrintPreviewMessageHandler::FromWebContents(GetWebContents())->
|
||||
HandleGetPreview(NULL);
|
||||
}
|
||||
|
||||
void NativeWindow::ShowDefinitionForSelection() {
|
||||
|
|
|
@ -0,0 +1,237 @@
|
|||
// 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 <vector>
|
||||
|
||||
#include "atom/browser/ui/file_dialog.h"
|
||||
#include "atom/browser/native_window.h"
|
||||
#include "base/bind.h"
|
||||
#include "base/json/json_reader.h"
|
||||
#include "base/memory/ref_counted.h"
|
||||
#include "base/memory/ref_counted_memory.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 "content/public/browser/web_ui.h"
|
||||
#include "printing/page_size_margins.h"
|
||||
#include "printing/print_job_constants.h"
|
||||
#include "printing/pdf_metafile_skia.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));
|
||||
}
|
||||
}
|
||||
|
||||
base::RefCountedBytes* GetDataFromHandle(base::SharedMemoryHandle handle,
|
||||
uint32 data_size) {
|
||||
scoped_ptr<base::SharedMemory> shared_buf(
|
||||
new base::SharedMemory(handle, true));
|
||||
if (!shared_buf->Map(data_size)) {
|
||||
NOTREACHED();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
unsigned char* data_begin = static_cast<unsigned char*>(shared_buf->memory());
|
||||
std::vector<unsigned char> data(data_begin, data_begin + data_size);
|
||||
return base::RefCountedBytes::TakeVector(&data);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace printing {
|
||||
|
||||
PrintPreviewMessageHandler::PrintPreviewMessageHandler(
|
||||
WebContents* web_contents)
|
||||
: content::WebContentsObserver(web_contents) {
|
||||
DCHECK(web_contents);
|
||||
}
|
||||
|
||||
PrintPreviewMessageHandler::~PrintPreviewMessageHandler() {
|
||||
}
|
||||
|
||||
void PrintPreviewMessageHandler::OnDidGetPreviewPageCount(
|
||||
const PrintHostMsg_DidGetPreviewPageCount_Params& params) {
|
||||
if (params.page_count <= 0) {
|
||||
NOTREACHED();
|
||||
return;
|
||||
}
|
||||
|
||||
LOG(ERROR) << "OnDidGetPreviewPageCount: " << params.page_count;
|
||||
}
|
||||
|
||||
void PrintPreviewMessageHandler::OnDidPreviewPage(
|
||||
const PrintHostMsg_DidPreviewPage_Params& params) {
|
||||
int page_number = params.page_number;
|
||||
if (page_number < FIRST_PAGE_INDEX || !params.data_size)
|
||||
return;
|
||||
LOG(ERROR) << "OnDidPreviewPage: " << params.data_size;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
// TODO(joth): This seems like a good match for using RefCountedStaticMemory
|
||||
// to avoid the memory copy, but the SetPrintPreviewData call chain below
|
||||
// needs updating to accept the RefCountedMemory* base class.
|
||||
scoped_refptr<base::RefCountedBytes> data(
|
||||
GetDataFromHandle(params.metafile_data_handle, params.data_size));
|
||||
if (!data || !data->size())
|
||||
return;
|
||||
|
||||
LOG(ERROR) << params.preview_request_id;
|
||||
atom::NativeWindow* window = atom::NativeWindow::FromWebContents(
|
||||
web_contents());
|
||||
base::FilePath save_path;
|
||||
file_dialog::ShowSaveDialog(window, "Save As",
|
||||
base::FilePath(FILE_PATH_LITERAL("print.pdf")),
|
||||
file_dialog::Filters(), &save_path);
|
||||
printing::PdfMetafileSkia metafile;
|
||||
metafile.InitFromData(static_cast<const void*>(data->front()), data->size());
|
||||
base::File file(save_path,
|
||||
base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE);
|
||||
metafile.SaveTo(&file);
|
||||
}
|
||||
|
||||
//void PrintPreviewMessageHandler::OnPrintPreviewFailed(int document_cookie) {
|
||||
//StopWorker(document_cookie);
|
||||
|
||||
////PrintPreviewUI* print_preview_ui = GetPrintPreviewUI();
|
||||
////if (!print_preview_ui)
|
||||
////return;
|
||||
////print_preview_ui->OnPrintPreviewFailed();
|
||||
//}
|
||||
|
||||
//void PrintPreviewMessageHandler::OnDidGetDefaultPageLayout(
|
||||
//const PageSizeMargins& page_layout_in_points,
|
||||
//const gfx::Rect& printable_area_in_points,
|
||||
//bool has_custom_page_size_style) {
|
||||
////PrintPreviewUI* print_preview_ui = GetPrintPreviewUI();
|
||||
////if (!print_preview_ui)
|
||||
////return;
|
||||
////print_preview_ui->OnDidGetDefaultPageLayout(page_layout_in_points,
|
||||
////printable_area_in_points,
|
||||
////has_custom_page_size_style);
|
||||
//}
|
||||
|
||||
//void PrintPreviewMessageHandler::OnPrintPreviewCancelled(int document_cookie) {
|
||||
//// Always need to stop the worker.
|
||||
//StopWorker(document_cookie);
|
||||
//}
|
||||
|
||||
//void PrintPreviewMessageHandler::OnInvalidPrinterSettings(int document_cookie) {
|
||||
//StopWorker(document_cookie);
|
||||
////PrintPreviewUI* print_preview_ui = GetPrintPreviewUI();
|
||||
////if (!print_preview_ui)
|
||||
////return;
|
||||
////print_preview_ui->OnInvalidPrinterSettings();
|
||||
//}
|
||||
|
||||
//void PrintPreviewMessageHandler::OnSetOptionsFromDocument(
|
||||
//const PrintHostMsg_SetOptionsFromDocument_Params& params) {
|
||||
////PrintPreviewUI* print_preview_ui = GetPrintPreviewUI();
|
||||
////if (!print_preview_ui)
|
||||
////return;
|
||||
////print_preview_ui->OnSetOptionsFromDocument(params);
|
||||
//}
|
||||
|
||||
bool PrintPreviewMessageHandler::OnMessageReceived(
|
||||
const IPC::Message& message) {
|
||||
bool handled = true;
|
||||
IPC_BEGIN_MESSAGE_MAP(PrintPreviewMessageHandler, message)
|
||||
IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPreviewPageCount,
|
||||
OnDidGetPreviewPageCount)
|
||||
IPC_MESSAGE_HANDLER(PrintHostMsg_DidPreviewPage,
|
||||
OnDidPreviewPage)
|
||||
IPC_MESSAGE_HANDLER(PrintHostMsg_MetafileReadyForPrinting,
|
||||
OnMetafileReadyForPrinting)
|
||||
//IPC_MESSAGE_HANDLER(PrintHostMsg_PrintPreviewFailed,
|
||||
//OnPrintPreviewFailed)
|
||||
//IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetDefaultPageLayout,
|
||||
//OnDidGetDefaultPageLayout)
|
||||
//IPC_MESSAGE_HANDLER(PrintHostMsg_PrintPreviewCancelled,
|
||||
//OnPrintPreviewCancelled)
|
||||
//IPC_MESSAGE_HANDLER(PrintHostMsg_PrintPreviewInvalidPrinterSettings,
|
||||
//OnInvalidPrinterSettings)
|
||||
//IPC_MESSAGE_HANDLER(PrintHostMsg_SetOptionsFromDocument,
|
||||
//OnSetOptionsFromDocument)
|
||||
IPC_MESSAGE_UNHANDLED(handled = false)
|
||||
IPC_END_MESSAGE_MAP()
|
||||
return handled;
|
||||
}
|
||||
|
||||
void PrintPreviewMessageHandler::HandleGetPreview(const base::ListValue* args) {
|
||||
static int request_id = 0;
|
||||
request_id++;
|
||||
// A simulated Chromium print preivew setting.
|
||||
const std::string setting_json_str = "{ \
|
||||
\"pageRage\":[], \
|
||||
\"mediaSize\":{ \
|
||||
\"height_microns\":297000, \
|
||||
\"is_default\":true, \
|
||||
\"name\":\"ISO_A4\", \
|
||||
\"width_microns\":210000, \
|
||||
\"custom_display_name\":\"A4\" \
|
||||
}, \
|
||||
\"landscape\":true, \
|
||||
\"color\":2, \
|
||||
\"headerFooterEnabled\":false, \
|
||||
\"marginsType\":0, \
|
||||
\"isFirstRequest\":false, \
|
||||
\"requestID\":1, \
|
||||
\"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\":true, \
|
||||
\"shouldPrintSelectionOnly\":false \
|
||||
}";
|
||||
|
||||
scoped_ptr<base::DictionaryValue> settings(
|
||||
static_cast<base::DictionaryValue*>(
|
||||
base::JSONReader::Read(setting_json_str)));
|
||||
settings->SetInteger(printing::kPreviewRequestID, request_id);
|
||||
|
||||
LOG(ERROR) << "Print preview request start";
|
||||
content::RenderViewHost* rvh = web_contents()->GetRenderViewHost();
|
||||
rvh->Send(new PrintMsg_PrintPreview(rvh->GetRoutingID(), *settings));
|
||||
}
|
||||
|
||||
} // namespace printing
|
|
@ -0,0 +1,70 @@
|
|||
// 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 "base/compiler_specific.h"
|
||||
#include "content/public/browser/web_contents_observer.h"
|
||||
#include "content/public/browser/web_contents_user_data.h"
|
||||
|
||||
struct PrintHostMsg_DidGetPreviewPageCount_Params;
|
||||
struct PrintHostMsg_DidPreviewDocument_Params;
|
||||
struct PrintHostMsg_DidPreviewPage_Params;
|
||||
|
||||
namespace content {
|
||||
class WebContents;
|
||||
}
|
||||
|
||||
namespace gfx {
|
||||
class Rect;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
// Asks the initiator renderer to generate a preview. First element of |args|
|
||||
// is a job settings JSON string.
|
||||
void HandleGetPreview(const base::ListValue* args);
|
||||
|
||||
private:
|
||||
explicit PrintPreviewMessageHandler(content::WebContents* web_contents);
|
||||
friend class content::WebContentsUserData<PrintPreviewMessageHandler>;
|
||||
|
||||
|
||||
// Message handlers.
|
||||
//void OnRequestPrintPreview(
|
||||
//const PrintHostMsg_RequestPrintPreview_Params& params);
|
||||
//void OnDidGetDefaultPageLayout(
|
||||
//const printing::PageSizeMargins& page_layout_in_points,
|
||||
//const gfx::Rect& printable_area_in_points,
|
||||
//bool has_custom_page_size_style);
|
||||
void OnDidGetPreviewPageCount(
|
||||
const PrintHostMsg_DidGetPreviewPageCount_Params& params);
|
||||
void OnDidPreviewPage(const PrintHostMsg_DidPreviewPage_Params& params);
|
||||
void OnMetafileReadyForPrinting(
|
||||
const PrintHostMsg_DidPreviewDocument_Params& params);
|
||||
//void OnPrintPreviewFailed(int document_cookie);
|
||||
//void OnPrintPreviewCancelled(int document_cookie);
|
||||
//void OnInvalidPrinterSettings(int document_cookie);
|
||||
//void OnSetOptionsFromDocument(
|
||||
//const PrintHostMsg_SetOptionsFromDocument_Params& params);
|
||||
|
||||
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(), ¶ms.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,61 @@ IPC_STRUCT_BEGIN(PrintHostMsg_ScriptedPrint_Params)
|
|||
IPC_STRUCT_MEMBER(printing::MarginType, margin_type)
|
||||
IPC_STRUCT_END()
|
||||
|
||||
// Parameters to describe a rendered preview page.
|
||||
IPC_STRUCT_BEGIN(PrintHostMsg_DidPreviewPage_Params)
|
||||
// A shared memory handle to metafile data for a draft document of the page.
|
||||
IPC_STRUCT_MEMBER(base::SharedMemoryHandle, metafile_data_handle)
|
||||
|
||||
// Size of metafile data.
|
||||
IPC_STRUCT_MEMBER(uint32, data_size)
|
||||
|
||||
// |page_number| is zero-based and can be |printing::INVALID_PAGE_INDEX| if it
|
||||
// is just a check.
|
||||
IPC_STRUCT_MEMBER(int, page_number)
|
||||
|
||||
// The id of the preview request.
|
||||
IPC_STRUCT_MEMBER(int, preview_request_id)
|
||||
IPC_STRUCT_END()
|
||||
|
||||
// Parameters sent along with the page count.
|
||||
IPC_STRUCT_BEGIN(PrintHostMsg_DidGetPreviewPageCount_Params)
|
||||
// Cookie for the document to ensure correctness.
|
||||
IPC_STRUCT_MEMBER(int, document_cookie)
|
||||
|
||||
// Total page count.
|
||||
IPC_STRUCT_MEMBER(int, page_count)
|
||||
|
||||
// Indicates whether the previewed document is modifiable.
|
||||
IPC_STRUCT_MEMBER(bool, is_modifiable)
|
||||
|
||||
// The id of the preview request.
|
||||
IPC_STRUCT_MEMBER(int, preview_request_id)
|
||||
|
||||
// Indicates whether the existing preview data needs to be cleared or not.
|
||||
IPC_STRUCT_MEMBER(bool, clear_preview_data)
|
||||
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 +255,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 +294,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 +318,20 @@ IPC_MESSAGE_ROUTED0(PrintHostMsg_ShowInvalidPrinterSettingsError)
|
|||
IPC_MESSAGE_ROUTED1(PrintHostMsg_PrintingFailed,
|
||||
int /* document cookie */)
|
||||
|
||||
// Notify the browser a print preview page has been rendered.
|
||||
IPC_MESSAGE_ROUTED1(PrintHostMsg_DidPreviewPage,
|
||||
PrintHostMsg_DidPreviewPage_Params /* params */)
|
||||
|
||||
// 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 */)
|
||||
|
||||
// Notify the browser the number of pages in the print preview document.
|
||||
IPC_MESSAGE_ROUTED1(PrintHostMsg_DidGetPreviewPageCount,
|
||||
PrintHostMsg_DidGetPreviewPageCount_Params /* params */)
|
||||
|
||||
|
||||
#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,157 @@ 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);
|
||||
LOG(ERROR) << "OnPrintPreview1";
|
||||
if (!print_preview_context_.source_frame()) {
|
||||
DidFinishPrinting(FAIL_PREVIEW);
|
||||
return;
|
||||
}
|
||||
|
||||
LOG(ERROR) << "OnPrintPreview2";
|
||||
//SetPrintPagesParams(settings)
|
||||
if (!UpdatePrintSettings(print_preview_context_.source_frame(),
|
||||
print_preview_context_.source_node(), settings)) {
|
||||
DidFinishPrinting(FAIL_PREVIEW);
|
||||
return;
|
||||
}
|
||||
LOG(ERROR) << "OnPrintPreview3";
|
||||
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;
|
||||
}
|
||||
|
||||
PageSizeMargins default_page_layout;
|
||||
ComputePageLayoutInPointsForCss(print_preview_context_.prepared_frame(), 0,
|
||||
print_params, ignore_css_margins_, NULL,
|
||||
&default_page_layout);
|
||||
|
||||
//bool has_page_size_style =
|
||||
//PrintingFrameHasPageSizeStyle(print_preview_context_.prepared_frame(),
|
||||
//print_preview_context_.total_page_count());
|
||||
int dpi = GetDPI(&print_params);
|
||||
|
||||
gfx::Rect printable_area_in_points(
|
||||
ConvertUnit(print_params.printable_area.x(), dpi, kPointsPerInch),
|
||||
ConvertUnit(print_params.printable_area.y(), dpi, kPointsPerInch),
|
||||
ConvertUnit(print_params.printable_area.width(), dpi, kPointsPerInch),
|
||||
ConvertUnit(print_params.printable_area.height(), dpi, kPointsPerInch));
|
||||
|
||||
|
||||
PrintHostMsg_DidGetPreviewPageCount_Params params;
|
||||
params.page_count = print_preview_context_.total_page_count();
|
||||
params.is_modifiable = print_preview_context_.IsModifiable();
|
||||
params.document_cookie = print_params.document_cookie;
|
||||
params.preview_request_id = print_params.preview_request_id;
|
||||
params.clear_preview_data = print_preview_context_.generate_draft_pages();
|
||||
Send(new PrintHostMsg_DidGetPreviewPageCount(routing_id(), params));
|
||||
|
||||
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.
|
||||
|
@ -916,6 +1068,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 +1201,280 @@ 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;
|
||||
}
|
||||
|
||||
PrintHostMsg_DidPreviewPage_Params preview_page_params;
|
||||
// Get the size of the resulting metafile.
|
||||
uint32 buf_size = metafile->GetDataSize();
|
||||
DCHECK_GT(buf_size, 0u);
|
||||
if (!CopyMetafileDataToSharedMem(
|
||||
metafile, &(preview_page_params.metafile_data_handle))) {
|
||||
LOG(ERROR) << "CopyMetafileDataToSharedMem failed";
|
||||
print_preview_context_.set_error(PREVIEW_ERROR_METAFILE_COPY_FAILED);
|
||||
return false;
|
||||
}
|
||||
preview_page_params.data_size = buf_size;
|
||||
preview_page_params.page_number = page_number;
|
||||
preview_page_params.preview_request_id =
|
||||
print_pages_params_->params.preview_request_id;
|
||||
|
||||
Send(new PrintHostMsg_DidPreviewPage(routing_id(), preview_page_params));
|
||||
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
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -314,6 +314,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',
|
||||
|
|
Загрузка…
Ссылка в новой задаче