Skip to content

Commit

Permalink
chore: backport printing changes (#15023, #15143) to 4-0-x (#15976)
Browse files Browse the repository at this point in the history
* refactor: move printing out of chromium_src (#15023)

* remove printing related things from chromium_src

* chore: add printing build flag and patch

* fix: include PrintingService on other platforms too

* fix: printing_handler is only needed on Windows

* fix: format BUILD.gn properly

* fix: rename printing build flag to avoid conflict with chromium

* fix: place previously missed printing calls behind build flag

* fix: accidentally renamed flag in patch file

* fix: don't include all printing strings

* fix: allow ShowItemInFolder and OpenItem to block, fixing a DCHECK crash

* fix: make things compile, some changes got lost while rebasing

* fix: remove rogue line from BUILD.gn

* chore: update patch description

* style: lint fix

* chore: use chromium printing buildflag, move node related stuff out of patch

* revert: remove ScopedAllowBlockingForTesting call

* fix: fix my rebase blooper

* fix: re-add header lost during rebase, update patch

* fix: add <map> include, tweak the patch a bit

* revert: remove rogue diff from patch

* fix: clean up after rebase

* refactor: printing implementation (#15143)

* 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

* chore: cleanup api usage from //chrome
  • Loading branch information
bengotow authored and ckerr committed Dec 17, 2018
1 parent bc68c26 commit f6fa88a
Show file tree
Hide file tree
Showing 66 changed files with 1,551 additions and 7,534 deletions.
25 changes: 22 additions & 3 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,11 @@ static_library("electron_lib") {
"native_mate",
"//base",
"//base:i18n",
"//chrome/app/resources:platform_locale_settings",
"//components/certificate_transparency",
"//components/net_log",
"//components/network_session_configurator/common",
"//components/prefs",
"//components/printing/common",
"//components/viz/service",
"//content/public/app:both",
"//content/public/child",
Expand All @@ -222,7 +222,6 @@ static_library("electron_lib") {
"//ppapi/host",
"//ppapi/proxy",
"//ppapi/shared_impl",
"//printing",
"//services/proxy_resolver:lib",
"//skia",
"//third_party/blink/public:blink",
Expand Down Expand Up @@ -367,6 +366,7 @@ static_library("electron_lib") {
# Disable warnings for g_settings_list_schemas.
"GLIB_DISABLE_DEPRECATION_WARNINGS",
]

sources += filenames.lib_sources_nss
}
if (is_win) {
Expand Down Expand Up @@ -437,6 +437,15 @@ static_library("electron_lib") {
]
}

if (enable_basic_printing) {
sources += [
"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 (enable_pepper_flash) {
deps += [ "components/pepper_flash" ]
}
Expand Down Expand Up @@ -831,7 +840,6 @@ group("chromium_unittests") {
"//mojo:mojo_unittests",
"//net:net_unittests",
"//ppapi:ppapi_unittests",
"//printing:printing_unittests",
"//skia:skia_unittests",
"//sql:sql_unittests",
"//storage:storage_unittests",
Expand All @@ -849,6 +857,10 @@ group("chromium_unittests") {
"//v8/test/unittests:unittests",
]

if (enable_basic_printing) {
deps += [ "//printing:printing_unittests" ]
}

if (is_linux) {
deps += [
"//net:disk_cache_memory_test",
Expand Down Expand Up @@ -991,6 +1003,13 @@ group("electron_content_manifest_overlays") {
service_manifest("electron_content_packaged_services_manifest_overlay") {
source = "//electron/manifests/electron_content_packaged_services_manifest_overlay.json"
packaged_services = [ "//services/proxy_resolver:proxy_resolver_manifest" ]

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

service_manifest("electron_content_browser_manifest_overlay") {
Expand Down
7 changes: 6 additions & 1 deletion atom/browser/api/atom_api_app.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include "atom/common/native_mate_converters/net_converter.h"
#include "atom/common/native_mate_converters/network_converter.h"
#include "atom/common/native_mate_converters/value_converter.h"
#include "atom/common/node_includes.h"
#include "atom/common/options_switches.h"
#include "base/command_line.h"
#include "base/environment.h"
Expand Down Expand Up @@ -54,6 +53,12 @@
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/image/image.h"

// clang-format off
// This header should be declared at the end to avoid
// redefinition errors.
#include "atom/common/node_includes.h" // NOLINT(build/include_alpha)
// clang-format on

#if defined(OS_WIN)
#include "atom/browser/ui/win/jump_list.h"
#include "base/strings/utf_string_conversions.h"
Expand Down
101 changes: 51 additions & 50 deletions atom/browser/api/atom_api_web_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@
#include "brightray/browser/inspectable_web_contents.h"
#include "brightray/browser/inspectable_web_contents_view.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/printing/print_preview_message_handler.h"
#include "chrome/browser/printing/print_view_manager_basic.h"
#include "chrome/browser/ssl/security_state_tab_helper.h"
#include "content/browser/frame_host/frame_tree_node.h"
#include "content/browser/frame_host/render_frame_host_manager.h"
Expand Down Expand Up @@ -102,34 +100,16 @@
#include "ui/gfx/font_render_params.h"
#endif

#include "atom/common/node_includes.h"

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

struct PrintSettings {
bool silent;
bool print_background;
base::string16 device_name;
};
} // namespace
#include "atom/common/node_includes.h"

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 @@ -143,6 +123,7 @@ struct Converter<printing::PrinterBasicInfo> {
return dict.GetHandle();
}
};
#endif

template <>
struct Converter<WindowOpenDisposition> {
Expand Down Expand Up @@ -1472,40 +1453,58 @@ bool WebContents::IsDOMReady() const {
return is_dom_ready_;
}

#if BUILDFLAG(ENABLE_PRINTING)
void WebContents::Print(mate::Arguments* args) {
PrintSettings settings = {false, false, base::string16()};
if (args->Length() >= 1 && !args->GetNext(&settings)) {
args->ThrowError();
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;
}
options.Get("silent", &silent);
options.Get("printBackground", &print_background);
if (options.Get("deviceName", &device_name) && !device_name.empty()) {
settings.SetString(printing::kSettingDeviceName, device_name);
}
print_view_manager_basic_ptr->PrintNow(
web_contents()->GetMainFrame(), settings.silent,
settings.print_background, settings.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;
auto print_backend = printing::PrintBackend::CreateInstance(nullptr);
base::ThreadRestrictions::ScopedAllowIO allow_io;
print_backend->EnumeratePrinters(&printers);
{
// TODO(deepak1556): Deprecate this api in favor of an
// async version and post a non blocing task call.
base::ThreadRestrictions::ScopedAllowIO allow_io;
auto print_backend = printing::PrintBackend::CreateInstance(nullptr);
print_backend->EnumeratePrinters(&printers);
}
return printers;
}

void WebContents::PrintToPDF(const base::DictionaryValue& setting,
const PrintToPDFCallback& callback) {
printing::PrintPreviewMessageHandler::FromWebContents(web_contents())
->PrintToPDF(setting, callback);
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 @@ -2125,9 +2124,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
Original file line number Diff line number Diff line change
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 @@ -78,10 +83,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 from an existing WebContents.
static mate::Handle<WebContents> CreateFrom(
v8::Isolate* isolate,
Expand Down Expand Up @@ -155,15 +156,18 @@ class WebContents : public mate::TrackableObject<WebContents>,
bool IsAudioMuted();
bool IsCurrentlyAudible();
bool IsDOMReady() const;
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 @@ -485,7 +489,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
25 changes: 21 additions & 4 deletions atom/browser/atom_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,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 "chrome/browser/speech/tts_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 All @@ -60,6 +58,7 @@
#include "net/base/escape.h"
#include "net/ssl/ssl_cert_request_info.h"
#include "ppapi/host/ppapi_host.h"
#include "printing/buildflags/buildflags.h"
#include "services/device/public/cpp/geolocation/location_provider.h"
#include "services/network/public/cpp/resource_request_body.h"
#include "services/proxy_resolver/public/mojom/proxy_resolver.mojom.h"
Expand All @@ -85,6 +84,12 @@
#include "atom/browser/fake_location_provider.h"
#endif // BUILDFLAG(OVERRIDE_LOCATION_PROVIDER)

#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;

namespace atom {
Expand Down Expand Up @@ -212,8 +217,10 @@ void AtomBrowserClient::RenderProcessWillLaunch(
if (IsProcessObserved(process_id))
return;

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

ProcessPreferences prefs;
auto* web_preferences =
Expand Down Expand Up @@ -537,6 +544,16 @@ void AtomBrowserClient::RegisterOutOfProcessServices(
(*services)[proxy_resolver::mojom::kProxyResolverServiceName] =
base::BindRepeating(&l10n_util::GetStringUTF16,
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);
#endif
}

std::unique_ptr<base::Value> AtomBrowserClient::GetServiceManifestOverlay(
Expand Down
6 changes: 6 additions & 0 deletions atom/browser/atom_browser_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "brightray/common/application_info.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/pref_names.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/prefs/json_pref_store.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
Expand Down Expand Up @@ -119,13 +120,18 @@ AtomBrowserContext::AtomBrowserContext(const std::string& partition,
proxy_config_monitor_ = std::make_unique<ProxyConfigMonitor>(prefs_.get());
io_handle_ = new URLRequestContextGetter::Handle(weak_factory_.GetWeakPtr());
cookie_change_notifier_ = std::make_unique<CookieChangeNotifier>(this);

BrowserContextDependencyManager::GetInstance()->MarkBrowserContextLive(this);
}

AtomBrowserContext::~AtomBrowserContext() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
NotifyWillBeDestroyed(this);
ShutdownStoragePartitions();
io_handle_->ShutdownOnUIThread();
// Notify any keyed services of browser context destruction.
BrowserContextDependencyManager::GetInstance()->DestroyBrowserContextServices(
this);
}

void AtomBrowserContext::InitPrefs() {
Expand Down
Loading

0 comments on commit f6fa88a

Please sign in to comment.