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, 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

View file

@ -119,14 +119,21 @@ void PrintPreviewMessageHandler::OnMetafileReadyForPrinting(
if (!data || !data->size()) if (!data || !data->size())
return; return;
atom::NativeWindow* window = atom::NativeWindow::FromWebContents( int request_id = params.preview_request_id;
web_contents()); std::string file_path =
base::FilePath save_path; print_to_pdf_request_details_map_[request_id].save_path;
if (!file_dialog::ShowSaveDialog(window, "Save As", base::FilePath save_path =
base::FilePath(FILE_PATH_LITERAL("print.pdf")), file_path.empty() ? base::FilePath(FILE_PATH_LITERAL("print.pdf")):
file_dialog::Filters(), &save_path)) { // Users cancel dialog. base::FilePath::FromUTF8Unsafe(file_path);
RunPrintToPDFCallback(params.preview_request_id, FAIL_CANCEL); if (!print_to_pdf_request_details_map_[request_id].silent) {
return; 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::PostTaskAndReplyWithResult(
BrowserThread::FILE, BrowserThread::FILE,
@ -134,7 +141,7 @@ void PrintPreviewMessageHandler::OnMetafileReadyForPrinting(
base::Bind(&SavePDF, data, save_path), base::Bind(&SavePDF, data, save_path),
base::Bind(&PrintPreviewMessageHandler::RunPrintToPDFCallback, base::Bind(&PrintPreviewMessageHandler::RunPrintToPDFCallback,
base::Unretained(this), base::Unretained(this),
params.preview_request_id)); request_id));
} }
void PrintPreviewMessageHandler::OnPrintPreviewFailed(int document_cookie, void PrintPreviewMessageHandler::OnPrintPreviewFailed(int document_cookie,
@ -165,7 +172,11 @@ 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);
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(); content::RenderViewHost* rvh = web_contents()->GetRenderViewHost();
rvh->Send(new PrintMsg_PrintPreview(rvh->GetRoutingID(), options)); rvh->Send(new PrintMsg_PrintPreview(rvh->GetRoutingID(), options));
@ -173,8 +184,9 @@ void PrintPreviewMessageHandler::PrintToPDF(
void PrintPreviewMessageHandler::RunPrintToPDFCallback( void PrintPreviewMessageHandler::RunPrintToPDFCallback(
int request_id, PrintPDFResult result) { int request_id, PrintPDFResult result) {
print_to_pdf_callback_map_[request_id].Run(static_cast<int>(result)); print_to_pdf_request_details_map_[request_id].callback.Run(
print_to_pdf_callback_map_.erase(request_id); static_cast<int>(result));
print_to_pdf_request_details_map_.erase(request_id);
} }
} // namespace printing } // namespace printing

View file

@ -53,8 +53,14 @@ class PrintPreviewMessageHandler
const atom::api::WebContents::PrintToPDFCallback& callback); const atom::api::WebContents::PrintToPDFCallback& callback);
private: private:
typedef std::map<int, atom::api::WebContents::PrintToPDFCallback> struct PrintToPDFRequestDetails {
PrintToPDFCallbackMap; std::string save_path;
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>;
@ -69,7 +75,7 @@ class PrintPreviewMessageHandler
void RunPrintToPDFCallback(int request_id, PrintPDFResult result); 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); DISALLOW_COPY_AND_ASSIGN(PrintPreviewMessageHandler);
}; };