chromium/printing/test_printing_context.cc

// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#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)
// Metafile data that is generated by
// chrome/browser/printing/pdf_to_emf_converter.cc can be of different types,
// which are expected to match particular printer languages.
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:
      // TODO(crbug.com/40100562):  Update to use an XPS MetafileDataType once
      // it is available.
      return false;
  }
}
#endif  // BUILDFLAG(IS_WIN)

}  // namespace

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)
  // Examine the driver type for the printer, to make sure the provided
  // metafile is of the correct type.
  const std::string device_name =
      base::UTF16ToUTF8(applied_settings_.device_name());
  // TODO(crbug.com/327554077):  Update to expect having a valid `device_name`
  // once sandboxed OOPPD gets fixed for current API call flow.
  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  // BUILDFLAG(IS_WIN)

  // No-op.
  return mojom::ResultCode::kSuccess;
}
#endif  // BUILDFLAG(IS_WIN)

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  // BUILDFLAG(IS_WIN)

}  // namespace printing