mirror of
https://github.com/electron/electron.git
synced 2026-04-10 03:01:51 -04:00
refactor: use platform-specific TaskRunner to print (#27328)
This commit is contained in:
@@ -378,8 +378,11 @@ bool IsDeviceNameValid(const base::string16& device_name) {
|
||||
}
|
||||
|
||||
base::string16 GetDefaultPrinterAsync() {
|
||||
base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
|
||||
base::BlockingType::MAY_BLOCK);
|
||||
#if defined(OS_WIN)
|
||||
// Blocking is needed here because Windows printer drivers are oftentimes
|
||||
// not thread-safe and have to be accessed on the UI thread.
|
||||
base::ThreadRestrictions::ScopedAllowIO allow_io;
|
||||
#endif
|
||||
|
||||
scoped_refptr<printing::PrintBackend> print_backend =
|
||||
printing::PrintBackend::CreateInstance(
|
||||
@@ -397,6 +400,29 @@ base::string16 GetDefaultPrinterAsync() {
|
||||
}
|
||||
return base::UTF8ToUTF16(printer_name);
|
||||
}
|
||||
|
||||
// Copied from
|
||||
// chrome/browser/ui/webui/print_preview/local_printer_handler_default.cc:L36-L54
|
||||
scoped_refptr<base::TaskRunner> CreatePrinterHandlerTaskRunner() {
|
||||
// USER_VISIBLE because the result is displayed in the print preview dialog.
|
||||
#if !defined(OS_WIN)
|
||||
static constexpr base::TaskTraits kTraits = {
|
||||
base::MayBlock(), base::TaskPriority::USER_VISIBLE};
|
||||
#endif
|
||||
|
||||
#if defined(USE_CUPS)
|
||||
// CUPS is thread safe.
|
||||
return base::ThreadPool::CreateTaskRunner(kTraits);
|
||||
#elif defined(OS_WIN)
|
||||
// Windows drivers are likely not thread-safe and need to be accessed on the
|
||||
// UI thread.
|
||||
return content::GetUIThreadTaskRunner(
|
||||
{base::MayBlock(), base::TaskPriority::USER_VISIBLE});
|
||||
#else
|
||||
// Be conservative on unsupported platforms.
|
||||
return base::ThreadPool::CreateSingleThreadTaskRunner(kTraits);
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace
|
||||
@@ -426,6 +452,7 @@ WebContents::WebContents(v8::Isolate* isolate,
|
||||
content::WebContents* web_contents)
|
||||
: content::WebContentsObserver(web_contents),
|
||||
type_(Type::REMOTE),
|
||||
print_task_runner_(CreatePrinterHandlerTaskRunner()),
|
||||
weak_factory_(this) {
|
||||
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
||||
// WebContents created by extension host will have valid ViewType set.
|
||||
@@ -460,6 +487,7 @@ WebContents::WebContents(v8::Isolate* isolate,
|
||||
Type type)
|
||||
: content::WebContentsObserver(web_contents.get()),
|
||||
type_(type),
|
||||
print_task_runner_(CreatePrinterHandlerTaskRunner()),
|
||||
weak_factory_(this) {
|
||||
DCHECK(type != Type::REMOTE)
|
||||
<< "Can't take ownership of a remote WebContents";
|
||||
@@ -471,7 +499,8 @@ WebContents::WebContents(v8::Isolate* isolate,
|
||||
|
||||
WebContents::WebContents(v8::Isolate* isolate,
|
||||
const gin_helper::Dictionary& options)
|
||||
: weak_factory_(this) {
|
||||
: print_task_runner_(CreatePrinterHandlerTaskRunner()),
|
||||
weak_factory_(this) {
|
||||
// Read options.
|
||||
options.Get("backgroundThrottling", &background_throttling_);
|
||||
|
||||
@@ -2153,9 +2182,8 @@ void WebContents::Print(gin_helper::Arguments* args) {
|
||||
settings.SetIntKey(printing::kSettingDpiVertical, dpi);
|
||||
}
|
||||
|
||||
base::ThreadPool::PostTaskAndReplyWithResult(
|
||||
FROM_HERE, {base::MayBlock(), base::TaskPriority::USER_BLOCKING},
|
||||
base::BindOnce(&GetDefaultPrinterAsync),
|
||||
print_task_runner_->PostTaskAndReplyWithResult(
|
||||
FROM_HERE, base::BindOnce(&GetDefaultPrinterAsync),
|
||||
base::BindOnce(&WebContents::OnGetDefaultPrinter,
|
||||
weak_factory_.GetWeakPtr(), std::move(settings),
|
||||
std::move(callback), device_name, silent));
|
||||
|
||||
@@ -656,6 +656,8 @@ class WebContents : public gin_helper::TrackableObject<WebContents>,
|
||||
// -1 means no speculative RVH has been committed yet.
|
||||
int currently_committed_process_id_ = -1;
|
||||
|
||||
scoped_refptr<base::TaskRunner> print_task_runner_;
|
||||
|
||||
service_manager::BinderRegistryWithArgs<content::RenderFrameHost*> registry_;
|
||||
mojo::ReceiverSet<mojom::ElectronBrowser, content::RenderFrameHost*>
|
||||
receivers_;
|
||||
|
||||
Reference in New Issue
Block a user