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