#ifndef PPAPI_PROXY_PPB_MESSAGE_LOOP_PROXY_H_
#define PPAPI_PROXY_PPB_MESSAGE_LOOP_PROXY_H_
#include <stdint.h>
#include <memory>
#include "base/functional/bind.h"
#include "base/memory/scoped_refptr.h"
#include "base/run_loop.h"
#include "base/task/single_thread_task_executor.h"
#include "base/task/single_thread_task_runner.h"
#include "ppapi/proxy/interface_proxy.h"
#include "ppapi/proxy/ppapi_proxy_export.h"
#include "ppapi/shared_impl/ppb_message_loop_shared.h"
#include "ppapi/thunk/ppb_message_loop_api.h"
struct PPB_MessageLoop_1_0;
namespace ppapi {
namespace proxy {
class PPAPI_PROXY_EXPORT MessageLoopResource : public MessageLoopShared {
public:
explicit MessageLoopResource(PP_Instance instance);
explicit MessageLoopResource(ForMainThread);
MessageLoopResource(const MessageLoopResource&) = delete;
MessageLoopResource& operator=(const MessageLoopResource&) = delete;
~MessageLoopResource() override;
thunk::PPB_MessageLoop_API* AsPPB_MessageLoop_API() override;
int32_t AttachToCurrentThread() override;
int32_t Run() override;
int32_t PostWork(PP_CompletionCallback callback, int64_t delay_ms) override;
int32_t PostQuit(PP_Bool should_destroy) override;
static MessageLoopResource* GetCurrent();
void DetachFromThread();
bool is_main_thread_loop() const {
return is_main_thread_loop_;
}
const scoped_refptr<base::SingleThreadTaskRunner>& task_runner() {
return task_runner_;
}
void set_currently_handling_blocking_message(bool handling_blocking_message) {
currently_handling_blocking_message_ = handling_blocking_message;
}
private:
struct TaskInfo {
base::Location from_here;
base::OnceClosure closure;
int64_t delay_ms;
};
bool IsCurrent() const;
void PostClosure(const base::Location& from_here,
base::OnceClosure closure,
int64_t delay_ms) override;
base::SingleThreadTaskRunner* GetTaskRunner() override;
bool CurrentlyHandlingBlockingMessage() override;
void QuitRunLoopWhenIdle();
static void ReleaseMessageLoop(void* value);
std::unique_ptr<base::SingleThreadTaskExecutor> single_thread_task_executor_;
scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
base::RunLoop* run_loop_ = nullptr;
int nested_invocations_;
bool destroyed_;
bool should_destroy_;
bool is_main_thread_loop_;
bool currently_handling_blocking_message_;
std::vector<TaskInfo> pending_tasks_;
};
class PPB_MessageLoop_Proxy : public InterfaceProxy {
public:
explicit PPB_MessageLoop_Proxy(Dispatcher* dispatcher);
PPB_MessageLoop_Proxy(const PPB_MessageLoop_Proxy&) = delete;
PPB_MessageLoop_Proxy& operator=(const PPB_MessageLoop_Proxy&) = delete;
~PPB_MessageLoop_Proxy() override;
static const PPB_MessageLoop_1_0* GetInterface();
};
}
}
#endif