#include "chrome/browser/printing/printer_query.h"
#include <memory>
#include <utility>
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/location.h"
#include "base/notreached.h"
#include "base/threading/thread_restrictions.h"
#include "base/values.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/printing/print_job_worker.h"
#include "components/crash/core/common/crash_keys.h"
#include "components/device_event_log/device_event_log.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/web_contents.h"
#include "printing/backend/print_backend.h"
#include "printing/buildflags/buildflags.h"
#include "printing/print_settings.h"
#if BUILDFLAG(IS_ANDROID)
#include "chrome/browser/android/tab_android.h"
#include "chrome/browser/android/tab_printer.h"
#include "printing/printing_context_android.h"
#endif
#if BUILDFLAG(ENABLE_OOP_PRINTING)
#include "chrome/browser/printing/oop_features.h"
#include "chrome/browser/printing/printer_query_oop.h"
#endif
#if BUILDFLAG(IS_WIN)
#include "base/strings/utf_string_conversions.h"
#endif
namespace printing {
namespace {
PrintingContext::ProcessBehavior GetPrintingContextProcessBehavior() { … }
class PrintingContextDelegate : public PrintingContext::Delegate { … };
PrintingContextDelegate::PrintingContextDelegate(
content::GlobalRenderFrameHostId rfh_id)
: … { … }
PrintingContextDelegate::~PrintingContextDelegate() = default;
gfx::NativeView PrintingContextDelegate::GetParentView() { … }
content::WebContents* PrintingContextDelegate::GetWebContents() { … }
std::string PrintingContextDelegate::GetAppLocale() { … }
CreatePrinterQueryCallback* g_create_printer_query_for_testing = …;
}
std::unique_ptr<PrinterQuery> PrinterQuery::Create(
content::GlobalRenderFrameHostId rfh_id) { … }
PrinterQuery::PrinterQuery(content::GlobalRenderFrameHostId rfh_id)
: … { … }
PrinterQuery::~PrinterQuery() { … }
void PrinterQuery::GetSettingsDone(base::OnceClosure callback,
std::optional<bool> maybe_is_modifiable,
std::unique_ptr<PrintSettings> new_settings,
mojom::ResultCode result) { … }
void PrinterQuery::PostSettingsDone(base::OnceClosure callback,
std::optional<bool> maybe_is_modifiable,
std::unique_ptr<PrintSettings> new_settings,
mojom::ResultCode result) { … }
std::unique_ptr<PrintJobWorker> PrinterQuery::TransferContextToNewWorker(
PrintJob* print_job) { … }
const PrintSettings& PrinterQuery::settings() const { … }
std::unique_ptr<PrintSettings> PrinterQuery::ExtractSettings() { … }
void PrinterQuery::SetSettingsForTest(std::unique_ptr<PrintSettings> settings) { … }
int PrinterQuery::cookie() const { … }
void PrinterQuery::GetDefaultSettings(base::OnceClosure callback,
bool is_modifiable,
bool want_pdf_settings) { … }
void PrinterQuery::GetSettingsFromUser(uint32_t document_page_count,
bool has_selection,
mojom::MarginType margin_type,
bool is_scripted,
bool is_modifiable,
base::OnceClosure callback) { … }
void PrinterQuery::SetSettings(base::Value::Dict new_settings,
base::OnceClosure callback) { … }
#if BUILDFLAG(IS_CHROMEOS)
void PrinterQuery::SetSettingsFromPOD(
std::unique_ptr<PrintSettings> new_settings,
base::OnceClosure callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
UpdatePrintSettingsFromPOD(
std::move(new_settings),
base::BindOnce(&PrinterQuery::PostSettingsDone, base::Unretained(this),
std::move(callback),
std::nullopt));
}
#endif
#if BUILDFLAG(IS_WIN)
void PrinterQuery::UpdatePrintableArea(
PrintSettings* print_settings,
OnDidUpdatePrintableAreaCallback callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
scoped_refptr<PrintBackend> print_backend =
PrintBackend::CreateInstance(g_browser_process->GetApplicationLocale());
base::ScopedAllowBlocking allow_blocking;
std::string printer_name = base::UTF16ToUTF8(print_settings->device_name());
crash_keys::ScopedPrinterInfo crash_key(
printer_name, print_backend->GetPrinterDriverInfo(printer_name));
PRINTER_LOG(EVENT) << "Updating paper printable area in-process for "
<< printer_name;
const PrintSettings::RequestedMedia& media =
print_settings->requested_media();
std::optional<gfx::Rect> printable_area_um =
print_backend->GetPaperPrintableArea(printer_name, media.vendor_id,
media.size_microns);
if (!printable_area_um.has_value()) {
std::move(callback).Run(false);
return;
}
print_settings->UpdatePrinterPrintableArea(printable_area_um.value());
std::move(callback).Run(true);
}
#endif
void PrinterQuery::ApplyDefaultPrintableAreaToVirtualPrinterPrintSettings(
PrintSettings& print_settings) { … }
#if BUILDFLAG(ENABLE_OOP_PRINTING)
void PrinterQuery::SetClientId(PrintBackendServiceManager::ClientId client_id) { … }
#endif
std::unique_ptr<PrintSettings> PrinterQuery::GetPdfSettings() { … }
void PrinterQuery::InvokeSettingsCallback(SettingsCallback callback,
mojom::ResultCode result) { … }
void PrinterQuery::UpdatePrintSettings(base::Value::Dict new_settings,
SettingsCallback callback) { … }
#if BUILDFLAG(IS_CHROMEOS)
void PrinterQuery::UpdatePrintSettingsFromPOD(
std::unique_ptr<PrintSettings> new_settings,
SettingsCallback callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
mojom::ResultCode result =
printing_context_->UpdatePrintSettingsFromPOD(std::move(new_settings));
InvokeSettingsCallback(std::move(callback), result);
}
#endif
std::unique_ptr<PrintJobWorker> PrinterQuery::CreatePrintJobWorker(
PrintJob* print_job) { … }
void PrinterQuery::GetSettingsWithUI(uint32_t document_page_count,
bool has_selection,
bool is_scripted,
SettingsCallback callback) { … }
void PrinterQuery::UseDefaultSettings(SettingsCallback callback) { … }
void PrinterQuery::SetCreatePrinterQueryCallbackForTest(
CreatePrinterQueryCallback* callback) { … }
bool PrinterQuery::is_valid() const { … }
content::WebContents* PrinterQuery::GetWebContents() { … }
}