1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
// 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.
#ifndef CHROME_BROWSER_PRINTING_PRINT_BACKEND_SERVICE_TEST_IMPL_H_
#define CHROME_BROWSER_PRINTING_PRINT_BACKEND_SERVICE_TEST_IMPL_H_
#include <memory>
#include <string>
#include "base/memory/scoped_refptr.h"
#include "base/task/single_thread_task_runner.h"
#include "base/values.h"
#include "build/build_config.h"
#include "chrome/services/printing/print_backend_service_impl.h"
#include "chrome/services/printing/public/mojom/print_backend_service.mojom.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "printing/backend/test_print_backend.h"
#if BUILDFLAG(IS_WIN)
#include "base/containers/queue.h"
#include "base/memory/read_only_shared_memory_region.h"
#include "base/memory/scoped_refptr.h"
#include "chrome/services/printing/public/mojom/printer_xml_parser.mojom-forward.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "printing/mojom/print.mojom.h"
#endif
#if BUILDFLAG(IS_WIN)
namespace gfx {
class Rect;
class Size;
} // namespace gfx
#endif
namespace printing {
#if BUILDFLAG(IS_WIN)
struct RenderPrintedPageData;
#endif
// `PrintBackendServiceTestImpl` uses a `TestPrintBackend` to enable testing
// of the `PrintBackendService` without relying upon the presence of real
// printer drivers.
class PrintBackendServiceTestImpl : public PrintBackendServiceImpl {
public:
// Launch the service in-process for testing using the provided backend.
// `sandboxed` identifies if this service is potentially subject to
// experiencing access-denied errors on some commands.
static std::unique_ptr<PrintBackendServiceTestImpl> LaunchForTesting(
mojo::Remote<mojom::PrintBackendService>& remote,
scoped_refptr<TestPrintBackend> backend,
bool sandboxed);
#if BUILDFLAG(IS_WIN)
// Launch the service in-process for testing using the provided backend.
// `sandboxed` identifies if this service is potentially subject to
// experiencing access-denied errors on some commands. Launches the service on
// the thread associated with `service_task_runner`.
static std::unique_ptr<PrintBackendServiceTestImpl>
LaunchForTestingWithServiceThread(
mojo::Remote<mojom::PrintBackendService>& remote,
scoped_refptr<TestPrintBackend> backend,
bool sandboxed,
mojo::PendingRemote<mojom::PrinterXmlParser> xml_parser_remote,
scoped_refptr<base::SingleThreadTaskRunner> service_task_runner);
#endif // BUILDFLAG(IS_WIN)
PrintBackendServiceTestImpl(const PrintBackendServiceTestImpl&) = delete;
PrintBackendServiceTestImpl& operator=(const PrintBackendServiceTestImpl&) =
delete;
~PrintBackendServiceTestImpl() override;
// Override which needs special handling for using `test_print_backend_`.
void Init(
#if BUILDFLAG(IS_WIN)
const std::string& locale,
mojo::PendingRemote<mojom::PrinterXmlParser> remote
#else
const std::string& locale
#endif // BUILDFLAG(IS_WIN)
) override;
// Overrides to support testing service termination scenarios.
void EnumeratePrinters(
mojom::PrintBackendService::EnumeratePrintersCallback callback) override;
void GetDefaultPrinterName(
mojom::PrintBackendService::GetDefaultPrinterNameCallback callback)
override;
#if BUILDFLAG(IS_CHROMEOS)
void GetPrinterSemanticCapsAndDefaults(
const std::string& printer_name,
mojom::PrintBackendService::GetPrinterSemanticCapsAndDefaultsCallback
callback) override;
#endif
void FetchCapabilities(
const std::string& printer_name,
mojom::PrintBackendService::FetchCapabilitiesCallback callback) override;
void UpdatePrintSettings(
uint32_t context_id,
base::Value::Dict job_settings,
mojom::PrintBackendService::UpdatePrintSettingsCallback callback)
override;
#if BUILDFLAG(IS_WIN)
void RenderPrintedPage(
int32_t document_cookie,
uint32_t page_index,
mojom::MetafileDataType page_data_type,
base::ReadOnlySharedMemoryRegion serialized_page,
const gfx::Size& page_size,
const gfx::Rect& page_content_rect,
float shrink_factor,
mojom::PrintBackendService::RenderPrintedPageCallback callback) override;
#endif // BUILDFLAG(IS_WIN)
// Tests which will have a leftover printing context established in the
// service can use this to skip the destructor check that all contexts were
// cleaned up.
void SkipPersistentContextsCheckOnShutdown() {
skip_dtor_persistent_contexts_check_ = true;
}
// Cause the service to terminate on the next interaction it receives. Once
// terminated no further Mojo calls will be possible since there will not be
// a receiver to handle them.
void SetTerminateReceiverOnNextInteraction() { terminate_receiver_ = true; }
#if BUILDFLAG(IS_WIN)
// Set the page number for which rendering should be delayed until. Pages
// are held in queue until this page number is seen, after which the pages
// are released in sequence for rendering.
void set_rendering_delayed_until_page(uint32_t page_number) {
rendering_delayed_until_page_number_ = page_number;
}
#endif
private:
// Use LaunchForTesting() or LaunchForTestingWithServiceThread().
PrintBackendServiceTestImpl(
mojo::PendingReceiver<mojom::PrintBackendService> receiver,
bool is_sandboxed,
scoped_refptr<TestPrintBackend> backend);
void OnDidGetDefaultPrinterName(
mojom::PrintBackendService::GetDefaultPrinterNameCallback callback,
mojom::PrintBackendService::GetDefaultPrinterNameResult printer_name);
void TerminateConnection();
#if BUILDFLAG(IS_WIN)
// Launches and returns a test Print Backend service run on a service thread.
// This runs on the service thread.
static std::unique_ptr<PrintBackendServiceTestImpl>
CreateServiceOnServiceThread(
mojo::PendingReceiver<mojom::PrintBackendService> receiver,
bool is_sandboxed,
scoped_refptr<TestPrintBackend> backend,
mojo::PendingRemote<mojom::PrinterXmlParser> xml_parser_remote);
#endif // BUILDFLAG(IS_WIN)
// When pretending to be sandboxed, have the possibility of getting access
// denied errors.
const bool is_sandboxed_;
// Marker for skipping check for empty persistent contexts at destruction.
bool skip_dtor_persistent_contexts_check_ = false;
// Marker to signal service should terminate on next interaction.
bool terminate_receiver_ = false;
#if BUILDFLAG(IS_WIN)
// Marker to signal that rendering should be delayed until the page with this
// index is reached. This provides a mechanism for the print pipeline to get
// multiple pages queued up.
uint32_t rendering_delayed_until_page_number_ = 0;
// The queue of pages whose rendering processing is being delayed.
base::queue<std::unique_ptr<RenderPrintedPageData>> delayed_rendering_pages_;
#endif
scoped_refptr<TestPrintBackend> test_print_backend_;
};
} // namespace printing
#endif // CHROME_BROWSER_PRINTING_PRINT_BACKEND_SERVICE_TEST_IMPL_H_
|