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"
|
2018-03-20 21:15:08 +01:00
|
|
|
#include "libcef/browser/content_browser_client.h"
|
2017-05-19 11:06:00 +02:00
|
|
|
#include "libcef/common/content_client.h"
|
2013-01-03 18:24:24 +01:00
|
|
|
#include "libcef/renderer/content_renderer_client.h"
|
|
|
|
|
|
|
|
#include "base/bind.h"
|
|
|
|
#include "base/location.h"
|
|
|
|
#include "base/logging.h"
|
2013-07-24 22:15:18 +02:00
|
|
|
#include "base/message_loop/message_loop.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);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
2013-01-03 18:24:24 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2013-01-03 18:24:24 +01:00
|
|
|
// Render process.
|
|
|
|
if (threadId == TID_RENDERER) {
|
|
|
|
CefContentRendererClient* client = CefContentRendererClient::Get();
|
|
|
|
if (client)
|
|
|
|
return client->render_task_runner();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Browser process.
|
2018-03-20 21:15:08 +01:00
|
|
|
CefContentBrowserClient* client = CefContentBrowserClient::Get();
|
|
|
|
if (!client)
|
|
|
|
return NULL;
|
|
|
|
|
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:
|
|
|
|
return client->background_task_runner();
|
|
|
|
case TID_FILE_USER_VISIBLE:
|
|
|
|
return client->user_visible_task_runner();
|
2017-05-17 11:29:28 +02:00
|
|
|
case TID_FILE_USER_BLOCKING:
|
2018-03-20 21:15:08 +01:00
|
|
|
return client->user_blocking_task_runner();
|
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;
|
|
|
|
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-10-02 14:14:11 +02:00
|
|
|
return base::CreateSingleThreadTaskRunnerWithTraits(
|
|
|
|
{static_cast<BrowserThread::ID>(id)});
|
2013-01-03 18:24:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2017-09-21 13:57:40 +02:00
|
|
|
scoped_refptr<base::SingleThreadTaskRunner>
|
2017-05-17 11:29:28 +02:00
|
|
|
CefTaskRunnerImpl::GetCurrentTaskRunner() {
|
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-10-02 14:14:11 +02:00
|
|
|
task_runner = base::CreateSingleThreadTaskRunnerWithTraits({current_id});
|
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.
|
|
|
|
CefContentRendererClient* client = CefContentRendererClient::Get();
|
|
|
|
if (client)
|
|
|
|
task_runner = client->GetCurrentTaskRunner();
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2017-05-17 11:29:28 +02:00
|
|
|
base::Bind(&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(
|
|
|
|
FROM_HERE, base::Bind(&CefTask::Execute, task.get()),
|
|
|
|
base::TimeDelta::FromMilliseconds(delay_ms));
|
2013-01-03 18:24:24 +01:00
|
|
|
}
|