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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
// 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.
#include "chrome/browser/printing/print_backend_service_test_impl.h"
#include <utility>
#include "base/check.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#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/browser/printing/print_backend_service_manager.h"
#include "printing/backend/test_print_backend.h"
#if BUILDFLAG(IS_WIN)
#include <memory>
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/threading/thread_restrictions.h"
#include "chrome/browser/printing/printer_xml_parser_impl.h"
#include "content/public/browser/browser_thread.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "printing/printing_features.h"
#endif // BUILDFLAG(IS_WIN)
namespace printing {
#if BUILDFLAG(IS_WIN)
struct RenderPrintedPageData {
RenderPrintedPageData(
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)
: document_cookie(document_cookie),
page_index(page_index),
page_data_type(page_data_type),
serialized_page(std::move(serialized_page)),
page_size(page_size),
page_content_rect(page_content_rect),
shrink_factor(shrink_factor),
callback(std::move(callback)) {}
RenderPrintedPageData(const RenderPrintedPageData&) = delete;
RenderPrintedPageData& operator=(const RenderPrintedPageData&) = delete;
~RenderPrintedPageData() = default;
int32_t document_cookie;
uint32_t page_index;
mojom::MetafileDataType page_data_type;
base::ReadOnlySharedMemoryRegion serialized_page;
gfx::Size page_size;
gfx::Rect page_content_rect;
float shrink_factor;
mojom::PrintBackendService::RenderPrintedPageCallback callback;
};
#endif
PrintBackendServiceTestImpl::PrintBackendServiceTestImpl(
mojo::PendingReceiver<mojom::PrintBackendService> receiver,
bool is_sandboxed,
scoped_refptr<TestPrintBackend> backend)
: PrintBackendServiceImpl(std::move(receiver)),
is_sandboxed_(is_sandboxed),
test_print_backend_(std::move(backend)) {}
PrintBackendServiceTestImpl::~PrintBackendServiceTestImpl() {
if (!skip_dtor_persistent_contexts_check_) {
// Make sure that all persistent contexts have been properly cleaned up.
DCHECK(persistent_printing_contexts_.empty());
}
if (is_sandboxed_) {
PrintBackendServiceManager::GetInstance().SetServiceForTesting(nullptr);
} else {
PrintBackendServiceManager::GetInstance().SetServiceForFallbackTesting(
nullptr);
}
}
void PrintBackendServiceTestImpl::Init(
#if BUILDFLAG(IS_WIN)
const std::string& locale,
mojo::PendingRemote<mojom::PrinterXmlParser> remote
#else
const std::string& locale
#endif // BUILDFLAG(IS_WIN)
) {
DCHECK(test_print_backend_);
print_backend_ = test_print_backend_;
#if BUILDFLAG(IS_WIN)
PrintBackendServiceImpl::InitCommon(locale, std::move(remote));
#else
PrintBackendServiceImpl::InitCommon(locale);
#endif // BUILDFLAG(IS_WIN)
}
void PrintBackendServiceTestImpl::EnumeratePrinters(
mojom::PrintBackendService::EnumeratePrintersCallback callback) {
if (terminate_receiver_) {
TerminateConnection();
return;
}
PrintBackendServiceImpl::EnumeratePrinters(std::move(callback));
}
void PrintBackendServiceTestImpl::GetDefaultPrinterName(
mojom::PrintBackendService::GetDefaultPrinterNameCallback callback) {
if (terminate_receiver_) {
TerminateConnection();
return;
}
PrintBackendServiceImpl::GetDefaultPrinterName(std::move(callback));
}
#if BUILDFLAG(IS_CHROMEOS)
void PrintBackendServiceTestImpl::GetPrinterSemanticCapsAndDefaults(
const std::string& printer_name,
mojom::PrintBackendService::GetPrinterSemanticCapsAndDefaultsCallback
callback) {
if (terminate_receiver_) {
TerminateConnection();
return;
}
PrintBackendServiceImpl::GetPrinterSemanticCapsAndDefaults(
printer_name, std::move(callback));
}
#endif // BUILDFLAG(IS_CHROMEOS)
void PrintBackendServiceTestImpl::FetchCapabilities(
const std::string& printer_name,
mojom::PrintBackendService::FetchCapabilitiesCallback callback) {
if (terminate_receiver_) {
TerminateConnection();
return;
}
#if BUILDFLAG(IS_WIN)
// Fetching capabilities with XPS uses synchronous mojo calls, which requires
// base sync primitives for testing with multiple threads.
base::ScopedAllowBaseSyncPrimitivesForTesting allow_base_sync_primitives;
#endif // BUILDFLAG(IS_WIN)
PrintBackendServiceImpl::FetchCapabilities(printer_name, std::move(callback));
}
void PrintBackendServiceTestImpl::UpdatePrintSettings(
uint32_t context_id,
base::Value::Dict job_settings,
mojom::PrintBackendService::UpdatePrintSettingsCallback callback) {
if (terminate_receiver_) {
TerminateConnection();
return;
}
PrintBackendServiceImpl::UpdatePrintSettings(
context_id, std::move(job_settings), std::move(callback));
}
#if BUILDFLAG(IS_WIN)
void PrintBackendServiceTestImpl::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) {
if (terminate_receiver_) {
TerminateConnection();
return;
}
// Page index is zero-based whereas page number is one-based.
uint32_t page_number = page_index + 1;
if (page_number < rendering_delayed_until_page_number_) {
DVLOG(2) << "Adding page " << page_number << " to delayed rendering queue";
delayed_rendering_pages_.push(std::make_unique<RenderPrintedPageData>(
document_cookie, page_index, page_data_type, std::move(serialized_page),
page_size, page_content_rect, shrink_factor, std::move(callback)));
return;
}
// Any previously delayed pages should now be rendered, before carrying on
// with the page for this call.
while (!delayed_rendering_pages_.empty()) {
RenderPrintedPageData* page_data = delayed_rendering_pages_.front().get();
DVLOG(2) << "Rendering deferred page " << (page_data->page_index + 1);
PrintBackendServiceImpl::RenderPrintedPage(
page_data->document_cookie, page_data->page_index,
page_data->page_data_type, std::move(page_data->serialized_page),
page_data->page_size, page_data->page_content_rect,
page_data->shrink_factor, std::move(page_data->callback));
delayed_rendering_pages_.pop();
}
DVLOG(2) << "Rendering page " << page_number;
PrintBackendServiceImpl::RenderPrintedPage(
document_cookie, page_index, page_data_type, std::move(serialized_page),
page_size, page_content_rect, shrink_factor, std::move(callback));
}
#endif // BUILDFLAG(IS_WIN)
void PrintBackendServiceTestImpl::TerminateConnection() {
DLOG(ERROR) << "Terminating print backend service test connection";
receiver_.reset();
}
// static
std::unique_ptr<PrintBackendServiceTestImpl>
PrintBackendServiceTestImpl::LaunchForTesting(
mojo::Remote<mojom::PrintBackendService>& remote,
scoped_refptr<TestPrintBackend> backend,
bool sandboxed) {
mojo::PendingReceiver<mojom::PrintBackendService> receiver =
remote.BindNewPipeAndPassReceiver();
// Private ctor.
auto service = base::WrapUnique(new PrintBackendServiceTestImpl(
std::move(receiver), sandboxed, std::move(backend)));
#if BUILDFLAG(IS_WIN)
// Initializes the service with an invalid PrinterXmlParser, so it won't be
// able to parse XML.
service->Init(/*locale=*/std::string(),
mojo::PendingRemote<mojom::PrinterXmlParser>());
#else
service->Init(/*locale=*/std::string());
#endif // BUILDFLAG(IS_WIN)
// Register this test version of print backend service to be used instead of
// launching instances out-of-process on-demand.
if (sandboxed) {
PrintBackendServiceManager::GetInstance().SetServiceForTesting(&remote);
} else {
PrintBackendServiceManager::GetInstance().SetServiceForFallbackTesting(
&remote);
}
return service;
}
#if BUILDFLAG(IS_WIN)
// static
std::unique_ptr<PrintBackendServiceTestImpl>
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) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
std::unique_ptr<PrintBackendServiceTestImpl> service;
base::RunLoop run_loop;
service_task_runner->PostTaskAndReplyWithResult(
FROM_HERE,
base::BindOnce(&PrintBackendServiceTestImpl::CreateServiceOnServiceThread,
remote.BindNewPipeAndPassReceiver(), sandboxed, backend,
std::move(xml_parser_remote)),
base::BindLambdaForTesting(
[&](std::unique_ptr<PrintBackendServiceTestImpl> result_service) {
service = std::move(result_service);
run_loop.Quit();
}));
run_loop.Run();
// Register this test version of print backend service to be used instead of
// launching instances out-of-process on-demand.
if (sandboxed) {
PrintBackendServiceManager::GetInstance().SetServiceForTesting(&remote);
} else {
PrintBackendServiceManager::GetInstance().SetServiceForFallbackTesting(
&remote);
}
return service;
}
// static
std::unique_ptr<PrintBackendServiceTestImpl>
PrintBackendServiceTestImpl::CreateServiceOnServiceThread(
mojo::PendingReceiver<mojom::PrintBackendService> receiver,
bool is_sandboxed,
scoped_refptr<TestPrintBackend> backend,
mojo::PendingRemote<mojom::PrinterXmlParser> xml_parser_remote) {
// Private ctor.
auto service = base::WrapUnique(new PrintBackendServiceTestImpl(
std::move(receiver), is_sandboxed, std::move(backend)));
service->Init(/*locale=*/std::string(), std::move(xml_parser_remote));
return service;
}
#endif // BUILDFLAG(IS_WIN)
} // namespace printing
|