mirror of
https://bitbucket.org/chromiumembedded/cef
synced 2025-01-08 08:11:36 +01:00
- 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:
parent
18b662fe26
commit
27f50277e5
@ -41,6 +41,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include "include/base/cef_bind.h"
|
||||||
#include "include/base/cef_logging.h"
|
#include "include/base/cef_logging.h"
|
||||||
#include "include/base/cef_macros.h"
|
#include "include/base/cef_macros.h"
|
||||||
#include "include/cef_task.h"
|
#include "include/cef_task.h"
|
||||||
@ -50,6 +51,46 @@
|
|||||||
#define CEF_REQUIRE_FILE_THREAD() DCHECK(CefCurrentlyOn(TID_FILE));
|
#define CEF_REQUIRE_FILE_THREAD() DCHECK(CefCurrentlyOn(TID_FILE));
|
||||||
#define CEF_REQUIRE_RENDERER_THREAD() DCHECK(CefCurrentlyOn(TID_RENDERER));
|
#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|.
|
// Helper class to manage a scoped copy of |argv|.
|
||||||
///
|
///
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
#ifndef CEF_TESTS_CEFCLIENT_MAIN_MESSAGE_LOOP_H_
|
#ifndef CEF_TESTS_CEFCLIENT_MAIN_MESSAGE_LOOP_H_
|
||||||
#define 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_bind.h"
|
||||||
#include "include/base/cef_scoped_ptr.h"
|
#include "include/base/cef_scoped_ptr.h"
|
||||||
@ -48,14 +49,6 @@ class MainMessageLoop {
|
|||||||
// Post a closure for execution on the main message loop.
|
// Post a closure for execution on the main message loop.
|
||||||
void PostClosure(const base::Closure& closure);
|
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:
|
protected:
|
||||||
// Only allow deletion via scoped_ptr.
|
// Only allow deletion via scoped_ptr.
|
||||||
friend struct base::DefaultDeleter<MainMessageLoop>;
|
friend struct base::DefaultDeleter<MainMessageLoop>;
|
||||||
@ -64,12 +57,6 @@ class MainMessageLoop {
|
|||||||
virtual ~MainMessageLoop();
|
virtual ~MainMessageLoop();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Helper for deleting |object|.
|
|
||||||
template<class T>
|
|
||||||
static void DeleteHelper(const T* object) {
|
|
||||||
delete object;
|
|
||||||
}
|
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(MainMessageLoop);
|
DISALLOW_COPY_AND_ASSIGN(MainMessageLoop);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -110,7 +97,8 @@ struct DeleteOnMainThread {
|
|||||||
if (CURRENTLY_ON_MAIN_THREAD()) {
|
if (CURRENTLY_ON_MAIN_THREAD()) {
|
||||||
delete x;
|
delete x;
|
||||||
} else {
|
} else {
|
||||||
client::MainMessageLoop::Get()->DeleteSoon(x);
|
client::MainMessageLoop::Get()->PostClosure(
|
||||||
|
base::Bind(&DeleteOnMainThread::Destruct<T>, x));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user