From 33d7c9ac3e19ba8f0b853938797630f9f69c22c4 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 24 Sep 2024 00:36:55 -0500 Subject: [PATCH] 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 --- .../browser/api/electron_api_web_contents.cc | 76 +++++++++---------- shell/browser/api/electron_api_web_contents.h | 11 --- .../bluetooth/electron_bluetooth_delegate.cc | 1 + 3 files changed, 39 insertions(+), 49 deletions(-) diff --git a/shell/browser/api/electron_api_web_contents.cc b/shell/browser/api/electron_api_web_contents.cc index c3359b4bf353..8b586fa3fcca 100644 --- a/shell/browser/api/electron_api_web_contents.cc +++ b/shell/browser/api/electron_api_web_contents.cc @@ -2911,14 +2911,16 @@ bool WebContents::IsCurrentlyAudible() { } #if BUILDFLAG(ENABLE_PRINTING) -void WebContents::OnGetDeviceNameToUse( - base::Value::Dict print_settings, - printing::CompletionCallback print_callback, - // - std::pair info) { +namespace { + +void OnGetDeviceNameToUse(base::WeakPtr web_contents, + base::Value::Dict print_settings, + printing::CompletionCallback print_callback, + // + std::pair 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> promise, + print_to_pdf::PdfPrintResult print_result, + scoped_refptr 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::New(isolate, promise.GetContext())); + + v8::Local buffer = + node::Buffer::Copy(isolate, reinterpret_cast(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 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> promise, - print_to_pdf::PdfPrintResult print_result, - scoped_refptr 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::New(isolate, promise.GetContext())); - - v8::Local buffer = - node::Buffer::Copy(isolate, reinterpret_cast(data->front()), - data->size()) - .ToLocalChecked(); - - promise.Resolve(buffer); -} #endif void WebContents::AddWorkSpace(gin::Arguments* args, diff --git a/shell/browser/api/electron_api_web_contents.h b/shell/browser/api/electron_api_web_contents.h index 1cfe2a1e9c49..918d7c0c2172 100644 --- a/shell/browser/api/electron_api_web_contents.h +++ b/shell/browser/api/electron_api_web_contents.h @@ -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, - // - std::pair info); void Print(gin::Arguments* args); // Print current page as PDF. v8::Local PrintToPDF(const base::Value& settings); - void OnPDFCreated(gin_helper::Promise> promise, - print_to_pdf::PdfPrintResult print_result, - scoped_refptr data); #endif void SetNextChildWebPreferences(const gin_helper::Dictionary); diff --git a/shell/browser/bluetooth/electron_bluetooth_delegate.cc b/shell/browser/bluetooth/electron_bluetooth_delegate.cc index 8db70add67ee..c23f60177695 100644 --- a/shell/browser/bluetooth/electron_bluetooth_delegate.cc +++ b/shell/browser/bluetooth/electron_bluetooth_delegate.cc @@ -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"