1/*
2 * Copyright (C) 2009 Google 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 are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#ifndef WebData_h
32#define WebData_h
33
34#include "WebCommon.h"
35
36#if WEBKIT_IMPLEMENTATION
37namespace WebCore { class SharedBuffer; }
38namespace WTF { template <typename T> class PassRefPtr; }
39namespace WTF { template <typename T> class RefPtr; }
40#endif
41
42namespace WebKit {
43
44class WebDataPrivate;
45
46// A container for raw bytes.  It is inexpensive to copy a WebData object.
47//
48// WARNING: It is not safe to pass a WebData across threads!!!
49//
50class WebData {
51public:
52    ~WebData() { reset(); }
53
54    WebData() : m_private(0) { }
55
56    WebData(const char* data, size_t size) : m_private(0)
57    {
58        assign(data, size);
59    }
60
61    template <int N>
62    WebData(const char (&data)[N]) : m_private(0)
63    {
64        assign(data, N - 1);
65    }
66
67    WebData(const WebData& d) : m_private(0) { assign(d); }
68
69    WebData& operator=(const WebData& d)
70    {
71        assign(d);
72        return *this;
73    }
74
75    WEBKIT_EXPORT void reset();
76    WEBKIT_EXPORT void assign(const WebData&);
77    WEBKIT_EXPORT void assign(const char* data, size_t size);
78
79    WEBKIT_EXPORT size_t size() const;
80    WEBKIT_EXPORT const char* data() const;
81
82    bool isEmpty() const { return !size(); }
83    bool isNull() const { return !m_private; }
84
85#if WEBKIT_IMPLEMENTATION
86    WebData(const WTF::PassRefPtr<WebCore::SharedBuffer>&);
87    WebData& operator=(const WTF::PassRefPtr<WebCore::SharedBuffer>&);
88    operator WTF::PassRefPtr<WebCore::SharedBuffer>() const;
89    operator WTF::RefPtr<WebCore::SharedBuffer>() const;
90#else
91    template <class C>
92    WebData(const C& c) : m_private(0)
93    {
94        assign(c.data(), c.size());
95    }
96
97    template <class C>
98    WebData& operator=(const C& c)
99    {
100        assign(c.data(), c.size());
101        return *this;
102    }
103#endif
104
105private:
106    void assign(WebDataPrivate*);
107    WebDataPrivate* m_private;
108};
109
110} // namespace WebKit
111
112#endif
113