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 COMPUTER, 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 COMPUTER, 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 PLATFORM(MAC) 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, int 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 PLATFORM(MAC) 68 void tryReplaceSharedBufferContents(SharedBuffer*); 69#endif 70 PassRefPtr<ResourceBuffer> copy() const; 71 72 bool hasPurgeableBuffer() const; 73 void createPurgeableBuffer() const; 74 75 // Ensure this buffer has no other clients before calling this. 76 PassOwnPtr<PurgeableBuffer> releasePurgeableBuffer(); 77 78#if PLATFORM(MAC) 79 NSData *createNSData(); 80#endif 81#if USE(CF) 82 CFDataRef createCFData(); 83#endif 84 85protected: 86 ResourceBuffer(); 87 88private: 89 ResourceBuffer(const char*, int); 90 ResourceBuffer(PassRefPtr<SharedBuffer>); 91 92 RefPtr<SharedBuffer> m_sharedBuffer; 93}; 94 95} // namespace WebCore 96 97#endif // ResourceBuffer_h 98