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
This commit is contained in:
Heilig Benedek 2018-10-13 03:57:04 +02:00 committed by Samuel Attard
parent c806c465fa
commit a82bcc7e3c
53 changed files with 1398 additions and 7358 deletions

View file

@ -51,8 +51,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"
@ -81,6 +79,7 @@
#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"
@ -101,6 +100,11 @@
#include "ui/gfx/font_render_params.h"
#endif
#if BUILDFLAG(ENABLE_PRINTING)
#include "atom/browser/atom_print_preview_message_handler.h"
#include "chrome/browser/printing/print_view_manager_basic.h"
#endif
#include "atom/common/node_includes.h"
namespace {
@ -110,6 +114,7 @@ struct PrintSettings {
bool print_background;
base::string16 device_name;
};
} // namespace
namespace mate {
@ -1423,6 +1428,7 @@ bool WebContents::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();
@ -1441,20 +1447,29 @@ void WebContents::Print(mate::Arguments* args) {
print_view_manager_basic_ptr->PrintNow(
web_contents()->GetMainFrame(), settings.silent,
settings.print_background, settings.device_name);
#else
LOG(ERROR) << "Printing is disabled";
#endif
}
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
return printers;
}
void WebContents::PrintToPDF(const base::DictionaryValue& setting,
const PrintToPDFCallback& callback) {
printing::PrintPreviewMessageHandler::FromWebContents(web_contents())
#if BUILDFLAG(ENABLE_PRINTING)
AtomPrintPreviewMessageHandler::FromWebContents(web_contents())
->PrintToPDF(setting, callback);
#endif
}
void WebContents::AddWorkSpace(mate::Arguments* args,

View file

@ -59,6 +59,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"
@ -88,6 +89,10 @@
#include "chrome/browser/speech/tts_message_filter.h"
#endif // BUILDFLAG(ENABLE_TTS)
#if BUILDFLAG(ENABLE_PRINTING)
#include "chrome/services/printing/public/mojom/constants.mojom.h"
#endif // BUILDFLAG(ENABLE_PRINTING)
using content::BrowserThread;
namespace atom {
@ -209,7 +214,9 @@ void AtomBrowserClient::RenderProcessWillLaunch(
if (IsProcessObserved(process_id))
return;
#if BUILDFLAG(ENABLE_PRINTING)
host->AddFilter(new printing::PrintingMessageFilter(process_id));
#endif
#if BUILDFLAG(ENABLE_TTS)
host->AddFilter(new TtsMessageFilter(host->GetBrowserContext()));
@ -535,6 +542,12 @@ 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::kChromePrintingServiceName] =
base::BindRepeating(&l10n_util::GetStringUTF16,
IDS_UTILITY_PROCESS_PRINTING_SERVICE_NAME);
#endif
}
std::unique_ptr<base::Value> AtomBrowserClient::GetServiceManifestOverlay(

View file

@ -0,0 +1,134 @@
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "atom/browser/atom_print_preview_message_handler.h"
#include <stdint.h>
#include <memory>
#include <utility>
#include <vector>
#include "base/bind.h"
#include "base/memory/ref_counted.h"
#include "base/memory/ref_counted_memory.h"
#include "base/memory/shared_memory.h"
#include "base/memory/shared_memory_handle.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/printing/print_job_manager.h"
#include "chrome/browser/printing/print_preview_dialog_controller.h"
#include "chrome/browser/printing/print_view_manager.h"
#include "chrome/browser/printing/printer_query.h"
#include "chrome/browser/ui/webui/print_preview/print_preview_ui.h"
#include "components/printing/browser/print_composite_client.h"
#include "components/printing/browser/print_manager_utils.h"
#include "components/printing/common/print_messages.h"
#include "components/services/pdf_compositor/public/cpp/pdf_service_mojo_types.h"
#include "components/services/pdf_compositor/public/cpp/pdf_service_mojo_utils.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_ui.h"
#include "printing/page_size_margins.h"
#include "printing/print_job_constants.h"
#include "printing/print_settings.h"
#include "atom/common/node_includes.h"
using content::BrowserThread;
using content::WebContents;
DEFINE_WEB_CONTENTS_USER_DATA_KEY(atom::AtomPrintPreviewMessageHandler);
namespace atom {
namespace {
char* CopyPDFDataOnIOThread(
const PrintHostMsg_DidPreviewDocument_Params& params) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
const PrintHostMsg_DidPrintContent_Params& content = params.content;
std::unique_ptr<base::SharedMemory> shared_buf(
new base::SharedMemory(content.metafile_data_handle, true));
if (!shared_buf->Map(content.data_size))
return nullptr;
char* pdf_data = new char[content.data_size];
memcpy(pdf_data, shared_buf->memory(), content.data_size);
return pdf_data;
}
void FreeNodeBufferData(char* data, void* hint) {
delete[] data;
}
} // namespace
AtomPrintPreviewMessageHandler::AtomPrintPreviewMessageHandler(
WebContents* web_contents)
: printing::PrintPreviewMessageHandler(web_contents),
weak_ptr_factory_(this) {
DCHECK(web_contents);
}
AtomPrintPreviewMessageHandler::~AtomPrintPreviewMessageHandler() {}
void AtomPrintPreviewMessageHandler::OnMetafileReadyForPrinting(
content::RenderFrameHost* render_frame_host,
const PrintHostMsg_DidPreviewDocument_Params& params,
const PrintHostMsg_PreviewIds& ids) {
printing::PrintPreviewMessageHandler::OnMetafileReadyForPrinting(
render_frame_host, params, ids);
BrowserThread::PostTaskAndReplyWithResult(
BrowserThread::IO, FROM_HERE, base::Bind(&CopyPDFDataOnIOThread, params),
base::Bind(&AtomPrintPreviewMessageHandler::RunPrintToPDFCallback,
base::Unretained(this), ids.request_id,
params.content.data_size));
}
void AtomPrintPreviewMessageHandler::OnPrintPreviewFailed(
int document_cookie,
const PrintHostMsg_PreviewIds& ids) {
printing::PrintPreviewMessageHandler::OnPrintPreviewFailed(document_cookie,
ids);
RunPrintToPDFCallback(ids.request_id, 0, nullptr);
}
void AtomPrintPreviewMessageHandler::PrintToPDF(
const base::DictionaryValue& options,
const PrintToPDFCallback& callback) {
int request_id;
options.GetInteger(printing::kPreviewRequestID, &request_id);
print_to_pdf_callback_map_[request_id] = callback;
content::RenderFrameHost* rfh = web_contents()->GetMainFrame();
rfh->Send(new PrintMsg_PrintPreview(rfh->GetRoutingID(), options));
}
void AtomPrintPreviewMessageHandler::RunPrintToPDFCallback(int request_id,
uint32_t data_size,
char* data) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::Locker locker(isolate);
v8::HandleScope handle_scope(isolate);
if (data) {
v8::Local<v8::Value> buffer =
node::Buffer::New(isolate, data, static_cast<size_t>(data_size),
&FreeNodeBufferData, nullptr)
.ToLocalChecked();
print_to_pdf_callback_map_[request_id].Run(v8::Null(isolate), buffer);
} else {
v8::Local<v8::String> error_message =
v8::String::NewFromUtf8(isolate, "Failed to generate PDF");
print_to_pdf_callback_map_[request_id].Run(
v8::Exception::Error(error_message), v8::Null(isolate));
}
print_to_pdf_callback_map_.erase(request_id);
}
} // namespace atom

View file

@ -0,0 +1,52 @@
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef ATOM_BROWSER_ATOM_PRINT_PREVIEW_MESSAGE_HANDLER_H_
#define ATOM_BROWSER_ATOM_PRINT_PREVIEW_MESSAGE_HANDLER_H_
#include <map>
#include "base/memory/weak_ptr.h"
#include "chrome/browser/printing/print_preview_message_handler.h"
#include "content/public/browser/web_contents_user_data.h"
#include "v8/include/v8.h"
namespace atom {
// Manages the print preview handling for a WebContents.
class AtomPrintPreviewMessageHandler
: public printing::PrintPreviewMessageHandler,
public content::WebContentsUserData<AtomPrintPreviewMessageHandler> {
public:
~AtomPrintPreviewMessageHandler() override;
using PrintToPDFCallback =
base::Callback<void(v8::Local<v8::Value>, v8::Local<v8::Value>)>;
void PrintToPDF(const base::DictionaryValue& options,
const PrintToPDFCallback& callback);
private:
explicit AtomPrintPreviewMessageHandler(content::WebContents* web_contents);
friend class content::WebContentsUserData<AtomPrintPreviewMessageHandler>;
typedef std::map<int, PrintToPDFCallback> PrintToPDFCallbackMap;
void OnMetafileReadyForPrinting(
content::RenderFrameHost* render_frame_host,
const PrintHostMsg_DidPreviewDocument_Params& params,
const PrintHostMsg_PreviewIds& ids) override;
void OnPrintPreviewFailed(int document_cookie,
const PrintHostMsg_PreviewIds& ids) override;
void RunPrintToPDFCallback(int request_id, uint32_t data_size, char* data);
PrintToPDFCallbackMap print_to_pdf_callback_map_;
base::WeakPtrFactory<PrintPreviewMessageHandler> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(AtomPrintPreviewMessageHandler);
};
} // namespace atom
#endif // ATOM_BROWSER_ATOM_PRINT_PREVIEW_MESSAGE_HANDLER_H_

View file

@ -21,8 +21,6 @@
#include "base/json/json_reader.h"
#include "base/task_scheduler/post_task.h"
#include "base/threading/sequenced_task_runner_handle.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 "chrome/browser/ui/browser_dialogs.h"
#include "chrome/common/pref_names.h"
@ -38,12 +36,18 @@
#include "content/public/browser/render_widget_host.h"
#include "content/public/browser/security_style_explanation.h"
#include "content/public/browser/security_style_explanations.h"
#include "printing/buildflags/buildflags.h"
#include "storage/browser/fileapi/isolated_context.h"
#if BUILDFLAG(ENABLE_OSR)
#include "atom/browser/osr/osr_render_widget_host_view.h"
#endif
#if BUILDFLAG(ENABLE_PRINTING)
#include "atom/browser/atom_print_preview_message_handler.h"
#include "chrome/browser/printing/print_view_manager_basic.h"
#endif
using content::BrowserThread;
namespace atom {
@ -173,8 +177,10 @@ void CommonWebContentsDelegate::InitWithWebContents(
browser_context_ = browser_context;
web_contents->SetDelegate(this);
#if BUILDFLAG(ENABLE_PRINTING)
printing::PrintViewManagerBasic::CreateForWebContents(web_contents);
printing::PrintPreviewMessageHandler::CreateForWebContents(web_contents);
AtomPrintPreviewMessageHandler::CreateForWebContents(web_contents);
#endif
// Determien whether the WebContents is offscreen.
auto* web_preferences = WebContentsPreferences::From(web_contents);