1 | /* |
2 | * Copyright (C) 2010 Apple Inc. All rights reserved. |
3 | * Copyright (C) 2017 Sony Interactive Entertainment Inc. |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
8 | * 1. Redistributions of source code must retain the above copyright |
9 | * notice, this list of conditions and the following disclaimer. |
10 | * 2. Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. |
13 | * |
14 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
15 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
16 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
17 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
18 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
19 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
20 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
21 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
22 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
23 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
24 | * THE POSSIBILITY OF SUCH DAMAGE. |
25 | */ |
26 | |
27 | # pragma once |
28 | |
29 | #if OS(DARWIN) && !USE(UNIX_DOMAIN_SOCKETS) |
30 | #include <mach/mach_init.h> |
31 | #include <mach/mach_traps.h> |
32 | #endif |
33 | |
34 | #if OS(WINDOWS) |
35 | #include <windows.h> |
36 | #endif |
37 | |
38 | namespace IPC { |
39 | |
40 | class Decoder; |
41 | class Encoder; |
42 | |
43 | class Attachment { |
44 | public: |
45 | Attachment(); |
46 | |
47 | enum Type { |
48 | Uninitialized, |
49 | #if USE(UNIX_DOMAIN_SOCKETS) |
50 | SocketType, |
51 | MappedMemoryType, |
52 | #elif OS(DARWIN) |
53 | MachPortType |
54 | #endif |
55 | }; |
56 | |
57 | #if USE(UNIX_DOMAIN_SOCKETS) |
58 | Attachment(Attachment&&); |
59 | Attachment& operator=(Attachment&&); |
60 | Attachment(int fileDescriptor, size_t); |
61 | Attachment(int fileDescriptor); |
62 | ~Attachment(); |
63 | #elif OS(DARWIN) |
64 | Attachment(mach_port_name_t, mach_msg_type_name_t disposition); |
65 | #elif OS(WINDOWS) |
66 | Attachment(HANDLE handle) |
67 | : m_handle(handle) |
68 | { } |
69 | #endif |
70 | |
71 | Type type() const { return m_type; } |
72 | |
73 | #if USE(UNIX_DOMAIN_SOCKETS) |
74 | size_t size() const { return m_size; } |
75 | |
76 | int releaseFileDescriptor() { int temp = m_fileDescriptor; m_fileDescriptor = -1; return temp; } |
77 | int fileDescriptor() const { return m_fileDescriptor; } |
78 | #elif OS(DARWIN) |
79 | void release(); |
80 | |
81 | // MachPortType |
82 | mach_port_name_t port() const { return m_port; } |
83 | mach_msg_type_name_t disposition() const { return m_disposition; } |
84 | #elif OS(WINDOWS) |
85 | HANDLE handle() const { return m_handle; } |
86 | #endif |
87 | |
88 | void encode(Encoder&) const; |
89 | static bool decode(Decoder&, Attachment&); |
90 | |
91 | private: |
92 | Type m_type; |
93 | |
94 | #if USE(UNIX_DOMAIN_SOCKETS) |
95 | int m_fileDescriptor { -1 }; |
96 | size_t m_size; |
97 | #elif OS(DARWIN) |
98 | mach_port_name_t m_port; |
99 | mach_msg_type_name_t m_disposition; |
100 | #elif OS(WINDOWS) |
101 | HANDLE m_handle { INVALID_HANDLE_VALUE }; |
102 | #endif |
103 | }; |
104 | |
105 | } // namespace IPC |
106 | |