refactor: hide printing impl details in api::WebContents (#43917)
* refactor: move api::WebContents::OnGetDeviceNameToUse() into an anonymous namespace Co-authored-by: Charles Kerr <charles@charleskerr.com> * refactor: move api::WebContents::OnPDFCreated() into an anonymous namespace Co-authored-by: Charles Kerr <charles@charleskerr.com> * refactor: remove unused #include Co-authored-by: Charles Kerr <charles@charleskerr.com> --------- Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Charles Kerr <charles@charleskerr.com>
This commit is contained in:
parent
cdcfe49592
commit
ae6d5c4661
3 changed files with 39 additions and 49 deletions
|
@ -2892,14 +2892,16 @@ bool WebContents::IsCurrentlyAudible() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#if BUILDFLAG(ENABLE_PRINTING)
|
#if BUILDFLAG(ENABLE_PRINTING)
|
||||||
void WebContents::OnGetDeviceNameToUse(
|
namespace {
|
||||||
base::Value::Dict print_settings,
|
|
||||||
printing::CompletionCallback print_callback,
|
void OnGetDeviceNameToUse(base::WeakPtr<content::WebContents> web_contents,
|
||||||
// <error, device_name>
|
base::Value::Dict print_settings,
|
||||||
std::pair<std::string, std::u16string> info) {
|
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
|
// The content::WebContents might be already deleted at this point, and the
|
||||||
// PrintViewManagerElectron class does not do null check.
|
// PrintViewManagerElectron class does not do null check.
|
||||||
if (!web_contents()) {
|
if (!web_contents) {
|
||||||
if (print_callback)
|
if (print_callback)
|
||||||
std::move(print_callback).Run(false, "failed");
|
std::move(print_callback).Run(false, "failed");
|
||||||
return;
|
return;
|
||||||
|
@ -2921,15 +2923,40 @@ void WebContents::OnGetDeviceNameToUse(
|
||||||
}
|
}
|
||||||
|
|
||||||
auto* print_view_manager =
|
auto* print_view_manager =
|
||||||
PrintViewManagerElectron::FromWebContents(web_contents());
|
PrintViewManagerElectron::FromWebContents(web_contents.get());
|
||||||
if (!print_view_manager)
|
if (!print_view_manager)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
content::RenderFrameHost* rfh = GetRenderFrameHostToUse(web_contents());
|
content::RenderFrameHost* rfh = GetRenderFrameHostToUse(web_contents.get());
|
||||||
print_view_manager->PrintNow(rfh, std::move(print_settings),
|
print_view_manager->PrintNow(rfh, std::move(print_settings),
|
||||||
std::move(print_callback));
|
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) {
|
void WebContents::Print(gin::Arguments* args) {
|
||||||
auto options = gin_helper::Dictionary::CreateEmpty(args->isolate());
|
auto options = gin_helper::Dictionary::CreateEmpty(args->isolate());
|
||||||
base::Value::Dict settings;
|
base::Value::Dict settings;
|
||||||
|
@ -3089,9 +3116,8 @@ void WebContents::Print(gin::Arguments* args) {
|
||||||
|
|
||||||
print_task_runner_->PostTaskAndReplyWithResult(
|
print_task_runner_->PostTaskAndReplyWithResult(
|
||||||
FROM_HERE, base::BindOnce(&GetDeviceNameToUse, device_name),
|
FROM_HERE, base::BindOnce(&GetDeviceNameToUse, device_name),
|
||||||
base::BindOnce(&WebContents::OnGetDeviceNameToUse,
|
base::BindOnce(&OnGetDeviceNameToUse, web_contents()->GetWeakPtr(),
|
||||||
weak_factory_.GetWeakPtr(), std::move(settings),
|
std::move(settings), std::move(callback)));
|
||||||
std::move(callback)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Partially duplicated and modified from
|
// Partially duplicated and modified from
|
||||||
|
@ -3149,36 +3175,10 @@ v8::Local<v8::Promise> WebContents::PrintToPDF(const base::Value& settings) {
|
||||||
params->params->document_cookie = unique_id.value_or(0);
|
params->params->document_cookie = unique_id.value_or(0);
|
||||||
|
|
||||||
manager->PrintToPdf(rfh, page_ranges, std::move(params),
|
manager->PrintToPdf(rfh, page_ranges, std::move(params),
|
||||||
base::BindOnce(&WebContents::OnPDFCreated, GetWeakPtr(),
|
base::BindOnce(&OnPDFCreated, std::move(promise)));
|
||||||
std::move(promise)));
|
|
||||||
|
|
||||||
return handle;
|
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
|
#endif
|
||||||
|
|
||||||
void WebContents::AddWorkSpace(gin::Arguments* args,
|
void WebContents::AddWorkSpace(gin::Arguments* args,
|
||||||
|
|
|
@ -45,10 +45,6 @@
|
||||||
#include "shell/common/gin_helper/pinnable.h"
|
#include "shell/common/gin_helper/pinnable.h"
|
||||||
#include "ui/base/models/image_model.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)
|
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
||||||
#include "extensions/common/mojom/view_type.mojom-forward.h"
|
#include "extensions/common/mojom/view_type.mojom-forward.h"
|
||||||
|
|
||||||
|
@ -246,16 +242,9 @@ class WebContents final : public ExclusiveAccessContext,
|
||||||
void HandleNewRenderFrame(content::RenderFrameHost* render_frame_host);
|
void HandleNewRenderFrame(content::RenderFrameHost* render_frame_host);
|
||||||
|
|
||||||
#if BUILDFLAG(ENABLE_PRINTING)
|
#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);
|
void Print(gin::Arguments* args);
|
||||||
// Print current page as PDF.
|
// Print current page as PDF.
|
||||||
v8::Local<v8::Promise> PrintToPDF(const base::Value& settings);
|
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
|
#endif
|
||||||
|
|
||||||
void SetNextChildWebPreferences(const gin_helper::Dictionary);
|
void SetNextChildWebPreferences(const gin_helper::Dictionary);
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
|
|
||||||
#include "base/strings/string_util.h"
|
#include "base/strings/string_util.h"
|
||||||
#include "base/strings/utf_string_conversions.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/render_frame_host.h"
|
||||||
#include "content/public/browser/web_contents.h"
|
#include "content/public/browser/web_contents.h"
|
||||||
#include "device/bluetooth/bluetooth_device.h"
|
#include "device/bluetooth/bluetooth_device.h"
|
||||||
|
|
Loading…
Reference in a new issue