Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / ppapi / proxy / serialized_handle.cc
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.
4
5 #include "ppapi/proxy/serialized_handle.h"
6
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"
12
13 #if defined(OS_NACL)
14 #include <unistd.h>
15 #endif
16
17 namespace ppapi {
18 namespace proxy {
19
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) {
27 }
28
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) {
36 }
37
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) {
46 }
47
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) {
57 }
58
59 bool SerializedHandle::IsHandleValid() const {
60   switch (type_) {
61     case SHARED_MEMORY:
62       return base::SharedMemory::IsHandleValid(shm_handle_);
63     case SOCKET:
64     case FILE:
65       return !(IPC::InvalidPlatformFileForTransit() == descriptor_);
66     case INVALID:
67       return false;
68     // No default so the compiler will warn us if a new type is added.
69   }
70   return false;
71 }
72
73 void SerializedHandle::Close() {
74   if (IsHandleValid()) {
75     switch (type_) {
76       case INVALID:
77         NOTREACHED();
78         break;
79       case SHARED_MEMORY:
80         base::SharedMemory::CloseHandle(shm_handle_);
81         break;
82       case SOCKET:
83       case FILE:
84         base::PlatformFile file =
85             IPC::PlatformFileForTransitToPlatformFile(descriptor_);
86 #if !defined(OS_NACL)
87         base::ClosePlatformFile(file);
88 #else
89         close(file);
90 #endif
91         break;
92       // No default so the compiler will warn us if a new type is added.
93     }
94   }
95   *this = SerializedHandle();
96 }
97
98 // static
99 bool SerializedHandle::WriteHeader(const Header& hdr, Pickle* pickle) {
100   if (!pickle->WriteInt(hdr.type))
101     return false;
102   if (hdr.type == SHARED_MEMORY) {
103     if (!pickle->WriteUInt32(hdr.size))
104       return false;
105   }
106   if (hdr.type == FILE) {
107     if (!pickle->WriteInt(hdr.open_flags) || !pickle->WriteInt(hdr.file_io))
108       return false;
109   }
110   return true;
111 }
112
113 // static
114 bool SerializedHandle::ReadHeader(PickleIterator* iter, Header* hdr) {
115   *hdr = Header(INVALID, 0, 0, 0);
116   int type = 0;
117   if (!iter->ReadInt(&type))
118     return false;
119   bool valid_type = false;
120   switch (type) {
121     case SHARED_MEMORY: {
122       uint32 size = 0;
123       if (!iter->ReadUInt32(&size))
124         return false;
125       hdr->size = size;
126       valid_type = true;
127       break;
128     }
129     case FILE: {
130       int open_flags = 0;
131       PP_Resource file_io = 0;
132       if (!iter->ReadInt(&open_flags) || !iter->ReadInt(&file_io))
133         return false;
134       hdr->open_flags = open_flags;
135       hdr->file_io = file_io;
136       valid_type = true;
137     }
138     case SOCKET:
139     case INVALID:
140       valid_type = true;
141       break;
142     // No default so the compiler will warn us if a new type is added.
143   }
144   if (valid_type)
145     hdr->type = Type(type);
146   return valid_type;
147 }
148
149 }  // namespace proxy
150 }  // namespace ppapi