| 1 | /* |
| 2 | * Copyright (C) 2012 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 | #include "config.h" |
| 27 | #include "ShareableResource.h" |
| 28 | |
| 29 | #if ENABLE(SHAREABLE_RESOURCE) |
| 30 | |
| 31 | #include "ArgumentCoders.h" |
| 32 | #include <WebCore/SharedBuffer.h> |
| 33 | |
| 34 | namespace WebKit { |
| 35 | using namespace WebCore; |
| 36 | |
| 37 | ShareableResource::Handle::Handle() |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | void ShareableResource::Handle::encode(IPC::Encoder& encoder) const |
| 42 | { |
| 43 | encoder << m_handle; |
| 44 | encoder << m_offset; |
| 45 | encoder << m_size; |
| 46 | } |
| 47 | |
| 48 | bool ShareableResource::Handle::decode(IPC::Decoder& decoder, Handle& handle) |
| 49 | { |
| 50 | if (!decoder.decode(handle.m_handle)) |
| 51 | return false; |
| 52 | if (!decoder.decode(handle.m_offset)) |
| 53 | return false; |
| 54 | if (!decoder.decode(handle.m_size)) |
| 55 | return false; |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | #if USE(CF) |
| 60 | static void shareableResourceDeallocate(void *ptr, void *info) |
| 61 | { |
| 62 | static_cast<ShareableResource*>(info)->deref(); // Balanced by ref() in createShareableResourceDeallocator() |
| 63 | } |
| 64 | |
| 65 | static CFAllocatorRef createShareableResourceDeallocator(ShareableResource* resource) |
| 66 | { |
| 67 | CFAllocatorContext context = { 0, |
| 68 | resource, |
| 69 | NULL, // retain |
| 70 | NULL, // release |
| 71 | NULL, // copyDescription |
| 72 | NULL, // allocate |
| 73 | NULL, // reallocate |
| 74 | shareableResourceDeallocate, |
| 75 | NULL, // preferredSize |
| 76 | }; |
| 77 | |
| 78 | return CFAllocatorCreate(kCFAllocatorDefault, &context); |
| 79 | } |
| 80 | #endif |
| 81 | |
| 82 | RefPtr<SharedBuffer> ShareableResource::wrapInSharedBuffer() |
| 83 | { |
| 84 | ref(); // Balanced by deref when SharedBuffer is deallocated. |
| 85 | |
| 86 | #if USE(CF) |
| 87 | RetainPtr<CFAllocatorRef> deallocator = adoptCF(createShareableResourceDeallocator(this)); |
| 88 | RetainPtr<CFDataRef> cfData = adoptCF(CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, reinterpret_cast<const UInt8*>(data()), static_cast<CFIndex>(size()), deallocator.get())); |
| 89 | return SharedBuffer::create(cfData.get()); |
| 90 | #elif USE(SOUP) |
| 91 | return SharedBuffer::wrapSoupBuffer(soup_buffer_new_with_owner(data(), size(), this, [](void* data) { static_cast<ShareableResource*>(data)->deref(); })); |
| 92 | #else |
| 93 | ASSERT_NOT_REACHED(); |
| 94 | return nullptr; |
| 95 | #endif |
| 96 | } |
| 97 | |
| 98 | RefPtr<SharedBuffer> ShareableResource::Handle::tryWrapInSharedBuffer() const |
| 99 | { |
| 100 | RefPtr<ShareableResource> resource = ShareableResource::map(*this); |
| 101 | if (!resource) { |
| 102 | LOG_ERROR("Failed to recreate ShareableResource from handle." ); |
| 103 | return nullptr; |
| 104 | } |
| 105 | |
| 106 | return resource->wrapInSharedBuffer(); |
| 107 | } |
| 108 | |
| 109 | Ref<ShareableResource> ShareableResource::create(Ref<SharedMemory>&& sharedMemory, unsigned offset, unsigned size) |
| 110 | { |
| 111 | return adoptRef(*new ShareableResource(WTFMove(sharedMemory), offset, size)); |
| 112 | } |
| 113 | |
| 114 | RefPtr<ShareableResource> ShareableResource::map(const Handle& handle) |
| 115 | { |
| 116 | auto sharedMemory = SharedMemory::map(handle.m_handle, SharedMemory::Protection::ReadOnly); |
| 117 | if (!sharedMemory) |
| 118 | return nullptr; |
| 119 | |
| 120 | return create(sharedMemory.releaseNonNull(), handle.m_offset, handle.m_size); |
| 121 | } |
| 122 | |
| 123 | ShareableResource::ShareableResource(Ref<SharedMemory>&& sharedMemory, unsigned offset, unsigned size) |
| 124 | : m_sharedMemory(WTFMove(sharedMemory)) |
| 125 | , m_offset(offset) |
| 126 | , m_size(size) |
| 127 | { |
| 128 | ASSERT(m_offset + m_size <= m_sharedMemory->size()); |
| 129 | |
| 130 | // FIXME (NetworkProcess): This data was received from another process. If it is bogus, should we assume that process is compromised and we should kill it? |
| 131 | } |
| 132 | |
| 133 | ShareableResource::~ShareableResource() |
| 134 | { |
| 135 | } |
| 136 | |
| 137 | bool ShareableResource::createHandle(Handle& handle) |
| 138 | { |
| 139 | if (!m_sharedMemory->createHandle(handle.m_handle, SharedMemory::Protection::ReadOnly)) |
| 140 | return false; |
| 141 | |
| 142 | handle.m_offset = m_offset; |
| 143 | handle.m_size = m_size; |
| 144 | |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | const char* ShareableResource::data() const |
| 149 | { |
| 150 | return static_cast<const char*>(m_sharedMemory->data()) + m_offset; |
| 151 | } |
| 152 | |
| 153 | unsigned ShareableResource::size() const |
| 154 | { |
| 155 | return m_size; |
| 156 | } |
| 157 | |
| 158 | } // namespace WebKit |
| 159 | |
| 160 | #endif // ENABLE(SHAREABLE_RESOURCE) |
| 161 | |