Alan Screen | f2fbeb7a | 2021-08-28 05:27:38 | [diff] [blame] | 1 | // Copyright 2021 The Chromium Authors. All rights reserved. |
| 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 "printing/test_printing_context.h" |
| 6 | |
| 7 | #include <memory> |
| 8 | #include <utility> |
| 9 | |
| 10 | #include "base/check.h" |
| 11 | #include "base/containers/flat_map.h" |
| 12 | #include "base/notreached.h" |
| 13 | #include "base/strings/utf_string_conversions.h" |
Alan Screen | 59767538 | 2021-09-11 05:14:53 | [diff] [blame] | 14 | #include "build/build_config.h" |
Alan Screen | 202e49a | 2021-09-21 06:34:28 | [diff] [blame] | 15 | #include "printing/mojom/print.mojom.h" |
Alan Screen | f2fbeb7a | 2021-08-28 05:27:38 | [diff] [blame] | 16 | #include "printing/print_settings.h" |
| 17 | #include "printing/printing_context.h" |
| 18 | #include "ui/gfx/geometry/size.h" |
| 19 | |
| 20 | namespace printing { |
| 21 | |
| 22 | TestPrintingContextDelegate::TestPrintingContextDelegate() = default; |
| 23 | |
| 24 | TestPrintingContextDelegate::~TestPrintingContextDelegate() = default; |
| 25 | |
| 26 | gfx::NativeView TestPrintingContextDelegate::GetParentView() { |
| 27 | return nullptr; |
| 28 | } |
| 29 | |
| 30 | std::string TestPrintingContextDelegate::GetAppLocale() { |
| 31 | return std::string(); |
| 32 | } |
| 33 | |
| 34 | TestPrintingContext::TestPrintingContext(Delegate* delegate) |
| 35 | : PrintingContext(delegate) {} |
| 36 | |
| 37 | TestPrintingContext::~TestPrintingContext() = default; |
| 38 | |
| 39 | void TestPrintingContext::SetDeviceSettings( |
| 40 | const std::string& device_name, |
| 41 | std::unique_ptr<PrintSettings> settings) { |
| 42 | device_settings_.emplace(device_name, std::move(settings)); |
| 43 | } |
| 44 | |
| 45 | void TestPrintingContext::AskUserForSettings(int max_pages, |
| 46 | bool has_selection, |
| 47 | bool is_scripted, |
| 48 | PrintSettingsCallback callback) { |
| 49 | NOTIMPLEMENTED(); |
| 50 | } |
| 51 | |
Alan Screen | 202e49a | 2021-09-21 06:34:28 | [diff] [blame] | 52 | mojom::ResultCode TestPrintingContext::UseDefaultSettings() { |
Alan Screen | f2fbeb7a | 2021-08-28 05:27:38 | [diff] [blame] | 53 | NOTIMPLEMENTED(); |
Alan Screen | 202e49a | 2021-09-21 06:34:28 | [diff] [blame] | 54 | return mojom::ResultCode::kFailed; |
Alan Screen | f2fbeb7a | 2021-08-28 05:27:38 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | gfx::Size TestPrintingContext::GetPdfPaperSizeDeviceUnits() { |
| 58 | NOTIMPLEMENTED(); |
| 59 | return gfx::Size(); |
| 60 | } |
| 61 | |
Alan Screen | 202e49a | 2021-09-21 06:34:28 | [diff] [blame] | 62 | mojom::ResultCode TestPrintingContext::UpdatePrinterSettings( |
Lei Zhang | a3fdad44 | 2021-11-15 23:55:33 | [diff] [blame] | 63 | const PrinterSettings& printer_settings) { |
Alan Screen | f2fbeb7a | 2021-08-28 05:27:38 | [diff] [blame] | 64 | DCHECK(!in_print_job_); |
Lei Zhang | a3fdad44 | 2021-11-15 23:55:33 | [diff] [blame] | 65 | #if defined(OS_MAC) |
| 66 | DCHECK(!printer_settings.external_preview) << "Not implemented"; |
| 67 | #endif |
| 68 | DCHECK(!printer_settings.show_system_dialog) << "Not implemented"; |
Alan Screen | f2fbeb7a | 2021-08-28 05:27:38 | [diff] [blame] | 69 | |
| 70 | // The printer name is to be embedded in the printing context's existing |
| 71 | // settings. |
| 72 | const std::string& device_name = base::UTF16ToUTF8(settings_->device_name()); |
| 73 | auto found = device_settings_.find(device_name); |
| 74 | if (found == device_settings_.end()) { |
| 75 | DLOG(ERROR) << "No such device found in test printing context: `" |
| 76 | << device_name << "`"; |
Alan Screen | 202e49a | 2021-09-21 06:34:28 | [diff] [blame] | 77 | return mojom::ResultCode::kFailed; |
Alan Screen | f2fbeb7a | 2021-08-28 05:27:38 | [diff] [blame] | 78 | } |
| 79 | |
Alan Screen | 59767538 | 2021-09-11 05:14:53 | [diff] [blame] | 80 | // Perform some initialization, akin to various platform-specific actions in |
| 81 | // `InitPrintSettings()`. |
Alan Screen | f2fbeb7a | 2021-08-28 05:27:38 | [diff] [blame] | 82 | DVLOG(1) << "Updating context settings for device `" << device_name << "`"; |
Alan Screen | 59767538 | 2021-09-11 05:14:53 | [diff] [blame] | 83 | std::unique_ptr<PrintSettings> existing_settings = std::move(settings_); |
| 84 | settings_ = std::make_unique<PrintSettings>(*found->second); |
| 85 | settings_->set_dpi(existing_settings->dpi()); |
| 86 | #if defined(OS_LINUX) || defined(OS_CHROMEOS) |
| 87 | for (const auto& item : existing_settings->advanced_settings()) |
| 88 | settings_->advanced_settings().emplace(item.first, item.second.Clone()); |
| 89 | #endif |
| 90 | |
Alan Screen | 202e49a | 2021-09-21 06:34:28 | [diff] [blame] | 91 | return mojom::ResultCode::kSuccess; |
Alan Screen | f2fbeb7a | 2021-08-28 05:27:38 | [diff] [blame] | 92 | } |
| 93 | |
Alan Screen | 202e49a | 2021-09-21 06:34:28 | [diff] [blame] | 94 | mojom::ResultCode TestPrintingContext::NewDocument( |
Alan Screen | f2fbeb7a | 2021-08-28 05:27:38 | [diff] [blame] | 95 | const std::u16string& document_name) { |
Alan Screen | 2166ab1 | 2021-09-01 23:09:25 | [diff] [blame] | 96 | // No-op. |
Alan Screen | 202e49a | 2021-09-21 06:34:28 | [diff] [blame] | 97 | return mojom::ResultCode::kSuccess; |
Alan Screen | f2fbeb7a | 2021-08-28 05:27:38 | [diff] [blame] | 98 | } |
| 99 | |
Alan Screen | 202e49a | 2021-09-21 06:34:28 | [diff] [blame] | 100 | mojom::ResultCode TestPrintingContext::NewPage() { |
Alan Screen | f2fbeb7a | 2021-08-28 05:27:38 | [diff] [blame] | 101 | NOTIMPLEMENTED(); |
Alan Screen | 202e49a | 2021-09-21 06:34:28 | [diff] [blame] | 102 | return mojom::ResultCode::kFailed; |
Alan Screen | f2fbeb7a | 2021-08-28 05:27:38 | [diff] [blame] | 103 | } |
| 104 | |
Alan Screen | 202e49a | 2021-09-21 06:34:28 | [diff] [blame] | 105 | mojom::ResultCode TestPrintingContext::PageDone() { |
Alan Screen | f2fbeb7a | 2021-08-28 05:27:38 | [diff] [blame] | 106 | NOTIMPLEMENTED(); |
Alan Screen | 202e49a | 2021-09-21 06:34:28 | [diff] [blame] | 107 | return mojom::ResultCode::kFailed; |
Alan Screen | f2fbeb7a | 2021-08-28 05:27:38 | [diff] [blame] | 108 | } |
| 109 | |
Alan Screen | 202e49a | 2021-09-21 06:34:28 | [diff] [blame] | 110 | mojom::ResultCode TestPrintingContext::DocumentDone() { |
Alan Screen | f2fbeb7a | 2021-08-28 05:27:38 | [diff] [blame] | 111 | NOTIMPLEMENTED(); |
Alan Screen | 202e49a | 2021-09-21 06:34:28 | [diff] [blame] | 112 | return mojom::ResultCode::kFailed; |
Alan Screen | f2fbeb7a | 2021-08-28 05:27:38 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | void TestPrintingContext::Cancel() { |
| 116 | NOTIMPLEMENTED(); |
| 117 | } |
| 118 | void TestPrintingContext::ReleaseContext() {} |
| 119 | |
| 120 | printing::NativeDrawingContext TestPrintingContext::context() const { |
| 121 | NOTIMPLEMENTED(); |
| 122 | return nullptr; |
| 123 | } |
| 124 | |
| 125 | #if defined(OS_WIN) |
Alan Screen | 202e49a | 2021-09-21 06:34:28 | [diff] [blame] | 126 | mojom::ResultCode TestPrintingContext::InitWithSettingsForTest( |
Alan Screen | f2fbeb7a | 2021-08-28 05:27:38 | [diff] [blame] | 127 | std::unique_ptr<PrintSettings> settings) { |
| 128 | NOTIMPLEMENTED(); |
Alan Screen | 202e49a | 2021-09-21 06:34:28 | [diff] [blame] | 129 | return mojom::ResultCode::kFailed; |
Alan Screen | f2fbeb7a | 2021-08-28 05:27:38 | [diff] [blame] | 130 | } |
| 131 | #endif // defined(OS_WIN) |
| 132 | |
| 133 | } // namespace printing |