2013-01-03 18:24:24 +01:00
|
|
|
// 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.
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
#include "libcef/common/task_runner_impl.h"
|
2022-05-19 12:28:44 +02:00
|
|
|
|
2020-06-25 04:34:12 +02:00
|
|
|
#include "libcef/common/task_runner_manager.h"
|
2013-01-03 18:24:24 +01:00
|
|
|
|
2023-01-30 18:43:54 +01:00
|
|
|
#include "base/functional/bind.h"
|
2013-01-03 18:24:24 +01:00
|
|
|
#include "base/location.h"
|
|
|
|
#include "base/logging.h"
|
2023-01-30 18:43:54 +01:00
|
|
|
#include "base/task/single_thread_task_runner.h"
|
2022-05-19 12:28:44 +02:00
|
|
|
#include "base/task/thread_pool.h"
|
|
|
|
#include "base/time/time.h"
|
2018-10-02 14:14:11 +02:00
|
|
|
#include "content/public/browser/browser_task_traits.h"
|
2013-01-03 18:24:24 +01:00
|
|
|
#include "content/public/browser/browser_thread.h"
|
2018-04-19 17:44:42 +02:00
|
|
|
#include "content/public/browser/child_process_launcher_utils.h"
|
2013-01-03 18:24:24 +01:00
|
|
|
|
|
|
|
using content::BrowserThread;
|
|
|
|
|
|
|
|
// CefTaskRunner
|
|
|
|
|
|
|
|
// static
|
|
|
|
CefRefPtr<CefTaskRunner> CefTaskRunner::GetForCurrentThread() {
|
2017-09-21 13:57:40 +02:00
|
|
|
scoped_refptr<base::SingleThreadTaskRunner> task_runner =
|
2013-01-03 18:24:24 +01:00
|
|
|
CefTaskRunnerImpl::GetCurrentTaskRunner();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (task_runner.get()) {
|
2013-01-03 18:24:24 +01:00
|
|
|
return new CefTaskRunnerImpl(task_runner);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-01-15 14:36:24 +01:00
|
|
|
return nullptr;
|
2013-01-03 18:24:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
CefRefPtr<CefTaskRunner> CefTaskRunner::GetForThread(CefThreadId threadId) {
|
2017-09-21 13:57:40 +02:00
|
|
|
scoped_refptr<base::SingleThreadTaskRunner> task_runner =
|
2013-01-03 18:24:24 +01:00
|
|
|
CefTaskRunnerImpl::GetTaskRunner(threadId);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (task_runner.get()) {
|
2013-01-03 18:24:24 +01:00
|
|
|
return new CefTaskRunnerImpl(task_runner);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2013-01-03 18:24:24 +01:00
|
|
|
|
2013-02-09 23:38:24 +01:00
|
|
|
LOG(WARNING) << "Invalid thread id " << threadId;
|
2020-01-15 14:36:24 +01:00
|
|
|
return nullptr;
|
2013-01-03 18:24:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// CefTaskRunnerImpl
|
|
|
|
|
|
|
|
CefTaskRunnerImpl::CefTaskRunnerImpl(
|
2017-09-21 13:57:40 +02:00
|
|
|
scoped_refptr<base::SingleThreadTaskRunner> task_runner)
|
2013-01-03 18:24:24 +01:00
|
|
|
: task_runner_(task_runner) {
|
|
|
|
DCHECK(task_runner_.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2017-09-21 13:57:40 +02:00
|
|
|
scoped_refptr<base::SingleThreadTaskRunner> CefTaskRunnerImpl::GetTaskRunner(
|
2017-05-17 11:29:28 +02:00
|
|
|
CefThreadId threadId) {
|
2020-06-25 04:34:12 +02:00
|
|
|
auto* manager = CefTaskRunnerManager::Get();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!manager) {
|
2020-01-15 14:36:24 +01:00
|
|
|
return nullptr;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2018-03-20 21:15:08 +01:00
|
|
|
|
2013-01-03 18:24:24 +01:00
|
|
|
switch (threadId) {
|
2017-05-17 11:29:28 +02:00
|
|
|
case TID_UI:
|
2023-01-02 23:59:03 +01:00
|
|
|
if (BrowserThread::IsThreadInitialized(BrowserThread::UI)) {
|
2022-05-19 12:28:44 +02:00
|
|
|
return content::GetUIThreadTaskRunner({});
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-05-17 11:29:28 +02:00
|
|
|
break;
|
2018-03-20 21:15:08 +01:00
|
|
|
case TID_FILE_BACKGROUND:
|
2020-06-25 04:34:12 +02:00
|
|
|
return manager->GetBackgroundTaskRunner();
|
2018-03-20 21:15:08 +01:00
|
|
|
case TID_FILE_USER_VISIBLE:
|
2020-06-25 04:34:12 +02:00
|
|
|
return manager->GetUserVisibleTaskRunner();
|
2017-05-17 11:29:28 +02:00
|
|
|
case TID_FILE_USER_BLOCKING:
|
2020-06-25 04:34:12 +02:00
|
|
|
return manager->GetUserBlockingTaskRunner();
|
2018-04-19 17:44:42 +02:00
|
|
|
case TID_PROCESS_LAUNCHER:
|
|
|
|
return content::GetProcessLauncherTaskRunner();
|
2017-05-17 11:29:28 +02:00
|
|
|
case TID_IO:
|
2023-01-02 23:59:03 +01:00
|
|
|
if (BrowserThread::IsThreadInitialized(BrowserThread::IO)) {
|
2022-05-19 12:28:44 +02:00
|
|
|
return content::GetIOThreadTaskRunner({});
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-05-17 11:29:28 +02:00
|
|
|
break;
|
2020-06-25 04:34:12 +02:00
|
|
|
case TID_RENDERER:
|
|
|
|
return manager->GetRenderTaskRunner();
|
2017-05-17 11:29:28 +02:00
|
|
|
default:
|
|
|
|
break;
|
2013-01-03 18:24:24 +01:00
|
|
|
};
|
|
|
|
|
2020-01-15 14:36:24 +01:00
|
|
|
return nullptr;
|
2013-01-03 18:24:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2017-09-21 13:57:40 +02:00
|
|
|
scoped_refptr<base::SingleThreadTaskRunner>
|
2017-05-17 11:29:28 +02:00
|
|
|
CefTaskRunnerImpl::GetCurrentTaskRunner() {
|
2020-06-25 04:34:12 +02:00
|
|
|
auto* manager = CefTaskRunnerManager::Get();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!manager) {
|
2020-06-25 04:34:12 +02:00
|
|
|
return nullptr;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-06-25 04:34:12 +02:00
|
|
|
|
2016-11-23 21:54:29 +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(¤t_id) &&
|
2018-04-19 17:44:42 +02:00
|
|
|
BrowserThread::IsThreadInitialized(current_id)) {
|
2022-05-19 12:28:44 +02:00
|
|
|
if (current_id == BrowserThread::UI) {
|
|
|
|
return content::GetUIThreadTaskRunner({});
|
|
|
|
} else if (current_id == BrowserThread::IO) {
|
|
|
|
return content::GetIOThreadTaskRunner({});
|
|
|
|
} else {
|
|
|
|
NOTREACHED();
|
|
|
|
}
|
2016-11-23 21:54:29 +01:00
|
|
|
}
|
|
|
|
|
2022-05-19 12:28:44 +02:00
|
|
|
// Check for a MessageLoopProxy. This covers all of the named browser and
|
|
|
|
// render process threads, plus a few extra.
|
2023-01-30 18:43:54 +01:00
|
|
|
if (auto task_runner = base::SingleThreadTaskRunner::GetCurrentDefault()) {
|
2022-05-19 12:28:44 +02:00
|
|
|
return task_runner;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2013-01-03 18:24:24 +01:00
|
|
|
|
2022-05-19 12:28:44 +02:00
|
|
|
// Check for a WebWorker thread.
|
|
|
|
return manager->GetWebWorkerTaskRunner();
|
2013-01-03 18:24:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefTaskRunnerImpl::IsSame(CefRefPtr<CefTaskRunner> that) {
|
|
|
|
CefTaskRunnerImpl* impl = static_cast<CefTaskRunnerImpl*>(that.get());
|
|
|
|
return (impl && task_runner_ == impl->task_runner_);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefTaskRunnerImpl::BelongsToCurrentThread() {
|
2017-09-06 23:40:58 +02:00
|
|
|
return task_runner_->RunsTasksInCurrentSequence();
|
2013-01-03 18:24:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefTaskRunnerImpl::BelongsToThread(CefThreadId threadId) {
|
2017-09-21 13:57:40 +02:00
|
|
|
scoped_refptr<base::SingleThreadTaskRunner> task_runner =
|
2013-01-03 18:24:24 +01:00
|
|
|
GetTaskRunner(threadId);
|
|
|
|
return (task_runner_ == task_runner);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefTaskRunnerImpl::PostTask(CefRefPtr<CefTask> task) {
|
|
|
|
return task_runner_->PostTask(FROM_HERE,
|
2021-06-04 03:34:56 +02:00
|
|
|
base::BindOnce(&CefTask::Execute, task.get()));
|
2013-01-03 18:24:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefTaskRunnerImpl::PostDelayedTask(CefRefPtr<CefTask> task,
|
|
|
|
int64 delay_ms) {
|
2017-05-17 11:29:28 +02:00
|
|
|
return task_runner_->PostDelayedTask(
|
2021-06-04 03:34:56 +02:00
|
|
|
FROM_HERE, base::BindOnce(&CefTask::Execute, task.get()),
|
2021-10-19 00:17:16 +02:00
|
|
|
base::Milliseconds(delay_ms));
|
2013-01-03 18:24:24 +01:00
|
|
|
}
|