Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2020 The Chromium Authors |
Alan Screen | 65304d5 | 2020-10-29 16:42:22 | [diff] [blame] | 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/services/printing/print_backend_service_impl.h" |
| 6 | |
Alan Screen | 366ce3e | 2021-06-16 23:26:04 | [diff] [blame] | 7 | #include <memory> |
Alan Screen | 65304d5 | 2020-10-29 16:42:22 | [diff] [blame] | 8 | #include <string> |
| 9 | #include <utility> |
Alan Screen | 2166ab1 | 2021-09-01 23:09:25 | [diff] [blame] | 10 | #include <vector> |
Alan Screen | 65304d5 | 2020-10-29 16:42:22 | [diff] [blame] | 11 | |
Alan Screen | 7a30b04 | 2022-09-19 17:51:24 | [diff] [blame] | 12 | #include "base/check.h" |
Victor Hugo Vianna Silva | 08f4b00 | 2022-01-17 13:20:04 | [diff] [blame] | 13 | #include "base/containers/adapters.h" |
Alan Screen | 65304d5 | 2020-10-29 16:42:22 | [diff] [blame] | 14 | #include "base/logging.h" |
Keishi Hattori | e175ac5 | 2022-06-07 06:24:57 | [diff] [blame] | 15 | #include "base/memory/raw_ptr.h" |
Alan Screen | 65304d5 | 2020-10-29 16:42:22 | [diff] [blame] | 16 | #include "base/notreached.h" |
Peter Kasting | 73f230d0 | 2022-09-17 22:29:30 | [diff] [blame] | 17 | #include "base/ranges/algorithm.h" |
Sean Maher | e672a66 | 2023-01-09 21:42:28 | [diff] [blame] | 18 | #include "base/task/sequenced_task_runner.h" |
Alan Screen | 2166ab1 | 2021-09-01 23:09:25 | [diff] [blame] | 19 | #include "base/task/task_traits.h" |
| 20 | #include "base/task/thread_pool.h" |
Alan Screen | cb6421f | 2021-12-09 21:54:18 | [diff] [blame] | 21 | #include "base/threading/sequence_bound.h" |
Peter Kasting | a7066c6 | 2023-04-25 02:39:31 | [diff] [blame] | 22 | #include "base/types/expected.h" |
Peter Kasting | f115ece | 2023-08-07 21:09:44 | [diff] [blame] | 23 | #include "base/types/expected_macros.h" |
Alan Screen | 59767538 | 2021-09-11 05:14:53 | [diff] [blame] | 24 | #include "base/values.h" |
Alan Screen | c15aea6 | 2021-01-08 03:32:12 | [diff] [blame] | 25 | #include "build/build_config.h" |
Lei Zhang | 3a48a1d | 2023-03-25 02:10:46 | [diff] [blame] | 26 | #include "build/chromeos_buildflags.h" |
Alan Screen | c7874de | 2022-09-01 21:00:10 | [diff] [blame] | 27 | #include "chrome/common/printing/printing_init.h" |
Alan Screen | 65304d5 | 2020-10-29 16:42:22 | [diff] [blame] | 28 | #include "chrome/services/printing/public/mojom/print_backend_service.mojom.h" |
Alan Screen | 366ce3e | 2021-06-16 23:26:04 | [diff] [blame] | 29 | #include "components/crash/core/common/crash_keys.h" |
Alan Screen | 65304d5 | 2020-10-29 16:42:22 | [diff] [blame] | 30 | #include "mojo/public/cpp/bindings/pending_receiver.h" |
| 31 | #include "printing/backend/print_backend.h" |
Andrew Rayskiy | 16f8cab | 2022-12-15 16:28:33 | [diff] [blame] | 32 | #include "printing/buildflags/buildflags.h" |
Alan Screen | bd4f851a | 2022-03-29 22:53:05 | [diff] [blame] | 33 | #include "printing/metafile.h" |
| 34 | #include "printing/metafile_skia.h" |
Alan Screen | 8f86e14 | 2021-05-07 09:52:19 | [diff] [blame] | 35 | #include "printing/mojom/print.mojom.h" |
Alan Screen | 61e1a37 | 2021-12-10 03:02:34 | [diff] [blame] | 36 | #include "printing/printed_document.h" |
Alan Screen | 59767538 | 2021-09-11 05:14:53 | [diff] [blame] | 37 | #include "printing/printing_context.h" |
Alan Screen | 252d981 | 2022-03-29 20:12:08 | [diff] [blame] | 38 | #include "third_party/abseil-cpp/absl/types/optional.h" |
Alan Screen | 65304d5 | 2020-10-29 16:42:22 | [diff] [blame] | 39 | |
Xiaohan Wang | d4bc274 | 2022-01-15 19:48:55 | [diff] [blame] | 40 | #if BUILDFLAG(IS_MAC) |
Alan Screen | c15aea6 | 2021-01-08 03:32:12 | [diff] [blame] | 41 | #include "base/threading/thread_restrictions.h" |
| 42 | #include "chrome/common/printing/printer_capabilities_mac.h" |
| 43 | #endif |
| 44 | |
Andrew Rayskiy | 16f8cab | 2022-12-15 16:28:33 | [diff] [blame] | 45 | #if BUILDFLAG(IS_CHROMEOS) && BUILDFLAG(USE_CUPS) |
Alan Screen | 2166ab1 | 2021-09-01 23:09:25 | [diff] [blame] | 46 | #include "printing/backend/cups_connection_pool.h" |
| 47 | #endif |
| 48 | |
Alan Screen | 65a6641 | 2022-09-24 02:29:22 | [diff] [blame] | 49 | #if BUILDFLAG(IS_LINUX) |
| 50 | #include "base/no_destructor.h" |
| 51 | #include "ui/linux/linux_ui.h" |
| 52 | #include "ui/linux/linux_ui_delegate_stub.h" |
| 53 | #include "ui/linux/linux_ui_factory.h" |
| 54 | #endif |
| 55 | |
Xiaohan Wang | d4bc274 | 2022-01-15 19:48:55 | [diff] [blame] | 56 | #if BUILDFLAG(IS_WIN) |
Alan Screen | 61e1a37 | 2021-12-10 03:02:34 | [diff] [blame] | 57 | #include "base/containers/queue.h" |
Andy Phan | 65e7356 | 2022-11-02 17:06:47 | [diff] [blame] | 58 | #include "base/types/expected.h" |
Alan Screen | c336bd86 | 2022-03-22 18:19:41 | [diff] [blame] | 59 | #include "base/win/win_util.h" |
Andy Phan | d8ca243 | 2022-10-14 21:11:52 | [diff] [blame] | 60 | #include "chrome/services/printing/public/mojom/printer_xml_parser.mojom.h" |
| 61 | #include "mojo/public/cpp/bindings/pending_remote.h" |
| 62 | #include "mojo/public/cpp/bindings/remote.h" |
Andy Phan | 65e7356 | 2022-11-02 17:06:47 | [diff] [blame] | 63 | #include "printing/backend/xps_utils_win.h" |
Alan Screen | 61e1a37 | 2021-12-10 03:02:34 | [diff] [blame] | 64 | #include "printing/emf_win.h" |
Alan Screen | 61e1a37 | 2021-12-10 03:02:34 | [diff] [blame] | 65 | #include "printing/printed_page_win.h" |
Andy Phan | 65e7356 | 2022-11-02 17:06:47 | [diff] [blame] | 66 | #include "printing/printing_features.h" |
Alan Screen | 61e1a37 | 2021-12-10 03:02:34 | [diff] [blame] | 67 | #include "ui/gfx/geometry/rect.h" |
| 68 | #include "ui/gfx/geometry/size.h" |
Alan Screen | c336bd86 | 2022-03-22 18:19:41 | [diff] [blame] | 69 | #include "ui/gfx/native_widget_types.h" |
Alan Screen | 61e1a37 | 2021-12-10 03:02:34 | [diff] [blame] | 70 | #endif |
| 71 | |
Alan Screen | 65304d5 | 2020-10-29 16:42:22 | [diff] [blame] | 72 | namespace printing { |
| 73 | |
Alan Screen | 2166ab1 | 2021-09-01 23:09:25 | [diff] [blame] | 74 | namespace { |
| 75 | |
Alan Screen | 65a6641 | 2022-09-24 02:29:22 | [diff] [blame] | 76 | #if BUILDFLAG(IS_LINUX) |
| 77 | void InstantiateLinuxUiDelegate() { |
| 78 | // TODO(crbug.com/809738) Until a real UI can be used in a utility process, |
| 79 | // need to use the stub version. |
| 80 | static base::NoDestructor<ui::LinuxUiDelegateStub> linux_ui_delegate; |
| 81 | } |
| 82 | #endif |
| 83 | |
Alan Screen | 2166ab1 | 2021-09-01 23:09:25 | [diff] [blame] | 84 | scoped_refptr<base::SequencedTaskRunner> GetPrintingTaskRunner() { |
Alan Screen | 7e8213cc7 | 2023-07-13 22:44:37 | [diff] [blame] | 85 | #if BUILDFLAG(IS_LINUX) |
| 86 | // Use task runner associated with equivalent of UI thread. Needed for calls |
| 87 | // made through `PrintDialogLinuxInterface` to properly execute. |
| 88 | CHECK(base::SequencedTaskRunner::HasCurrentDefault()); |
Lei Zhang | 11694fa | 2023-09-28 08:27:22 | [diff] [blame] | 89 | return base::SequencedTaskRunner::GetCurrentDefault(); |
Alan Screen | 7e8213cc7 | 2023-07-13 22:44:37 | [diff] [blame] | 90 | #else |
| 91 | |
Alan Screen | 2166ab1 | 2021-09-01 23:09:25 | [diff] [blame] | 92 | static constexpr base::TaskTraits kTraits = { |
| 93 | base::MayBlock(), base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN}; |
| 94 | |
Andrew Rayskiy | 16f8cab | 2022-12-15 16:28:33 | [diff] [blame] | 95 | #if BUILDFLAG(USE_CUPS) |
Alan Screen | 2166ab1 | 2021-09-01 23:09:25 | [diff] [blame] | 96 | // CUPS is thread safe, so a task runner can be allocated for each job. |
Lei Zhang | 11694fa | 2023-09-28 08:27:22 | [diff] [blame] | 97 | return base::ThreadPool::CreateSequencedTaskRunner(kTraits); |
Xiaohan Wang | d4bc274 | 2022-01-15 19:48:55 | [diff] [blame] | 98 | #elif BUILDFLAG(IS_WIN) |
Alan Screen | 2166ab1 | 2021-09-01 23:09:25 | [diff] [blame] | 99 | // For Windows, we want a single threaded task runner shared for all print |
| 100 | // jobs in the process because Windows printer drivers are oftentimes not |
| 101 | // thread-safe. This protects against multiple print jobs to the same device |
| 102 | // from running in the driver at the same time. |
Lei Zhang | 11694fa | 2023-09-28 08:27:22 | [diff] [blame] | 103 | return base::ThreadPool::CreateSingleThreadTaskRunner(kTraits); |
Alan Screen | 2166ab1 | 2021-09-01 23:09:25 | [diff] [blame] | 104 | #else |
| 105 | // Be conservative for unsupported platforms, use a single threaded runner |
| 106 | // so that concurrent print jobs are not in driver code at the same time. |
Lei Zhang | 11694fa | 2023-09-28 08:27:22 | [diff] [blame] | 107 | return base::ThreadPool::CreateSingleThreadTaskRunner(kTraits); |
Alan Screen | 2166ab1 | 2021-09-01 23:09:25 | [diff] [blame] | 108 | #endif |
Alan Screen | 7e8213cc7 | 2023-07-13 22:44:37 | [diff] [blame] | 109 | #endif // BUILDFLAG(IS_LINUX) |
Alan Screen | 2166ab1 | 2021-09-01 23:09:25 | [diff] [blame] | 110 | } |
| 111 | |
Alan Screen | 61e1a37 | 2021-12-10 03:02:34 | [diff] [blame] | 112 | std::unique_ptr<Metafile> CreateMetafile(mojom::MetafileDataType data_type) { |
| 113 | switch (data_type) { |
| 114 | case mojom::MetafileDataType::kPDF: |
| 115 | return std::make_unique<MetafileSkia>(); |
Alan Screen | bd4f851a | 2022-03-29 22:53:05 | [diff] [blame] | 116 | #if BUILDFLAG(IS_WIN) |
Alan Screen | 61e1a37 | 2021-12-10 03:02:34 | [diff] [blame] | 117 | case mojom::MetafileDataType::kEMF: |
| 118 | return std::make_unique<Emf>(); |
Alan Screen | bd4f851a | 2022-03-29 22:53:05 | [diff] [blame] | 119 | #endif |
Alan Screen | 61e1a37 | 2021-12-10 03:02:34 | [diff] [blame] | 120 | } |
| 121 | } |
Alan Screen | 252d981 | 2022-03-29 20:12:08 | [diff] [blame] | 122 | |
| 123 | struct RenderData { |
| 124 | std::unique_ptr<uint8_t[]> data_copy; |
| 125 | std::unique_ptr<Metafile> metafile; |
| 126 | }; |
| 127 | |
| 128 | absl::optional<RenderData> PrepareRenderData( |
| 129 | int document_cookie, |
| 130 | mojom::MetafileDataType page_data_type, |
| 131 | const base::ReadOnlySharedMemoryRegion& serialized_data) { |
| 132 | base::ReadOnlySharedMemoryMapping mapping = serialized_data.Map(); |
| 133 | if (!mapping.IsValid()) { |
| 134 | DLOG(ERROR) << "Failure printing document " << document_cookie |
| 135 | << ", cannot map input."; |
| 136 | return absl::nullopt; |
| 137 | } |
| 138 | |
| 139 | RenderData render_data; |
| 140 | render_data.metafile = CreateMetafile(page_data_type); |
| 141 | |
| 142 | // For security reasons we need to use a copy of the data, and not operate |
| 143 | // on it directly out of shared memory. Make a copy here if the underlying |
| 144 | // `Metafile` implementation doesn't do it automatically. |
| 145 | // TODO(crbug.com/1135729) Eliminate this copy when the shared memory can't |
| 146 | // be written by the sender. |
| 147 | base::span<const uint8_t> data = mapping.GetMemoryAsSpan<uint8_t>(); |
| 148 | if (render_data.metafile->ShouldCopySharedMemoryRegionData()) { |
| 149 | render_data.data_copy = std::make_unique<uint8_t[]>(data.size()); |
Peter Kasting | 965f02fc | 2022-10-28 23:35:43 | [diff] [blame] | 150 | base::ranges::copy(data, render_data.data_copy.get()); |
Alan Screen | 252d981 | 2022-03-29 20:12:08 | [diff] [blame] | 151 | data = base::span<const uint8_t>(render_data.data_copy.get(), data.size()); |
| 152 | } |
| 153 | if (!render_data.metafile->InitFromData(data)) { |
| 154 | DLOG(ERROR) << "Failure printing document " << document_cookie |
| 155 | << ", unable to initialize."; |
| 156 | return absl::nullopt; |
| 157 | } |
| 158 | return render_data; |
| 159 | } |
Alan Screen | 61e1a37 | 2021-12-10 03:02:34 | [diff] [blame] | 160 | |
Alan Screen | 2166ab1 | 2021-09-01 23:09:25 | [diff] [blame] | 161 | // Local storage of document and associated data needed to submit to job to |
Alan Screen | cb6421f | 2021-12-09 21:54:18 | [diff] [blame] | 162 | // the operating system's printing API. All access to the document occurs on |
| 163 | // a worker task runner. |
| 164 | class DocumentContainer { |
| 165 | public: |
Alan Screen | 9aa4614 | 2023-03-24 19:56:25 | [diff] [blame] | 166 | DocumentContainer(std::unique_ptr<PrintingContext::Delegate> context_delegate, |
| 167 | std::unique_ptr<PrintingContext> context, |
Alan Screen | fd714c1 | 2023-03-24 21:03:29 | [diff] [blame] | 168 | scoped_refptr<PrintedDocument> document) |
Alan Screen | 9aa4614 | 2023-03-24 19:56:25 | [diff] [blame] | 169 | : context_delegate_(std::move(context_delegate)), |
| 170 | context_(std::move(context)), |
Alan Screen | fd714c1 | 2023-03-24 21:03:29 | [diff] [blame] | 171 | document_(document) {} |
Alan Screen | 2166ab1 | 2021-09-01 23:09:25 | [diff] [blame] | 172 | |
| 173 | ~DocumentContainer() = default; |
| 174 | |
Alan Screen | 61e1a37 | 2021-12-10 03:02:34 | [diff] [blame] | 175 | // Helper functions that runs on a task runner. |
Alan Screen | cb6421f | 2021-12-09 21:54:18 | [diff] [blame] | 176 | mojom::ResultCode StartPrintingReadyDocument(); |
Xiaohan Wang | d4bc274 | 2022-01-15 19:48:55 | [diff] [blame] | 177 | #if BUILDFLAG(IS_WIN) |
Alan Screen | 61e1a37 | 2021-12-10 03:02:34 | [diff] [blame] | 178 | mojom::ResultCode DoRenderPrintedPage( |
| 179 | uint32_t page_index, |
| 180 | mojom::MetafileDataType page_data_type, |
| 181 | base::ReadOnlySharedMemoryRegion serialized_page, |
| 182 | gfx::Size page_size, |
| 183 | gfx::Rect page_content_rect, |
| 184 | float shrink_factor); |
| 185 | #endif |
Alan Screen | bd4f851a | 2022-03-29 22:53:05 | [diff] [blame] | 186 | mojom::ResultCode DoRenderPrintedDocument( |
Alan Screen | a6cebd8 | 2022-11-03 19:56:29 | [diff] [blame] | 187 | uint32_t page_count, |
Alan Screen | bd4f851a | 2022-03-29 22:53:05 | [diff] [blame] | 188 | mojom::MetafileDataType data_type, |
| 189 | base::ReadOnlySharedMemoryRegion serialized_document); |
Alan Screen | 6bf1d5fb | 2022-02-08 15:51:14 | [diff] [blame] | 190 | mojom::ResultCode DoDocumentDone(); |
Alan Screen | 2a7dcc9 | 2022-11-17 04:52:01 | [diff] [blame] | 191 | void DoCancel(); |
Alan Screen | cb6421f | 2021-12-09 21:54:18 | [diff] [blame] | 192 | |
| 193 | private: |
Alan Screen | 9aa4614 | 2023-03-24 19:56:25 | [diff] [blame] | 194 | std::unique_ptr<PrintingContext::Delegate> context_delegate_; |
Alan Screen | cb6421f | 2021-12-09 21:54:18 | [diff] [blame] | 195 | std::unique_ptr<PrintingContext> context_; |
Alan Screen | 2166ab1 | 2021-09-01 23:09:25 | [diff] [blame] | 196 | |
Alan Screen | 9aa4614 | 2023-03-24 19:56:25 | [diff] [blame] | 197 | scoped_refptr<PrintedDocument> document_; |
| 198 | |
Alan Screen | cb6421f | 2021-12-09 21:54:18 | [diff] [blame] | 199 | // Ensure all interactions for this document are issued from the same runner. |
| 200 | SEQUENCE_CHECKER(sequence_checker_); |
| 201 | }; |
| 202 | |
| 203 | mojom::ResultCode DocumentContainer::StartPrintingReadyDocument() { |
| 204 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 205 | |
| 206 | DVLOG(1) << "Start printing for document " << document_->cookie(); |
| 207 | |
Alan Screen | fd714c1 | 2023-03-24 21:03:29 | [diff] [blame] | 208 | mojom::ResultCode result = context_->NewDocument(document_->name()); |
Alan Screen | cb6421f | 2021-12-09 21:54:18 | [diff] [blame] | 209 | if (result != mojom::ResultCode::kSuccess) { |
| 210 | DLOG(ERROR) << "Failure initializing new document " << document_->cookie() |
| 211 | << ", error: " << result; |
Alan Screen | ca9c993 | 2022-11-21 19:31:26 | [diff] [blame] | 212 | context_->Cancel(); |
Alan Screen | cb6421f | 2021-12-09 21:54:18 | [diff] [blame] | 213 | return result; |
| 214 | } |
| 215 | |
| 216 | return mojom::ResultCode::kSuccess; |
| 217 | } |
| 218 | |
Xiaohan Wang | d4bc274 | 2022-01-15 19:48:55 | [diff] [blame] | 219 | #if BUILDFLAG(IS_WIN) |
Alan Screen | 61e1a37 | 2021-12-10 03:02:34 | [diff] [blame] | 220 | mojom::ResultCode DocumentContainer::DoRenderPrintedPage( |
| 221 | uint32_t page_index, |
| 222 | mojom::MetafileDataType page_data_type, |
| 223 | base::ReadOnlySharedMemoryRegion serialized_page, |
| 224 | gfx::Size page_size, |
| 225 | gfx::Rect page_content_rect, |
| 226 | float shrink_factor) { |
| 227 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 228 | |
| 229 | DVLOG(1) << "Render printed page " << page_index << " for document " |
| 230 | << document_->cookie(); |
| 231 | |
Alan Screen | 252d981 | 2022-03-29 20:12:08 | [diff] [blame] | 232 | absl::optional<RenderData> render_data = |
| 233 | PrepareRenderData(document_->cookie(), page_data_type, serialized_page); |
Alan Screen | ca9c993 | 2022-11-21 19:31:26 | [diff] [blame] | 234 | if (!render_data) { |
| 235 | DLOG(ERROR) << "Failure preparing render data for document " |
| 236 | << document_->cookie(); |
| 237 | context_->Cancel(); |
Alan Screen | 61e1a37 | 2021-12-10 03:02:34 | [diff] [blame] | 238 | return mojom::ResultCode::kFailed; |
Alan Screen | ca9c993 | 2022-11-21 19:31:26 | [diff] [blame] | 239 | } |
Alan Screen | 61e1a37 | 2021-12-10 03:02:34 | [diff] [blame] | 240 | |
Alan Screen | 252d981 | 2022-03-29 20:12:08 | [diff] [blame] | 241 | document_->SetPage(page_index, std::move(render_data->metafile), |
| 242 | shrink_factor, page_size, page_content_rect); |
Alan Screen | 61e1a37 | 2021-12-10 03:02:34 | [diff] [blame] | 243 | |
Alan Screen | 0ad9df7 | 2022-09-27 18:34:35 | [diff] [blame] | 244 | mojom::ResultCode result = document_->RenderPrintedPage( |
| 245 | *document_->GetPage(page_index), context_.get()); |
| 246 | if (result != mojom::ResultCode::kSuccess) { |
| 247 | DLOG(ERROR) << "Failure rendering page " << page_index << " of document " |
| 248 | << document_->cookie() << ", error: " << result; |
Alan Screen | ca9c993 | 2022-11-21 19:31:26 | [diff] [blame] | 249 | context_->Cancel(); |
Alan Screen | 0ad9df7 | 2022-09-27 18:34:35 | [diff] [blame] | 250 | } |
Alan Screen | ca9c993 | 2022-11-21 19:31:26 | [diff] [blame] | 251 | return result; |
Alan Screen | 61e1a37 | 2021-12-10 03:02:34 | [diff] [blame] | 252 | } |
Xiaohan Wang | d4bc274 | 2022-01-15 19:48:55 | [diff] [blame] | 253 | #endif // BUILDFLAG(IS_WIN) |
Alan Screen | 61e1a37 | 2021-12-10 03:02:34 | [diff] [blame] | 254 | |
Alan Screen | bd4f851a | 2022-03-29 22:53:05 | [diff] [blame] | 255 | mojom::ResultCode DocumentContainer::DoRenderPrintedDocument( |
Alan Screen | a6cebd8 | 2022-11-03 19:56:29 | [diff] [blame] | 256 | uint32_t page_count, |
Alan Screen | bd4f851a | 2022-03-29 22:53:05 | [diff] [blame] | 257 | mojom::MetafileDataType data_type, |
| 258 | base::ReadOnlySharedMemoryRegion serialized_document) { |
| 259 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 260 | |
| 261 | DVLOG(1) << "Render printed document " << document_->cookie(); |
| 262 | |
| 263 | absl::optional<RenderData> render_data = |
| 264 | PrepareRenderData(document_->cookie(), data_type, serialized_document); |
Alan Screen | ca9c993 | 2022-11-21 19:31:26 | [diff] [blame] | 265 | if (!render_data) { |
| 266 | DLOG(ERROR) << "Failure preparing render data for document " |
| 267 | << document_->cookie(); |
| 268 | context_->Cancel(); |
Alan Screen | bd4f851a | 2022-03-29 22:53:05 | [diff] [blame] | 269 | return mojom::ResultCode::kFailed; |
Alan Screen | ca9c993 | 2022-11-21 19:31:26 | [diff] [blame] | 270 | } |
Alan Screen | bd4f851a | 2022-03-29 22:53:05 | [diff] [blame] | 271 | |
Alan Screen | a6cebd8 | 2022-11-03 19:56:29 | [diff] [blame] | 272 | document_->set_page_count(page_count); |
Alan Screen | bd4f851a | 2022-03-29 22:53:05 | [diff] [blame] | 273 | document_->SetDocument(std::move(render_data->metafile)); |
| 274 | |
Alan Screen | ca9c993 | 2022-11-21 19:31:26 | [diff] [blame] | 275 | mojom::ResultCode result = document_->RenderPrintedDocument(context_.get()); |
| 276 | if (result != mojom::ResultCode::kSuccess) { |
| 277 | DLOG(ERROR) << "Failure rendering document " << document_->cookie() |
| 278 | << ", error: " << result; |
| 279 | context_->Cancel(); |
| 280 | } |
| 281 | return result; |
Alan Screen | bd4f851a | 2022-03-29 22:53:05 | [diff] [blame] | 282 | } |
| 283 | |
Alan Screen | 6bf1d5fb | 2022-02-08 15:51:14 | [diff] [blame] | 284 | mojom::ResultCode DocumentContainer::DoDocumentDone() { |
| 285 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 286 | |
| 287 | DVLOG(1) << "Document done for document " << document_->cookie(); |
Alan Screen | ca9c993 | 2022-11-21 19:31:26 | [diff] [blame] | 288 | mojom::ResultCode result = context_->DocumentDone(); |
| 289 | if (result != mojom::ResultCode::kSuccess) { |
| 290 | DLOG(ERROR) << "Failure completing document " << document_->cookie() |
| 291 | << ", error: " << result; |
| 292 | context_->Cancel(); |
| 293 | } |
| 294 | return result; |
Alan Screen | 6bf1d5fb | 2022-02-08 15:51:14 | [diff] [blame] | 295 | } |
| 296 | |
Alan Screen | 2a7dcc9 | 2022-11-17 04:52:01 | [diff] [blame] | 297 | void DocumentContainer::DoCancel() { |
| 298 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 299 | |
| 300 | DVLOG(1) << "Canceling document " << document_->cookie(); |
| 301 | context_->Cancel(); |
| 302 | } |
| 303 | |
Alan Screen | cb6421f | 2021-12-09 21:54:18 | [diff] [blame] | 304 | } // namespace |
| 305 | |
| 306 | // Helper for managing `DocumentContainer` objects. All access to this occurs |
| 307 | // on the main thread. |
| 308 | class PrintBackendServiceImpl::DocumentHelper { |
| 309 | public: |
| 310 | DocumentHelper( |
| 311 | int document_cookie, |
| 312 | base::SequenceBound<DocumentContainer> document_container, |
| 313 | mojom::PrintBackendService::StartPrintingCallback start_printing_callback) |
| 314 | : document_cookie_(document_cookie), |
| 315 | document_container_(std::move(document_container)), |
| 316 | start_printing_callback_(std::move(start_printing_callback)) {} |
| 317 | |
| 318 | ~DocumentHelper() = default; |
| 319 | |
| 320 | int document_cookie() const { |
| 321 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 322 | return document_cookie_; |
| 323 | } |
| 324 | |
| 325 | base::SequenceBound<DocumentContainer>& document_container() { |
| 326 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 327 | return document_container_; |
| 328 | } |
| 329 | |
| 330 | mojom::PrintBackendService::StartPrintingCallback |
| 331 | TakeStartPrintingCallback() { |
| 332 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 333 | return std::move(start_printing_callback_); |
| 334 | } |
| 335 | |
| 336 | private: |
| 337 | const int document_cookie_; |
| 338 | |
| 339 | base::SequenceBound<DocumentContainer> document_container_; |
| 340 | |
| 341 | // `start_printing_callback_` is held until the document is ready for |
Alan Screen | 2166ab1 | 2021-09-01 23:09:25 | [diff] [blame] | 342 | // printing. |
Alan Screen | cb6421f | 2021-12-09 21:54:18 | [diff] [blame] | 343 | mojom::PrintBackendService::StartPrintingCallback start_printing_callback_; |
Alan Screen | 2166ab1 | 2021-09-01 23:09:25 | [diff] [blame] | 344 | |
Alan Screen | cb6421f | 2021-12-09 21:54:18 | [diff] [blame] | 345 | // Ensure all interactions for this document are issued from the same runner. |
| 346 | SEQUENCE_CHECKER(sequence_checker_); |
Alan Screen | 2166ab1 | 2021-09-01 23:09:25 | [diff] [blame] | 347 | }; |
| 348 | |
Alex Gough | 59a0e96 | 2021-09-30 23:42:44 | [diff] [blame] | 349 | // Sandboxed service helper. |
| 350 | SandboxedPrintBackendHostImpl::SandboxedPrintBackendHostImpl( |
| 351 | mojo::PendingReceiver<mojom::SandboxedPrintBackendHost> receiver) |
| 352 | : receiver_(this, std::move(receiver)) {} |
| 353 | |
| 354 | SandboxedPrintBackendHostImpl::~SandboxedPrintBackendHostImpl() = default; |
| 355 | |
| 356 | void SandboxedPrintBackendHostImpl::BindBackend( |
| 357 | mojo::PendingReceiver<mojom::PrintBackendService> receiver) { |
| 358 | CHECK(!print_backend_service_) |
| 359 | << "Cannot bind service twice in same process."; |
| 360 | print_backend_service_ = |
| 361 | std::make_unique<PrintBackendServiceImpl>(std::move(receiver)); |
| 362 | } |
| 363 | |
| 364 | // Unsandboxed service helper. |
| 365 | UnsandboxedPrintBackendHostImpl::UnsandboxedPrintBackendHostImpl( |
| 366 | mojo::PendingReceiver<mojom::UnsandboxedPrintBackendHost> receiver) |
| 367 | : receiver_(this, std::move(receiver)) {} |
| 368 | |
| 369 | UnsandboxedPrintBackendHostImpl::~UnsandboxedPrintBackendHostImpl() = default; |
| 370 | |
| 371 | void UnsandboxedPrintBackendHostImpl::BindBackend( |
| 372 | mojo::PendingReceiver<mojom::PrintBackendService> receiver) { |
| 373 | CHECK(!print_backend_service_) |
| 374 | << "Cannot bind service twice in same process."; |
| 375 | print_backend_service_ = |
| 376 | std::make_unique<PrintBackendServiceImpl>(std::move(receiver)); |
| 377 | } |
| 378 | |
Alan Screen | 2166ab1 | 2021-09-01 23:09:25 | [diff] [blame] | 379 | PrintBackendServiceImpl::PrintingContextDelegate::PrintingContextDelegate() = |
| 380 | default; |
| 381 | PrintBackendServiceImpl::PrintingContextDelegate::~PrintingContextDelegate() = |
| 382 | default; |
| 383 | |
| 384 | gfx::NativeView |
| 385 | PrintBackendServiceImpl::PrintingContextDelegate::GetParentView() { |
Alan Screen | 49f16ac | 2023-01-13 05:38:34 | [diff] [blame] | 386 | #if BUILDFLAG(ENABLE_OOP_BASIC_PRINT_DIALOG) |
Alan Screen | c336bd86 | 2022-03-22 18:19:41 | [diff] [blame] | 387 | return parent_native_view_; |
| 388 | #else |
Alan Screen | 2166ab1 | 2021-09-01 23:09:25 | [diff] [blame] | 389 | NOTREACHED(); |
| 390 | return nullptr; |
Alan Screen | c336bd86 | 2022-03-22 18:19:41 | [diff] [blame] | 391 | #endif |
Alan Screen | 2166ab1 | 2021-09-01 23:09:25 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | std::string PrintBackendServiceImpl::PrintingContextDelegate::GetAppLocale() { |
| 395 | return locale_; |
| 396 | } |
| 397 | |
Alan Screen | 49f16ac | 2023-01-13 05:38:34 | [diff] [blame] | 398 | #if BUILDFLAG(ENABLE_OOP_BASIC_PRINT_DIALOG) |
Alan Screen | c336bd86 | 2022-03-22 18:19:41 | [diff] [blame] | 399 | void PrintBackendServiceImpl::PrintingContextDelegate::SetParentWindow( |
| 400 | uint32_t parent_window_id) { |
Alan Screen | 49f16ac | 2023-01-13 05:38:34 | [diff] [blame] | 401 | #if BUILDFLAG(IS_WIN) |
Alan Screen | c336bd86 | 2022-03-22 18:19:41 | [diff] [blame] | 402 | parent_native_view_ = reinterpret_cast<gfx::NativeView>( |
| 403 | base::win::Uint32ToHandle(parent_window_id)); |
Alan Screen | 49f16ac | 2023-01-13 05:38:34 | [diff] [blame] | 404 | #else |
| 405 | NOTREACHED(); |
| 406 | #endif |
Alan Screen | c336bd86 | 2022-03-22 18:19:41 | [diff] [blame] | 407 | } |
| 408 | #endif |
| 409 | |
Alan Screen | 2166ab1 | 2021-09-01 23:09:25 | [diff] [blame] | 410 | void PrintBackendServiceImpl::PrintingContextDelegate::SetAppLocale( |
| 411 | const std::string& locale) { |
| 412 | locale_ = locale; |
| 413 | } |
| 414 | |
Alan Screen | 74c6632f | 2023-02-24 22:40:07 | [diff] [blame] | 415 | // Holds the context and associated delegate for persistent usage across |
| 416 | // multiple settings calls until they are ready to be used to print a |
| 417 | // document. Required since `PrintingContext` does not own the corresponding |
| 418 | // delegate object that it relies upon. |
| 419 | struct PrintBackendServiceImpl::ContextContainer { |
| 420 | ContextContainer() = default; |
| 421 | ~ContextContainer() = default; |
| 422 | |
| 423 | std::unique_ptr<PrintingContextDelegate> delegate; |
| 424 | std::unique_ptr<PrintingContext> context; |
| 425 | }; |
| 426 | |
Alan Screen | 65304d5 | 2020-10-29 16:42:22 | [diff] [blame] | 427 | PrintBackendServiceImpl::PrintBackendServiceImpl( |
| 428 | mojo::PendingReceiver<mojom::PrintBackendService> receiver) |
| 429 | : receiver_(this, std::move(receiver)) {} |
| 430 | |
| 431 | PrintBackendServiceImpl::~PrintBackendServiceImpl() = default; |
| 432 | |
Andy Phan | 5f3db32 | 2022-11-11 19:53:02 | [diff] [blame] | 433 | void PrintBackendServiceImpl::InitCommon( |
| 434 | #if BUILDFLAG(IS_WIN) |
| 435 | const std::string& locale, |
| 436 | mojo::PendingRemote<mojom::PrinterXmlParser> remote |
| 437 | #else |
| 438 | const std::string& locale |
| 439 | #endif // BUILDFLAG(IS_WIN) |
| 440 | ) { |
Alan Screen | 74c6632f | 2023-02-24 22:40:07 | [diff] [blame] | 441 | locale_ = locale; |
Andy Phan | 5f3db32 | 2022-11-11 19:53:02 | [diff] [blame] | 442 | #if BUILDFLAG(IS_WIN) |
| 443 | if (remote.is_valid()) |
| 444 | xml_parser_remote_.Bind(std::move(remote)); |
| 445 | #endif // BUILDFLAG(IS_WIN) |
Alan Screen | 71ef2a0 | 2022-09-19 23:48:55 | [diff] [blame] | 446 | } |
| 447 | |
Andy Phan | 5f3db32 | 2022-11-11 19:53:02 | [diff] [blame] | 448 | void PrintBackendServiceImpl::Init( |
| 449 | #if BUILDFLAG(IS_WIN) |
| 450 | const std::string& locale, |
| 451 | mojo::PendingRemote<mojom::PrinterXmlParser> remote |
| 452 | #else |
| 453 | const std::string& locale |
| 454 | #endif // BUILDFLAG(IS_WIN) |
| 455 | ) { |
Alan Screen | 71ef2a0 | 2022-09-19 23:48:55 | [diff] [blame] | 456 | // Test classes should not invoke this base initialization method, as process |
| 457 | // initialization is very different for test frameworks. Test classes |
| 458 | // will also provide their own test version of a `PrintBackend`. |
| 459 | // Common initialization for production and testing should instead reside in |
| 460 | // `InitCommon()`. |
Alan Screen | c7874de | 2022-09-01 21:00:10 | [diff] [blame] | 461 | InitializeProcessForPrinting(); |
Alan Screen | 65304d5 | 2020-10-29 16:42:22 | [diff] [blame] | 462 | print_backend_ = PrintBackend::CreateInstance(locale); |
Alan Screen | 65a6641 | 2022-09-24 02:29:22 | [diff] [blame] | 463 | #if BUILDFLAG(IS_LINUX) |
| 464 | // Test framework already initializes the UI, so this should not go in |
| 465 | // `InitCommon()`. Additionally, low-level Linux UI is not needed when tests |
| 466 | // are using `TestPrintingContext`. |
| 467 | InstantiateLinuxUiDelegate(); |
| 468 | ui::LinuxUi::SetInstance(ui::GetDefaultLinuxUi()); |
Andy Phan | 5f3db32 | 2022-11-11 19:53:02 | [diff] [blame] | 469 | #endif // BUILDFLAG(IS_LINUX) |
Alan Screen | 71ef2a0 | 2022-09-19 23:48:55 | [diff] [blame] | 470 | |
Andy Phan | 5f3db32 | 2022-11-11 19:53:02 | [diff] [blame] | 471 | #if BUILDFLAG(IS_WIN) |
| 472 | InitCommon(locale, std::move(remote)); |
| 473 | #else |
Alan Screen | 71ef2a0 | 2022-09-19 23:48:55 | [diff] [blame] | 474 | InitCommon(locale); |
Andy Phan | 5f3db32 | 2022-11-11 19:53:02 | [diff] [blame] | 475 | #endif // BUILDFLAG(IS_WIN) |
Alan Screen | 65304d5 | 2020-10-29 16:42:22 | [diff] [blame] | 476 | } |
| 477 | |
Alan Screen | 5754f54 | 2021-07-09 00:43:50 | [diff] [blame] | 478 | // TODO(crbug.com/1225111) Do nothing, this is just to assist an idle timeout |
| 479 | // change by providing a low-cost call to ensure it is applied. |
| 480 | void PrintBackendServiceImpl::Poke() {} |
| 481 | |
Alan Screen | 2a5c5426 | 2021-02-11 10:44:29 | [diff] [blame] | 482 | void PrintBackendServiceImpl::EnumeratePrinters( |
| 483 | mojom::PrintBackendService::EnumeratePrintersCallback callback) { |
Alan Screen | 7a30b04 | 2022-09-19 17:51:24 | [diff] [blame] | 484 | DCHECK(print_backend_); |
Alan Screen | 2a5c5426 | 2021-02-11 10:44:29 | [diff] [blame] | 485 | PrinterList printer_list; |
Alan Screen | b92927f | 2022-06-14 21:27:20 | [diff] [blame] | 486 | mojom::ResultCode result = print_backend_->EnumeratePrinters(printer_list); |
Alan Screen | 5ad643b | 2021-05-13 16:40:18 | [diff] [blame] | 487 | if (result != mojom::ResultCode::kSuccess) { |
| 488 | std::move(callback).Run(mojom::PrinterListResult::NewResultCode(result)); |
Alan Screen | 2a5c5426 | 2021-02-11 10:44:29 | [diff] [blame] | 489 | return; |
| 490 | } |
Alan Screen | 5ad643b | 2021-05-13 16:40:18 | [diff] [blame] | 491 | std::move(callback).Run( |
| 492 | mojom::PrinterListResult::NewPrinterList(std::move(printer_list))); |
Alan Screen | 2a5c5426 | 2021-02-11 10:44:29 | [diff] [blame] | 493 | } |
| 494 | |
Alan Screen | 65304d5 | 2020-10-29 16:42:22 | [diff] [blame] | 495 | void PrintBackendServiceImpl::GetDefaultPrinterName( |
| 496 | mojom::PrintBackendService::GetDefaultPrinterNameCallback callback) { |
Alan Screen | 7a30b04 | 2022-09-19 17:51:24 | [diff] [blame] | 497 | DCHECK(print_backend_); |
Alan Screen | 322ed618 | 2021-06-03 15:26:47 | [diff] [blame] | 498 | std::string default_printer; |
| 499 | mojom::ResultCode result = |
| 500 | print_backend_->GetDefaultPrinterName(default_printer); |
| 501 | if (result != mojom::ResultCode::kSuccess) { |
| 502 | std::move(callback).Run( |
| 503 | mojom::DefaultPrinterNameResult::NewResultCode(result)); |
| 504 | return; |
| 505 | } |
| 506 | std::move(callback).Run( |
| 507 | mojom::DefaultPrinterNameResult::NewDefaultPrinterName(default_printer)); |
Alan Screen | 65304d5 | 2020-10-29 16:42:22 | [diff] [blame] | 508 | } |
| 509 | |
Lei Zhang | 3a48a1d | 2023-03-25 02:10:46 | [diff] [blame] | 510 | #if BUILDFLAG(IS_CHROMEOS_ASH) |
Alan Screen | d00ff1d | 2020-12-08 04:17:40 | [diff] [blame] | 511 | void PrintBackendServiceImpl::GetPrinterSemanticCapsAndDefaults( |
| 512 | const std::string& printer_name, |
| 513 | mojom::PrintBackendService::GetPrinterSemanticCapsAndDefaultsCallback |
| 514 | callback) { |
Alan Screen | 7a30b04 | 2022-09-19 17:51:24 | [diff] [blame] | 515 | DCHECK(print_backend_); |
Alan Screen | 366ce3e | 2021-06-16 23:26:04 | [diff] [blame] | 516 | crash_keys_ = std::make_unique<crash_keys::ScopedPrinterInfo>( |
| 517 | print_backend_->GetPrinterDriverInfo(printer_name)); |
| 518 | |
Alan Screen | d00ff1d | 2020-12-08 04:17:40 | [diff] [blame] | 519 | PrinterSemanticCapsAndDefaults printer_caps; |
Alan Screen | 8f86e14 | 2021-05-07 09:52:19 | [diff] [blame] | 520 | const mojom::ResultCode result = |
| 521 | print_backend_->GetPrinterSemanticCapsAndDefaults(printer_name, |
| 522 | &printer_caps); |
| 523 | if (result != mojom::ResultCode::kSuccess) { |
Alan Screen | 5ad643b | 2021-05-13 16:40:18 | [diff] [blame] | 524 | std::move(callback).Run( |
| 525 | mojom::PrinterSemanticCapsAndDefaultsResult::NewResultCode(result)); |
Alan Screen | d00ff1d | 2020-12-08 04:17:40 | [diff] [blame] | 526 | return; |
| 527 | } |
Alan Screen | 5ad643b | 2021-05-13 16:40:18 | [diff] [blame] | 528 | std::move(callback).Run( |
| 529 | mojom::PrinterSemanticCapsAndDefaultsResult::NewPrinterCaps( |
| 530 | std::move(printer_caps))); |
Alan Screen | d00ff1d | 2020-12-08 04:17:40 | [diff] [blame] | 531 | } |
Lei Zhang | 3a48a1d | 2023-03-25 02:10:46 | [diff] [blame] | 532 | #endif // BUILDFLAG(IS_CHROMEOS_ASH) |
Alan Screen | d00ff1d | 2020-12-08 04:17:40 | [diff] [blame] | 533 | |
Alan Screen | c15aea6 | 2021-01-08 03:32:12 | [diff] [blame] | 534 | void PrintBackendServiceImpl::FetchCapabilities( |
| 535 | const std::string& printer_name, |
| 536 | mojom::PrintBackendService::FetchCapabilitiesCallback callback) { |
Alan Screen | 7a30b04 | 2022-09-19 17:51:24 | [diff] [blame] | 537 | DCHECK(print_backend_); |
Alan Screen | 366ce3e | 2021-06-16 23:26:04 | [diff] [blame] | 538 | crash_keys_ = std::make_unique<crash_keys::ScopedPrinterInfo>( |
| 539 | print_backend_->GetPrinterDriverInfo(printer_name)); |
| 540 | |
Alan Screen | c15aea6 | 2021-01-08 03:32:12 | [diff] [blame] | 541 | PrinterBasicInfo printer_info; |
Alan Screen | 8f86e14 | 2021-05-07 09:52:19 | [diff] [blame] | 542 | mojom::ResultCode result = |
Alan Screen | c15aea6 | 2021-01-08 03:32:12 | [diff] [blame] | 543 | print_backend_->GetPrinterBasicInfo(printer_name, &printer_info); |
Alan Screen | 8f86e14 | 2021-05-07 09:52:19 | [diff] [blame] | 544 | if (result != mojom::ResultCode::kSuccess) { |
Alan Screen | 5ad643b | 2021-05-13 16:40:18 | [diff] [blame] | 545 | std::move(callback).Run( |
| 546 | mojom::PrinterCapsAndInfoResult::NewResultCode(result)); |
Alan Screen | c15aea6 | 2021-01-08 03:32:12 | [diff] [blame] | 547 | return; |
| 548 | } |
Lei Zhang | 0ce71b4 | 2023-02-15 23:02:06 | [diff] [blame] | 549 | |
Alan Screen | c15aea6 | 2021-01-08 03:32:12 | [diff] [blame] | 550 | PrinterSemanticCapsAndDefaults caps; |
| 551 | result = |
| 552 | print_backend_->GetPrinterSemanticCapsAndDefaults(printer_name, &caps); |
Alan Screen | 8f86e14 | 2021-05-07 09:52:19 | [diff] [blame] | 553 | if (result != mojom::ResultCode::kSuccess) { |
Alan Screen | 5ad643b | 2021-05-13 16:40:18 | [diff] [blame] | 554 | std::move(callback).Run( |
| 555 | mojom::PrinterCapsAndInfoResult::NewResultCode(result)); |
Alan Screen | c15aea6 | 2021-01-08 03:32:12 | [diff] [blame] | 556 | return; |
| 557 | } |
Lei Zhang | 0ce71b4 | 2023-02-15 23:02:06 | [diff] [blame] | 558 | |
Andy Phan | 65e7356 | 2022-11-02 17:06:47 | [diff] [blame] | 559 | #if BUILDFLAG(IS_WIN) |
| 560 | if (xml_parser_remote_.is_bound() && |
| 561 | base::FeatureList::IsEnabled(features::kReadPrinterCapabilitiesWithXps)) { |
Peter Kasting | f115ece | 2023-08-07 21:09:44 | [diff] [blame] | 562 | ASSIGN_OR_RETURN( |
| 563 | XpsCapabilities xps_capabilities, GetXpsCapabilities(printer_name), |
| 564 | [&](mojom::ResultCode error) { |
| 565 | return std::move(callback).Run( |
| 566 | mojom::PrinterCapsAndInfoResult::NewResultCode(error)); |
| 567 | }); |
Andy Phan | 65e7356 | 2022-11-02 17:06:47 | [diff] [blame] | 568 | |
Peter Kasting | f115ece | 2023-08-07 21:09:44 | [diff] [blame] | 569 | MergeXpsCapabilities(std::move(xps_capabilities), caps); |
Andy Phan | 65e7356 | 2022-11-02 17:06:47 | [diff] [blame] | 570 | } |
| 571 | #endif // BUILDFLAG(IS_WIN) |
Lei Zhang | 0ce71b4 | 2023-02-15 23:02:06 | [diff] [blame] | 572 | |
| 573 | #if BUILDFLAG(IS_MAC) |
| 574 | { |
| 575 | // Blocking is needed here for when macOS reads paper sizes from file. |
| 576 | // |
| 577 | // Fetching capabilities in the browser process happens from the thread |
| 578 | // pool with the MayBlock() trait for macOS. However this call can also |
| 579 | // run from a utility process's main thread where blocking is not |
| 580 | // implicitly allowed. In order to preserve ordering, the utility process |
| 581 | // must process this synchronously by blocking. |
| 582 | // |
| 583 | // TODO(crbug.com/1163635): Investigate whether utility process main |
| 584 | // thread should be allowed to block like in-process workers are. |
| 585 | base::ScopedAllowBlocking allow_blocking; |
| 586 | caps.user_defined_papers = GetMacCustomPaperSizes(); |
| 587 | } |
| 588 | #endif |
| 589 | |
| 590 | mojom::PrinterCapsAndInfoPtr caps_and_info = |
| 591 | mojom::PrinterCapsAndInfo::New(std::move(printer_info), std::move(caps)); |
Alan Screen | 5ad643b | 2021-05-13 16:40:18 | [diff] [blame] | 592 | std::move(callback).Run( |
| 593 | mojom::PrinterCapsAndInfoResult::NewPrinterCapsAndInfo( |
| 594 | std::move(caps_and_info))); |
Alan Screen | c15aea6 | 2021-01-08 03:32:12 | [diff] [blame] | 595 | } |
| 596 | |
Alan Screen | 9fcfd372 | 2023-04-24 20:29:49 | [diff] [blame] | 597 | #if BUILDFLAG(IS_WIN) |
| 598 | void PrintBackendServiceImpl::GetPaperPrintableArea( |
| 599 | const std::string& printer_name, |
| 600 | const PrintSettings::RequestedMedia& media, |
| 601 | mojom::PrintBackendService::GetPaperPrintableAreaCallback callback) { |
| 602 | CHECK(print_backend_); |
| 603 | crash_keys_ = std::make_unique<crash_keys::ScopedPrinterInfo>( |
| 604 | print_backend_->GetPrinterDriverInfo(printer_name)); |
| 605 | |
| 606 | absl::optional<gfx::Rect> printable_area_um = |
| 607 | print_backend_->GetPaperPrintableArea(printer_name, media.vendor_id, |
| 608 | media.size_microns); |
| 609 | std::move(callback).Run(printable_area_um.value_or(gfx::Rect())); |
| 610 | } |
| 611 | #endif |
| 612 | |
Alan Screen | 74c6632f | 2023-02-24 22:40:07 | [diff] [blame] | 613 | void PrintBackendServiceImpl::EstablishPrintingContext(uint32_t context_id |
| 614 | #if BUILDFLAG(ENABLE_OOP_BASIC_PRINT_DIALOG) |
| 615 | , |
| 616 | uint32_t parent_window_id |
| 617 | #endif |
| 618 | ) { |
| 619 | auto context_container = std::make_unique<ContextContainer>(); |
| 620 | |
| 621 | context_container->delegate = CreatePrintingContextDelegate(); |
| 622 | #if BUILDFLAG(ENABLE_OOP_BASIC_PRINT_DIALOG) |
| 623 | context_container->delegate->SetParentWindow(parent_window_id); |
| 624 | #endif |
| 625 | |
| 626 | context_container->context = PrintingContext::Create( |
Alan Screen | 96a12395 | 2023-11-06 21:53:16 | [diff] [blame^] | 627 | context_container->delegate.get(), |
| 628 | PrintingContext::ProcessBehavior::kOopEnabledPerformSystemCalls); |
Alan Screen | 74c6632f | 2023-02-24 22:40:07 | [diff] [blame] | 629 | |
| 630 | bool inserted = persistent_printing_contexts_ |
| 631 | .insert({context_id, std::move(context_container)}) |
| 632 | .second; |
| 633 | DCHECK(inserted); |
| 634 | } |
| 635 | |
Alan Screen | fa756fb | 2022-02-15 00:02:24 | [diff] [blame] | 636 | void PrintBackendServiceImpl::UseDefaultSettings( |
Alan Screen | 9aa4614 | 2023-03-24 19:56:25 | [diff] [blame] | 637 | uint32_t context_id, |
Alan Screen | fa756fb | 2022-02-15 00:02:24 | [diff] [blame] | 638 | mojom::PrintBackendService::UseDefaultSettingsCallback callback) { |
Alan Screen | 9aa4614 | 2023-03-24 19:56:25 | [diff] [blame] | 639 | PrintingContext* context = GetPrintingContext(context_id); |
| 640 | CHECK(context) << "No context found for id " << context_id; |
| 641 | mojom::ResultCode result = context->UseDefaultSettings(); |
Alan Screen | fa756fb | 2022-02-15 00:02:24 | [diff] [blame] | 642 | if (result != mojom::ResultCode::kSuccess) { |
Alan Screen | 01cd3bc | 2022-02-15 02:38:47 | [diff] [blame] | 643 | DLOG(ERROR) << "Failure getting default settings of default printer, " |
| 644 | << "error: " << result; |
Alan Screen | 9aa4614 | 2023-03-24 19:56:25 | [diff] [blame] | 645 | persistent_printing_contexts_.erase(context_id); |
Alan Screen | fa756fb | 2022-02-15 00:02:24 | [diff] [blame] | 646 | std::move(callback).Run(mojom::PrintSettingsResult::NewResultCode(result)); |
| 647 | return; |
| 648 | } |
| 649 | std::move(callback).Run(mojom::PrintSettingsResult::NewSettings( |
| 650 | *context->TakeAndResetSettings())); |
| 651 | } |
| 652 | |
Alan Screen | 49f16ac | 2023-01-13 05:38:34 | [diff] [blame] | 653 | #if BUILDFLAG(ENABLE_OOP_BASIC_PRINT_DIALOG) |
Alan Screen | c336bd86 | 2022-03-22 18:19:41 | [diff] [blame] | 654 | void PrintBackendServiceImpl::AskUserForSettings( |
Alan Screen | 9aa4614 | 2023-03-24 19:56:25 | [diff] [blame] | 655 | uint32_t context_id, |
Alan Screen | c336bd86 | 2022-03-22 18:19:41 | [diff] [blame] | 656 | int max_pages, |
| 657 | bool has_selection, |
| 658 | bool is_scripted, |
| 659 | mojom::PrintBackendService::AskUserForSettingsCallback callback) { |
Alan Screen | 9aa4614 | 2023-03-24 19:56:25 | [diff] [blame] | 660 | // Safe to use `base::Unretained(this)` because `this` outlives the async |
| 661 | // call and callback. The entire service process goes away when `this` |
| 662 | // lifetime expires. |
| 663 | PrintingContext* context = GetPrintingContext(context_id); |
| 664 | CHECK(context) << "No context found for id " << context_id; |
| 665 | context->AskUserForSettings( |
Alan Screen | c336bd86 | 2022-03-22 18:19:41 | [diff] [blame] | 666 | max_pages, has_selection, is_scripted, |
Alan Screen | 9aa4614 | 2023-03-24 19:56:25 | [diff] [blame] | 667 | base::BindOnce(&PrintBackendServiceImpl::OnDidAskUserForSettings, |
| 668 | base::Unretained(this), context_id, std::move(callback))); |
Alan Screen | c336bd86 | 2022-03-22 18:19:41 | [diff] [blame] | 669 | } |
Alan Screen | 49f16ac | 2023-01-13 05:38:34 | [diff] [blame] | 670 | #endif // BUILDFLAG(ENABLE_OOP_BASIC_PRINT_DIALOG) |
Alan Screen | c336bd86 | 2022-03-22 18:19:41 | [diff] [blame] | 671 | |
Alan Screen | 59767538 | 2021-09-11 05:14:53 | [diff] [blame] | 672 | void PrintBackendServiceImpl::UpdatePrintSettings( |
Alan Screen | fd714c1 | 2023-03-24 21:03:29 | [diff] [blame] | 673 | uint32_t context_id, |
Lei Zhang | 15e92b9 | 2022-04-12 20:06:23 | [diff] [blame] | 674 | base::Value::Dict job_settings, |
Alan Screen | 59767538 | 2021-09-11 05:14:53 | [diff] [blame] | 675 | mojom::PrintBackendService::UpdatePrintSettingsCallback callback) { |
Alan Screen | 7a30b04 | 2022-09-19 17:51:24 | [diff] [blame] | 676 | DCHECK(print_backend_); |
Alan Screen | 59767538 | 2021-09-11 05:14:53 | [diff] [blame] | 677 | |
Lei Zhang | 15e92b9 | 2022-04-12 20:06:23 | [diff] [blame] | 678 | const std::string* printer_name = job_settings.FindString(kSettingDeviceName); |
Alan Screen | 7a30b04 | 2022-09-19 17:51:24 | [diff] [blame] | 679 | DCHECK(printer_name); |
Alan Screen | 59767538 | 2021-09-11 05:14:53 | [diff] [blame] | 680 | |
| 681 | crash_keys_ = std::make_unique<crash_keys::ScopedPrinterInfo>( |
Lei Zhang | 15e92b9 | 2022-04-12 20:06:23 | [diff] [blame] | 682 | print_backend_->GetPrinterDriverInfo(*printer_name)); |
Alan Screen | 59767538 | 2021-09-11 05:14:53 | [diff] [blame] | 683 | |
Andrew Rayskiy | 16f8cab | 2022-12-15 16:28:33 | [diff] [blame] | 684 | #if BUILDFLAG(IS_LINUX) && BUILDFLAG(USE_CUPS) |
Alan Screen | 59767538 | 2021-09-11 05:14:53 | [diff] [blame] | 685 | // Try to fill in advanced settings based upon basic info options. |
| 686 | PrinterBasicInfo basic_info; |
Lei Zhang | 15e92b9 | 2022-04-12 20:06:23 | [diff] [blame] | 687 | if (print_backend_->GetPrinterBasicInfo(*printer_name, &basic_info) == |
Alan Screen | 59767538 | 2021-09-11 05:14:53 | [diff] [blame] | 688 | mojom::ResultCode::kSuccess) { |
Lei Zhang | 15e92b9 | 2022-04-12 20:06:23 | [diff] [blame] | 689 | base::Value::Dict advanced_settings; |
Alan Screen | 59767538 | 2021-09-11 05:14:53 | [diff] [blame] | 690 | for (const auto& pair : basic_info.options) |
Lei Zhang | 15e92b9 | 2022-04-12 20:06:23 | [diff] [blame] | 691 | advanced_settings.Set(pair.first, pair.second); |
Alan Screen | 59767538 | 2021-09-11 05:14:53 | [diff] [blame] | 692 | |
Lei Zhang | 15e92b9 | 2022-04-12 20:06:23 | [diff] [blame] | 693 | job_settings.Set(kSettingAdvancedSettings, std::move(advanced_settings)); |
Alan Screen | 59767538 | 2021-09-11 05:14:53 | [diff] [blame] | 694 | } |
Andrew Rayskiy | 16f8cab | 2022-12-15 16:28:33 | [diff] [blame] | 695 | #endif // BUILDFLAG(IS_LINUX) && BUILDFLAG(USE_CUPS) |
Alan Screen | 59767538 | 2021-09-11 05:14:53 | [diff] [blame] | 696 | |
Alan Screen | fd714c1 | 2023-03-24 21:03:29 | [diff] [blame] | 697 | PrintingContext* context = GetPrintingContext(context_id); |
| 698 | CHECK(context) << "No context found for id " << context_id; |
Alan Screen | 202e49a | 2021-09-21 06:34:28 | [diff] [blame] | 699 | mojom::ResultCode result = |
Lei Zhang | 4bc85297 | 2022-04-12 20:22:19 | [diff] [blame] | 700 | context->UpdatePrintSettings(std::move(job_settings)); |
Alan Screen | 59767538 | 2021-09-11 05:14:53 | [diff] [blame] | 701 | |
Alan Screen | 202e49a | 2021-09-21 06:34:28 | [diff] [blame] | 702 | if (result != mojom::ResultCode::kSuccess) { |
Alan Screen | 39bc763 | 2023-04-25 18:04:58 | [diff] [blame] | 703 | persistent_printing_contexts_.erase(context_id); |
Alan Screen | 202e49a | 2021-09-21 06:34:28 | [diff] [blame] | 704 | std::move(callback).Run(mojom::PrintSettingsResult::NewResultCode(result)); |
Alan Screen | 59767538 | 2021-09-11 05:14:53 | [diff] [blame] | 705 | return; |
| 706 | } |
| 707 | |
Alan Screen | fd714c1 | 2023-03-24 21:03:29 | [diff] [blame] | 708 | std::move(callback).Run( |
| 709 | mojom::PrintSettingsResult::NewSettings(context->settings())); |
Alan Screen | 59767538 | 2021-09-11 05:14:53 | [diff] [blame] | 710 | } |
| 711 | |
Alan Screen | 2166ab1 | 2021-09-01 23:09:25 | [diff] [blame] | 712 | void PrintBackendServiceImpl::StartPrinting( |
Alan Screen | 9aa4614 | 2023-03-24 19:56:25 | [diff] [blame] | 713 | uint32_t context_id, |
Alan Screen | 2166ab1 | 2021-09-01 23:09:25 | [diff] [blame] | 714 | int document_cookie, |
| 715 | const std::u16string& document_name, |
Alan Screen | fd714c1 | 2023-03-24 21:03:29 | [diff] [blame] | 716 | #if !BUILDFLAG(ENABLE_OOP_BASIC_PRINT_DIALOG) |
| 717 | const absl::optional<PrintSettings>& settings, |
| 718 | #endif |
Alan Screen | 2166ab1 | 2021-09-01 23:09:25 | [diff] [blame] | 719 | mojom::PrintBackendService::StartPrintingCallback callback) { |
Andrew Rayskiy | 16f8cab | 2022-12-15 16:28:33 | [diff] [blame] | 720 | #if BUILDFLAG(IS_CHROMEOS) && BUILDFLAG(USE_CUPS) |
Alan Screen | 2166ab1 | 2021-09-01 23:09:25 | [diff] [blame] | 721 | CupsConnectionPool* connection_pool = CupsConnectionPool::GetInstance(); |
| 722 | if (connection_pool) { |
| 723 | // If a pool exists then this document can only proceed with printing if |
| 724 | // there is a connection available for use by a `PrintingContext`. |
| 725 | if (!connection_pool->IsConnectionAvailable()) { |
| 726 | // This document has to wait until a connection becomes available. Hold |
| 727 | // off on issuing the callback. |
| 728 | // TODO(crbug.com/809738) Place this in a queue of waiting jobs. |
| 729 | DLOG(ERROR) << "Need queue for print jobs awaiting a connection"; |
| 730 | std::move(callback).Run(mojom::ResultCode::kFailed); |
| 731 | return; |
| 732 | } |
| 733 | } |
| 734 | #endif |
| 735 | |
Alan Screen | 9aa4614 | 2023-03-24 19:56:25 | [diff] [blame] | 736 | // This job takes ownership of this printing context and associates it with |
| 737 | // the document. |
| 738 | auto item = persistent_printing_contexts_.find(context_id); |
| 739 | CHECK(item != persistent_printing_contexts_.end()) |
| 740 | << "No context found for id " << context_id; |
| 741 | std::unique_ptr<ContextContainer> context_container = std::move(item->second); |
| 742 | persistent_printing_contexts_.erase(item); |
| 743 | |
Alan Screen | fd714c1 | 2023-03-24 21:03:29 | [diff] [blame] | 744 | #if !BUILDFLAG(ENABLE_OOP_BASIC_PRINT_DIALOG) |
| 745 | if (settings) { |
| 746 | // Apply the settings from the in-browser system dialog to the context. |
Alan Screen | 2750f0905 | 2023-09-01 22:32:33 | [diff] [blame] | 747 | context_container->context->SetPrintSettings(*settings); |
Alan Screen | fd714c1 | 2023-03-24 21:03:29 | [diff] [blame] | 748 | } |
| 749 | #endif |
| 750 | |
Alan Screen | 4b7ebf78 | 2021-12-07 19:10:02 | [diff] [blame] | 751 | // Save all the document settings for use through the print job, until the |
| 752 | // time that this document can complete printing. Track the order of |
| 753 | // received documents with position in `documents_`. |
| 754 | auto document = base::MakeRefCounted<PrintedDocument>( |
Alan Screen | fd714c1 | 2023-03-24 21:03:29 | [diff] [blame] | 755 | std::make_unique<PrintSettings>(context_container->context->settings()), |
| 756 | document_name, document_cookie); |
Alan Screen | cb6421f | 2021-12-09 21:54:18 | [diff] [blame] | 757 | base::SequenceBound<DocumentContainer> document_container( |
Alan Screen | 9aa4614 | 2023-03-24 19:56:25 | [diff] [blame] | 758 | GetPrintingTaskRunner(), std::move(context_container->delegate), |
Alan Screen | fd714c1 | 2023-03-24 21:03:29 | [diff] [blame] | 759 | std::move(context_container->context), document); |
Alan Screen | cb6421f | 2021-12-09 21:54:18 | [diff] [blame] | 760 | documents_.push_back(std::make_unique<DocumentHelper>( |
| 761 | document_cookie, std::move(document_container), std::move(callback))); |
| 762 | DocumentHelper& document_helper = *documents_.back(); |
Alan Screen | 4b7ebf78 | 2021-12-07 19:10:02 | [diff] [blame] | 763 | |
Alan Screen | cb6421f | 2021-12-09 21:54:18 | [diff] [blame] | 764 | // Safe to use `base::Unretained(this)` because `this` outlives the async |
| 765 | // call and callback. The entire service process goes away when `this` |
| 766 | // lifetime expires. |
| 767 | document_helper.document_container() |
| 768 | .AsyncCall(&DocumentContainer::StartPrintingReadyDocument) |
| 769 | .Then(base::BindOnce( |
| 770 | &PrintBackendServiceImpl::OnDidStartPrintingReadyDocument, |
| 771 | base::Unretained(this), std::ref(document_helper))); |
Alan Screen | 2166ab1 | 2021-09-01 23:09:25 | [diff] [blame] | 772 | } |
| 773 | |
Xiaohan Wang | d4bc274 | 2022-01-15 19:48:55 | [diff] [blame] | 774 | #if BUILDFLAG(IS_WIN) |
Alan Screen | 61e1a37 | 2021-12-10 03:02:34 | [diff] [blame] | 775 | void PrintBackendServiceImpl::RenderPrintedPage( |
| 776 | int32_t document_cookie, |
| 777 | uint32_t page_index, |
| 778 | mojom::MetafileDataType page_data_type, |
| 779 | base::ReadOnlySharedMemoryRegion serialized_page, |
| 780 | const gfx::Size& page_size, |
| 781 | const gfx::Rect& page_content_rect, |
| 782 | float shrink_factor, |
| 783 | mojom::PrintBackendService::RenderPrintedPageCallback callback) { |
Alan Screen | 61e1a37 | 2021-12-10 03:02:34 | [diff] [blame] | 784 | DocumentHelper* document_helper = GetDocumentHelper(document_cookie); |
Alan Screen | 7a30b04 | 2022-09-19 17:51:24 | [diff] [blame] | 785 | DCHECK(document_helper); |
Alan Screen | 61e1a37 | 2021-12-10 03:02:34 | [diff] [blame] | 786 | |
Alan Screen | 61e1a37 | 2021-12-10 03:02:34 | [diff] [blame] | 787 | document_helper->document_container() |
| 788 | .AsyncCall(&DocumentContainer::DoRenderPrintedPage) |
| 789 | .WithArgs(page_index, page_data_type, std::move(serialized_page), |
| 790 | page_size, page_content_rect, shrink_factor) |
Alan Screen | ca9c993 | 2022-11-21 19:31:26 | [diff] [blame] | 791 | .Then(std::move(callback)); |
Alan Screen | 61e1a37 | 2021-12-10 03:02:34 | [diff] [blame] | 792 | } |
Xiaohan Wang | d4bc274 | 2022-01-15 19:48:55 | [diff] [blame] | 793 | #endif // BUILDFLAG(IS_WIN) |
Alan Screen | 61e1a37 | 2021-12-10 03:02:34 | [diff] [blame] | 794 | |
Alan Screen | bd4f851a | 2022-03-29 22:53:05 | [diff] [blame] | 795 | void PrintBackendServiceImpl::RenderPrintedDocument( |
| 796 | int32_t document_cookie, |
Alan Screen | a6cebd8 | 2022-11-03 19:56:29 | [diff] [blame] | 797 | uint32_t page_count, |
Alan Screen | bd4f851a | 2022-03-29 22:53:05 | [diff] [blame] | 798 | mojom::MetafileDataType data_type, |
| 799 | base::ReadOnlySharedMemoryRegion serialized_document, |
| 800 | mojom::PrintBackendService::RenderPrintedDocumentCallback callback) { |
Alan Screen | bd4f851a | 2022-03-29 22:53:05 | [diff] [blame] | 801 | DocumentHelper* document_helper = GetDocumentHelper(document_cookie); |
Alan Screen | 7a30b04 | 2022-09-19 17:51:24 | [diff] [blame] | 802 | DCHECK(document_helper); |
Alan Screen | bd4f851a | 2022-03-29 22:53:05 | [diff] [blame] | 803 | |
Alan Screen | bd4f851a | 2022-03-29 22:53:05 | [diff] [blame] | 804 | document_helper->document_container() |
| 805 | .AsyncCall(&DocumentContainer::DoRenderPrintedDocument) |
Alan Screen | a6cebd8 | 2022-11-03 19:56:29 | [diff] [blame] | 806 | .WithArgs(page_count, data_type, std::move(serialized_document)) |
Alan Screen | ca9c993 | 2022-11-21 19:31:26 | [diff] [blame] | 807 | .Then(std::move(callback)); |
Alan Screen | bd4f851a | 2022-03-29 22:53:05 | [diff] [blame] | 808 | } |
| 809 | |
Alan Screen | 6bf1d5fb | 2022-02-08 15:51:14 | [diff] [blame] | 810 | void PrintBackendServiceImpl::DocumentDone( |
| 811 | int document_cookie, |
| 812 | mojom::PrintBackendService::DocumentDoneCallback callback) { |
Alan Screen | 6bf1d5fb | 2022-02-08 15:51:14 | [diff] [blame] | 813 | DocumentHelper* document_helper = GetDocumentHelper(document_cookie); |
Alan Screen | 7a30b04 | 2022-09-19 17:51:24 | [diff] [blame] | 814 | DCHECK(document_helper); |
Alan Screen | 6bf1d5fb | 2022-02-08 15:51:14 | [diff] [blame] | 815 | |
| 816 | // Safe to use `base::Unretained(this)` because `this` outlives the async |
| 817 | // call and callback. The entire service process goes away when `this` |
| 818 | // lifetime expires. |
| 819 | document_helper->document_container() |
| 820 | .AsyncCall(&DocumentContainer::DoDocumentDone) |
| 821 | .Then(base::BindOnce(&PrintBackendServiceImpl::OnDidDocumentDone, |
| 822 | base::Unretained(this), std::ref(*document_helper), |
| 823 | std::move(callback))); |
| 824 | } |
| 825 | |
Alan Screen | 2a7dcc9 | 2022-11-17 04:52:01 | [diff] [blame] | 826 | void PrintBackendServiceImpl::Cancel( |
| 827 | int document_cookie, |
| 828 | mojom::PrintBackendService::CancelCallback callback) { |
| 829 | DCHECK(print_backend_); |
| 830 | DocumentHelper* document_helper = GetDocumentHelper(document_cookie); |
| 831 | DCHECK(document_helper); |
| 832 | |
| 833 | // Safe to use `base::Unretained(this)` because `this` outlives the async |
| 834 | // call and callback. The entire service process goes away when `this` |
| 835 | // lifetime expires. |
| 836 | document_helper->document_container() |
| 837 | .AsyncCall(&DocumentContainer::DoCancel) |
| 838 | .Then(base::BindOnce(&PrintBackendServiceImpl::OnDidCancel, |
| 839 | base::Unretained(this), std::ref(*document_helper), |
| 840 | std::move(callback))); |
| 841 | } |
| 842 | |
Alan Screen | 9aa4614 | 2023-03-24 19:56:25 | [diff] [blame] | 843 | #if BUILDFLAG(ENABLE_OOP_BASIC_PRINT_DIALOG) |
| 844 | void PrintBackendServiceImpl::OnDidAskUserForSettings( |
| 845 | uint32_t context_id, |
| 846 | mojom::PrintBackendService::AskUserForSettingsCallback callback, |
| 847 | mojom::ResultCode result) { |
| 848 | auto* context = GetPrintingContext(context_id); |
| 849 | DCHECK(context); |
| 850 | if (result != mojom::ResultCode::kSuccess) { |
| 851 | DLOG(ERROR) << "Did not get user settings, error: " << result; |
| 852 | persistent_printing_contexts_.erase(context_id); |
| 853 | std::move(callback).Run(mojom::PrintSettingsResult::NewResultCode(result)); |
| 854 | return; |
| 855 | } |
| 856 | std::move(callback).Run(mojom::PrintSettingsResult::NewSettings( |
| 857 | *context->TakeAndResetSettings())); |
| 858 | } |
| 859 | #endif // BUILDFLAG(ENABLE_OOP_BASIC_PRINT_DIALOG) |
| 860 | |
Alan Screen | 2166ab1 | 2021-09-01 23:09:25 | [diff] [blame] | 861 | void PrintBackendServiceImpl::OnDidStartPrintingReadyDocument( |
Alan Screen | cb6421f | 2021-12-09 21:54:18 | [diff] [blame] | 862 | DocumentHelper& document_helper, |
Alan Screen | 2166ab1 | 2021-09-01 23:09:25 | [diff] [blame] | 863 | mojom::ResultCode result) { |
Alan Screen | 61e1a37 | 2021-12-10 03:02:34 | [diff] [blame] | 864 | DCHECK_CALLED_ON_VALID_SEQUENCE(main_sequence_checker_); |
Alan Screen | cb6421f | 2021-12-09 21:54:18 | [diff] [blame] | 865 | document_helper.TakeStartPrintingCallback().Run(result); |
Alan Screen | bd4f851a | 2022-03-29 22:53:05 | [diff] [blame] | 866 | } |
| 867 | |
Alan Screen | 6bf1d5fb | 2022-02-08 15:51:14 | [diff] [blame] | 868 | void PrintBackendServiceImpl::OnDidDocumentDone( |
| 869 | DocumentHelper& document_helper, |
| 870 | mojom::PrintBackendService::DocumentDoneCallback callback, |
| 871 | mojom::ResultCode result) { |
| 872 | std::move(callback).Run(result); |
| 873 | |
Alan Screen | ca9c993 | 2022-11-21 19:31:26 | [diff] [blame] | 874 | // The service expects that the calling process will call `Cancel()` if there |
| 875 | // are any errors during printing. |
| 876 | if (result == mojom::ResultCode::kSuccess) { |
| 877 | // All complete for this document. |
| 878 | RemoveDocumentHelper(document_helper); |
| 879 | } |
Alan Screen | 6bf1d5fb | 2022-02-08 15:51:14 | [diff] [blame] | 880 | } |
| 881 | |
Alan Screen | 2a7dcc9 | 2022-11-17 04:52:01 | [diff] [blame] | 882 | void PrintBackendServiceImpl::OnDidCancel( |
| 883 | DocumentHelper& document_helper, |
| 884 | mojom::PrintBackendService::CancelCallback callback) { |
| 885 | std::move(callback).Run(); |
| 886 | |
| 887 | // Do nothing more with this document. |
| 888 | RemoveDocumentHelper(document_helper); |
| 889 | } |
| 890 | |
Alan Screen | 74c6632f | 2023-02-24 22:40:07 | [diff] [blame] | 891 | std::unique_ptr<PrintBackendServiceImpl::PrintingContextDelegate> |
| 892 | PrintBackendServiceImpl::CreatePrintingContextDelegate() { |
| 893 | auto context_delegate = std::make_unique<PrintingContextDelegate>(); |
| 894 | context_delegate->SetAppLocale(locale_); |
| 895 | return context_delegate; |
| 896 | } |
| 897 | |
Alan Screen | 9aa4614 | 2023-03-24 19:56:25 | [diff] [blame] | 898 | PrintingContext* PrintBackendServiceImpl::GetPrintingContext( |
| 899 | uint32_t context_id) { |
| 900 | DCHECK_CALLED_ON_VALID_SEQUENCE(main_sequence_checker_); |
| 901 | auto item = persistent_printing_contexts_.find(context_id); |
| 902 | if (item == persistent_printing_contexts_.end()) { |
| 903 | return nullptr; |
| 904 | } |
| 905 | return item->second->context.get(); |
| 906 | } |
| 907 | |
Alan Screen | 61e1a37 | 2021-12-10 03:02:34 | [diff] [blame] | 908 | PrintBackendServiceImpl::DocumentHelper* |
| 909 | PrintBackendServiceImpl::GetDocumentHelper(int document_cookie) { |
| 910 | DCHECK_CALLED_ON_VALID_SEQUENCE(main_sequence_checker_); |
| 911 | |
| 912 | // Most new calls are expected to be relevant to the most recently added |
| 913 | // document, which would be at the end of the list. So search the list |
| 914 | // backwards to hopefully reduce the time to find the document. |
Victor Hugo Vianna Silva | 08f4b00 | 2022-01-17 13:20:04 | [diff] [blame] | 915 | for (const std::unique_ptr<DocumentHelper>& helper : |
| 916 | base::Reversed(documents_)) { |
Alan Screen | 61e1a37 | 2021-12-10 03:02:34 | [diff] [blame] | 917 | if (helper->document_cookie() == document_cookie) { |
Victor Hugo Vianna Silva | 08f4b00 | 2022-01-17 13:20:04 | [diff] [blame] | 918 | return helper.get(); |
Alan Screen | 61e1a37 | 2021-12-10 03:02:34 | [diff] [blame] | 919 | } |
| 920 | } |
| 921 | return nullptr; |
| 922 | } |
| 923 | |
| 924 | void PrintBackendServiceImpl::RemoveDocumentHelper( |
| 925 | DocumentHelper& document_helper) { |
| 926 | DCHECK_CALLED_ON_VALID_SEQUENCE(main_sequence_checker_); |
| 927 | |
| 928 | // Must search forwards because std::vector::erase() doesn't work with a |
| 929 | // reverse iterator. |
Alan Screen | cb6421f | 2021-12-09 21:54:18 | [diff] [blame] | 930 | int cookie = document_helper.document_cookie(); |
| 931 | auto item = |
Peter Kasting | 73f230d0 | 2022-09-17 22:29:30 | [diff] [blame] | 932 | base::ranges::find(documents_, cookie, &DocumentHelper::document_cookie); |
Alan Screen | 2166ab1 | 2021-09-01 23:09:25 | [diff] [blame] | 933 | DCHECK(item != documents_.end()) |
Alan Screen | cb6421f | 2021-12-09 21:54:18 | [diff] [blame] | 934 | << "Document " << cookie << " to be deleted not found"; |
Alan Screen | 2166ab1 | 2021-09-01 23:09:25 | [diff] [blame] | 935 | documents_.erase(item); |
| 936 | |
| 937 | // TODO(crbug.com/809738) This releases a connection; try to start the |
| 938 | // next job waiting to be started (if any). |
| 939 | } |
| 940 | |
Andy Phan | 65e7356 | 2022-11-02 17:06:47 | [diff] [blame] | 941 | #if BUILDFLAG(IS_WIN) |
| 942 | base::expected<XpsCapabilities, mojom::ResultCode> |
| 943 | PrintBackendServiceImpl::GetXpsCapabilities(const std::string& printer_name) { |
Peter Kasting | f115ece | 2023-08-07 21:09:44 | [diff] [blame] | 944 | ASSIGN_OR_RETURN( |
| 945 | std::string xml, |
| 946 | print_backend_->GetXmlPrinterCapabilitiesForXpsDriver(printer_name), |
| 947 | [&](mojom::ResultCode error) { |
| 948 | DLOG(ERROR) << "Failure getting XPS capabilities of printer " |
| 949 | << printer_name << ", error: " << error; |
| 950 | return error; |
| 951 | }); |
Andy Phan | 65e7356 | 2022-11-02 17:06:47 | [diff] [blame] | 952 | |
| 953 | mojom::PrinterCapabilitiesValueResultPtr value_result; |
Peter Kasting | f115ece | 2023-08-07 21:09:44 | [diff] [blame] | 954 | xml_parser_remote_->ParseXmlForPrinterCapabilities(xml, &value_result); |
Andy Phan | 65e7356 | 2022-11-02 17:06:47 | [diff] [blame] | 955 | if (value_result->is_result_code()) { |
| 956 | DLOG(ERROR) << "Failure parsing XML of XPS capabilities of printer " |
Peter Kasting | 1841e69 | 2023-04-24 19:50:52 | [diff] [blame] | 957 | << printer_name |
| 958 | << ", error: " << value_result->get_result_code(); |
Andy Phan | 65e7356 | 2022-11-02 17:06:47 | [diff] [blame] | 959 | return base::unexpected(value_result->get_result_code()); |
| 960 | } |
| 961 | |
Peter Kasting | a7066c6 | 2023-04-25 02:39:31 | [diff] [blame] | 962 | return ParseValueForXpsPrinterCapabilities(value_result->get_capabilities()) |
| 963 | .transform_error([&](mojom::ResultCode code) { |
| 964 | DLOG(ERROR) << "Failure parsing value of XPS capabilities of printer " |
| 965 | << printer_name << ", error: " << code; |
| 966 | return code; |
| 967 | }); |
Andy Phan | 65e7356 | 2022-11-02 17:06:47 | [diff] [blame] | 968 | } |
| 969 | #endif // BUILDFLAG(IS_WIN) |
| 970 | |
Alan Screen | 65304d5 | 2020-10-29 16:42:22 | [diff] [blame] | 971 | } // namespace printing |