#include "native_mate/callback.h"
#include "native_mate/constructor.h"
#include "native_mate/dictionary.h"
-#include "printing/print_job_constants.h"
#include "ui/gfx/geometry/rect.h"
#include "atom/common/node_includes.h"
void Window::PrintToPDF(mate::Arguments* args) {
mate::Dictionary options;
- if (args->Length() == 1 && !args->GetNext(&options)) {
+ base::Callback<void(int)> callback;
+ if (!(args->Length() == 1 && !args->GetNext(&callback)) &&
+ !(args->Length() == 2 && args->GetNext(&options)
+ && args->GetNext(&callback))) {
args->ThrowError();
return;
}
- window_->PrintToPDF(options);
+ window_->PrintToPDF(options, callback);
}
void Window::SetProgressBar(double progress) {
return base::RefCountedBytes::TakeVector(&data);
}
-void PrintToPdfCallback(const scoped_refptr<base::RefCountedBytes>& data,
- const base::FilePath& save_path) {
+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);
- metafile.SaveTo(&file);
+ return metafile.SaveTo(&file)?
+ printing::PrintPreviewMessageHandler::SUCCESS :
+ printing::PrintPreviewMessageHandler::FAIL_SAVEFILE;
}
} // namespace
namespace printing {
+
PrintPreviewMessageHandler::PrintPreviewMessageHandler(
WebContents* web_contents)
: content::WebContentsObserver(web_contents) {
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);
- BrowserThread::PostTask(BrowserThread::FILE,
- FROM_HERE,
- base::Bind(&PrintToPdfCallback,
- data,
- 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);
+ return;
+ }
+ BrowserThread::PostTaskAndReplyWithResult(
+ BrowserThread::FILE,
+ FROM_HERE,
+ base::Bind(&SavePDF, data, save_path),
+ base::Bind(&PrintPreviewMessageHandler::RunPrintToPDFCallback,
+ base::Unretained(this),
+ params.preview_request_id));
}
//void PrintPreviewMessageHandler::OnPrintPreviewFailed(int document_cookie) {
}
void PrintPreviewMessageHandler::HandleGetPreview(
- const mate::Dictionary& options) {
+ const mate::Dictionary& options,
+ const atom::NativeWindow::PrintToPDFCallback& callback) {
static int request_id = 0;
request_id++;
// A simulated Chromium print preivew setting.
static_cast<base::DictionaryValue*>(
base::JSONReader::Read(setting_json_str)));
settings->SetInteger(printing::kPreviewRequestID, request_id);
+ print_to_pdf_callback_map_[request_id] = callback;
+
+
// Default Print PDF settings:
int margins_type = 0; // DEFAULT_MARGINS
bool print_background = false;
rvh->Send(new PrintMsg_PrintPreview(rvh->GetRoutingID(), *settings));
}
+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);
+}
+
} // namespace printing
#ifndef CHROME_BROWSER_PRINTING_PRINT_PREVIEW_MESSAGE_HANDLER_H_
#define CHROME_BROWSER_PRINTING_PRINT_PREVIEW_MESSAGE_HANDLER_H_
+#include <map>
+
+#include "atom/browser/native_window.h"
#include "base/compiler_specific.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_contents_user_data.h"
: public content::WebContentsObserver,
public content::WebContentsUserData<PrintPreviewMessageHandler> {
public:
+ enum PrintPDFResult {
+ SUCCESS,
+ FAIL_PREVIEW,
+ FAIL_SAVEFILE,
+ FAIL_CANCEL,
+ };
+
~PrintPreviewMessageHandler() override;
// content::WebContentsObserver implementation.
// Asks the initiator renderer to generate a preview. First element of |args|
// is a job settings JSON string.
- void HandleGetPreview(const mate::Dictionary& options);
+ void HandleGetPreview(const mate::Dictionary& options,
+ const atom::NativeWindow::PrintToPDFCallback& callback);
+
private:
+ typedef std::map<int, atom::NativeWindow::PrintToPDFCallback> PrintToPDFCallbackMap;
+
explicit PrintPreviewMessageHandler(content::WebContents* web_contents);
friend class content::WebContentsUserData<PrintPreviewMessageHandler>;
//void OnSetOptionsFromDocument(
//const PrintHostMsg_SetOptionsFromDocument_Params& params);
+ void RunPrintToPDFCallback(int request_id, PrintPDFResult result);
+
+ PrintToPDFCallbackMap print_to_pdf_callback_map_;
+
DISALLOW_COPY_AND_ASSIGN(PrintPreviewMessageHandler);
};