Pepper: add quota_amount and max_written_offset fields to SerializedHandle.
[chromium-blink-merge.git] / ppapi / proxy / serialized_handle.cc
blobfb3404e74646b352d0431d4d265a43ae8f213558
1 // Copyright (c) 2013 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 #include "ppapi/proxy/serialized_handle.h"
7 #include "base/memory/shared_memory.h"
8 #include "base/pickle.h"
9 #include "base/platform_file.h"
10 #include "build/build_config.h"
11 #include "ipc/ipc_platform_file.h"
13 #if defined(OS_NACL)
14 #include <unistd.h>
15 #endif
17 namespace ppapi {
18 namespace proxy {
20 SerializedHandle::SerializedHandle()
21 : type_(INVALID),
22 shm_handle_(base::SharedMemory::NULLHandle()),
23 size_(0),
24 descriptor_(IPC::InvalidPlatformFileForTransit()),
25 open_flags_(0),
26 file_io_(0) {
29 SerializedHandle::SerializedHandle(Type type_param)
30 : type_(type_param),
31 shm_handle_(base::SharedMemory::NULLHandle()),
32 size_(0),
33 descriptor_(IPC::InvalidPlatformFileForTransit()),
34 open_flags_(0),
35 file_io_(0) {
38 SerializedHandle::SerializedHandle(const base::SharedMemoryHandle& handle,
39 uint32 size)
40 : type_(SHARED_MEMORY),
41 shm_handle_(handle),
42 size_(size),
43 descriptor_(IPC::InvalidPlatformFileForTransit()),
44 open_flags_(0),
45 file_io_(0) {
48 SerializedHandle::SerializedHandle(
49 Type type,
50 const IPC::PlatformFileForTransit& socket_descriptor)
51 : type_(type),
52 shm_handle_(base::SharedMemory::NULLHandle()),
53 size_(0),
54 descriptor_(socket_descriptor),
55 open_flags_(0),
56 file_io_(0) {
59 bool SerializedHandle::IsHandleValid() const {
60 switch (type_) {
61 case SHARED_MEMORY:
62 return base::SharedMemory::IsHandleValid(shm_handle_);
63 case SOCKET:
64 case CHANNEL_HANDLE:
65 case FILE:
66 return !(IPC::InvalidPlatformFileForTransit() == descriptor_);
67 case INVALID:
68 return false;
69 // No default so the compiler will warn us if a new type is added.
71 return false;
74 void SerializedHandle::Close() {
75 if (IsHandleValid()) {
76 switch (type_) {
77 case INVALID:
78 NOTREACHED();
79 break;
80 case SHARED_MEMORY:
81 base::SharedMemory::CloseHandle(shm_handle_);
82 break;
83 case SOCKET:
84 case CHANNEL_HANDLE:
85 case FILE:
86 base::PlatformFile file =
87 IPC::PlatformFileForTransitToPlatformFile(descriptor_);
88 #if !defined(OS_NACL)
89 base::ClosePlatformFile(file);
90 #else
91 close(file);
92 #endif
93 break;
94 // No default so the compiler will warn us if a new type is added.
97 *this = SerializedHandle();
100 // static
101 bool SerializedHandle::WriteHeader(const Header& hdr, Pickle* pickle) {
102 if (!pickle->WriteInt(hdr.type))
103 return false;
104 if (hdr.type == SHARED_MEMORY) {
105 if (!pickle->WriteUInt32(hdr.size))
106 return false;
108 if (hdr.type == FILE) {
109 if (!pickle->WriteInt(hdr.open_flags) || !pickle->WriteInt(hdr.file_io))
110 return false;
112 return true;
115 // static
116 bool SerializedHandle::ReadHeader(PickleIterator* iter, Header* hdr) {
117 *hdr = Header(INVALID, 0, 0, 0);
118 int type = 0;
119 if (!iter->ReadInt(&type))
120 return false;
121 bool valid_type = false;
122 switch (type) {
123 case SHARED_MEMORY: {
124 uint32 size = 0;
125 if (!iter->ReadUInt32(&size))
126 return false;
127 hdr->size = size;
128 valid_type = true;
129 break;
131 case FILE: {
132 int open_flags = 0;
133 PP_Resource file_io = 0;
134 if (!iter->ReadInt(&open_flags) || !iter->ReadInt(&file_io))
135 return false;
136 hdr->open_flags = open_flags;
137 hdr->file_io = file_io;
138 valid_type = true;
140 case SOCKET:
141 case CHANNEL_HANDLE:
142 case INVALID:
143 valid_type = true;
144 break;
145 // No default so the compiler will warn us if a new type is added.
147 if (valid_type)
148 hdr->type = Type(type);
149 return valid_type;
152 } // namespace proxy
153 } // namespace ppapi