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. ``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 "ResourceBuffer.h"
28
29#include "PurgeableBuffer.h"
30
31namespace WebCore {
32
33ResourceBuffer::ResourceBuffer()
34    : m_sharedBuffer(SharedBuffer::create())
35{
36}
37
38ResourceBuffer::ResourceBuffer(const char* data, unsigned size)
39    : m_sharedBuffer(SharedBuffer::create(data, size))
40{
41}
42
43ResourceBuffer::ResourceBuffer(PassRefPtr<SharedBuffer> sharedBuffer)
44    : m_sharedBuffer(sharedBuffer)
45{
46    ASSERT(m_sharedBuffer);
47}
48
49ResourceBuffer::~ResourceBuffer()
50{
51}
52
53const char* ResourceBuffer::data() const
54{
55    return m_sharedBuffer->data();
56}
57
58unsigned ResourceBuffer::size() const
59{
60    return m_sharedBuffer->size();
61}
62
63bool ResourceBuffer::isEmpty() const
64{
65    return m_sharedBuffer->isEmpty();
66}
67
68void ResourceBuffer::append(const char* data, unsigned size)
69{
70    m_sharedBuffer->append(data, size);
71}
72
73void ResourceBuffer::append(SharedBuffer* buffer)
74{
75    m_sharedBuffer->append(buffer);
76}
77
78#if USE(NETWORK_CFDATA_ARRAY_CALLBACK)
79void ResourceBuffer::append(CFDataRef data)
80{
81    ASSERT(m_sharedBuffer);
82    m_sharedBuffer->append(data);
83}
84#endif
85
86void ResourceBuffer::clear()
87{
88    m_sharedBuffer->clear();
89}
90
91unsigned ResourceBuffer::getSomeData(const char*& data, unsigned position) const
92{
93    return m_sharedBuffer->getSomeData(data, position);
94}
95
96SharedBuffer* ResourceBuffer::sharedBuffer() const
97{
98    // Currently all ResourceBuffers are backed by SharedBuffers.
99    // In the future we might have to create the SharedBuffer on demand here.
100    // We should also phase out as much use of this accessor as possible and have clients
101    // either use the ResourceBuffer directly or use getSomeData() when sensical.
102    return m_sharedBuffer.get();
103}
104
105PassRefPtr<ResourceBuffer> ResourceBuffer::copy() const
106{
107    return ResourceBuffer::adoptSharedBuffer(m_sharedBuffer->copy());
108}
109
110bool ResourceBuffer::hasPurgeableBuffer() const
111{
112    return m_sharedBuffer->hasPurgeableBuffer();
113}
114
115#if PLATFORM(IOS)
116void ResourceBuffer::setShouldUsePurgeableMemory(bool shouldUsePurgeableMemory)
117{
118    ASSERT(m_sharedBuffer);
119    sharedBuffer()->shouldUsePurgeableMemory(shouldUsePurgeableMemory);
120}
121#endif
122
123void ResourceBuffer::createPurgeableBuffer() const
124{
125    ASSERT(m_sharedBuffer);
126    sharedBuffer()->createPurgeableBuffer();
127}
128
129PassOwnPtr<PurgeableBuffer> ResourceBuffer::releasePurgeableBuffer()
130{
131    return m_sharedBuffer->releasePurgeableBuffer();
132}
133
134#if USE(CF)
135RetainPtr<CFDataRef> ResourceBuffer::createCFData()
136{
137    return m_sharedBuffer->createCFData();
138}
139#endif
140
141#if ENABLE(DISK_IMAGE_CACHE)
142bool ResourceBuffer::isUsingDiskImageCache() const
143{
144    ASSERT(m_sharedBuffer);
145    return m_sharedBuffer && m_sharedBuffer->isAllowedToBeMemoryMapped();
146}
147#endif
148
149} // namespace WebCore
150