Add "silent" and "print_background" option for printing.

This commit is contained in:
Cheng Zhao 2014-08-22 15:01:07 +08:00
parent d0c1b63064
commit e43b3309af
10 changed files with 56 additions and 34 deletions

View file

@ -16,8 +16,31 @@
#include "atom/common/node_includes.h"
namespace {
struct PrintSettings {
bool silent;
bool print_backgournd;
};
} // namespace
namespace mate {
template<>
struct Converter<PrintSettings> {
static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val,
PrintSettings* out) {
mate::Dictionary dict;
if (!ConvertFromV8(isolate, val, &dict))
return false;
if (!dict.Get("silent", &(out->silent)) ||
!dict.Get("printBackground", &(out->print_backgournd)))
return false;
return true;
}
};
template<>
struct Converter<gfx::Rect> {
static bool FromV8(v8::Isolate* isolate,
@ -334,8 +357,14 @@ void Window::CapturePage(mate::Arguments* args) {
rect, base::Bind(&OnCapturePageDone, args->isolate(), callback));
}
void Window::Print() {
window_->Print();
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_backgournd);
}
mate::Handle<WebContents> Window::GetWebContents(v8::Isolate* isolate) const {

View file

@ -102,7 +102,7 @@ class Window : public mate::EventEmitter,
void SetDocumentEdited(bool edited);
bool IsDocumentEdited();
void CapturePage(mate::Arguments* args);
void Print();
void Print(mate::Arguments* args);
// APIs for WebContents.
mate::Handle<WebContents> GetWebContents(v8::Isolate* isolate) const;

View file

@ -202,9 +202,9 @@ bool NativeWindow::IsDocumentEdited() {
void NativeWindow::SetMenu(ui::MenuModel* menu) {
}
void NativeWindow::Print() {
void NativeWindow::Print(bool silent, bool print_background) {
printing::PrintViewManagerBasic::FromWebContents(GetWebContents())->
PrintNow();
PrintNow(silent, print_background);
}
bool NativeWindow::HasModalDialog() {

View file

@ -157,7 +157,7 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate,
const CapturePageCallback& callback);
// Print current page.
virtual void Print();
virtual void Print(bool silent, bool print_background);
// The same with closing a tab in a real browser.
//

View file

@ -60,8 +60,9 @@ PrintViewManagerBase::~PrintViewManagerBase() {
DisconnectFromCurrentPrintJob();
}
bool PrintViewManagerBase::PrintNow() {
return PrintNowInternal(new PrintMsg_PrintPages(routing_id()));
bool PrintViewManagerBase::PrintNow(bool silent, bool print_background) {
return PrintNowInternal(new PrintMsg_PrintPages(
routing_id(), silent, print_background));
}
void PrintViewManagerBase::NavigationStopped() {

View file

@ -37,7 +37,7 @@ class PrintViewManagerBase : public content::NotificationObserver,
// Prints the current document immediately. Since the rendering is
// asynchronous, the actual printing will not be completed on the return of
// this function. Returns false if printing is impossible at the moment.
virtual bool PrintNow();
virtual bool PrintNow(bool silent, bool print_background);
// PrintedPagesSource implementation.
virtual base::string16 RenderSourceName() OVERRIDE;

View file

@ -22,7 +22,6 @@ PrintMsg_Print_Params::PrintMsg_Print_Params()
selection_only(false),
supports_alpha_blend(false),
print_scaling_option(blink::WebPrintScalingOptionSourceSize),
print_to_pdf(false),
title(),
url(),
should_print_backgrounds(false) {
@ -44,7 +43,6 @@ void PrintMsg_Print_Params::Reset() {
selection_only = false;
supports_alpha_blend = false;
print_scaling_option = blink::WebPrintScalingOptionSourceSize;
print_to_pdf = false;
title.clear();
url.clear();
should_print_backgrounds = false;

View file

@ -40,7 +40,6 @@ struct PrintMsg_Print_Params {
bool selection_only;
bool supports_alpha_blend;
blink::WebPrintScalingOption print_scaling_option;
bool print_to_pdf;
base::string16 title;
base::string16 url;
bool should_print_backgrounds;
@ -108,9 +107,6 @@ IPC_STRUCT_TRAITS_BEGIN(PrintMsg_Print_Params)
// Specifies the page scaling option for preview printing.
IPC_STRUCT_TRAITS_MEMBER(print_scaling_option)
// True if print to pdf is requested.
IPC_STRUCT_TRAITS_MEMBER(print_to_pdf)
// Title string to be printed as header if requested by the user.
IPC_STRUCT_TRAITS_MEMBER(title)
@ -187,7 +183,9 @@ IPC_STRUCT_END()
// Tells the render view to switch the CSS to print media type, renders every
// requested pages and switch back the CSS to display media type.
IPC_MESSAGE_ROUTED0(PrintMsg_PrintPages)
IPC_MESSAGE_ROUTED2(PrintMsg_PrintPages,
bool /* silent print */,
bool /* print page's background */)
// Tells the render view that printing is done so it can clean up.
IPC_MESSAGE_ROUTED1(PrintMsg_PrintingDone,

View file

@ -672,10 +672,10 @@ bool PrintWebViewHelper::GetPrintFrame(blink::WebLocalFrame** frame) {
return true;
}
void PrintWebViewHelper::OnPrintPages() {
void PrintWebViewHelper::OnPrintPages(bool silent, bool print_background) {
blink::WebLocalFrame* frame;
if (GetPrintFrame(&frame))
Print(frame, blink::WebNode());
Print(frame, blink::WebNode(), silent, print_background);
}
void PrintWebViewHelper::GetPageSizeAndContentAreaFromPageLayout(
@ -703,14 +703,6 @@ void PrintWebViewHelper::UpdateFrameMarginsCssInfo(
ignore_css_margins_ = (margins_type != DEFAULT_MARGINS);
}
bool PrintWebViewHelper::IsPrintToPdfRequested(
const base::DictionaryValue& job_settings) {
bool print_to_pdf = false;
if (!job_settings.GetBoolean(kSettingPrintToPDF, &print_to_pdf))
NOTREACHED();
return print_to_pdf;
}
void PrintWebViewHelper::OnPrintingDone(bool success) {
notify_browser_of_print_failure_ = false;
if (!success)
@ -743,7 +735,9 @@ void PrintWebViewHelper::PrintNode(const blink::WebNode& node) {
}
void PrintWebViewHelper::Print(blink::WebLocalFrame* frame,
const blink::WebNode& node) {
const blink::WebNode& node,
bool silent,
bool print_background) {
// If still not finished with earlier print request simply ignore.
if (prep_frame_view_)
return;
@ -763,12 +757,14 @@ void PrintWebViewHelper::Print(blink::WebLocalFrame* frame,
}
// Ask the browser to show UI to retrieve the final print settings.
if (!GetPrintSettingsFromUser(frame_ref.GetFrame(), node,
expected_page_count)) {
if (!silent && !GetPrintSettingsFromUser(frame_ref.GetFrame(), node,
expected_page_count)) {
DidFinishPrinting(OK); // Release resources and fail silently.
return;
}
print_pages_params_->params.should_print_backgrounds = print_background;
// Render Pages for printing.
if (!RenderPagesForPrint(frame_ref.GetFrame(), node)) {
LOG(ERROR) << "RenderPagesForPrint failed";

View file

@ -81,7 +81,7 @@ class PrintWebViewHelper
bool user_initiated) OVERRIDE;
// Message handlers ---------------------------------------------------------
void OnPrintPages();
void OnPrintPages(bool silent, bool print_background);
void OnPrintingDone(bool success);
// Get |page_size| and |content_area| information from
@ -94,12 +94,12 @@ class PrintWebViewHelper
// Update |ignore_css_margins_| based on settings.
void UpdateFrameMarginsCssInfo(const base::DictionaryValue& settings);
// Returns true if the current destination printer is PRINT_TO_PDF.
bool IsPrintToPdfRequested(const base::DictionaryValue& settings);
// Main printing code -------------------------------------------------------
void Print(blink::WebLocalFrame* frame, const blink::WebNode& node);
void Print(blink::WebLocalFrame* frame,
const blink::WebNode& node,
bool silent = false,
bool print_background = false);
// Notification when printing is done - signal tear-down/free resources.
void DidFinishPrinting(PrintingResult result);