refactor: hide printing impl details in api::WebContents (#43893)

* refactor: move api::WebContents::OnGetDeviceNameToUse() into an anonymous namespace

* refactor: move api::WebContents::OnPDFCreated() into an anonymous namespace

* refactor: remove unused #include
This commit is contained in:
Charles Kerr 2024-09-24 00:36:55 -05:00 committed by GitHub
parent a3af8ea768
commit 33d7c9ac3e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 39 additions and 49 deletions

View file

@ -2911,14 +2911,16 @@ bool WebContents::IsCurrentlyAudible() {
}
#if BUILDFLAG(ENABLE_PRINTING)
void WebContents::OnGetDeviceNameToUse(
namespace {
void OnGetDeviceNameToUse(base::WeakPtr<content::WebContents> web_contents,
base::Value::Dict print_settings,
printing::CompletionCallback print_callback,
// <error, device_name>
std::pair<std::string, std::u16string> info) {
// The content::WebContents might be already deleted at this point, and the
// PrintViewManagerElectron class does not do null check.
if (!web_contents()) {
if (!web_contents) {
if (print_callback)
std::move(print_callback).Run(false, "failed");
return;
@ -2940,15 +2942,40 @@ void WebContents::OnGetDeviceNameToUse(
}
auto* print_view_manager =
PrintViewManagerElectron::FromWebContents(web_contents());
PrintViewManagerElectron::FromWebContents(web_contents.get());
if (!print_view_manager)
return;
content::RenderFrameHost* rfh = GetRenderFrameHostToUse(web_contents());
content::RenderFrameHost* rfh = GetRenderFrameHostToUse(web_contents.get());
print_view_manager->PrintNow(rfh, std::move(print_settings),
std::move(print_callback));
}
void OnPDFCreated(gin_helper::Promise<v8::Local<v8::Value>> promise,
print_to_pdf::PdfPrintResult print_result,
scoped_refptr<base::RefCountedMemory> data) {
if (print_result != print_to_pdf::PdfPrintResult::kPrintSuccess) {
promise.RejectWithErrorMessage(
"Failed to generate PDF: " +
print_to_pdf::PdfPrintResultToString(print_result));
return;
}
v8::Isolate* isolate = promise.isolate();
gin_helper::Locker locker(isolate);
v8::HandleScope handle_scope(isolate);
v8::Context::Scope context_scope(
v8::Local<v8::Context>::New(isolate, promise.GetContext()));
v8::Local<v8::Value> buffer =
node::Buffer::Copy(isolate, reinterpret_cast<const char*>(data->front()),
data->size())
.ToLocalChecked();
promise.Resolve(buffer);
}
} // namespace
void WebContents::Print(gin::Arguments* args) {
auto options = gin_helper::Dictionary::CreateEmpty(args->isolate());
base::Value::Dict settings;
@ -3108,9 +3135,8 @@ void WebContents::Print(gin::Arguments* args) {
print_task_runner_->PostTaskAndReplyWithResult(
FROM_HERE, base::BindOnce(&GetDeviceNameToUse, device_name),
base::BindOnce(&WebContents::OnGetDeviceNameToUse,
weak_factory_.GetWeakPtr(), std::move(settings),
std::move(callback)));
base::BindOnce(&OnGetDeviceNameToUse, web_contents()->GetWeakPtr(),
std::move(settings), std::move(callback)));
}
// Partially duplicated and modified from
@ -3168,36 +3194,10 @@ v8::Local<v8::Promise> WebContents::PrintToPDF(const base::Value& settings) {
params->params->document_cookie = unique_id.value_or(0);
manager->PrintToPdf(rfh, page_ranges, std::move(params),
base::BindOnce(&WebContents::OnPDFCreated, GetWeakPtr(),
std::move(promise)));
base::BindOnce(&OnPDFCreated, std::move(promise)));
return handle;
}
void WebContents::OnPDFCreated(
gin_helper::Promise<v8::Local<v8::Value>> promise,
print_to_pdf::PdfPrintResult print_result,
scoped_refptr<base::RefCountedMemory> data) {
if (print_result != print_to_pdf::PdfPrintResult::kPrintSuccess) {
promise.RejectWithErrorMessage(
"Failed to generate PDF: " +
print_to_pdf::PdfPrintResultToString(print_result));
return;
}
v8::Isolate* isolate = promise.isolate();
gin_helper::Locker locker(isolate);
v8::HandleScope handle_scope(isolate);
v8::Context::Scope context_scope(
v8::Local<v8::Context>::New(isolate, promise.GetContext()));
v8::Local<v8::Value> buffer =
node::Buffer::Copy(isolate, reinterpret_cast<const char*>(data->front()),
data->size())
.ToLocalChecked();
promise.Resolve(buffer);
}
#endif
void WebContents::AddWorkSpace(gin::Arguments* args,

View file

@ -46,10 +46,6 @@
#include "shell/common/gin_helper/pinnable.h"
#include "ui/base/models/image_model.h"
#if BUILDFLAG(ENABLE_PRINTING)
#include "shell/browser/printing/print_view_manager_electron.h"
#endif
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
#include "extensions/common/mojom/view_type.mojom-forward.h"
@ -247,16 +243,9 @@ class WebContents final : public ExclusiveAccessContext,
void HandleNewRenderFrame(content::RenderFrameHost* render_frame_host);
#if BUILDFLAG(ENABLE_PRINTING)
void OnGetDeviceNameToUse(base::Value::Dict print_settings,
printing::CompletionCallback print_callback,
// <error, device_name>
std::pair<std::string, std::u16string> info);
void Print(gin::Arguments* args);
// Print current page as PDF.
v8::Local<v8::Promise> PrintToPDF(const base::Value& settings);
void OnPDFCreated(gin_helper::Promise<v8::Local<v8::Value>> promise,
print_to_pdf::PdfPrintResult print_result,
scoped_refptr<base::RefCountedMemory> data);
#endif
void SetNextChildWebPreferences(const gin_helper::Dictionary);

View file

@ -9,6 +9,7 @@
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "device/bluetooth/bluetooth_device.h"