2010-07-24 03:13:55 +02:00
|
|
|
// Copyright (c) 2010 The Chromium Embedded Framework Authors.
|
|
|
|
// Portions copyright (c) 2010 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
#include "libcef/cef_thread.h"
|
2010-07-24 03:13:55 +02:00
|
|
|
|
2011-09-10 21:40:03 +02:00
|
|
|
#include "base/compiler_specific.h"
|
2010-07-24 03:13:55 +02:00
|
|
|
#include "base/message_loop.h"
|
|
|
|
#include "base/message_loop_proxy.h"
|
|
|
|
|
2012-02-10 17:22:47 +01:00
|
|
|
#if defined(OS_WIN)
|
|
|
|
#include <Objbase.h> // NOLINT(build/include_order)
|
|
|
|
#endif
|
|
|
|
|
2010-07-24 03:13:55 +02:00
|
|
|
using base::MessageLoopProxy;
|
|
|
|
|
|
|
|
// Friendly names for the well-known threads.
|
|
|
|
static const char* cef_thread_names[CefThread::ID_COUNT] = {
|
|
|
|
"Cef_UIThread", // UI
|
|
|
|
"Cef_FileThread", // FILE
|
|
|
|
"Cef_IOThread", // IO
|
|
|
|
};
|
|
|
|
|
|
|
|
// An implementation of MessageLoopProxy to be used in conjunction
|
|
|
|
// with CefThread.
|
|
|
|
class CefThreadMessageLoopProxy : public MessageLoopProxy {
|
|
|
|
public:
|
|
|
|
explicit CefThreadMessageLoopProxy(CefThread::ID identifier)
|
|
|
|
: id_(identifier) {
|
|
|
|
}
|
|
|
|
|
2012-04-05 00:32:24 +02:00
|
|
|
// TaskRunner implementation.
|
|
|
|
virtual bool PostDelayedTask(const tracked_objects::Location& from_here,
|
|
|
|
const base::Closure& task,
|
|
|
|
base::TimeDelta delay) OVERRIDE {
|
|
|
|
return CefThread::PostDelayedTask(id_, from_here, task, delay);
|
|
|
|
}
|
|
|
|
|
|
|
|
// SequencedTaskRunner implementation.
|
|
|
|
virtual bool PostNonNestableDelayedTask(
|
|
|
|
const tracked_objects::Location& from_here,
|
|
|
|
const base::Closure& task,
|
|
|
|
base::TimeDelta delay) OVERRIDE {
|
|
|
|
return CefThread::PostNonNestableDelayedTask(id_, from_here, task, delay);
|
|
|
|
}
|
|
|
|
|
2012-02-18 01:19:52 +01:00
|
|
|
virtual bool RunsTasksOnCurrentThread() const OVERRIDE {
|
2010-07-24 03:13:55 +02:00
|
|
|
return CefThread::CurrentlyOn(id_);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
CefThread::ID id_;
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(CefThreadMessageLoopProxy);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-02-15 19:07:24 +01:00
|
|
|
base::Lock CefThread::lock_;
|
2010-07-24 03:13:55 +02:00
|
|
|
|
|
|
|
CefThread* CefThread::cef_threads_[ID_COUNT];
|
|
|
|
|
|
|
|
CefThread::CefThread(CefThread::ID identifier)
|
|
|
|
: Thread(cef_thread_names[identifier]),
|
|
|
|
identifier_(identifier) {
|
|
|
|
Initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
CefThread::CefThread(ID identifier, MessageLoop* message_loop)
|
|
|
|
: Thread(cef_thread_names[identifier]),
|
|
|
|
identifier_(identifier) {
|
|
|
|
message_loop->set_thread_name(cef_thread_names[identifier]);
|
|
|
|
set_message_loop(message_loop);
|
|
|
|
Initialize();
|
|
|
|
}
|
|
|
|
|
2012-02-10 17:22:47 +01:00
|
|
|
void CefThread::Init() {
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
// Initializes the COM library on the current thread.
|
|
|
|
CoInitialize(NULL);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(OS_MACOSX)
|
|
|
|
autorelease_pool_.reset(new base::mac::ScopedNSAutoreleasePool);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefThread::Cleanup() {
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
// Closes the COM library on the current thread. CoInitialize must
|
|
|
|
// be balanced by a corresponding call to CoUninitialize.
|
|
|
|
CoUninitialize();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(OS_MACOSX)
|
|
|
|
autorelease_pool_.reset(NULL);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-07-24 03:13:55 +02:00
|
|
|
void CefThread::Initialize() {
|
2011-02-15 19:07:24 +01:00
|
|
|
base::AutoLock lock(lock_);
|
2010-07-24 03:13:55 +02:00
|
|
|
DCHECK(identifier_ >= 0 && identifier_ < ID_COUNT);
|
|
|
|
DCHECK(cef_threads_[identifier_] == NULL);
|
|
|
|
cef_threads_[identifier_] = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefThread::~CefThread() {
|
|
|
|
// Stop the thread here, instead of the parent's class destructor. This is so
|
|
|
|
// that if there are pending tasks that run, code that checks that it's on the
|
|
|
|
// correct CefThread succeeds.
|
|
|
|
Stop();
|
|
|
|
|
2011-02-15 19:07:24 +01:00
|
|
|
base::AutoLock lock(lock_);
|
2010-07-24 03:13:55 +02:00
|
|
|
cef_threads_[identifier_] = NULL;
|
|
|
|
#ifndef NDEBUG
|
|
|
|
// Double check that the threads are ordererd correctly in the enumeration.
|
|
|
|
for (int i = identifier_ + 1; i < ID_COUNT; ++i) {
|
|
|
|
DCHECK(!cef_threads_[i]) <<
|
|
|
|
"Threads must be listed in the reverse order that they die";
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
bool CefThread::IsWellKnownThread(ID identifier) {
|
2011-02-15 19:07:24 +01:00
|
|
|
base::AutoLock lock(lock_);
|
2010-07-24 03:13:55 +02:00
|
|
|
return (identifier >= 0 && identifier < ID_COUNT &&
|
|
|
|
cef_threads_[identifier]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
bool CefThread::CurrentlyOn(ID identifier) {
|
2011-02-15 19:07:24 +01:00
|
|
|
base::AutoLock lock(lock_);
|
2010-07-24 03:13:55 +02:00
|
|
|
DCHECK(identifier >= 0 && identifier < ID_COUNT);
|
|
|
|
return cef_threads_[identifier] &&
|
|
|
|
cef_threads_[identifier]->message_loop() == MessageLoop::current();
|
|
|
|
}
|
|
|
|
|
2011-09-10 21:40:03 +02:00
|
|
|
// static
|
|
|
|
bool CefThread::PostTask(ID identifier,
|
|
|
|
const tracked_objects::Location& from_here,
|
|
|
|
const base::Closure& task) {
|
2012-08-07 15:58:35 +02:00
|
|
|
return PostTaskHelper(identifier, from_here, task, base::TimeDelta(), true);
|
2011-09-10 21:40:03 +02:00
|
|
|
}
|
|
|
|
|
2012-04-05 00:32:24 +02:00
|
|
|
// static
|
|
|
|
bool CefThread::PostDelayedTask(ID identifier,
|
|
|
|
const tracked_objects::Location& from_here,
|
|
|
|
const base::Closure& task,
|
|
|
|
base::TimeDelta delay) {
|
2012-08-07 15:58:35 +02:00
|
|
|
return PostTaskHelper(identifier, from_here, task, delay, true);
|
2012-04-05 00:32:24 +02:00
|
|
|
}
|
|
|
|
|
2011-09-10 21:40:03 +02:00
|
|
|
// static
|
|
|
|
bool CefThread::PostNonNestableTask(
|
|
|
|
ID identifier,
|
|
|
|
const tracked_objects::Location& from_here,
|
|
|
|
const base::Closure& task) {
|
2012-08-07 15:58:35 +02:00
|
|
|
return PostTaskHelper(identifier, from_here, task, base::TimeDelta(), false);
|
2011-09-10 21:40:03 +02:00
|
|
|
}
|
|
|
|
|
2012-04-05 00:32:24 +02:00
|
|
|
// static
|
|
|
|
bool CefThread::PostNonNestableDelayedTask(
|
|
|
|
ID identifier,
|
|
|
|
const tracked_objects::Location& from_here,
|
|
|
|
const base::Closure& task,
|
|
|
|
base::TimeDelta delay) {
|
2012-08-07 15:58:35 +02:00
|
|
|
return PostTaskHelper(identifier, from_here, task, delay, false);
|
2012-04-05 00:32:24 +02:00
|
|
|
}
|
|
|
|
|
2010-07-24 03:13:55 +02:00
|
|
|
// static
|
|
|
|
bool CefThread::GetCurrentThreadIdentifier(ID* identifier) {
|
|
|
|
MessageLoop* cur_message_loop = MessageLoop::current();
|
|
|
|
for (int i = 0; i < ID_COUNT; ++i) {
|
|
|
|
if (cef_threads_[i] &&
|
|
|
|
cef_threads_[i]->message_loop() == cur_message_loop) {
|
|
|
|
*identifier = cef_threads_[i]->identifier_;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
scoped_refptr<MessageLoopProxy> CefThread::GetMessageLoopProxyForThread(
|
|
|
|
ID identifier) {
|
|
|
|
scoped_refptr<MessageLoopProxy> proxy =
|
|
|
|
new CefThreadMessageLoopProxy(identifier);
|
|
|
|
return proxy;
|
|
|
|
}
|
|
|
|
|
2011-09-10 21:40:03 +02:00
|
|
|
// static
|
|
|
|
bool CefThread::PostTaskHelper(
|
|
|
|
ID identifier,
|
|
|
|
const tracked_objects::Location& from_here,
|
|
|
|
const base::Closure& task,
|
2012-08-07 15:58:35 +02:00
|
|
|
base::TimeDelta delay,
|
2011-09-10 21:40:03 +02:00
|
|
|
bool nestable) {
|
|
|
|
DCHECK(identifier >= 0 && identifier < ID_COUNT);
|
|
|
|
// Optimization: to avoid unnecessary locks, we listed the ID enumeration in
|
|
|
|
// order of lifetime. So no need to lock if we know that the other thread
|
|
|
|
// outlives this one.
|
|
|
|
// Note: since the array is so small, ok to loop instead of creating a map,
|
|
|
|
// which would require a lock because std::map isn't thread safe, defeating
|
|
|
|
// the whole purpose of this optimization.
|
|
|
|
ID current_thread;
|
|
|
|
bool guaranteed_to_outlive_target_thread =
|
|
|
|
GetCurrentThreadIdentifier(¤t_thread) &&
|
|
|
|
current_thread >= identifier;
|
|
|
|
|
|
|
|
if (!guaranteed_to_outlive_target_thread)
|
|
|
|
lock_.Acquire();
|
|
|
|
|
|
|
|
MessageLoop* message_loop = cef_threads_[identifier] ?
|
|
|
|
cef_threads_[identifier]->message_loop() : NULL;
|
|
|
|
if (message_loop) {
|
|
|
|
if (nestable) {
|
2012-08-07 15:58:35 +02:00
|
|
|
message_loop->PostDelayedTask(from_here, task, delay);
|
2011-09-10 21:40:03 +02:00
|
|
|
} else {
|
2012-08-07 15:58:35 +02:00
|
|
|
message_loop->PostNonNestableDelayedTask(from_here, task, delay);
|
2011-09-10 21:40:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!guaranteed_to_outlive_target_thread)
|
|
|
|
lock_.Release();
|
|
|
|
|
|
|
|
return !!message_loop;
|
|
|
|
}
|