#ifndef PPAPI_SHARED_IMPL_THREAD_AWARE_CALLBACK_H_
#define PPAPI_SHARED_IMPL_THREAD_AWARE_CALLBACK_H_
#include "base/basictypes.h"
#include "base/bind.h"
#include "base/memory/ref_counted.h"
#include "ppapi/shared_impl/ppapi_shared_export.h"
#include "ppapi/shared_impl/proxy_lock.h"
namespace ppapi {
class MessageLoopShared;
namespace internal {
class PPAPI_SHARED_EXPORT ThreadAwareCallbackBase {
protected:
ThreadAwareCallbackBase();
~ThreadAwareCallbackBase();
static bool HasTargetLoop();
void InternalRunOnTargetThread(const base::Closure& closure);
private:
class Core;
scoped_refptr<MessageLoopShared> target_loop_;
scoped_refptr<Core> core_;
DISALLOW_COPY_AND_ASSIGN(ThreadAwareCallbackBase);
};
}
template <class FuncType>
class ThreadAwareCallback : public internal::ThreadAwareCallbackBase {
public:
static ThreadAwareCallback* Create(FuncType func) {
if (!func || !HasTargetLoop())
return NULL;
return new ThreadAwareCallback(func);
}
~ThreadAwareCallback() {}
void RunOnTargetThread() { InternalRunOnTargetThread(base::Bind(func_)); }
template <class P1>
void RunOnTargetThread(const P1& p1) {
InternalRunOnTargetThread(base::Bind(func_, p1));
}
template <class P1, class P2>
void RunOnTargetThread(const P1& p1, const P2& p2) {
InternalRunOnTargetThread(base::Bind(func_, p1, p2));
}
template <class P1, class P2, class P3>
void RunOnTargetThread(const P1& p1, const P2& p2, const P3& p3) {
InternalRunOnTargetThread(base::Bind(func_, p1, p2, p3));
}
template <class P1, class P2, class P3, class P4>
void RunOnTargetThread(const P1& p1,
const P2& p2,
const P3& p3,
const P4& p4) {
InternalRunOnTargetThread(base::Bind(func_, p1, p2, p3, p4));
}
template <class P1, class P2, class P3, class P4, class P5>
void RunOnTargetThread(const P1& p1,
const P2& p2,
const P3& p3,
const P4& p4,
const P5& p5) {
InternalRunOnTargetThread(base::Bind(func_, p1, p2, p3, p4, p5));
}
private:
explicit ThreadAwareCallback(FuncType func) : func_(func) {}
FuncType func_;
};
}
#endif