1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef LIBRARIES_NACL_IO_KERNEL_HANDLE_H_
6 #define LIBRARIES_NACL_IO_KERNEL_HANDLE_H_
10 #include <ppapi/c/pp_resource.h>
12 #include "nacl_io/error.h"
13 #include "nacl_io/mount.h"
14 #include "nacl_io/mount_node.h"
15 #include "nacl_io/ossocket.h"
16 #include "nacl_io/ostypes.h"
18 #include "sdk_util/macros.h"
19 #include "sdk_util/ref_object.h"
20 #include "sdk_util/scoped_ref.h"
21 #include "sdk_util/simple_lock.h"
25 class MountNodeSocket;
27 // HandleAttr struct is passed the MountNode in calls
28 // to Read and Write. It contains handle specific state
29 // such as the file offset and the open flags.
31 HandleAttr() : offs(0), flags(0) {}
32 bool IsBlocking() const { return !(flags & O_NONBLOCK); }
38 // KernelHandle provides a reference counted container for the open
39 // file information, such as it's mount, node, access type and offset.
40 // KernelHandle can only be referenced when the KernelProxy lock is held.
41 class KernelHandle : public sdk_util::RefObject {
45 KernelHandle(const ScopedMount& mnt, const ScopedMountNode& node);
48 Error Init(int open_flags);
50 Error Accept(PP_Resource* new_sock, struct sockaddr* addr, socklen_t* len);
51 Error Connect(const struct sockaddr* addr, socklen_t len);
52 Error Fcntl(int request, int* result, ...);
53 Error VFcntl(int request, int* result, va_list args);
54 Error GetDents(struct dirent* pdir, size_t count, int* bytes_written);
55 Error Read(void* buf, size_t nbytes, int* bytes_read);
56 Error Recv(void* buf, size_t len, int flags, int* out_len);
57 Error RecvFrom(void* buf,
60 struct sockaddr* src_addr,
63 // Assumes |out_offset| is non-NULL.
64 Error Seek(off_t offset, int whence, off_t* out_offset);
65 Error Send(const void* buf, size_t len, int flags, int* out_len);
66 Error SendTo(const void* buf,
69 const struct sockaddr* dest_addr,
72 Error Write(const void* buf, size_t nbytes, int* bytes_written);
74 const ScopedMountNode& node() { return node_; }
75 const ScopedMount& mount() { return mount_; }
77 const HandleAttr& Attr() { return handle_attr_; }
79 int OpenMode() { return handle_attr_.flags & 3; }
82 // Returns the MountNodeSocket* if this node is a socket otherwise returns
84 MountNodeSocket* socket_node();
87 ScopedMountNode node_;
88 sdk_util::SimpleLock handle_lock_;
89 HandleAttr handle_attr_;
91 friend class KernelProxy;
92 DISALLOW_COPY_AND_ASSIGN(KernelHandle);
95 typedef sdk_util::ScopedRef<KernelHandle> ScopedKernelHandle;
97 } // namespace nacl_io
99 #endif // LIBRARIES_NACL_IO_KERNEL_HANDLE_H_