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#ifndef ResourceBuffer_h
27#define ResourceBuffer_h
28
29#include "SharedBuffer.h"
30
31#include <wtf/PassOwnPtr.h>
32#include <wtf/PassRefPtr.h>
33#include <wtf/RefCounted.h>
34
35#if USE(FOUNDATION)
36OBJC_CLASS NSData;
37#endif
38
39namespace WebCore {
40
41class PurgeableBuffer;
42class SharedBuffer;
43
44class ResourceBuffer : public RefCounted<ResourceBuffer> {
45public:
46
47    static PassRefPtr<ResourceBuffer> create() { return adoptRef(new ResourceBuffer); }
48    static PassRefPtr<ResourceBuffer> create(const char* data, unsigned size) { return adoptRef(new ResourceBuffer(data, size)); }
49    static PassRefPtr<ResourceBuffer> adoptSharedBuffer(PassRefPtr<SharedBuffer> shared) { return shared ? adoptRef(new ResourceBuffer(shared)) : 0; }
50
51    virtual ~ResourceBuffer();
52
53    virtual const char* data() const;
54    virtual unsigned size() const;
55    virtual bool isEmpty() const;
56
57    void append(const char*, unsigned);
58    void append(SharedBuffer*);
59#if USE(NETWORK_CFDATA_ARRAY_CALLBACK)
60    void append(CFDataRef);
61#endif
62    void clear();
63
64    unsigned getSomeData(const char*& data, unsigned position = 0) const;
65
66    SharedBuffer* sharedBuffer() const;
67#if USE(FOUNDATION)
68    void tryReplaceSharedBufferContents(SharedBuffer*);
69#endif
70    PassRefPtr<ResourceBuffer> copy() const;
71
72    bool hasPurgeableBuffer() const;
73    void createPurgeableBuffer() const;
74
75#if PLATFORM(IOS)
76    // FIXME: Remove PLATFORM(IOS)-guard once we upstream the iOS changes to SharedBuffer.{cpp, h} and SharedBufferCF.cpp.
77    void setShouldUsePurgeableMemory(bool);
78#endif
79
80    // Ensure this buffer has no other clients before calling this.
81    PassOwnPtr<PurgeableBuffer> releasePurgeableBuffer();
82
83#if USE(FOUNDATION)
84    RetainPtr<NSData> createNSData();
85#endif
86#if USE(CF)
87    RetainPtr<CFDataRef> createCFData();
88#endif
89#if ENABLE(DISK_IMAGE_CACHE)
90    bool isUsingDiskImageCache() const;
91#endif
92
93protected:
94    ResourceBuffer();
95
96private:
97    ResourceBuffer(const char*, unsigned);
98    ResourceBuffer(PassRefPtr<SharedBuffer>);
99
100    RefPtr<SharedBuffer> m_sharedBuffer;
101};
102
103} // namespace WebCore
104
105#endif // ResourceBuffer_h
106