Skip to content

Commit

Permalink
refactor: printing implementation (#15143)
Browse files Browse the repository at this point in the history
* refactor: basic printing

* move build files to chromium_src/BUILD.gn
* remove dependency on chrome prerender sources

* spec: move printing specs behind feature flag

* build: register pdf compositor service
  • Loading branch information
deepak1556 authored and MarshallOfSound committed Nov 9, 2018
1 parent 53642b2 commit 8232296
Show file tree
Hide file tree
Showing 23 changed files with 780 additions and 1,073 deletions.
64 changes: 8 additions & 56 deletions BUILD.gn
Expand Up @@ -209,7 +209,6 @@ static_library("electron_lib") {
"//base:base_static",
"//base:i18n",
"//chrome/app/resources:platform_locale_settings",
"//chrome/common",
"//components/certificate_transparency",
"//components/net_log",
"//components/network_session_configurator/common",
Expand Down Expand Up @@ -433,62 +432,12 @@ static_library("electron_lib") {
}

if (enable_basic_printing) {
deps += [
"//chrome/common:mojo_bindings",
"//chrome/services/printing:lib",
"//components/printing/browser",
"//components/printing/common",
"//components/printing/renderer",
"//components/services/pdf_compositor/public/cpp:utils",
"//components/services/pdf_compositor/public/interfaces",
"//printing",
]

sources += [
"//chrome/browser/printing/print_job.cc",
"//chrome/browser/printing/print_job.h",
"//chrome/browser/printing/print_job_manager.cc",
"//chrome/browser/printing/print_job_manager.h",
"//chrome/browser/printing/print_job_worker.cc",
"//chrome/browser/printing/print_job_worker.h",
"//chrome/browser/printing/print_preview_message_handler.cc",
"//chrome/browser/printing/print_preview_message_handler.h",
"//chrome/browser/printing/print_view_manager_base.cc",
"//chrome/browser/printing/print_view_manager_base.h",
"//chrome/browser/printing/print_view_manager_basic.cc",
"//chrome/browser/printing/print_view_manager_basic.h",
"//chrome/browser/printing/print_view_manager_common.cc",
"//chrome/browser/printing/print_view_manager_common.h",
"//chrome/browser/printing/printer_manager_dialog.h",
"//chrome/browser/printing/printer_manager_dialog_linux.cc",
"//chrome/browser/printing/printer_manager_dialog_mac.mm",
"//chrome/browser/printing/printer_manager_dialog_win.cc",
"//chrome/browser/printing/printer_query.cc",
"//chrome/browser/printing/printer_query.h",
"//chrome/browser/printing/printing_message_filter.cc",
"//chrome/browser/printing/printing_message_filter.h",
"//chrome/renderer/prerender/prerender_dispatcher.cc",
"//chrome/renderer/prerender/prerender_dispatcher.h",
"//chrome/renderer/prerender/prerender_extra_data.cc",
"//chrome/renderer/prerender/prerender_extra_data.h",
"//chrome/renderer/prerender/prerender_helper.cc",
"//chrome/renderer/prerender/prerender_helper.h",
"//chrome/renderer/prerender/prerenderer_client.cc",
"//chrome/renderer/prerender/prerenderer_client.h",
"//chrome/renderer/printing/chrome_print_render_frame_helper_delegate.cc",
"//chrome/renderer/printing/chrome_print_render_frame_helper_delegate.h",
"//electron/atom/browser/atom_print_preview_message_handler.cc",
"//electron/atom/browser/atom_print_preview_message_handler.h",
"atom/browser/printing/print_preview_message_handler.cc",
"atom/browser/printing/print_preview_message_handler.h",
"atom/renderer/printing/print_render_frame_helper_delegate.cc",
"atom/renderer/printing/print_render_frame_helper_delegate.h",
]

if (is_win) {
sources += [
"//chrome/browser/printing/pdf_to_emf_converter.cc",
"//chrome/browser/printing/pdf_to_emf_converter.h",
"//chrome/utility/printing_handler.cc",
"//chrome/utility/printing_handler.h",
]
}
}

if (enable_pepper_flash) {
Expand Down Expand Up @@ -939,7 +888,10 @@ service_manifest("electron_content_packaged_services_manifest_overlay") {
packaged_services = [ "//services/proxy_resolver:proxy_resolver_manifest" ]

if (enable_basic_printing) {
packaged_services += [ "//chrome/services/printing:manifest" ]
packaged_services += [
"//chrome/services/printing:manifest",
"//components/services/pdf_compositor:pdf_compositor_manifest",
]
}
}

Expand Down
106 changes: 46 additions & 60 deletions atom/browser/api/atom_api_web_contents.cc
Expand Up @@ -80,7 +80,6 @@
#include "native_mate/dictionary.h"
#include "native_mate/object_template_builder.h"
#include "net/url_request/url_request_context.h"
#include "printing/buildflags/buildflags.h"
#include "third_party/blink/public/platform/web_input_event.h"
#include "third_party/blink/public/web/web_find_options.h"
#include "ui/display/screen.h"
Expand All @@ -102,39 +101,15 @@
#endif

#if BUILDFLAG(ENABLE_PRINTING)
#include "atom/browser/atom_print_preview_message_handler.h"
#include "chrome/browser/printing/print_view_manager_basic.h"
#include "components/printing/common/print_messages.h"
#endif

#include "atom/common/node_includes.h"

namespace {

struct PrintSettings {
bool silent;
bool print_background;
base::string16 device_name;
};

} // 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));
dict.Get("deviceName", &(out->device_name));
return true;
}
};

#if BUILDFLAG(ENABLE_PRINTING)
template <>
struct Converter<printing::PrinterBasicInfo> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
Expand All @@ -148,6 +123,7 @@ struct Converter<printing::PrinterBasicInfo> {
return dict.GetHandle();
}
};
#endif

template <>
struct Converter<WindowOpenDisposition> {
Expand Down Expand Up @@ -1482,50 +1458,58 @@ bool WebContents::IsCurrentlyAudible() {
return web_contents()->IsCurrentlyAudible();
}

void WebContents::Print(mate::Arguments* args) {
#if BUILDFLAG(ENABLE_PRINTING)
PrintSettings settings = {false, false, base::string16()};
if (args->Length() >= 1 && !args->GetNext(&settings)) {
args->ThrowError();
void WebContents::Print(mate::Arguments* args) {
bool silent, print_background = false;
base::string16 device_name;
mate::Dictionary options = mate::Dictionary::CreateEmpty(args->isolate());
base::DictionaryValue settings;
if (args->Length() >= 1 && !args->GetNext(&options)) {
args->ThrowError("Invalid print settings specified");
return;
}
auto* print_view_manager_basic_ptr =
printing::PrintViewManagerBasic::FromWebContents(web_contents());
if (args->Length() == 2) {
base::Callback<void(bool)> callback;
if (!args->GetNext(&callback)) {
args->ThrowError();
return;
}
print_view_manager_basic_ptr->SetCallback(callback);
printing::CompletionCallback callback;
if (args->Length() == 2 && !args->GetNext(&callback)) {
args->ThrowError("Invalid optional callback provided");
return;
}
print_view_manager_basic_ptr->PrintNow(
web_contents()->GetMainFrame(), settings.silent,
settings.print_background, settings.device_name);
#else
LOG(ERROR) << "Printing is disabled";
#endif
options.Get("silent", &silent);
options.Get("printBackground", &print_background);
if (options.Get("deviceName", &device_name) && !device_name.empty()) {
settings.SetString(printing::kSettingDeviceName, device_name);
}
auto* print_view_manager =
printing::PrintViewManagerBasic::FromWebContents(web_contents());
auto* focused_frame = web_contents()->GetFocusedFrame();
auto* rfh = focused_frame && focused_frame->HasSelection()
? focused_frame
: web_contents()->GetMainFrame();
print_view_manager->PrintNow(
rfh,
std::make_unique<PrintMsg_PrintPages>(rfh->GetRoutingID(), silent,
print_background, settings),
std::move(callback));
}

std::vector<printing::PrinterBasicInfo> WebContents::GetPrinterList() {
std::vector<printing::PrinterBasicInfo> printers;

#if BUILDFLAG(ENABLE_PRINTING)
auto print_backend = printing::PrintBackend::CreateInstance(nullptr);
base::ThreadRestrictions::ScopedAllowIO allow_io;
print_backend->EnumeratePrinters(&printers);
#endif

{
// TODO(deepak1556): Deprecate this api in favor of an
// async version and post a non blocing task call.
base::ThreadRestrictions::ScopedAllowIO allow_io;
print_backend->EnumeratePrinters(&printers);
}
return printers;
}

void WebContents::PrintToPDF(const base::DictionaryValue& setting,
const PrintToPDFCallback& callback) {
#if BUILDFLAG(ENABLE_PRINTING)
AtomPrintPreviewMessageHandler::FromWebContents(web_contents())
->PrintToPDF(setting, callback);
#endif
void WebContents::PrintToPDF(
const base::DictionaryValue& settings,
const PrintPreviewMessageHandler::PrintToPDFCallback& callback) {
PrintPreviewMessageHandler::FromWebContents(web_contents())
->PrintToPDF(settings, callback);
}
#endif

void WebContents::AddWorkSpace(mate::Arguments* args,
const base::FilePath& path) {
Expand Down Expand Up @@ -2144,9 +2128,11 @@ void WebContents::BuildPrototype(v8::Isolate* isolate,
.SetMethod("unregisterServiceWorker",
&WebContents::UnregisterServiceWorker)
.SetMethod("inspectServiceWorker", &WebContents::InspectServiceWorker)
.SetMethod("print", &WebContents::Print)
.SetMethod("getPrinters", &WebContents::GetPrinterList)
#if BUILDFLAG(ENABLE_PRINTING)
.SetMethod("_print", &WebContents::Print)
.SetMethod("_getPrinters", &WebContents::GetPrinterList)
.SetMethod("_printToPDF", &WebContents::PrintToPDF)
#endif
.SetMethod("addWorkSpace", &WebContents::AddWorkSpace)
.SetMethod("removeWorkSpace", &WebContents::RemoveWorkSpace)
.SetMethod("showDefinitionForSelection",
Expand Down
23 changes: 13 additions & 10 deletions atom/browser/api/atom_api_web_contents.h
Expand Up @@ -22,9 +22,14 @@
#include "content/public/common/favicon_url.h"
#include "electron/buildflags/buildflags.h"
#include "native_mate/handle.h"
#include "printing/backend/print_backend.h"
#include "printing/buildflags/buildflags.h"
#include "ui/gfx/image/image.h"

#if BUILDFLAG(ENABLE_PRINTING)
#include "atom/browser/printing/print_preview_message_handler.h"
#include "printing/backend/print_backend.h"
#endif

namespace blink {
struct WebDeviceEmulationParams;
}
Expand Down Expand Up @@ -75,10 +80,6 @@ class WebContents : public mate::TrackableObject<WebContents>,
OFF_SCREEN, // Used for offscreen rendering
};

// For node.js callback function type: function(error, buffer)
using PrintToPDFCallback =
base::Callback<void(v8::Local<v8::Value>, v8::Local<v8::Value>)>;

// Create a new WebContents and return the V8 wrapper of it.
static mate::Handle<WebContents> Create(v8::Isolate* isolate,
const mate::Dictionary& options);
Expand Down Expand Up @@ -162,15 +163,18 @@ class WebContents : public mate::TrackableObject<WebContents>,
void SetAudioMuted(bool muted);
bool IsAudioMuted();
bool IsCurrentlyAudible();
void Print(mate::Arguments* args);
std::vector<printing::PrinterBasicInfo> GetPrinterList();
void SetEmbedder(const WebContents* embedder);
void SetDevToolsWebContents(const WebContents* devtools);
v8::Local<v8::Value> GetNativeView() const;

#if BUILDFLAG(ENABLE_PRINTING)
void Print(mate::Arguments* args);
std::vector<printing::PrinterBasicInfo> GetPrinterList();
// Print current page as PDF.
void PrintToPDF(const base::DictionaryValue& setting,
const PrintToPDFCallback& callback);
void PrintToPDF(
const base::DictionaryValue& settings,
const PrintPreviewMessageHandler::PrintToPDFCallback& callback);
#endif

// DevTools workspace api.
void AddWorkSpace(mate::Arguments* args, const base::FilePath& path);
Expand Down Expand Up @@ -498,7 +502,6 @@ class WebContents : public mate::TrackableObject<WebContents>,

std::unique_ptr<AtomJavaScriptDialogManager> dialog_manager_;
std::unique_ptr<WebViewGuestDelegate> guest_delegate_;

std::unique_ptr<FrameSubscriber> frame_subscriber_;

// The host webcontents that may contain this webcontents.
Expand Down
10 changes: 8 additions & 2 deletions atom/browser/atom_browser_client.cc
Expand Up @@ -45,7 +45,6 @@
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/printing/printing_message_filter.h"
#include "components/net_log/chrome_net_log.h"
#include "content/public/browser/browser_ppapi_host.h"
#include "content/public/browser/client_certificate_delegate.h"
Expand Down Expand Up @@ -96,7 +95,9 @@
#endif // BUILDFLAG(ENABLE_TTS)

#if BUILDFLAG(ENABLE_PRINTING)
#include "chrome/browser/printing/printing_message_filter.h"
#include "chrome/services/printing/public/mojom/constants.mojom.h"
#include "components/services/pdf_compositor/public/interfaces/pdf_compositor.mojom.h"
#endif // BUILDFLAG(ENABLE_PRINTING)

using content::BrowserThread;
Expand Down Expand Up @@ -256,7 +257,8 @@ void AtomBrowserClient::RenderProcessWillLaunch(
return;

#if BUILDFLAG(ENABLE_PRINTING)
host->AddFilter(new printing::PrintingMessageFilter(process_id));
host->AddFilter(new printing::PrintingMessageFilter(
process_id, host->GetBrowserContext()));
#endif

#if BUILDFLAG(ENABLE_TTS)
Expand Down Expand Up @@ -592,6 +594,10 @@ void AtomBrowserClient::RegisterOutOfProcessServices(
IDS_UTILITY_PROCESS_PROXY_RESOLVER_NAME);

#if BUILDFLAG(ENABLE_PRINTING)
(*services)[printing::mojom::kServiceName] =
base::BindRepeating(&l10n_util::GetStringUTF16,
IDS_UTILITY_PROCESS_PDF_COMPOSITOR_SERVICE_NAME);

(*services)[printing::mojom::kChromePrintingServiceName] =
base::BindRepeating(&l10n_util::GetStringUTF16,
IDS_UTILITY_PROCESS_PRINTING_SERVICE_NAME);
Expand Down

0 comments on commit 8232296

Please sign in to comment.