Return node::Buffer as a printToPDF callback result.
This commit is contained in:
parent
ab40da3f31
commit
ac62871645
4 changed files with 25 additions and 71 deletions
|
@ -52,7 +52,7 @@ class WebContents : public mate::EventEmitter,
|
||||||
public content::WebContentsObserver,
|
public content::WebContentsObserver,
|
||||||
public content::GpuDataManagerObserver {
|
public content::GpuDataManagerObserver {
|
||||||
public:
|
public:
|
||||||
typedef base::Callback<void(int)> PrintToPDFCallback;
|
typedef base::Callback<void(v8::Local<v8::Value>)> PrintToPDFCallback;
|
||||||
|
|
||||||
// Create from an existing WebContents.
|
// Create from an existing WebContents.
|
||||||
static mate::Handle<WebContents> CreateFrom(
|
static mate::Handle<WebContents> CreateFrom(
|
||||||
|
|
|
@ -89,7 +89,6 @@ wrapWebContents = (webContents) ->
|
||||||
collate:true,
|
collate:true,
|
||||||
shouldPrintBackgrounds:false,
|
shouldPrintBackgrounds:false,
|
||||||
shouldPrintSelectionOnly:false
|
shouldPrintSelectionOnly:false
|
||||||
silent:false
|
|
||||||
|
|
||||||
if options.landscape
|
if options.landscape
|
||||||
printingSetting.landscape = options.landscape
|
printingSetting.landscape = options.landscape
|
||||||
|
|
|
@ -25,6 +25,8 @@
|
||||||
#include "printing/print_job_constants.h"
|
#include "printing/print_job_constants.h"
|
||||||
#include "printing/pdf_metafile_skia.h"
|
#include "printing/pdf_metafile_skia.h"
|
||||||
|
|
||||||
|
#include "atom/common/node_includes.h"
|
||||||
|
|
||||||
using content::BrowserThread;
|
using content::BrowserThread;
|
||||||
using content::WebContents;
|
using content::WebContents;
|
||||||
|
|
||||||
|
@ -60,18 +62,6 @@ base::RefCountedBytes* GetDataFromHandle(base::SharedMemoryHandle handle,
|
||||||
return base::RefCountedBytes::TakeVector(&data);
|
return base::RefCountedBytes::TakeVector(&data);
|
||||||
}
|
}
|
||||||
|
|
||||||
printing::PrintPreviewMessageHandler::PrintPDFResult
|
|
||||||
SavePDF(const scoped_refptr<base::RefCountedBytes>& data,
|
|
||||||
const base::FilePath& 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);
|
|
||||||
return metafile.SaveTo(&file)?
|
|
||||||
printing::PrintPreviewMessageHandler::SUCCESS :
|
|
||||||
printing::PrintPreviewMessageHandler::FAIL_SAVEFILE;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
namespace printing {
|
namespace printing {
|
||||||
|
@ -111,43 +101,15 @@ void PrintPreviewMessageHandler::OnMetafileReadyForPrinting(
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(joth): This seems like a good match for using RefCountedStaticMemory
|
base::RefCountedBytes *data = (
|
||||||
// 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));
|
GetDataFromHandle(params.metafile_data_handle, params.data_size));
|
||||||
if (!data || !data->size())
|
RunPrintToPDFCallback(params.preview_request_id, data);
|
||||||
return;
|
|
||||||
|
|
||||||
int request_id = params.preview_request_id;
|
|
||||||
std::string file_path =
|
|
||||||
print_to_pdf_request_details_map_[request_id].save_path;
|
|
||||||
base::FilePath save_path =
|
|
||||||
file_path.empty() ? base::FilePath(FILE_PATH_LITERAL("print.pdf")):
|
|
||||||
base::FilePath::FromUTF8Unsafe(file_path);
|
|
||||||
if (!print_to_pdf_request_details_map_[request_id].silent) {
|
|
||||||
atom::NativeWindow* window = atom::NativeWindow::FromWebContents(
|
|
||||||
web_contents());
|
|
||||||
if (!file_dialog::ShowSaveDialog(window, "Save As",
|
|
||||||
base::FilePath(FILE_PATH_LITERAL("print.pdf")),
|
|
||||||
file_dialog::Filters(), &save_path)) { // Users cancel dialog.
|
|
||||||
RunPrintToPDFCallback(request_id, FAIL_CANCEL);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
BrowserThread::PostTaskAndReplyWithResult(
|
|
||||||
BrowserThread::FILE,
|
|
||||||
FROM_HERE,
|
|
||||||
base::Bind(&SavePDF, data, save_path),
|
|
||||||
base::Bind(&PrintPreviewMessageHandler::RunPrintToPDFCallback,
|
|
||||||
base::Unretained(this),
|
|
||||||
request_id));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void PrintPreviewMessageHandler::OnPrintPreviewFailed(int document_cookie,
|
void PrintPreviewMessageHandler::OnPrintPreviewFailed(int document_cookie,
|
||||||
int request_id) {
|
int request_id) {
|
||||||
StopWorker(document_cookie);
|
StopWorker(document_cookie);
|
||||||
RunPrintToPDFCallback(request_id, FAIL_PREVIEW);
|
RunPrintToPDFCallback(request_id, nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PrintPreviewMessageHandler::OnMessageReceived(
|
bool PrintPreviewMessageHandler::OnMessageReceived(
|
||||||
|
@ -172,21 +134,26 @@ void PrintPreviewMessageHandler::PrintToPDF(
|
||||||
const atom::api::WebContents::PrintToPDFCallback& callback) {
|
const atom::api::WebContents::PrintToPDFCallback& callback) {
|
||||||
int request_id;
|
int request_id;
|
||||||
options.GetInteger(printing::kPreviewRequestID, &request_id);
|
options.GetInteger(printing::kPreviewRequestID, &request_id);
|
||||||
PrintToPDFRequestDetails details;
|
print_to_pdf_callback_map_[request_id] = callback;
|
||||||
options.GetBoolean("silent", &details.silent);
|
|
||||||
options.GetString("savePath", &details.save_path);
|
|
||||||
details.callback = callback;
|
|
||||||
print_to_pdf_request_details_map_[request_id] = details;
|
|
||||||
|
|
||||||
content::RenderViewHost* rvh = web_contents()->GetRenderViewHost();
|
content::RenderViewHost* rvh = web_contents()->GetRenderViewHost();
|
||||||
rvh->Send(new PrintMsg_PrintPreview(rvh->GetRoutingID(), options));
|
rvh->Send(new PrintMsg_PrintPreview(rvh->GetRoutingID(), options));
|
||||||
}
|
}
|
||||||
|
|
||||||
void PrintPreviewMessageHandler::RunPrintToPDFCallback(
|
void PrintPreviewMessageHandler::RunPrintToPDFCallback(
|
||||||
int request_id, PrintPDFResult result) {
|
int request_id, base::RefCountedBytes* data) {
|
||||||
print_to_pdf_request_details_map_[request_id].callback.Run(
|
v8::Isolate* isolate = v8::Isolate::GetCurrent();
|
||||||
static_cast<int>(result));
|
v8::Locker locker(isolate);
|
||||||
print_to_pdf_request_details_map_.erase(request_id);
|
v8::HandleScope handle_scope(isolate);
|
||||||
|
if (data) {
|
||||||
|
v8::Local<v8::Value> buffer = node::Buffer::Use(
|
||||||
|
const_cast<char*>(reinterpret_cast<const char*>(data->front())),
|
||||||
|
data->size());
|
||||||
|
print_to_pdf_callback_map_[request_id].Run(buffer);
|
||||||
|
} else {
|
||||||
|
print_to_pdf_callback_map_[request_id].Run(v8::Null(isolate));
|
||||||
|
}
|
||||||
|
print_to_pdf_callback_map_.erase(request_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace printing
|
} // namespace printing
|
||||||
|
|
|
@ -37,13 +37,6 @@ class PrintPreviewMessageHandler
|
||||||
: public content::WebContentsObserver,
|
: public content::WebContentsObserver,
|
||||||
public content::WebContentsUserData<PrintPreviewMessageHandler> {
|
public content::WebContentsUserData<PrintPreviewMessageHandler> {
|
||||||
public:
|
public:
|
||||||
enum PrintPDFResult {
|
|
||||||
SUCCESS,
|
|
||||||
FAIL_PREVIEW,
|
|
||||||
FAIL_SAVEFILE,
|
|
||||||
FAIL_CANCEL,
|
|
||||||
};
|
|
||||||
|
|
||||||
~PrintPreviewMessageHandler() override;
|
~PrintPreviewMessageHandler() override;
|
||||||
|
|
||||||
// content::WebContentsObserver implementation.
|
// content::WebContentsObserver implementation.
|
||||||
|
@ -53,14 +46,8 @@ class PrintPreviewMessageHandler
|
||||||
const atom::api::WebContents::PrintToPDFCallback& callback);
|
const atom::api::WebContents::PrintToPDFCallback& callback);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct PrintToPDFRequestDetails {
|
typedef std::map<int, atom::api::WebContents::PrintToPDFCallback>
|
||||||
std::string save_path;
|
PrintToPDFCallbackMap;
|
||||||
bool silent;
|
|
||||||
atom::api::WebContents::PrintToPDFCallback callback;
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef std::map<int, PrintToPDFRequestDetails>
|
|
||||||
PrintToPDFRequestDetailsMap;
|
|
||||||
|
|
||||||
explicit PrintPreviewMessageHandler(content::WebContents* web_contents);
|
explicit PrintPreviewMessageHandler(content::WebContents* web_contents);
|
||||||
friend class content::WebContentsUserData<PrintPreviewMessageHandler>;
|
friend class content::WebContentsUserData<PrintPreviewMessageHandler>;
|
||||||
|
@ -73,9 +60,10 @@ class PrintPreviewMessageHandler
|
||||||
const PrintHostMsg_DidPreviewDocument_Params& params);
|
const PrintHostMsg_DidPreviewDocument_Params& params);
|
||||||
void OnPrintPreviewFailed(int document_cookie, int request_id);
|
void OnPrintPreviewFailed(int document_cookie, int request_id);
|
||||||
|
|
||||||
void RunPrintToPDFCallback(int request_id, PrintPDFResult result);
|
void RunPrintToPDFCallback(
|
||||||
|
int request_id, base::RefCountedBytes* data);
|
||||||
|
|
||||||
PrintToPDFRequestDetailsMap print_to_pdf_request_details_map_;
|
PrintToPDFCallbackMap print_to_pdf_callback_map_;
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(PrintPreviewMessageHandler);
|
DISALLOW_COPY_AND_ASSIGN(PrintPreviewMessageHandler);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue