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 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
|
// Copyright 2016 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/ui/webui/print_preview/local_printer_handler_default.h"
#include <string>
#include <utility>
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/memory/ref_counted_memory.h"
#include "base/metrics/histogram_functions.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/ui/webui/print_preview/print_preview_utils.h"
#include "chrome/common/printing/printer_capabilities.h"
#include "components/device_event_log/device_event_log.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "printing/buildflags/buildflags.h"
#include "printing/mojom/print.mojom.h"
#include "third_party/abseil-cpp/absl/cleanup/cleanup.h"
#if BUILDFLAG(IS_MAC)
#include "chrome/common/printing/printer_capabilities_mac.h"
#endif
#if BUILDFLAG(IS_WIN)
#include "base/threading/thread_restrictions.h"
#endif
#if BUILDFLAG(ENABLE_OOP_PRINTING)
#include "chrome/browser/printing/oop_features.h"
#include "chrome/browser/printing/print_backend_service_manager.h"
#include "chrome/browser/ui/webui/print_preview/printer_handler.h"
#include "chrome/services/printing/public/mojom/print_backend_service.mojom.h"
#endif
namespace printing {
namespace {
scoped_refptr<base::TaskRunner> CreatePrinterHandlerTaskRunner() {
// USER_VISIBLE because the result is displayed in the print preview dialog.
#if !BUILDFLAG(IS_WIN)
static constexpr base::TaskTraits kTraits = {
base::MayBlock(), base::TaskPriority::USER_VISIBLE};
#endif
#if BUILDFLAG(USE_CUPS)
// CUPS is thread safe.
return base::ThreadPool::CreateTaskRunner(kTraits);
#elif BUILDFLAG(IS_WIN)
// Windows drivers are likely not thread-safe and need to be accessed on the
// UI thread.
return content::GetUIThreadTaskRunner({base::TaskPriority::USER_VISIBLE});
#else
// Be conservative on unsupported platforms.
return base::ThreadPool::CreateSingleThreadTaskRunner(kTraits);
#endif
}
void ReportFetchCapabilitiesTime(base::TimeTicks start_time) {
base::UmaHistogramTimes("PrintPreview.FetchCapabilitiesTime2",
base::TimeTicks::Now() - start_time);
}
} // namespace
// static
PrinterList LocalPrinterHandlerDefault::EnumeratePrintersOnBlockingTaskRunner(
const std::string& locale) {
#if BUILDFLAG(IS_WIN)
// Blocking is needed here because Windows printer drivers are oftentimes
// not thread-safe and have to be accessed on the UI thread.
base::ScopedAllowBlocking allow_blocking;
#endif
auto query_start_time = base::TimeTicks::Now();
scoped_refptr<PrintBackend> print_backend(
PrintBackend::CreateInstance(locale));
PrinterList printer_list;
mojom::ResultCode result = print_backend->EnumeratePrinters(printer_list);
base::UmaHistogramTimes("PrintPreview.EnumeratePrintersTime",
base::TimeTicks::Now() - query_start_time);
if (result != mojom::ResultCode::kSuccess) {
PRINTER_LOG(ERROR) << "Failure enumerating local printers, result: "
<< result;
return PrinterList();
}
PRINTER_LOG(EVENT) << "Enumerated " << printer_list.size() << " printer(s)";
return printer_list;
}
// static
base::Value::Dict
LocalPrinterHandlerDefault::FetchCapabilitiesOnBlockingTaskRunner(
const std::string& device_name,
const std::string& locale) {
scoped_refptr<PrintBackend> print_backend(
PrintBackend::CreateInstance(locale));
// Start timing after CreateInstance() to match what gets timed for the
// out-of-process query.
auto query_start_time = base::TimeTicks::Now();
absl::Cleanup metric_reporter = [query_start_time] {
ReportFetchCapabilitiesTime(query_start_time);
};
PrinterSemanticCapsAndDefaults::Papers user_defined_papers;
#if BUILDFLAG(IS_MAC)
user_defined_papers = GetMacCustomPaperSizes();
#endif
#if BUILDFLAG(IS_WIN)
// Blocking is needed here because Windows printer drivers are oftentimes
// not thread-safe and have to be accessed on the UI thread.
base::ScopedAllowBlocking allow_blocking;
#endif
PrinterBasicInfo basic_info;
mojom::ResultCode result =
print_backend->GetPrinterBasicInfo(device_name, &basic_info);
if (result != mojom::ResultCode::kSuccess) {
PRINTER_LOG(ERROR) << "Invalid printer when getting basic info for "
<< device_name << ", result: " << result;
return base::Value::Dict();
}
PRINTER_LOG(EVENT) << "Got basic info for " << device_name;
return GetSettingsOnBlockingTaskRunner(
device_name, basic_info, std::move(user_defined_papers), print_backend);
}
// static
std::string LocalPrinterHandlerDefault::GetDefaultPrinterOnBlockingTaskRunner(
const std::string& locale) {
#if BUILDFLAG(IS_WIN)
// Blocking is needed here because Windows printer drivers are oftentimes
// not thread-safe and have to be accessed on the UI thread.
base::ScopedAllowBlocking allow_blocking;
#endif
auto query_start_time = base::TimeTicks::Now();
scoped_refptr<PrintBackend> print_backend(
PrintBackend::CreateInstance(locale));
std::string default_printer;
mojom::ResultCode result =
print_backend->GetDefaultPrinterName(default_printer);
base::UmaHistogramTimes("PrintPreview.GetDefaultPrinterNameTime",
base::TimeTicks::Now() - query_start_time);
if (result != mojom::ResultCode::kSuccess) {
PRINTER_LOG(ERROR) << "Failure getting default printer name, result: "
<< result;
return std::string();
}
PRINTER_LOG(EVENT) << "Default Printer: " << default_printer;
return default_printer;
}
LocalPrinterHandlerDefault::LocalPrinterHandlerDefault(
content::WebContents* preview_web_contents)
: preview_web_contents_(preview_web_contents),
task_runner_(CreatePrinterHandlerTaskRunner()) {}
LocalPrinterHandlerDefault::~LocalPrinterHandlerDefault() = default;
void LocalPrinterHandlerDefault::Reset() {}
void LocalPrinterHandlerDefault::GetDefaultPrinter(DefaultPrinterCallback cb) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
#if BUILDFLAG(ENABLE_OOP_PRINTING)
if (IsOopPrintingEnabled()) {
PRINTER_LOG(EVENT) << "Getting default printer via service";
auto query_start_time = base::TimeTicks::Now();
PrintBackendServiceManager& service_mgr =
PrintBackendServiceManager::GetInstance();
service_mgr.GetDefaultPrinterName(base::BindOnce(
&LocalPrinterHandlerDefault::
OnDidGetDefaultPrinterNameFromPrintBackendService,
weak_ptr_factory_.GetWeakPtr(), query_start_time, std::move(cb)));
return;
}
#endif // BUILDFLAG(ENABLE_OOP_PRINTING)
PRINTER_LOG(EVENT) << "Getting default printer in-process";
task_runner_->PostTaskAndReplyWithResult(
FROM_HERE,
base::BindOnce(&GetDefaultPrinterOnBlockingTaskRunner,
g_browser_process->GetApplicationLocale()),
std::move(cb));
}
void LocalPrinterHandlerDefault::StartGetPrinters(
AddedPrintersCallback callback,
GetPrintersDoneCallback done_callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
#if BUILDFLAG(ENABLE_OOP_PRINTING)
if (IsOopPrintingEnabled()) {
PRINTER_LOG(EVENT) << "Enumerate printers start via service";
auto query_start_time = base::TimeTicks::Now();
PrintBackendServiceManager& service_mgr =
PrintBackendServiceManager::GetInstance();
service_mgr.EnumeratePrinters(
base::BindOnce(&LocalPrinterHandlerDefault::
OnDidEnumeratePrintersFromPrintBackendService,
weak_ptr_factory_.GetWeakPtr(), query_start_time,
std::move(callback), std::move(done_callback)));
return;
}
#endif // BUILDFLAG(ENABLE_OOP_PRINTING)
PRINTER_LOG(EVENT) << "Enumerate printers start in-process";
task_runner_->PostTaskAndReplyWithResult(
FROM_HERE,
base::BindOnce(&EnumeratePrintersOnBlockingTaskRunner,
g_browser_process->GetApplicationLocale()),
base::BindOnce(&ConvertPrinterListForCallback, std::move(callback),
std::move(done_callback)));
}
void LocalPrinterHandlerDefault::StartGetCapability(
const std::string& device_name,
GetCapabilityCallback cb) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
#if BUILDFLAG(ENABLE_OOP_PRINTING)
if (IsOopPrintingEnabled()) {
PRINTER_LOG(EVENT) << "Getting printer capabilities via service for "
<< device_name;
auto query_start_time = base::TimeTicks::Now();
PrintBackendServiceManager& service_mgr =
PrintBackendServiceManager::GetInstance();
service_mgr.FetchCapabilities(
device_name,
base::BindOnce(&LocalPrinterHandlerDefault::
OnDidFetchCapabilitiesFromPrintBackendService,
weak_ptr_factory_.GetWeakPtr(), device_name,
service_mgr.PrinterDriverFoundToRequireElevatedPrivilege(
device_name),
query_start_time, std::move(cb)));
return;
}
#endif // BUILDFLAG(ENABLE_OOP_PRINTING)
PRINTER_LOG(EVENT) << "Getting printer capabilities in-process for "
<< device_name;
task_runner_->PostTaskAndReplyWithResult(
FROM_HERE,
base::BindOnce(&FetchCapabilitiesOnBlockingTaskRunner, device_name,
g_browser_process->GetApplicationLocale()),
std::move(cb));
}
void LocalPrinterHandlerDefault::StartPrint(
const std::u16string& job_title,
base::Value::Dict settings,
scoped_refptr<base::RefCountedMemory> print_data,
PrintCallback callback) {
StartLocalPrint(std::move(settings), std::move(print_data),
preview_web_contents_, std::move(callback));
}
#if BUILDFLAG(ENABLE_OOP_PRINTING)
void LocalPrinterHandlerDefault::
OnDidGetDefaultPrinterNameFromPrintBackendService(
base::TimeTicks query_start_time,
DefaultPrinterCallback callback,
mojom::DefaultPrinterNameResultPtr result) {
base::UmaHistogramTimes("PrintPreview.GetDefaultPrinterNameTime",
base::TimeTicks::Now() - query_start_time);
if (result->is_result_code()) {
PRINTER_LOG(ERROR)
<< "Failure getting default printer via service, result: "
<< result->get_result_code();
std::move(callback).Run(std::string());
return;
}
PRINTER_LOG(EVENT) << "Default Printer from service: "
<< result->get_default_printer_name();
std::move(callback).Run(result->get_default_printer_name());
}
void LocalPrinterHandlerDefault::OnDidEnumeratePrintersFromPrintBackendService(
base::TimeTicks query_start_time,
AddedPrintersCallback added_printers_callback,
GetPrintersDoneCallback done_callback,
mojom::PrinterListResultPtr result) {
base::UmaHistogramTimes("PrintPreview.EnumeratePrintersTime",
base::TimeTicks::Now() - query_start_time);
PrinterList printer_list;
if (result->is_printer_list()) {
printer_list = std::move(result->get_printer_list());
PRINTER_LOG(EVENT) << "Enumerated " << printer_list.size() << " printer(s)";
} else {
PRINTER_LOG(ERROR)
<< "Failure enumerating local printers via service, result: "
<< result->get_result_code();
}
ConvertPrinterListForCallback(std::move(added_printers_callback),
std::move(done_callback), printer_list);
}
void LocalPrinterHandlerDefault::OnDidFetchCapabilitiesFromPrintBackendService(
const std::string& device_name,
bool elevated_privileges,
base::TimeTicks query_start_time,
GetCapabilityCallback callback,
mojom::PrinterCapsAndInfoResultPtr result) {
ReportFetchCapabilitiesTime(query_start_time);
if (result->is_result_code()) {
PRINTER_LOG(ERROR)
<< "Failure fetching printer capabilities via service for "
<< device_name << ", result: " << result->get_result_code();
// If we failed because of access denied then we could retry at an elevated
// privilege (if not already elevated).
if (result->get_result_code() == mojom::ResultCode::kAccessDenied &&
!elevated_privileges) {
// Register that this printer requires elevated privileges.
PrintBackendServiceManager& service_mgr =
PrintBackendServiceManager::GetInstance();
service_mgr.SetPrinterDriverFoundToRequireElevatedPrivilege(device_name);
// Retry the operation which should now happen at a higher privilege
// level.
auto query_restart_time = base::TimeTicks::Now();
service_mgr.FetchCapabilities(
device_name,
base::BindOnce(&LocalPrinterHandlerDefault::
OnDidFetchCapabilitiesFromPrintBackendService,
weak_ptr_factory_.GetWeakPtr(), device_name,
/*elevated_privileges=*/true, query_restart_time,
std::move(callback)));
return;
}
// Unable to fallback, call back without data.
std::move(callback).Run(base::Value::Dict());
return;
}
PRINTER_LOG(EVENT) << "Received printer info & capabilities via service for "
<< device_name;
const mojom::PrinterCapsAndInfoPtr& caps_and_info =
result->get_printer_caps_and_info();
base::Value::Dict settings = AssemblePrinterSettings(
device_name, caps_and_info->printer_info,
/*has_secure_protocol=*/false, &caps_and_info->printer_caps);
std::move(callback).Run(std::move(settings));
}
#endif // BUILDFLAG(ENABLE_OOP_PRINTING)
} // namespace printing
|