#ifndef PPAPI_PROXY_SERIALIZED_HANDLES_H_
#define PPAPI_PROXY_SERIALIZED_HANDLES_H_
#include <string>
#include <vector>
#include "base/atomicops.h"
#include "base/basictypes.h"
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/memory/shared_memory.h"
#include "build/build_config.h"
#include "ipc/ipc_platform_file.h"
#include "ppapi/c/pp_resource.h"
#include "ppapi/proxy/ppapi_proxy_export.h"
class Pickle;
namespace ppapi {
namespace proxy {
class PPAPI_PROXY_EXPORT SerializedHandle {
public:
enum Type { INVALID, SHARED_MEMORY, SOCKET, FILE };
struct Header {
Header() : type(INVALID), size(0), open_flags(0) {}
Header(Type type_arg,
uint32 size_arg,
int32 open_flags_arg,
PP_Resource file_io_arg)
: type(type_arg),
size(size_arg),
open_flags(open_flags_arg),
file_io(file_io_arg) {
}
Type type;
uint32 size;
int32 open_flags;
PP_Resource file_io;
};
SerializedHandle();
explicit SerializedHandle(Type type);
SerializedHandle(const base::SharedMemoryHandle& handle, uint32 size);
SerializedHandle(const Type type,
const IPC::PlatformFileForTransit& descriptor);
Type type() const { return type_; }
bool is_shmem() const { return type_ == SHARED_MEMORY; }
bool is_socket() const { return type_ == SOCKET; }
bool is_file() const { return type_ == FILE; }
const base::SharedMemoryHandle& shmem() const {
DCHECK(is_shmem());
return shm_handle_;
}
uint32 size() const {
DCHECK(is_shmem());
return size_;
}
const IPC::PlatformFileForTransit& descriptor() const {
DCHECK(is_socket() || is_file());
return descriptor_;
}
int32 open_flags() const {
return open_flags_;
}
PP_Resource file_io() const {
return file_io_;
}
void set_shmem(const base::SharedMemoryHandle& handle, uint32 size) {
type_ = SHARED_MEMORY;
shm_handle_ = handle;
size_ = size;
descriptor_ = IPC::InvalidPlatformFileForTransit();
}
void set_socket(const IPC::PlatformFileForTransit& socket) {
type_ = SOCKET;
descriptor_ = socket;
shm_handle_ = base::SharedMemory::NULLHandle();
size_ = 0;
}
void set_file_handle(const IPC::PlatformFileForTransit& descriptor,
int32 open_flags,
PP_Resource file_io) {
type_ = FILE;
descriptor_ = descriptor;
shm_handle_ = base::SharedMemory::NULLHandle();
size_ = 0;
open_flags_ = open_flags;
file_io_ = file_io;
}
void set_null_shmem() {
set_shmem(base::SharedMemory::NULLHandle(), 0);
}
void set_null_socket() {
set_socket(IPC::InvalidPlatformFileForTransit());
}
void set_null_file_handle() {
set_file_handle(IPC::InvalidPlatformFileForTransit(), 0, 0);
}
bool IsHandleValid() const;
Header header() const {
return Header(type_, size_, open_flags_, file_io_);
}
void Close();
static bool WriteHeader(const Header& hdr, Pickle* pickle);
static bool ReadHeader(PickleIterator* iter, Header* hdr);
private:
Type type_;
base::SharedMemoryHandle shm_handle_;
uint32 size_;
IPC::PlatformFileForTransit descriptor_;
int32 open_flags_;
PP_Resource file_io_;
};
}
}
#endif