Andrew Rayskiy | f90c7e8 | 2023-11-07 14:46:18 | [diff] [blame] | 1 | // 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 | |
Arthur Sonzogni | fe132ee | 2024-01-15 11:01:04 | [diff] [blame] | 7 | #include <optional> |
Andrew Rayskiy | f90c7e8 | 2023-11-07 14:46:18 | [diff] [blame] | 8 | #include <string> |
Andrei Nedoluzhko | 3eea9fb | 2024-11-14 13:48:07 | [diff] [blame] | 9 | #include <type_traits> |
| 10 | #include <utility> |
Andrew Rayskiy | f90c7e8 | 2023-11-07 14:46:18 | [diff] [blame] | 11 | |
Andrei Nedoluzhko | 3eea9fb | 2024-11-14 13:48:07 | [diff] [blame] | 12 | #include "base/functional/bind.h" |
| 13 | #include "base/functional/callback_forward.h" |
Andrew Rayskiy | f90c7e8 | 2023-11-07 14:46:18 | [diff] [blame] | 14 | #include "chrome/browser/ash/crosapi/crosapi_ash.h" |
| 15 | #include "chrome/browser/ash/crosapi/crosapi_manager.h" |
| 16 | #include "chrome/browser/ash/crosapi/local_printer_ash.h" |
Andrei Nedoluzhko | 3eea9fb | 2024-11-14 13:48:07 | [diff] [blame] | 17 | #include "chromeos/crosapi/mojom/local_printer.mojom.h" |
| 18 | #include "chromeos/printing/cups_printer_status.h" |
| 19 | #include "chromeos/printing/printer_configuration.h" |
Andrew Rayskiy | f90c7e8 | 2023-11-07 14:46:18 | [diff] [blame] | 20 | |
| 21 | namespace printing { |
| 22 | |
Andrei Nedoluzhko | 3eea9fb | 2024-11-14 13:48:07 | [diff] [blame] | 23 | namespace { |
| 24 | |
| 25 | template <typename MojoOptionType, |
| 26 | typename MojoOptionValueType, |
| 27 | typename ChromeOsOptionValueType> |
| 28 | mojo::StructPtr<MojoOptionType> PrintOptionToMojom( |
| 29 | const chromeos::Printer::PrintOption<ChromeOsOptionValueType>& print_option, |
| 30 | base::RepeatingCallback<MojoOptionValueType(const ChromeOsOptionValueType&)> |
| 31 | get_mojo_option_value) { |
| 32 | auto result = MojoOptionType::New(); |
| 33 | |
| 34 | if (print_option.default_value.has_value()) { |
| 35 | result->default_value = |
| 36 | get_mojo_option_value.Run(print_option.default_value.value()); |
| 37 | } |
| 38 | |
| 39 | if (!print_option.allowed_values.empty()) { |
| 40 | result->allowed_values = std::vector<MojoOptionValueType>(); |
| 41 | result->allowed_values->reserve(print_option.allowed_values.size()); |
| 42 | for (const auto& allowed_value : print_option.allowed_values) { |
| 43 | result->allowed_values->push_back( |
| 44 | get_mojo_option_value.Run(allowed_value)); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | return result; |
| 49 | } |
| 50 | |
| 51 | template <typename MojoOptionType, |
| 52 | typename MojoOptionValueType = std::remove_reference_t< |
| 53 | decltype(MojoOptionType().default_value.value())>, |
| 54 | typename ChromeOsOptionValueType> |
| 55 | mojo::StructPtr<MojoOptionType> PrintOptionToMojom( |
| 56 | const chromeos::Printer::PrintOption<ChromeOsOptionValueType>& |
| 57 | print_option) { |
| 58 | return PrintOptionToMojom<MojoOptionType>( |
| 59 | print_option, |
| 60 | base::BindRepeating([](const ChromeOsOptionValueType& value) { |
| 61 | return static_cast<MojoOptionValueType>(value); |
| 62 | })); |
| 63 | } |
| 64 | |
| 65 | } // namespace |
| 66 | |
Andrew Rayskiy | f90c7e8 | 2023-11-07 14:46:18 | [diff] [blame] | 67 | crosapi::mojom::LocalPrinter* GetLocalPrinterInterface() { |
Andrew Rayskiy | f90c7e8 | 2023-11-07 14:46:18 | [diff] [blame] | 68 | CHECK(crosapi::CrosapiManager::IsInitialized()); |
| 69 | return crosapi::CrosapiManager::Get()->crosapi_ash()->local_printer_ash(); |
Andrew Rayskiy | f90c7e8 | 2023-11-07 14:46:18 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | crosapi::mojom::CapabilitiesResponsePtr PrinterWithCapabilitiesToMojom( |
| 73 | const chromeos::Printer& printer, |
Arthur Sonzogni | fe132ee | 2024-01-15 11:01:04 | [diff] [blame] | 74 | const std::optional<printing::PrinterSemanticCapsAndDefaults>& caps) { |
Andrew Rayskiy | f90c7e8 | 2023-11-07 14:46:18 | [diff] [blame] | 75 | return crosapi::mojom::CapabilitiesResponse::New( |
| 76 | PrinterToMojom(printer), printer.HasSecureProtocol(), |
| 77 | caps, // comment to prevent git cl format |
| 78 | 0, 0, 0, // deprecated |
| 79 | printing::mojom::PinModeRestriction::kUnset, // deprecated |
| 80 | printing::mojom::ColorModeRestriction::kUnset, // deprecated |
| 81 | printing::mojom::DuplexModeRestriction::kUnset, // deprecated |
| 82 | printing::mojom::PinModeRestriction::kUnset); // deprecated |
| 83 | } |
| 84 | |
| 85 | crosapi::mojom::LocalDestinationInfoPtr PrinterToMojom( |
| 86 | const chromeos::Printer& printer) { |
| 87 | return crosapi::mojom::LocalDestinationInfo::New( |
| 88 | printer.id(), printer.display_name(), printer.description(), |
| 89 | printer.source() == chromeos::Printer::SRC_POLICY, |
| 90 | printer.uri().GetNormalized(/*always_print_port=*/true), |
Andrei Nedoluzhko | 3eea9fb | 2024-11-14 13:48:07 | [diff] [blame] | 91 | StatusToMojom(printer.printer_status()), |
| 92 | ManagedPrintOptionsToMojom(printer.print_job_options())); |
Andrew Rayskiy | f90c7e8 | 2023-11-07 14:46:18 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | crosapi::mojom::PrinterStatusPtr StatusToMojom( |
| 96 | const chromeos::CupsPrinterStatus& status) { |
| 97 | auto ptr = crosapi::mojom::PrinterStatus::New(); |
| 98 | ptr->printer_id = status.GetPrinterId(); |
| 99 | ptr->timestamp = status.GetTimestamp(); |
| 100 | for (const auto& reason : status.GetStatusReasons()) { |
| 101 | if (reason.GetReason() == crosapi::mojom::StatusReason::Reason::kNoError) { |
| 102 | continue; |
| 103 | } |
| 104 | ptr->status_reasons.push_back(crosapi::mojom::StatusReason::New( |
| 105 | reason.GetReason(), reason.GetSeverity())); |
| 106 | } |
| 107 | return ptr; |
| 108 | } |
| 109 | |
Andrei Nedoluzhko | 3eea9fb | 2024-11-14 13:48:07 | [diff] [blame] | 110 | crosapi::mojom::ManagedPrintOptionsPtr ManagedPrintOptionsToMojom( |
| 111 | const chromeos::Printer::ManagedPrintOptions& managed_print_options) { |
| 112 | auto result = crosapi::mojom::ManagedPrintOptions::New(); |
| 113 | |
| 114 | result->media_size = PrintOptionToMojom<crosapi::mojom::SizeOption>( |
| 115 | managed_print_options.media_size, |
| 116 | base::BindRepeating([](const chromeos::Printer::Size& value) { |
| 117 | return crosapi::mojom::Size::New(value.width, value.height); |
| 118 | })); |
| 119 | |
| 120 | result->media_type = PrintOptionToMojom<crosapi::mojom::StringOption>( |
| 121 | managed_print_options.media_type); |
| 122 | |
| 123 | result->duplex = PrintOptionToMojom<crosapi::mojom::DuplexOption>( |
| 124 | managed_print_options.duplex, |
| 125 | base::BindRepeating([](const chromeos::Printer::DuplexType& value) { |
| 126 | switch (value) { |
| 127 | case chromeos::Printer::DuplexType::kOneSided: |
| 128 | return crosapi::mojom::DuplexType::kOneSided; |
| 129 | case chromeos::Printer::DuplexType::kShortEdge: |
| 130 | return crosapi::mojom::DuplexType::kShortEdge; |
| 131 | case chromeos::Printer::DuplexType::kLongEdge: |
| 132 | return crosapi::mojom::DuplexType::kLongEdge; |
| 133 | default: |
| 134 | return crosapi::mojom::DuplexType::kUnknownDuplex; |
| 135 | } |
| 136 | })); |
| 137 | |
| 138 | result->color = PrintOptionToMojom<crosapi::mojom::BoolOption>( |
| 139 | managed_print_options.color); |
| 140 | |
| 141 | result->dpi = PrintOptionToMojom<crosapi::mojom::DpiOption>( |
| 142 | managed_print_options.dpi, |
| 143 | base::BindRepeating([](const chromeos::Printer::Dpi& value) { |
| 144 | return crosapi::mojom::Dpi::New(value.horizontal, value.vertical); |
| 145 | })); |
| 146 | |
| 147 | result->quality = PrintOptionToMojom<crosapi::mojom::QualityOption>( |
| 148 | managed_print_options.quality, |
| 149 | base::BindRepeating([](const chromeos::Printer::QualityType& value) { |
| 150 | switch (value) { |
| 151 | case chromeos::Printer::QualityType::kDraft: |
| 152 | return crosapi::mojom::QualityType::kDraft; |
| 153 | case chromeos::Printer::QualityType::kNormal: |
| 154 | return crosapi::mojom::QualityType::kNormal; |
| 155 | case chromeos::Printer::QualityType::kHigh: |
| 156 | return crosapi::mojom::QualityType::kHigh; |
| 157 | default: |
| 158 | return crosapi::mojom::QualityType::kUnknownQuality; |
| 159 | } |
| 160 | })); |
| 161 | |
| 162 | result->print_as_image = PrintOptionToMojom<crosapi::mojom::BoolOption>( |
| 163 | managed_print_options.print_as_image); |
| 164 | |
| 165 | return result; |
| 166 | } |
| 167 | |
Andrew Rayskiy | f90c7e8 | 2023-11-07 14:46:18 | [diff] [blame] | 168 | } // namespace printing |