cef/libcef/common/task_runner_impl.cc

146 lines
4.4 KiB
C++
Raw Normal View History

// Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights
// reserved. Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.
#include "cef/libcef/common/task_runner_impl.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/task/single_thread_task_runner.h"
#include "base/task/thread_pool.h"
#include "base/time/time.h"
#include "cef/libcef/common/task_runner_manager.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/child_process_launcher_utils.h"
using content::BrowserThread;
// CefTaskRunner
// static
CefRefPtr<CefTaskRunner> CefTaskRunner::GetForCurrentThread() {
2017-09-21 13:57:40 +02:00
scoped_refptr<base::SingleThreadTaskRunner> task_runner =
CefTaskRunnerImpl::GetCurrentTaskRunner();
2023-01-02 23:59:03 +01:00
if (task_runner.get()) {
return new CefTaskRunnerImpl(task_runner);
2023-01-02 23:59:03 +01:00
}
return nullptr;
}
// static
CefRefPtr<CefTaskRunner> CefTaskRunner::GetForThread(CefThreadId threadId) {
2017-09-21 13:57:40 +02:00
scoped_refptr<base::SingleThreadTaskRunner> task_runner =
CefTaskRunnerImpl::GetTaskRunner(threadId);
2023-01-02 23:59:03 +01:00
if (task_runner.get()) {
return new CefTaskRunnerImpl(task_runner);
2023-01-02 23:59:03 +01:00
}
LOG(WARNING) << "Invalid thread id " << threadId;
return nullptr;
}
// CefTaskRunnerImpl
CefTaskRunnerImpl::CefTaskRunnerImpl(
2017-09-21 13:57:40 +02:00
scoped_refptr<base::SingleThreadTaskRunner> task_runner)
: task_runner_(task_runner) {
DCHECK(task_runner_.get());
}
// static
2017-09-21 13:57:40 +02:00
scoped_refptr<base::SingleThreadTaskRunner> CefTaskRunnerImpl::GetTaskRunner(
CefThreadId threadId) {
auto* manager = CefTaskRunnerManager::Get();
2023-01-02 23:59:03 +01:00
if (!manager) {
return nullptr;
2023-01-02 23:59:03 +01:00
}
switch (threadId) {
case TID_UI:
2023-01-02 23:59:03 +01:00
if (BrowserThread::IsThreadInitialized(BrowserThread::UI)) {
return content::GetUIThreadTaskRunner({});
2023-01-02 23:59:03 +01:00
}
break;
case TID_FILE_BACKGROUND:
return manager->GetBackgroundTaskRunner();
case TID_FILE_USER_VISIBLE:
return manager->GetUserVisibleTaskRunner();
case TID_FILE_USER_BLOCKING:
return manager->GetUserBlockingTaskRunner();
case TID_PROCESS_LAUNCHER:
return content::GetProcessLauncherTaskRunner();
case TID_IO:
2023-01-02 23:59:03 +01:00
if (BrowserThread::IsThreadInitialized(BrowserThread::IO)) {
return content::GetIOThreadTaskRunner({});
2023-01-02 23:59:03 +01:00
}
break;
case TID_RENDERER:
return manager->GetRenderTaskRunner();
default:
break;
};
return nullptr;
}
// static
2017-09-21 13:57:40 +02:00
scoped_refptr<base::SingleThreadTaskRunner>
CefTaskRunnerImpl::GetCurrentTaskRunner() {
auto* manager = CefTaskRunnerManager::Get();
2023-01-02 23:59:03 +01:00
if (!manager) {
return nullptr;
2023-01-02 23:59:03 +01:00
}
// For named browser process threads return the same TaskRunner as
// GetTaskRunner(). Otherwise BelongsToThread() will return incorrect results.
BrowserThread::ID current_id;
if (BrowserThread::GetCurrentThreadIdentifier(&current_id) &&
BrowserThread::IsThreadInitialized(current_id)) {
if (current_id == BrowserThread::UI) {
return content::GetUIThreadTaskRunner({});
} else if (current_id == BrowserThread::IO) {
return content::GetIOThreadTaskRunner({});
} else {
DCHECK(false);
}
}
// Check for a MessageLoopProxy. This covers all of the named browser and
// render process threads, plus a few extra.
if (auto task_runner = base::SingleThreadTaskRunner::GetCurrentDefault()) {
return task_runner;
2023-01-02 23:59:03 +01:00
}
// Check for a WebWorker thread.
return manager->GetWebWorkerTaskRunner();
}
bool CefTaskRunnerImpl::IsSame(CefRefPtr<CefTaskRunner> that) {
CefTaskRunnerImpl* impl = static_cast<CefTaskRunnerImpl*>(that.get());
return (impl && task_runner_ == impl->task_runner_);
}
bool CefTaskRunnerImpl::BelongsToCurrentThread() {
return task_runner_->RunsTasksInCurrentSequence();
}
bool CefTaskRunnerImpl::BelongsToThread(CefThreadId threadId) {
2017-09-21 13:57:40 +02:00
scoped_refptr<base::SingleThreadTaskRunner> task_runner =
GetTaskRunner(threadId);
return (task_runner_ == task_runner);
}
bool CefTaskRunnerImpl::PostTask(CefRefPtr<CefTask> task) {
return task_runner_->PostTask(FROM_HERE,
base::BindOnce(&CefTask::Execute, task.get()));
}
bool CefTaskRunnerImpl::PostDelayedTask(CefRefPtr<CefTask> task,
int64_t delay_ms) {
return task_runner_->PostDelayedTask(
FROM_HERE, base::BindOnce(&CefTask::Execute, task.get()),
base::Milliseconds(delay_ms));
}