]> khamidou — GitHub mirrors - electron.git/commitdiff
Expose Print API to webContents and webView.
authorHaojian Wu <hokein.wu@gmail.com>
Sat, 13 Jun 2015 13:23:45 +0000 (21:23 +0800)
committerHaojian Wu <hokein.wu@gmail.com>
Tue, 16 Jun 2015 12:08:30 +0000 (20:08 +0800)
Also move the print implementation from window to webContents.

atom/browser/api/atom_api_web_contents.cc
atom/browser/api/atom_api_web_contents.h
atom/browser/api/atom_api_window.cc
atom/browser/api/atom_api_window.h
atom/browser/api/lib/browser-window.coffee
atom/browser/common_web_contents_delegate.cc
atom/browser/native_window.cc
atom/browser/native_window.h
atom/renderer/lib/web-view/web-view.coffee
docs/api/browser-window.md
docs/api/web-view-tag.md

index 959b0e344dbfdbe850d82ddff8da717ef9066ec4..192955e6acbb3829614501a03ba81b947200a844 100644 (file)
@@ -18,6 +18,7 @@
 #include "base/strings/string_util.h"
 #include "base/strings/utf_string_conversions.h"
 #include "brightray/browser/inspectable_web_contents.h"
+#include "chrome/browser/printing/print_view_manager_basic.h"
 #include "chrome/browser/printing/print_preview_message_handler.h"
 #include "content/public/browser/favicon_status.h"
 #include "content/public/browser/guest_host.h"
 
 #include "atom/common/node_includes.h"
 
+namespace {
+
+struct PrintSettings {
+  bool silent;
+  bool print_background;
+};
+
+}  // namespace
+
 namespace mate {
 
 template<>
@@ -65,6 +75,19 @@ struct Converter<atom::api::SetSizeParams> {
   }
 };
 
+template<>
+struct Converter<PrintSettings> {
+  static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val,
+                     PrintSettings* out) {
+    mate::Dictionary dict;
+    if (!ConvertFromV8(isolate, val, &dict))
+      return false;
+    dict.Get("silent", &(out->silent));
+    dict.Get("printBackground", &(out->print_background));
+    return true;
+  }
+};
+
 }  // namespace mate
 
 
@@ -581,8 +604,6 @@ void WebContents::UnregisterServiceWorker(
                                    callback);
 }
 
-<<<<<<< HEAD
-<<<<<<< HEAD
 void WebContents::SetAudioMuted(bool muted) {
   web_contents()->SetAudioMuted(muted);
 }
@@ -591,6 +612,17 @@ bool WebContents::IsAudioMuted() {
   return web_contents()->IsAudioMuted();
 }
 
+void WebContents::Print(mate::Arguments* args) {
+  PrintSettings settings = { false, false };
+  if (args->Length() == 1 && !args->GetNext(&settings)) {
+    args->ThrowError();
+    return;
+  }
+
+  printing::PrintViewManagerBasic::FromWebContents(web_contents())->
+      PrintNow(settings.silent, settings.print_background);
+}
+
 void WebContents::PrintToPDF(const base::DictionaryValue& setting,
                              const PrintToPDFCallback& callback) {
   printing::PrintPreviewMessageHandler::FromWebContents(web_contents())->
@@ -769,6 +801,7 @@ mate::ObjectTemplateBuilder WebContents::GetObjectTemplateBuilder(
         .SetMethod("unregisterServiceWorker",
                    &WebContents::UnregisterServiceWorker)
         .SetMethod("inspectServiceWorker", &WebContents::InspectServiceWorker)
+        .SetMethod("print", &WebContents::Print)
         .SetMethod("_printToPDF", &WebContents::PrintToPDF)
         .Build());
 
index bab7c9e4064343538fb22bfb795b81321c335203..655087f9b0f5e4ed0441ee3e0578c1d9fdd26430 100644 (file)
@@ -93,6 +93,7 @@ class WebContents : public mate::EventEmitter,
   void UnregisterServiceWorker(const base::Callback<void(bool)>&);
   void SetAudioMuted(bool muted);
   bool IsAudioMuted();
+  void Print(mate::Arguments* args);
 
   // Print current page as PDF.
   void PrintToPDF(const base::DictionaryValue& setting,
index 69271c0db328eab84676d017db9dcda95e7515b3..0ad9a329e6ccc7182cfa37b490bc393096d0552d 100644 (file)
 
 #include "atom/common/node_includes.h"
 
-namespace {
-
-struct PrintSettings {
-  bool silent;
-  bool print_background;
-};
-
-}  // namespace
-
-namespace mate {
-
-template<>
-struct Converter<PrintSettings> {
-  static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val,
-                     PrintSettings* out) {
-    mate::Dictionary dict;
-    if (!ConvertFromV8(isolate, val, &dict))
-      return false;
-    dict.Get("silent", &(out->silent));
-    dict.Get("printBackground", &(out->print_background));
-    return true;
-  }
-};
-
-}  // namespace mate
-
 namespace atom {
 
 namespace api {
@@ -420,16 +394,6 @@ void Window::CapturePage(mate::Arguments* args) {
       rect, base::Bind(&OnCapturePageDone, args->isolate(), callback));
 }
 
-void Window::Print(mate::Arguments* args) {
-  PrintSettings settings = { false, false };
-  if (args->Length() == 1 && !args->GetNext(&settings)) {
-    args->ThrowError();
-    return;
-  }
-
-  window_->Print(settings.silent, settings.print_background);
-}
-
 void Window::SetProgressBar(double progress) {
   window_->SetProgressBar(progress);
 }
@@ -541,7 +505,6 @@ void Window::BuildPrototype(v8::Isolate* isolate,
       .SetMethod("blurWebView", &Window::BlurWebView)
       .SetMethod("isWebViewFocused", &Window::IsWebViewFocused)
       .SetMethod("capturePage", &Window::CapturePage)
-      .SetMethod("print", &Window::Print)
       .SetMethod("setProgressBar", &Window::SetProgressBar)
       .SetMethod("setOverlayIcon", &Window::SetOverlayIcon)
       .SetMethod("_setMenu", &Window::SetMenu)
index 43a32e86f45a5958022f8faea01410fcde942f1d..d2b9d28034df9eebcc0f1e68376ff5952355290b 100644 (file)
@@ -131,7 +131,6 @@ class Window : public mate::EventEmitter,
   void SetDocumentEdited(bool edited);
   bool IsDocumentEdited();
   void CapturePage(mate::Arguments* args);
-  void Print(mate::Arguments* args);
   void SetProgressBar(double progress);
   void SetOverlayIcon(const gfx::Image& overlay,
                       const std::string& description);
index f0691b19b8fa4eb40d15af8dfffb9da093921d44..b0ffe6b37ef9f09b3165b825cd8b8a2ccfaad893 100644 (file)
@@ -83,5 +83,6 @@ BrowserWindow::isDevToolsOpened = -> @webContents.isDevToolsOpened()
 BrowserWindow::toggleDevTools = -> @webContents.toggleDevTools()
 BrowserWindow::inspectElement = -> @webContents.inspectElement.apply @webContents, arguments
 BrowserWindow::inspectServiceWorker = -> @webContents.inspectServiceWorker()
+BrowserWindow::print = -> @webContents.print.apply @webContents, arguments
 
 module.exports = BrowserWindow
index 437d4d725f7339ec7f63d61938928063f67b3e26..285b93014e449e3592b26e35437ee7f91b8d5fb6 100644 (file)
@@ -13,6 +13,7 @@
 #include "atom/browser/web_dialog_helper.h"
 #include "base/files/file_util.h"
 #include "chrome/browser/printing/print_preview_message_handler.h"
+#include "chrome/browser/printing/print_view_manager_basic.h"
 #include "chrome/browser/ui/browser_dialogs.h"
 #include "content/public/browser/child_process_security_policy.h"
 #include "content/public/browser/render_process_host.h"
@@ -103,6 +104,7 @@ void CommonWebContentsDelegate::InitWithWebContents(
   owner_window_ = owner_window;
   web_contents->SetDelegate(this);
 
+  printing::PrintViewManagerBasic::CreateForWebContents(web_contents);
   printing::PrintPreviewMessageHandler::CreateForWebContents(web_contents);
 
   // Create InspectableWebContents.
index 8eb05cb9b0f22f413d6077ddf6403412d796a412..48ea9e76c32caa66e964579f279b59318d1879e9 100644 (file)
@@ -27,7 +27,6 @@
 #include "base/strings/utf_string_conversions.h"
 #include "brightray/browser/inspectable_web_contents.h"
 #include "brightray/browser/inspectable_web_contents_view.h"
-#include "chrome/browser/printing/print_view_manager_basic.h"
 #include "content/browser/renderer_host/render_widget_host_impl.h"
 #include "content/public/browser/navigation_entry.h"
 #include "content/public/browser/notification_details.h"
@@ -96,8 +95,6 @@ NativeWindow::NativeWindow(content::WebContents* web_contents,
       has_dialog_attached_(false),
       zoom_factor_(1.0),
       weak_factory_(this) {
-  printing::PrintViewManagerBasic::CreateForWebContents(web_contents);
-
   InitWithWebContents(web_contents, this);
 
   options.Get(switches::kFrame, &has_frame_);
@@ -257,11 +254,6 @@ bool NativeWindow::IsDocumentEdited() {
 void NativeWindow::SetMenu(ui::MenuModel* menu) {
 }
 
-void NativeWindow::Print(bool silent, bool print_background) {
-  printing::PrintViewManagerBasic::FromWebContents(GetWebContents())->
-      PrintNow(silent, print_background);
-}
-
 void NativeWindow::ShowDefinitionForSelection() {
   NOTIMPLEMENTED();
 }
index 2038775f8d9a53a2fd41c7f9e06f6b28f9d9d060..0298101ce043e282ed87cae56b529625f80bfb0c 100644 (file)
@@ -154,9 +154,6 @@ class NativeWindow : public CommonWebContentsDelegate,
   virtual void CapturePage(const gfx::Rect& rect,
                            const CapturePageCallback& callback);
 
-  // Print current page.
-  virtual void Print(bool silent, bool print_background);
-
   // Show popup dictionary.
   virtual void ShowDefinitionForSelection();
 
index 3778d7f48d3f76c8714c592664145948d6d424be..72fee948d137ced2e8c8890d14065d764037149a 100644 (file)
@@ -291,6 +291,7 @@ registerWebViewElement = ->
     "send"
     "getId"
     "inspectServiceWorker"
+    "print"
     "printToPDF"
   ]
 
index fb45ce1103714e8d24cf7c9e97f32689dce739b3..a25c4d88527dcc7299a6967a749f0dacb0cc1ff8 100644 (file)
@@ -556,20 +556,7 @@ process.
 
 ### BrowserWindow.print([options])
 
-* `options` Object
-  * `silent` Boolean - Don't ask user for print settings, defaults to `false`
-  * `printBackground` Boolean - Also prints the background color and image of
-    the web page, defaults to `false`.
-
-Prints window's web page. When `silent` is set to `false`, Electron will pick
-up system's default printer and default settings for printing.
-
-Calling `window.print()` in web page is equivalent to call
-`BrowserWindow.print({silent: false, printBackground: false})`.
-
-**Note:** On Windows, the print API relies on `pdf.dll`. If your application
-doesn't need print feature, you can safely remove `pdf.dll` in saving binary
-size.
+Same with `webContents.print([options])`
 
 ### BrowserWindow.loadUrl(url, [options])
 
@@ -950,6 +937,23 @@ Unregisters any serviceworker if present and returns boolean as
 response to `callback` when the JS promise is fullfilled or false
 when the JS promise is rejected.  
 
+### WebContents.print([options])
+
+* `options` Object
+  * `silent` Boolean - Don't ask user for print settings, defaults to `false`
+  * `printBackground` Boolean - Also prints the background color and image of
+    the web page, defaults to `false`.
+
+Prints window's web page. When `silent` is set to `false`, Electron will pick
+up system's default printer and default settings for printing.
+
+Calling `window.print()` in web page is equivalent to call
+`WebContents.print({silent: false, printBackground: false})`.
+
+**Note:** On Windows, the print API relies on `pdf.dll`. If your application
+doesn't need print feature, you can safely remove `pdf.dll` in saving binary
+size.
+
 ### WebContents.printToPDF(options, callback)
 
 * `options` Object
index 4a227d769cb76c252dfad93a485c687d471ceb9d..2a93440482537d570542d2d160f9848a24669165 100644 (file)
@@ -308,6 +308,10 @@ Executes editing command `replace` in page.
 
 Executes editing command `replaceMisspelling` in page.
 
+### `<webview>.print([options])`
+
+Prints webview's web page. Same with `webContents.print([options])`.
+
 ### `<webview>`.send(channel[, args...])
 
 * `channel` String