1/*
2 * Copyright (C) 2010 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
6 * are met:
7 *
8 * 1.  Redistributions of source code must retain the above copyright
9 *     notice, this list of conditions and the following disclaimer.
10 * 2.  Redistributions in binary form must reproduce the above copyright
11 *     notice, this list of conditions and the following disclaimer in the
12 *     documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef IDBAny_h
27#define IDBAny_h
28
29#if ENABLE(INDEXED_DATABASE)
30
31#include "IDBKeyPath.h"
32#include "ScriptWrappable.h"
33#include <bindings/ScriptValue.h>
34#include <wtf/PassRefPtr.h>
35#include <wtf/RefCounted.h>
36#include <wtf/RefPtr.h>
37#include <wtf/text/WTFString.h>
38
39namespace WebCore {
40
41class DOMStringList;
42class IDBCursor;
43class IDBCursorWithValue;
44class IDBDatabase;
45class IDBFactory;
46class IDBIndex;
47class IDBKeyPath;
48class IDBObjectStore;
49class IDBTransaction;
50
51class IDBAny : public ScriptWrappable, public RefCounted<IDBAny> {
52public:
53    static PassRefPtr<IDBAny> createInvalid();
54    static PassRefPtr<IDBAny> createNull();
55    static PassRefPtr<IDBAny> createString(const String&);
56    template<typename T>
57    static PassRefPtr<IDBAny> create(T* idbObject)
58    {
59        return adoptRef(new IDBAny(idbObject));
60    }
61    template<typename T>
62    static PassRefPtr<IDBAny> create(const T& idbObject)
63    {
64        return adoptRef(new IDBAny(idbObject));
65    }
66    template<typename T>
67    static PassRefPtr<IDBAny> create(PassRefPtr<T> idbObject)
68    {
69        return adoptRef(new IDBAny(idbObject));
70    }
71    static PassRefPtr<IDBAny> create(int64_t value)
72    {
73        return adoptRef(new IDBAny(value));
74    }
75    ~IDBAny();
76
77    enum Type {
78        UndefinedType = 0,
79        NullType,
80        DOMStringListType,
81        IDBCursorType,
82        IDBCursorWithValueType,
83        IDBDatabaseType,
84        IDBFactoryType,
85        IDBIndexType,
86        IDBObjectStoreType,
87        IDBTransactionType,
88        ScriptValueType,
89        IntegerType,
90        StringType,
91        KeyPathType,
92    };
93
94    Type type() const { return m_type; }
95    // Use type() to figure out which one of these you're allowed to call.
96    PassRefPtr<DOMStringList> domStringList();
97    PassRefPtr<IDBCursor> idbCursor();
98    PassRefPtr<IDBCursorWithValue> idbCursorWithValue();
99    PassRefPtr<IDBDatabase> idbDatabase();
100    PassRefPtr<IDBFactory> idbFactory();
101    PassRefPtr<IDBIndex> idbIndex();
102    PassRefPtr<IDBObjectStore> idbObjectStore();
103    PassRefPtr<IDBTransaction> idbTransaction();
104    const Deprecated::ScriptValue& scriptValue();
105    int64_t integer();
106    const String& string();
107    const IDBKeyPath& keyPath() { return m_idbKeyPath; };
108
109private:
110    explicit IDBAny(Type);
111    explicit IDBAny(PassRefPtr<DOMStringList>);
112    explicit IDBAny(PassRefPtr<IDBCursor>);
113    explicit IDBAny(PassRefPtr<IDBCursorWithValue>);
114    explicit IDBAny(PassRefPtr<IDBDatabase>);
115    explicit IDBAny(PassRefPtr<IDBFactory>);
116    explicit IDBAny(PassRefPtr<IDBIndex>);
117    explicit IDBAny(PassRefPtr<IDBObjectStore>);
118    explicit IDBAny(PassRefPtr<IDBTransaction>);
119    explicit IDBAny(const IDBKeyPath&);
120    explicit IDBAny(const String&);
121    explicit IDBAny(const Deprecated::ScriptValue&);
122    explicit IDBAny(int64_t);
123
124    const Type m_type;
125
126    // Only one of the following should ever be in use at any given time.
127    const RefPtr<DOMStringList> m_domStringList;
128    const RefPtr<IDBCursor> m_idbCursor;
129    const RefPtr<IDBCursorWithValue> m_idbCursorWithValue;
130    const RefPtr<IDBDatabase> m_idbDatabase;
131    const RefPtr<IDBFactory> m_idbFactory;
132    const RefPtr<IDBIndex> m_idbIndex;
133    const RefPtr<IDBObjectStore> m_idbObjectStore;
134    const RefPtr<IDBTransaction> m_idbTransaction;
135    const IDBKeyPath m_idbKeyPath;
136    const Deprecated::ScriptValue m_scriptValue;
137    const String m_string;
138    const int64_t m_integer;
139};
140
141} // namespace WebCore
142
143#endif // ENABLE(INDEXED_DATABASE)
144
145#endif // IDBAny_h
146