- Add CefDeleteOnThread helper for deleting ref-counted types on a named CEF thread.

- cefclient: Fix implementation of DeleteOnMainThread.

git-svn-id: https://chromiumembedded.googlecode.com/svn/branches/2272@2027 5089003a-bbd8-11dd-ad1f-f1f9622dbc98
This commit is contained in:
Marshall Greenblatt 2015-02-10 22:09:03 +00:00
parent 18b662fe26
commit 27f50277e5
2 changed files with 44 additions and 15 deletions

View File

@ -41,6 +41,7 @@
#include <string>
#include <vector>
#include "include/base/cef_bind.h"
#include "include/base/cef_logging.h"
#include "include/base/cef_macros.h"
#include "include/cef_task.h"
@ -50,6 +51,46 @@
#define CEF_REQUIRE_FILE_THREAD() DCHECK(CefCurrentlyOn(TID_FILE));
#define CEF_REQUIRE_RENDERER_THREAD() DCHECK(CefCurrentlyOn(TID_RENDERER));
// Use this struct in conjuction with refcounted types to ensure that an
// object is deleted on the specified thread. For example:
//
// class Foo : public base::RefCountedThreadSafe<Foo, CefDeleteOnUIThread> {
// public:
// Foo();
// void DoSomething();
//
// private:
// // Allow deletion via scoped_refptr only.
// friend struct CefDeleteOnThread<TID_UI>;
// friend class base::RefCountedThreadSafe<Foo, CefDeleteOnUIThread>;
//
// virtual ~Foo() {}
// };
//
// base::scoped_refptr<Foo> foo = new Foo();
// foo->DoSomething();
// foo = NULL; // Deletion of |foo| will occur on the UI thread.
//
template<CefThreadId thread>
struct CefDeleteOnThread {
template<typename T>
static void Destruct(const T* x) {
if (CefCurrentlyOn(thread)) {
delete x;
} else {
CefPostTask(thread,
base::Bind(&CefDeleteOnThread<thread>::Destruct<T>, x));
}
}
};
struct CefDeleteOnUIThread : public CefDeleteOnThread<TID_UI> { };
struct CefDeleteOnIOThread : public CefDeleteOnThread<TID_IO> { };
struct CefDeleteOnFileThread : public CefDeleteOnThread<TID_FILE> { };
struct CefDeleteOnRendererThread : public CefDeleteOnThread<TID_RENDERER> { };
///
// Helper class to manage a scoped copy of |argv|.
///

View File

@ -4,6 +4,7 @@
#ifndef CEF_TESTS_CEFCLIENT_MAIN_MESSAGE_LOOP_H_
#define CEF_TESTS_CEFCLIENT_MAIN_MESSAGE_LOOP_H_
#pragma once
#include "include/base/cef_bind.h"
#include "include/base/cef_scoped_ptr.h"
@ -48,14 +49,6 @@ class MainMessageLoop {
// Post a closure for execution on the main message loop.
void PostClosure(const base::Closure& closure);
// Used in combination with DeleteOnMainThread to delete |object| on the
// correct thread.
template<class T>
void DeleteSoon(const T* object) {
// Execute DeleteHelper on the main thread.
PostClosure(base::Bind(&MainMessageLoop::DeleteHelper, object));
}
protected:
// Only allow deletion via scoped_ptr.
friend struct base::DefaultDeleter<MainMessageLoop>;
@ -64,12 +57,6 @@ class MainMessageLoop {
virtual ~MainMessageLoop();
private:
// Helper for deleting |object|.
template<class T>
static void DeleteHelper(const T* object) {
delete object;
}
DISALLOW_COPY_AND_ASSIGN(MainMessageLoop);
};
@ -110,7 +97,8 @@ struct DeleteOnMainThread {
if (CURRENTLY_ON_MAIN_THREAD()) {
delete x;
} else {
client::MainMessageLoop::Get()->DeleteSoon(x);
client::MainMessageLoop::Get()->PostClosure(
base::Bind(&DeleteOnMainThread::Destruct<T>, x));
}
}
};