#include "chrome/browser/printing/print_view_manager_base.h"
#include <memory>
#include <string_view>
#include <utility>
#include <vector>
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/memory/read_only_shared_memory_region.h"
#include "base/memory/ref_counted_memory.h"
#include "base/numerics/safe_conversions.h"
#include "base/observer_list.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/current_thread.h"
#include "base/timer/timer.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/bad_message.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/printing/print_compositor_util.h"
#include "chrome/browser/printing/print_error_dialog.h"
#include "chrome/browser/printing/print_job.h"
#include "chrome/browser/printing/print_job_manager.h"
#include "chrome/browser/printing/print_view_manager_common.h"
#include "chrome/browser/printing/printer_query.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/webui/print_preview/printer_handler.h"
#include "chrome/common/pref_names.h"
#include "chrome/grit/generated_resources.h"
#include "components/device_event_log/device_event_log.h"
#include "components/enterprise/buildflags/buildflags.h"
#include "components/prefs/pref_service.h"
#include "components/printing/browser/print_composite_client.h"
#include "components/printing/browser/print_manager_utils.h"
#include "components/printing/common/print.mojom.h"
#include "components/printing/common/print_params.h"
#include "components/services/print_compositor/public/cpp/print_service_mojo_types.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/global_routing_id.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 "mojo/public/cpp/system/buffer.h"
#include "printing/buildflags/buildflags.h"
#include "printing/metafile_skia.h"
#include "printing/mojom/print.mojom.h"
#include "printing/print_settings.h"
#include "printing/printed_document.h"
#include "printing/printing_utils.h"
#include "ui/base/l10n/l10n_util.h"
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
#include "chrome/browser/printing/print_view_manager.h"
#include "printing/print_settings_conversion.h"
#endif
#if BUILDFLAG(ENABLE_OOP_PRINTING)
#include "chrome/browser/printing/oop_features.h"
#include "chrome/browser/printing/print_backend_service_manager.h"
#endif
#if BUILDFLAG(IS_WIN)
#include "chrome/browser/printing/xps_features.h"
#endif
#if BUILDFLAG(IS_WIN) && BUILDFLAG(GOOGLE_CHROME_BRANDING)
#include "chrome/browser/win/conflicts/module_database.h"
#endif
#if BUILDFLAG(IS_CHROMEOS_LACROS)
#include "chrome/browser/printing/local_printer_utils_chromeos.h"
#endif
#if BUILDFLAG(ENTERPRISE_CONTENT_ANALYSIS)
#include "chrome/browser/enterprise/data_protection/print_utils.h"
#endif
namespace printing {
namespace {
void OnDidGetDefaultPrintSettings(
scoped_refptr<PrintQueriesQueue> queue,
bool want_pdf_settings,
std::unique_ptr<PrinterQuery> printer_query,
mojom::PrintManagerHost::GetDefaultPrintSettingsCallback callback) { … }
void OnDidScriptedPrint(
scoped_refptr<PrintQueriesQueue> queue,
std::unique_ptr<PrinterQuery> printer_query,
mojom::PrintManagerHost::ScriptedPrintCallback callback) { … }
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
std::string PrintMsgPrintParamsErrorDetails(const mojom::PrintParams& params) { … }
#endif
}
BASE_FEATURE(…);
PrintViewManagerBase::PrintViewManagerBase(content::WebContents* web_contents)
: … { … }
PrintViewManagerBase::~PrintViewManagerBase() { … }
#if BUILDFLAG(IS_WIN) && BUILDFLAG(GOOGLE_CHROME_BRANDING)
void PrintViewManagerBase::DisableThirdPartyBlocking() {
#if BUILDFLAG(ENABLE_OOP_PRINTING) && BUILDFLAG(ENABLE_OOP_BASIC_PRINT_DIALOG)
const bool loads_print_drivers_in_browser_process = !ShouldPrintJobOop();
#else
constexpr bool loads_print_drivers_in_browser_process = true;
#endif
if (loads_print_drivers_in_browser_process) {
ModuleDatabase::DisableThirdPartyBlocking();
}
}
#endif
bool PrintViewManagerBase::PrintNow(content::RenderFrameHost* rfh) { … }
void PrintViewManagerBase::PrintNodeUnderContextMenu(
content::RenderFrameHost* rfh) { … }
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
void PrintViewManagerBase::PrintForPrintPreview(
base::Value::Dict job_settings,
scoped_refptr<base::RefCountedMemory> print_data,
content::RenderFrameHost* rfh,
PrinterHandler::PrintCallback callback) { … }
#endif
void PrintViewManagerBase::PrintToPdf(
content::RenderFrameHost* rfh,
const std::string& page_ranges,
mojom::PrintPagesParamsPtr print_pages_params,
print_to_pdf::PdfPrintJob::PrintToPdfCallback callback) { … }
void PrintViewManagerBase::PrintDocument(
scoped_refptr<base::RefCountedMemory> print_data,
const gfx::Size& page_size,
const gfx::Rect& content_area,
const gfx::Point& offsets) { … }
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
#if BUILDFLAG(IS_WIN)
void PrintViewManagerBase::OnDidUpdatePrintableArea(
std::unique_ptr<PrinterQuery> printer_query,
base::Value::Dict job_settings,
std::unique_ptr<PrintSettings> print_settings,
UpdatePrintSettingsCallback callback,
bool success) {
if (!success) {
PRINTER_LOG(ERROR) << "Unable to update printable area for "
<< base::UTF16ToUTF8(print_settings->device_name())
<< " (paper vendor id "
<< print_settings->requested_media().vendor_id << ")";
std::move(callback).Run(nullptr);
return;
}
PRINTER_LOG(EVENT) << "Paper printable area updated for vendor id "
<< print_settings->requested_media().vendor_id;
CompleteUpdatePrintSettings(std::move(job_settings),
std::move(print_settings), std::move(callback));
}
#endif
void PrintViewManagerBase::CompleteUpdatePrintSettings(
base::Value::Dict job_settings,
std::unique_ptr<PrintSettings> print_settings,
UpdatePrintSettingsCallback callback) { … }
void PrintViewManagerBase::OnPrintSettingsDone(
scoped_refptr<base::RefCountedMemory> print_data,
uint32_t page_count,
#if BUILDFLAG(ENTERPRISE_CONTENT_ANALYSIS)
bool show_system_dialog,
#endif
PrinterHandler::PrintCallback callback,
std::unique_ptr<printing::PrinterQuery> printer_query) { … }
void PrintViewManagerBase::StartLocalPrintJob(
scoped_refptr<base::RefCountedMemory> print_data,
uint32_t page_count,
#if BUILDFLAG(ENTERPRISE_CONTENT_ANALYSIS)
bool show_system_dialog,
#endif
int cookie,
PrinterHandler::PrintCallback callback) { … }
#endif
void PrintViewManagerBase::GetDefaultPrintSettingsReply(
GetDefaultPrintSettingsCallback callback,
mojom::PrintParamsPtr params) { … }
void PrintViewManagerBase::ScriptedPrintReply(
ScriptedPrintCallback callback,
int process_id,
mojom::PrintPagesParamsPtr params) { … }
void PrintViewManagerBase::NavigationStopped() { … }
std::u16string PrintViewManagerBase::RenderSourceName() { … }
void PrintViewManagerBase::DidGetPrintedPagesCount(int32_t cookie,
uint32_t number_pages) { … }
bool PrintViewManagerBase::PrintJobHasDocument(int cookie) { … }
bool PrintViewManagerBase::OnComposePdfDoneImpl(
int document_cookie,
const gfx::Size& page_size,
const gfx::Rect& content_area,
const gfx::Point& physical_offsets,
mojom::PrintCompositor::Status status,
base::ReadOnlySharedMemoryRegion region) { … }
void PrintViewManagerBase::OnComposeDocumentDone(
int document_cookie,
const gfx::Size& page_size,
const gfx::Rect& content_area,
const gfx::Point& physical_offsets,
DidPrintDocumentCallback callback,
mojom::PrintCompositor::Status status,
base::ReadOnlySharedMemoryRegion region) { … }
void PrintViewManagerBase::OnDidPrintDocument(DidPrintDocumentCallback callback,
bool succeeded) { … }
void PrintViewManagerBase::DidPrintDocument(
mojom::DidPrintDocumentParamsPtr params,
DidPrintDocumentCallback callback) { … }
void PrintViewManagerBase::GetDefaultPrintSettings(
GetDefaultPrintSettingsCallback callback) { … }
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
void PrintViewManagerBase::UpdatePrintSettings(
base::Value::Dict job_settings,
UpdatePrintSettingsCallback callback) { … }
void PrintViewManagerBase::SetAccessibilityTree(
int32_t cookie,
const ui::AXTreeUpdate& accessibility_tree) { … }
#endif
void PrintViewManagerBase::IsPrintingEnabled(
IsPrintingEnabledCallback callback) { … }
void PrintViewManagerBase::ScriptedPrint(mojom::ScriptedPrintParamsPtr params,
ScriptedPrintCallback callback) { … }
void PrintViewManagerBase::PrintingFailed(int32_t cookie,
mojom::PrintFailureReason reason) { … }
void PrintViewManagerBase::AddTestObserver(TestObserver& observer) { … }
void PrintViewManagerBase::RemoveTestObserver(TestObserver& observer) { … }
void PrintViewManagerBase::RenderFrameHostStateChanged(
content::RenderFrameHost* render_frame_host,
content::RenderFrameHost::LifecycleState ,
content::RenderFrameHost::LifecycleState new_state) { … }
void PrintViewManagerBase::RenderFrameDeleted(
content::RenderFrameHost* render_frame_host) { … }
#if BUILDFLAG(IS_WIN) && BUILDFLAG(ENABLE_PRINT_PREVIEW)
void PrintViewManagerBase::SystemDialogCancelled() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
ReleasePrinterQuery();
TerminatePrintJob(true);
}
#endif
bool PrintViewManagerBase::GetPrintingEnabledBooleanPref() const { … }
void PrintViewManagerBase::OnDocDone(int job_id, PrintedDocument* document) { … }
void PrintViewManagerBase::OnJobDone() { … }
void PrintViewManagerBase::OnCanceling() { … }
void PrintViewManagerBase::OnFailed() { … }
bool PrintViewManagerBase::RenderAllMissingPagesNow() { … }
void PrintViewManagerBase::ShouldQuitFromInnerMessageLoop() { … }
scoped_refptr<PrintJob> PrintViewManagerBase::CreatePrintJob(
PrintJobManager* print_job_manager) { … }
bool PrintViewManagerBase::SetupNewPrintJob(
std::unique_ptr<PrinterQuery> query) { … }
void PrintViewManagerBase::DisconnectFromCurrentPrintJob() { … }
void PrintViewManagerBase::TerminatePrintJob(bool cancel) { … }
void PrintViewManagerBase::ReleasePrintJob() { … }
bool PrintViewManagerBase::RunInnerMessageLoop() { … }
bool PrintViewManagerBase::OpportunisticallyCreatePrintJob(int cookie) { … }
bool PrintViewManagerBase::IsCrashed() { … }
void PrintViewManagerBase::SetPrintingRFH(content::RenderFrameHost* rfh) { … }
bool PrintViewManagerBase::StartPrintCommon(content::RenderFrameHost* rfh) { … }
#if BUILDFLAG(ENABLE_OOP_PRINTING)
bool PrintViewManagerBase::RegisterSystemPrintClient() { … }
void PrintViewManagerBase::UnregisterSystemPrintClient() { … }
#endif
void PrintViewManagerBase::ReleasePrinterQuery() { … }
void PrintViewManagerBase::CompleteScriptedPrint(
content::RenderFrameHost* rfh,
mojom::ScriptedPrintParamsPtr params,
ScriptedPrintCallback callback) { … }
#if BUILDFLAG(ENTERPRISE_CONTENT_ANALYSIS)
void PrintViewManagerBase::CompletePrintDocumentAfterContentAnalysis(
scoped_refptr<base::RefCountedMemory> print_data,
const gfx::Size& page_size,
const gfx::Rect& content_area,
const gfx::Point& offsets,
bool allowed) { … }
void PrintViewManagerBase::ContentAnalysisBeforePrintingDocument(
enterprise_connectors::ContentAnalysisDelegate::Data scanning_data,
scoped_refptr<base::RefCountedMemory> print_data,
const gfx::Size& page_size,
const gfx::Rect& content_area,
const gfx::Point& offsets) { … }
void PrintViewManagerBase::set_analyzing_content(bool analyzing) { … }
void PrintViewManagerBase::set_content_analysis_before_printing_document(
PrintDocumentCallback callback) { … }
#endif
}