#include "printing/test_printing_context.h"
#include <memory>
#include <utility>
#include "base/check.h"
#include "base/containers/flat_map.h"
#include "base/notreached.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "printing/backend/print_backend.h"
#include "printing/buildflags/buildflags.h"
#include "printing/mojom/print.mojom.h"
#include "printing/print_settings.h"
#include "printing/printing_context.h"
#include "printing/units.h"
#include "ui/gfx/geometry/size.h"
#if BUILDFLAG(ENABLE_OOP_PRINTING_NO_OOP_BASIC_PRINT_DIALOG)
#include "printing/printing_features.h"
#endif
#if BUILDFLAG(IS_WIN)
#include "printing/printed_page_win.h"
#endif
namespace printing {
namespace {
#if BUILDFLAG(IS_WIN)
bool AreLanguageAndMetafileTypesCompatible(
mojom::PrinterLanguageType printer_language_type,
mojom::MetafileDataType metafile_data_type) {
switch (printer_language_type) {
case mojom::PrinterLanguageType::kTextOnly:
case mojom::PrinterLanguageType::kPostscriptLevel2:
case mojom::PrinterLanguageType::kPostscriptLevel3:
return metafile_data_type == mojom::MetafileDataType::kPostScriptEmf;
case mojom::PrinterLanguageType::kNone:
return metafile_data_type == mojom::MetafileDataType::kEMF;
case mojom::PrinterLanguageType::kXps:
return false;
}
}
#endif
}
TestPrintingContextDelegate::TestPrintingContextDelegate() = default;
TestPrintingContextDelegate::~TestPrintingContextDelegate() = default;
gfx::NativeView TestPrintingContextDelegate::GetParentView() { … }
std::string TestPrintingContextDelegate::GetAppLocale() { … }
TestPrintingContext::TestPrintingContext(Delegate* delegate,
ProcessBehavior process_behavior)
: … { … }
TestPrintingContext::~TestPrintingContext() = default;
void TestPrintingContext::SetDeviceSettings(
const std::string& device_name,
std::unique_ptr<PrintSettings> settings) { … }
void TestPrintingContext::SetNewDocumentJobId(int job_id) { … }
void TestPrintingContext::SetUserSettings(const PrintSettings& settings) { … }
void TestPrintingContext::AskUserForSettings(int max_pages,
bool has_selection,
bool is_scripted,
PrintSettingsCallback callback) { … }
mojom::ResultCode TestPrintingContext::AskUserForSettingsImpl(
int max_pages,
bool has_selection,
bool is_scripted) { … }
mojom::ResultCode TestPrintingContext::UseDefaultSettings() { … }
gfx::Size TestPrintingContext::GetPdfPaperSizeDeviceUnits() { … }
mojom::ResultCode TestPrintingContext::UpdatePrinterSettings(
const PrinterSettings& printer_settings) { … }
mojom::ResultCode TestPrintingContext::NewDocument(
const std::u16string& document_name) { … }
#if BUILDFLAG(IS_WIN)
mojom::ResultCode TestPrintingContext::RenderPage(const PrintedPage& page,
const PageSetup& page_setup) {
if (abort_printing_)
return mojom::ResultCode::kCanceled;
DCHECK(in_print_job_);
DVLOG(1) << "Render page " << page.page_number();
if (render_page_blocked_by_permissions_)
return mojom::ResultCode::kAccessDenied;
if (render_page_fail_for_page_number_.has_value() &&
*render_page_fail_for_page_number_ == page.page_number()) {
return mojom::ResultCode::kFailed;
}
#if BUILDFLAG(IS_WIN)
const std::string device_name =
base::UTF16ToUTF8(applied_settings_.device_name());
if (!device_name.empty()) {
auto found = device_settings_.find(device_name);
if (found == device_settings_.end()) {
DLOG(ERROR) << "No such device found in test printing context: `"
<< device_name << "`";
return mojom::ResultCode::kFailed;
}
const PrintSettings* device_default_print_settings = found->second.get();
if (!AreLanguageAndMetafileTypesCompatible(
device_default_print_settings->printer_language_type(),
page.metafile()->GetDataType())) {
DVLOG(1) << "Incompatible metafile type for printer language type: "
<< device_default_print_settings->printer_language_type() << ", "
<< page.metafile()->GetDataType();
return mojom::ResultCode::kFailed;
}
}
#endif
return mojom::ResultCode::kSuccess;
}
#endif
mojom::ResultCode TestPrintingContext::PrintDocument(
const MetafilePlayer& metafile,
const PrintSettings& settings,
uint32_t num_pages) { … }
mojom::ResultCode TestPrintingContext::DocumentDone() { … }
void TestPrintingContext::Cancel() { … }
void TestPrintingContext::ReleaseContext() { … }
printing::NativeDrawingContext TestPrintingContext::context() const { … }
#if BUILDFLAG(IS_WIN)
mojom::ResultCode TestPrintingContext::InitWithSettingsForTest(
std::unique_ptr<PrintSettings> settings) {
NOTIMPLEMENTED();
return mojom::ResultCode::kFailed;
}
#endif
}