#ifndef MOJO_CORE_HANDLE_TABLE_H_
#define MOJO_CORE_HANDLE_TABLE_H_
#include <stdint.h>
#include <unordered_map>
#include <vector>
#include "base/gtest_prod_util.h"
#include "base/synchronization/lock.h"
#include "base/trace_event/memory_dump_provider.h"
#include "mojo/core/dispatcher.h"
#include "mojo/core/system_impl_export.h"
#include "mojo/public/c/system/types.h"
namespace mojo {
namespace core {
class MOJO_SYSTEM_IMPL_EXPORT HandleTable
: public base::trace_event::MemoryDumpProvider {
public:
HandleTable();
HandleTable(const HandleTable&) = delete;
HandleTable& operator=(const HandleTable&) = delete;
~HandleTable() override;
base::Lock& GetLock();
MojoHandle AddDispatcher(scoped_refptr<Dispatcher> dispatcher);
bool AddDispatchersFromTransit(
const std::vector<Dispatcher::DispatcherInTransit>& dispatchers,
MojoHandle* handles);
scoped_refptr<Dispatcher> GetDispatcher(MojoHandle handle);
MojoResult GetAndRemoveDispatcher(MojoHandle,
scoped_refptr<Dispatcher>* dispatcher);
MojoResult BeginTransit(
const MojoHandle* handles,
size_t num_handles,
std::vector<Dispatcher::DispatcherInTransit>* dispatchers);
void CompleteTransitAndClose(
const std::vector<Dispatcher::DispatcherInTransit>& dispatchers);
void CancelTransit(
const std::vector<Dispatcher::DispatcherInTransit>& dispatchers);
void GetActiveHandlesForTest(std::vector<MojoHandle>* handles);
private:
FRIEND_TEST_ALL_PREFIXES(HandleTableTest, OnMemoryDump);
bool OnMemoryDump(const base::trace_event::MemoryDumpArgs& args,
base::trace_event::ProcessMemoryDump* pmd) override;
struct Entry {
explicit Entry(scoped_refptr<Dispatcher> dispatcher);
~Entry();
Entry(const Entry& entry);
Entry& operator=(const Entry&) = delete;
const scoped_refptr<Dispatcher> dispatcher;
bool busy = false;
};
class EntriesAccessor {
public:
EntriesAccessor();
~EntriesAccessor();
bool Add(MojoHandle handle, Entry entry);
const scoped_refptr<Dispatcher>* GetDispatcher(MojoHandle handle);
Entry* GetMutable(MojoHandle handle);
enum RemovalCondition { kRemoveOnlyIfBusy, kRemoveOnlyIfNotBusy };
MojoResult Remove(MojoHandle handle,
RemovalCondition removal_condition,
scoped_refptr<Dispatcher>* dispatcher);
const std::unordered_map<MojoHandle, Entry>& GetUnderlyingMap() const;
private:
std::unordered_map<MojoHandle, Entry> handles_;
scoped_refptr<Dispatcher> last_read_dispatcher_;
MojoHandle last_read_handle_ = MOJO_HANDLE_INVALID;
};
EntriesAccessor entries_;
base::Lock lock_;
uintptr_t next_available_handle_ = 1;
};
}
}
#endif