|
@@ -2892,14 +2892,16 @@ bool WebContents::IsCurrentlyAudible() {
|
|
|
}
|
|
|
|
|
|
#if BUILDFLAG(ENABLE_PRINTING)
|
|
|
-void WebContents::OnGetDeviceNameToUse(
|
|
|
- base::Value::Dict print_settings,
|
|
|
- printing::CompletionCallback print_callback,
|
|
|
- // <error, device_name>
|
|
|
- std::pair<std::string, std::u16string> info) {
|
|
|
+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;
|
|
@@ -2921,15 +2923,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;
|
|
@@ -3089,9 +3116,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
|
|
@@ -3149,36 +3175,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,
|