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"
|
2020-06-25 04:34:12 +02:00
|
|
|
#include "libcef/common/task_runner_manager.h"
|
2013-01-03 18:24:24 +01:00
|
|
|
|
|
|
|
#include "base/bind.h"
|
|
|
|
#include "base/location.h"
|
|
|
|
#include "base/logging.h"
|
2020-06-25 04:34:12 +02:00
|
|
|
#include "base/task/post_task.h"
|
2016-05-25 01:35:43 +02:00
|
|
|
#include "base/threading/thread_task_runner_handle.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();
|
|
|
|
if (task_runner.get())
|
|
|
|
return new CefTaskRunnerImpl(task_runner);
|
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);
|
|
|
|
if (task_runner.get())
|
|
|
|
return new CefTaskRunnerImpl(task_runner);
|
|
|
|
|
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();
|
|
|
|
if (!manager)
|
2020-01-15 14:36:24 +01:00
|
|
|
return nullptr;
|
2018-03-20 21:15:08 +01:00
|
|
|
|
2013-01-03 18:24:24 +01:00
|
|
|
int id = -1;
|
|
|
|
switch (threadId) {
|
2017-05-17 11:29:28 +02:00
|
|
|
case TID_UI:
|
|
|
|
id = BrowserThread::UI;
|
|
|
|
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:
|
|
|
|
id = BrowserThread::IO;
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2018-03-20 21:15:08 +01:00
|
|
|
if (id >= 0 &&
|
2018-04-19 17:44:42 +02:00
|
|
|
BrowserThread::IsThreadInitialized(static_cast<BrowserThread::ID>(id))) {
|
2018-11-03 02:15:09 +01:00
|
|
|
// Specify USER_BLOCKING so that BrowserTaskExecutor::GetTaskRunner always
|
|
|
|
// gives us the same TaskRunner object.
|
2019-10-01 15:55:16 +02:00
|
|
|
return base::CreateSingleThreadTaskRunner(
|
2018-11-03 02:15:09 +01:00
|
|
|
{static_cast<BrowserThread::ID>(id),
|
|
|
|
base::TaskPriority::USER_BLOCKING});
|
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();
|
|
|
|
if (!manager)
|
|
|
|
return nullptr;
|
|
|
|
|
2017-09-21 13:57:40 +02:00
|
|
|
scoped_refptr<base::SingleThreadTaskRunner> task_runner;
|
2013-01-03 18:24:24 +01: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)) {
|
2018-11-03 02:15:09 +01:00
|
|
|
// Specify USER_BLOCKING so that BrowserTaskExecutor::GetTaskRunner always
|
|
|
|
// gives us the same TaskRunner object.
|
2019-10-01 15:55:16 +02:00
|
|
|
task_runner = base::CreateSingleThreadTaskRunner(
|
2018-11-03 02:15:09 +01:00
|
|
|
{current_id, base::TaskPriority::USER_BLOCKING});
|
2016-11-23 21:54:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!task_runner.get()) {
|
|
|
|
// Check for a MessageLoopProxy. This covers all of the named browser and
|
|
|
|
// render process threads, plus a few extra.
|
|
|
|
task_runner = base::ThreadTaskRunnerHandle::Get();
|
|
|
|
}
|
2013-01-03 18:24:24 +01:00
|
|
|
|
|
|
|
if (!task_runner.get()) {
|
|
|
|
// Check for a WebWorker thread.
|
2020-06-25 04:34:12 +02:00
|
|
|
return manager->GetWebWorkerTaskRunner();
|
2013-01-03 18:24:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return task_runner;
|
|
|
|
}
|
|
|
|
|
|
|
|
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()),
|
2017-05-17 11:29:28 +02:00
|
|
|
base::TimeDelta::FromMilliseconds(delay_ms));
|
2013-01-03 18:24:24 +01:00
|
|
|
}
|