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.
|
|
|
|
|
|
|
|
#include "libcef/common/task_runner_impl.h"
|
2013-04-16 00:16:01 +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"
|
2013-01-03 18:24:24 +01:00
|
|
|
#include "content/public/browser/browser_thread.h"
|
|
|
|
|
|
|
|
using content::BrowserThread;
|
|
|
|
|
|
|
|
// CefTaskRunner
|
|
|
|
|
|
|
|
// static
|
|
|
|
CefRefPtr<CefTaskRunner> CefTaskRunner::GetForCurrentThread() {
|
|
|
|
scoped_refptr<base::SequencedTaskRunner> task_runner =
|
|
|
|
CefTaskRunnerImpl::GetCurrentTaskRunner();
|
|
|
|
if (task_runner.get())
|
|
|
|
return new CefTaskRunnerImpl(task_runner);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
CefRefPtr<CefTaskRunner> CefTaskRunner::GetForThread(CefThreadId threadId) {
|
|
|
|
scoped_refptr<base::SequencedTaskRunner> task_runner =
|
|
|
|
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(
|
|
|
|
scoped_refptr<base::SequencedTaskRunner> task_runner)
|
|
|
|
: task_runner_(task_runner) {
|
|
|
|
DCHECK(task_runner_.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
scoped_refptr<base::SequencedTaskRunner>
|
|
|
|
CefTaskRunnerImpl::GetTaskRunner(CefThreadId threadId) {
|
|
|
|
// Render process.
|
|
|
|
if (threadId == TID_RENDERER) {
|
|
|
|
CefContentRendererClient* client = CefContentRendererClient::Get();
|
|
|
|
if (client)
|
|
|
|
return client->render_task_runner();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Browser process.
|
|
|
|
int id = -1;
|
|
|
|
switch (threadId) {
|
|
|
|
case TID_UI:
|
|
|
|
id = BrowserThread::UI;
|
|
|
|
break;
|
|
|
|
case TID_DB:
|
|
|
|
id = BrowserThread::DB;
|
|
|
|
break;
|
|
|
|
case TID_FILE:
|
|
|
|
id = BrowserThread::FILE;
|
|
|
|
break;
|
|
|
|
case TID_FILE_USER_BLOCKING:
|
|
|
|
id = BrowserThread::FILE_USER_BLOCKING;
|
|
|
|
break;
|
|
|
|
case TID_PROCESS_LAUNCHER:
|
|
|
|
id = BrowserThread::PROCESS_LAUNCHER;
|
|
|
|
break;
|
|
|
|
case TID_CACHE:
|
|
|
|
id = BrowserThread::CACHE;
|
|
|
|
break;
|
|
|
|
case TID_IO:
|
|
|
|
id = BrowserThread::IO;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
|
2016-06-15 20:11:30 +02:00
|
|
|
if (id >= 0 && CefContentClient::Get() &&
|
|
|
|
CefContentClient::Get()->browser() &&
|
2013-01-03 18:24:24 +01:00
|
|
|
BrowserThread::IsMessageLoopValid(static_cast<BrowserThread::ID>(id))) {
|
2016-07-21 23:21:32 +02:00
|
|
|
// Don't use BrowserThread::GetTaskRunnerForThread because it returns
|
2013-01-03 18:24:24 +01:00
|
|
|
// a new MessageLoopProxy object for each call and makes pointer equality
|
|
|
|
// testing impossible.
|
2013-04-16 00:16:01 +02:00
|
|
|
base::MessageLoop* message_loop =
|
2013-01-03 18:24:24 +01:00
|
|
|
BrowserThread::UnsafeGetMessageLoopForThread(
|
|
|
|
static_cast<BrowserThread::ID>(id));
|
|
|
|
if (message_loop)
|
2015-07-24 02:06:56 +02:00
|
|
|
return message_loop->task_runner();
|
2013-01-03 18:24:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
scoped_refptr<base::SequencedTaskRunner>
|
|
|
|
CefTaskRunnerImpl::GetCurrentTaskRunner() {
|
|
|
|
scoped_refptr<base::SequencedTaskRunner> task_runner;
|
|
|
|
|
|
|
|
// Check for a MessageLoopProxy. This covers all of the named browser and
|
|
|
|
// render process threads, plus a few extra.
|
2015-07-24 02:06:56 +02:00
|
|
|
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() {
|
|
|
|
return task_runner_->RunsTasksOnCurrentThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefTaskRunnerImpl::BelongsToThread(CefThreadId threadId) {
|
|
|
|
scoped_refptr<base::SequencedTaskRunner> task_runner =
|
|
|
|
GetTaskRunner(threadId);
|
|
|
|
return (task_runner_ == task_runner);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefTaskRunnerImpl::PostTask(CefRefPtr<CefTask> task) {
|
|
|
|
return task_runner_->PostTask(FROM_HERE,
|
2014-09-27 01:48:19 +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) {
|
|
|
|
return task_runner_->PostDelayedTask(FROM_HERE,
|
2014-09-27 01:48:19 +02:00
|
|
|
base::Bind(&CefTask::Execute, task.get()),
|
2013-01-03 18:24:24 +01:00
|
|
|
base::TimeDelta::FromMilliseconds(delay_ms));
|
|
|
|
}
|