| 1 | /* |
| 2 | * Copyright (C) 2010 Apple Inc. All rights reserved. |
| 3 | * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies) |
| 4 | * Copyright (C) 2011,2017 Igalia S.L. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 17 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 18 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 19 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 25 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #pragma once |
| 29 | |
| 30 | #include "Attachment.h" |
| 31 | #include <wtf/Vector.h> |
| 32 | |
| 33 | namespace IPC { |
| 34 | |
| 35 | class MessageInfo { |
| 36 | public: |
| 37 | MessageInfo() = default; |
| 38 | |
| 39 | MessageInfo(size_t bodySize, size_t initialAttachmentCount) |
| 40 | : m_bodySize(bodySize) |
| 41 | , m_attachmentCount(initialAttachmentCount) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | void setBodyOutOfLine() |
| 46 | { |
| 47 | ASSERT(!isBodyOutOfLine()); |
| 48 | |
| 49 | m_isBodyOutOfLine = true; |
| 50 | m_attachmentCount++; |
| 51 | } |
| 52 | |
| 53 | bool isBodyOutOfLine() const { return m_isBodyOutOfLine; } |
| 54 | size_t bodySize() const { return m_bodySize; } |
| 55 | size_t attachmentCount() const { return m_attachmentCount; } |
| 56 | |
| 57 | private: |
| 58 | size_t m_bodySize { 0 }; |
| 59 | size_t m_attachmentCount { 0 }; |
| 60 | bool m_isBodyOutOfLine { false }; |
| 61 | }; |
| 62 | |
| 63 | class UnixMessage { |
| 64 | WTF_MAKE_FAST_ALLOCATED; |
| 65 | public: |
| 66 | UnixMessage(Encoder& encoder) |
| 67 | : m_attachments(encoder.releaseAttachments()) |
| 68 | , m_messageInfo(encoder.bufferSize(), m_attachments.size()) |
| 69 | , m_body(encoder.buffer()) |
| 70 | { |
| 71 | } |
| 72 | |
| 73 | UnixMessage(UnixMessage&& other) |
| 74 | { |
| 75 | m_attachments = WTFMove(other.m_attachments); |
| 76 | m_messageInfo = WTFMove(other.m_messageInfo); |
| 77 | if (other.m_bodyOwned) { |
| 78 | std::swap(m_body, other.m_body); |
| 79 | std::swap(m_bodyOwned, other.m_bodyOwned); |
| 80 | } else if (!m_messageInfo.isBodyOutOfLine()) { |
| 81 | m_body = static_cast<uint8_t*>(fastMalloc(m_messageInfo.bodySize())); |
| 82 | memcpy(m_body, other.m_body, m_messageInfo.bodySize()); |
| 83 | m_bodyOwned = true; |
| 84 | other.m_body = nullptr; |
| 85 | other.m_bodyOwned = false; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | ~UnixMessage() |
| 90 | { |
| 91 | if (m_bodyOwned) |
| 92 | fastFree(m_body); |
| 93 | } |
| 94 | |
| 95 | const Vector<Attachment>& attachments() const { return m_attachments; } |
| 96 | MessageInfo& messageInfo() { return m_messageInfo; } |
| 97 | |
| 98 | uint8_t* body() const { return m_body; } |
| 99 | size_t bodySize() const { return m_messageInfo.bodySize(); } |
| 100 | |
| 101 | void appendAttachment(Attachment&& attachment) |
| 102 | { |
| 103 | m_attachments.append(WTFMove(attachment)); |
| 104 | } |
| 105 | |
| 106 | private: |
| 107 | Vector<Attachment> m_attachments; |
| 108 | MessageInfo m_messageInfo; |
| 109 | uint8_t* m_body { nullptr }; |
| 110 | bool m_bodyOwned { false }; |
| 111 | }; |
| 112 | |
| 113 | } // namespace IPC |
| 114 | |