]> khamidou — GitHub mirrors - electron.git/commitdiff
Return node::Buffer as a printToPDF callback result.
authorHaojian Wu <hokein.wu@gmail.com>
Wed, 10 Jun 2015 07:16:00 +0000 (15:16 +0800)
committerHaojian Wu <hokein.wu@gmail.com>
Tue, 16 Jun 2015 12:07:44 +0000 (20:07 +0800)
atom/browser/api/atom_api_web_contents.h
atom/browser/api/lib/web-contents.coffee
chromium_src/chrome/browser/printing/print_preview_message_handler.cc
chromium_src/chrome/browser/printing/print_preview_message_handler.h

index 13abf5184d1520ea405385d332ff45b03b8cc6ea..dbd75db286fd6bdb360693441729eeba246e2d2c 100644 (file)
@@ -52,7 +52,7 @@ class WebContents : public mate::EventEmitter,
                     public content::WebContentsObserver,
                     public content::GpuDataManagerObserver {
  public:
-  typedef base::Callback<void(int)> PrintToPDFCallback;
+  typedef base::Callback<void(v8::Local<v8::Value>)> PrintToPDFCallback;
 
   // Create from an existing WebContents.
   static mate::Handle<WebContents> CreateFrom(
index 865154f0cdc45b9d213a817e1d8e4216e92f6546..5725f00119080597853f67b655e206445d680ff8 100644 (file)
@@ -89,7 +89,6 @@ wrapWebContents = (webContents) ->
       collate:true,
       shouldPrintBackgrounds:false,
       shouldPrintSelectionOnly:false
-      silent:false
 
     if options.landscape
       printingSetting.landscape = options.landscape
index 9b72a9a10445bb771349ce9462444ade9ca19063..50739ed57323902b6c860e0c1e01477b7eaa94b8 100644 (file)
@@ -25,6 +25,8 @@
 #include "printing/print_job_constants.h"
 #include "printing/pdf_metafile_skia.h"
 
+#include "atom/common/node_includes.h"
+
 using content::BrowserThread;
 using content::WebContents;
 
@@ -60,18 +62,6 @@ base::RefCountedBytes* GetDataFromHandle(base::SharedMemoryHandle handle,
   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 printing {
@@ -111,43 +101,15 @@ void PrintPreviewMessageHandler::OnMetafileReadyForPrinting(
     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(
+  base::RefCountedBytes *data = (
       GetDataFromHandle(params.metafile_data_handle, params.data_size));
-  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());
-    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));
+  RunPrintToPDFCallback(params.preview_request_id, data);
 }
 
 void PrintPreviewMessageHandler::OnPrintPreviewFailed(int document_cookie,
                                                       int request_id) {
   StopWorker(document_cookie);
-  RunPrintToPDFCallback(request_id, FAIL_PREVIEW);
+  RunPrintToPDFCallback(request_id, nullptr);
 }
 
 bool PrintPreviewMessageHandler::OnMessageReceived(
@@ -172,21 +134,26 @@ void PrintPreviewMessageHandler::PrintToPDF(
     const atom::api::WebContents::PrintToPDFCallback& callback) {
   int request_id;
   options.GetInteger(printing::kPreviewRequestID, &request_id);
-  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;
+  print_to_pdf_callback_map_[request_id] = callback;
 
   content::RenderViewHost* rvh = web_contents()->GetRenderViewHost();
   rvh->Send(new PrintMsg_PrintPreview(rvh->GetRoutingID(), options));
 }
 
 void PrintPreviewMessageHandler::RunPrintToPDFCallback(
-     int request_id, PrintPDFResult result) {
-  print_to_pdf_request_details_map_[request_id].callback.Run(
-      static_cast<int>(result));
-  print_to_pdf_request_details_map_.erase(request_id);
+     int request_id, base::RefCountedBytes* data) {
+  v8::Isolate* isolate = v8::Isolate::GetCurrent();
+  v8::Locker locker(isolate);
+  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
index 656fde4a6083256f549b740416eabaa0b44d6b75..bfdd436b196cfaf36620612e7c7a7e2d14d3e102 100644 (file)
@@ -37,13 +37,6 @@ class PrintPreviewMessageHandler
     : public content::WebContentsObserver,
       public content::WebContentsUserData<PrintPreviewMessageHandler> {
  public:
-  enum PrintPDFResult {
-    SUCCESS,
-    FAIL_PREVIEW,
-    FAIL_SAVEFILE,
-    FAIL_CANCEL,
-  };
-
   ~PrintPreviewMessageHandler() override;
 
   // content::WebContentsObserver implementation.
@@ -53,14 +46,8 @@ class PrintPreviewMessageHandler
                   const atom::api::WebContents::PrintToPDFCallback& callback);
 
  private:
-  struct PrintToPDFRequestDetails {
-    std::string save_path;
-    bool silent;
-    atom::api::WebContents::PrintToPDFCallback callback;
-  };
-
-  typedef std::map<int, PrintToPDFRequestDetails>
-      PrintToPDFRequestDetailsMap;
+  typedef std::map<int, atom::api::WebContents::PrintToPDFCallback>
+      PrintToPDFCallbackMap;
 
   explicit PrintPreviewMessageHandler(content::WebContents* web_contents);
   friend class content::WebContentsUserData<PrintPreviewMessageHandler>;
@@ -73,9 +60,10 @@ class PrintPreviewMessageHandler
       const PrintHostMsg_DidPreviewDocument_Params& params);
   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);
 };