Add silent and savePath options.

This commit is contained in:
Haojian Wu 2015-06-10 13:18:41 +08:00
parent 6e099af5fe
commit ab40da3f31
3 changed files with 34 additions and 15 deletions

View file

@ -89,6 +89,7 @@ wrapWebContents = (webContents) ->
collate:true,
shouldPrintBackgrounds:false,
shouldPrintSelectionOnly:false
silent:false
if options.landscape
printingSetting.landscape = options.landscape

View file

@ -119,22 +119,29 @@ void PrintPreviewMessageHandler::OnMetafileReadyForPrinting(
if (!data || !data->size())
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());
base::FilePath save_path;
if (!file_dialog::ShowSaveDialog(window, "Save As",
base::FilePath(FILE_PATH_LITERAL("print.pdf")),
file_dialog::Filters(), &save_path)) { // Users cancel dialog.
RunPrintToPDFCallback(params.preview_request_id, FAIL_CANCEL);
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),
params.preview_request_id));
request_id));
}
void PrintPreviewMessageHandler::OnPrintPreviewFailed(int document_cookie,
@ -165,7 +172,11 @@ void PrintPreviewMessageHandler::PrintToPDF(
const atom::api::WebContents::PrintToPDFCallback& callback) {
int request_id;
options.GetInteger(printing::kPreviewRequestID, &request_id);
print_to_pdf_callback_map_[request_id] = callback;
PrintToPDFRequestDetails details;
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();
rvh->Send(new PrintMsg_PrintPreview(rvh->GetRoutingID(), options));
@ -173,8 +184,9 @@ void PrintPreviewMessageHandler::PrintToPDF(
void PrintPreviewMessageHandler::RunPrintToPDFCallback(
int request_id, PrintPDFResult result) {
print_to_pdf_callback_map_[request_id].Run(static_cast<int>(result));
print_to_pdf_callback_map_.erase(request_id);
print_to_pdf_request_details_map_[request_id].callback.Run(
static_cast<int>(result));
print_to_pdf_request_details_map_.erase(request_id);
}
} // namespace printing

View file

@ -53,8 +53,14 @@ class PrintPreviewMessageHandler
const atom::api::WebContents::PrintToPDFCallback& callback);
private:
typedef std::map<int, atom::api::WebContents::PrintToPDFCallback>
PrintToPDFCallbackMap;
struct PrintToPDFRequestDetails {
std::string save_path;
bool silent;
atom::api::WebContents::PrintToPDFCallback callback;
};
typedef std::map<int, PrintToPDFRequestDetails>
PrintToPDFRequestDetailsMap;
explicit PrintPreviewMessageHandler(content::WebContents* web_contents);
friend class content::WebContentsUserData<PrintPreviewMessageHandler>;
@ -69,7 +75,7 @@ class PrintPreviewMessageHandler
void RunPrintToPDFCallback(int request_id, PrintPDFResult result);
PrintToPDFCallbackMap print_to_pdf_callback_map_;
PrintToPDFRequestDetailsMap print_to_pdf_request_details_map_;
DISALLOW_COPY_AND_ASSIGN(PrintPreviewMessageHandler);
};