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