1/*
2 * Copyright (C) 2010 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#ifndef DataReference_h
27#define DataReference_h
28
29#include <WebCore/SharedBuffer.h>
30#include <wtf/Vector.h>
31
32namespace IPC {
33
34class ArgumentDecoder;
35class ArgumentEncoder;
36
37class DataReference {
38public:
39    DataReference()
40        : m_data(0)
41        , m_size(0)
42    {
43    }
44
45    DataReference(const uint8_t* data, size_t size)
46        : m_data(data)
47        , m_size(size)
48    {
49    }
50
51    template<size_t inlineCapacity>
52    DataReference(const Vector<uint8_t, inlineCapacity>& vector)
53        : m_data(vector.data())
54        , m_size(vector.size())
55    {
56    }
57
58    bool isEmpty() const { return size() == 0; }
59
60    size_t size() const { return m_size; }
61    const uint8_t* data() const
62    {
63        if (isEmpty())
64            return 0;
65        return m_data;
66    }
67
68    Vector<uint8_t> vector() const
69    {
70        Vector<uint8_t> result;
71        result.append(m_data, m_size);
72
73        return result;
74    }
75
76    virtual void encode(ArgumentEncoder&) const;
77    static bool decode(ArgumentDecoder&, DataReference&);
78
79    virtual ~DataReference() { }
80
81private:
82    const uint8_t* m_data;
83    size_t m_size;
84};
85
86class SharedBufferDataReference: public DataReference {
87public:
88    SharedBufferDataReference(WebCore::SharedBuffer* buffer)
89    {
90        m_buffer = buffer;
91    }
92
93    size_t size() const { return m_buffer->size(); }
94    const uint8_t* data() const = delete;
95    Vector<uint8_t> vector() const = delete;
96
97    void encode(ArgumentEncoder&) const override;
98    virtual ~SharedBufferDataReference() { m_buffer = 0; }
99
100private:
101    RefPtr<WebCore::SharedBuffer> m_buffer;
102};
103
104} // namespace IPC
105
106#endif // DataReference_h
107