Sharing SharedBuffer between WebCore and ImageIO is racy and crash prone
[WebKit-https.git] / Source / WebCore / platform / mac / SharedBufferMac.mm
1 /*
2  * Copyright (C) 2006 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. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
26 #include "config.h"
27 #include "SharedBuffer.h"
28
29 #include "WebCoreObjCExtras.h"
30 #include <runtime/InitializeThreading.h>
31 #include <string.h>
32 #include <wtf/MainThread.h>
33 #include <wtf/PassRefPtr.h>
34
35 using namespace WebCore;
36
37 @interface WebCoreSharedBufferData : NSData
38 {
39     RefPtr<SharedBuffer::DataBuffer> buffer;
40 }
41
42 - (id)initWithSharedBufferDataBuffer:(SharedBuffer::DataBuffer*)dataBuffer;
43 @end
44
45 @implementation WebCoreSharedBufferData
46
47 + (void)initialize
48 {
49 #if !USE(WEB_THREAD)
50     JSC::initializeThreading();
51     WTF::initializeMainThreadToProcessMainThread();
52 #endif // !USE(WEB_THREAD)
53     WebCoreObjCFinalizeOnMainThread(self);
54 }
55
56 - (void)dealloc
57 {
58     if (WebCoreObjCScheduleDeallocateOnMainThread([WebCoreSharedBufferData class], self))
59         return;
60
61     [super dealloc];
62 }
63
64 - (void)finalize
65 {
66     [super finalize];
67 }
68
69 - (id)initWithSharedBufferDataBuffer:(SharedBuffer::DataBuffer*)dataBuffer
70 {
71     self = [super init];
72     
73     if (self)
74         buffer = dataBuffer;
75
76     return self;
77 }
78
79 - (NSUInteger)length
80 {
81     return buffer->data.size();
82 }
83
84 - (const void *)bytes
85 {
86     return reinterpret_cast<const void*>(buffer->data.data());
87 }
88
89 @end
90
91 namespace WebCore {
92
93 PassRefPtr<SharedBuffer> SharedBuffer::wrapNSData(NSData *nsData)
94 {
95     return adoptRef(new SharedBuffer((CFDataRef)nsData));
96 }
97
98 RetainPtr<NSData> SharedBuffer::createNSData()
99 {
100     return adoptNS((NSData *)createCFData().leakRef());
101 }
102
103 RetainPtr<CFDataRef> SharedBuffer::createCFData()
104 {
105     if (m_cfData)
106         return m_cfData;
107
108     data(); // Force data into m_buffer from segments or data array.
109     if (hasPurgeableBuffer()) {
110         RefPtr<SharedBuffer::DataBuffer> copiedBuffer = adoptRef(new DataBuffer);
111         copiedBuffer->data.append(data(), size());
112         return adoptCF(reinterpret_cast<CFDataRef>([[WebCoreSharedBufferData alloc] initWithSharedBufferDataBuffer:copiedBuffer.get()]));
113     }
114
115     return adoptCF(reinterpret_cast<CFDataRef>([[WebCoreSharedBufferData alloc] initWithSharedBufferDataBuffer:m_buffer.get()]));
116 }
117
118 PassRefPtr<SharedBuffer> SharedBuffer::createWithContentsOfFile(const String& filePath)
119 {
120     NSData *resourceData = [NSData dataWithContentsOfFile:filePath];
121     if (resourceData) 
122         return SharedBuffer::wrapNSData(resourceData);
123     return 0;
124 }
125
126 }