blob: c4da604aaa679597cb27d9eaf583f92a436f0637 [file] [log] [blame]
Andrew Rayskiyf90c7e82023-11-07 14:46:181// Copyright 2022 The Chromium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/printing/local_printer_utils_chromeos.h"
6
7#include <string>
8
9#include "chromeos/crosapi/mojom/local_printer.mojom.h"
10#include "chromeos/printing/cups_printer_status.h"
11#include "chromeos/printing/printer_configuration.h"
12#include "third_party/abseil-cpp/absl/types/optional.h"
13
14#if BUILDFLAG(IS_CHROMEOS_ASH)
15#include "chrome/browser/ash/crosapi/crosapi_ash.h"
16#include "chrome/browser/ash/crosapi/crosapi_manager.h"
17#include "chrome/browser/ash/crosapi/local_printer_ash.h"
18#elif BUILDFLAG(IS_CHROMEOS_LACROS)
19#include "base/check_op.h"
20#include "base/functional/callback_helpers.h"
21#include "base/strings/utf_string_conversions.h"
22#include "chrome/browser/printing/print_job.h"
23#include "chrome/grit/generated_resources.h"
24#include "chromeos/lacros/lacros_service.h"
25#include "printing/print_settings.h"
26#include "printing/printed_document.h"
27#include "printing/printing_utils.h"
28#include "ui/base/l10n/l10n_util.h"
29#endif
30
31namespace printing {
32
33#if BUILDFLAG(IS_CHROMEOS_LACROS)
34namespace {
35
36crosapi::mojom::PrintJobPtr PrintJobToMojom(int job_id,
37 const PrintedDocument& document,
38 PrintJob::Source source,
39 const std::string& source_id) {
40 std::u16string title = SimplifyDocumentTitle(document.name());
41 if (title.empty()) {
42 title = SimplifyDocumentTitle(
43 l10n_util::GetStringUTF16(IDS_DEFAULT_PRINT_DOCUMENT_TITLE));
44 }
45 const PrintSettings& settings = document.settings();
46 int duplex = static_cast<int>(settings.duplex_mode());
47 CHECK_GE(duplex, 0);
48 CHECK_LT(duplex, 3);
49
50 CHECK_NE(settings.color(), mojom::ColorModel::kUnknownColorModel);
51 return crosapi::mojom::PrintJob::New(
52 base::UTF16ToUTF8(settings.device_name()), base::UTF16ToUTF8(title),
53 job_id, document.page_count(), source, source_id, settings.color(),
54 static_cast<crosapi::mojom::PrintJob::DuplexMode>(duplex),
55 settings.requested_media().size_microns,
56 settings.requested_media().vendor_id, settings.copies());
57}
58
59} // namespace
60
61void NotifyAshJobCreated(int job_id,
62 const PrintedDocument& document,
63 const crosapi::mojom::PrintJob::Source& source,
64 const std::string& source_id,
65 crosapi::mojom::LocalPrinter* local_printer) {
66 CHECK(local_printer);
67 local_printer->CreatePrintJob(
68 PrintJobToMojom(job_id, document, source, source_id), base::DoNothing());
69}
70
71void NotifyAshJobCreated(const PrintJob& job,
72 int job_id,
73 const PrintedDocument& document) {
74 NotifyAshJobCreated(job_id, document, job.source(), job.source_id(),
75 GetLocalPrinterInterface());
76}
77
78#endif
79
80crosapi::mojom::LocalPrinter* GetLocalPrinterInterface() {
81#if BUILDFLAG(IS_CHROMEOS_ASH)
82 CHECK(crosapi::CrosapiManager::IsInitialized());
83 return crosapi::CrosapiManager::Get()->crosapi_ash()->local_printer_ash();
84#else
85 auto* service = chromeos::LacrosService::Get();
86 CHECK(service->IsAvailable<crosapi::mojom::LocalPrinter>());
87 return service->GetRemote<crosapi::mojom::LocalPrinter>().get();
88#endif
89}
90
91crosapi::mojom::CapabilitiesResponsePtr PrinterWithCapabilitiesToMojom(
92 const chromeos::Printer& printer,
93 const absl::optional<printing::PrinterSemanticCapsAndDefaults>& caps) {
94 return crosapi::mojom::CapabilitiesResponse::New(
95 PrinterToMojom(printer), printer.HasSecureProtocol(),
96 caps, // comment to prevent git cl format
97 0, 0, 0, // deprecated
98 printing::mojom::PinModeRestriction::kUnset, // deprecated
99 printing::mojom::ColorModeRestriction::kUnset, // deprecated
100 printing::mojom::DuplexModeRestriction::kUnset, // deprecated
101 printing::mojom::PinModeRestriction::kUnset); // deprecated
102}
103
104crosapi::mojom::LocalDestinationInfoPtr PrinterToMojom(
105 const chromeos::Printer& printer) {
106 return crosapi::mojom::LocalDestinationInfo::New(
107 printer.id(), printer.display_name(), printer.description(),
108 printer.source() == chromeos::Printer::SRC_POLICY,
109 printer.uri().GetNormalized(/*always_print_port=*/true),
110 StatusToMojom(printer.printer_status()));
111}
112
113crosapi::mojom::PrinterStatusPtr StatusToMojom(
114 const chromeos::CupsPrinterStatus& status) {
115 auto ptr = crosapi::mojom::PrinterStatus::New();
116 ptr->printer_id = status.GetPrinterId();
117 ptr->timestamp = status.GetTimestamp();
118 for (const auto& reason : status.GetStatusReasons()) {
119 if (reason.GetReason() == crosapi::mojom::StatusReason::Reason::kNoError) {
120 continue;
121 }
122 ptr->status_reasons.push_back(crosapi::mojom::StatusReason::New(
123 reason.GetReason(), reason.GetSeverity()));
124 }
125 return ptr;
126}
127
128} // namespace printing