Home | History | Annotate | Download | only in indexeddb
      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 #include "bindings/v8/ScriptWrappable.h"
     30 #include "modules/indexeddb/IDBKey.h"
     31 #include "modules/indexeddb/IDBKeyPath.h"
     32 #include "platform/SharedBuffer.h"
     33 #include "wtf/PassRefPtr.h"
     34 #include "wtf/RefCounted.h"
     35 #include "wtf/RefPtr.h"
     36 #include "wtf/text/WTFString.h"
     37 
     38 namespace WebCore {
     39 
     40 class DOMStringList;
     41 class IDBCursor;
     42 class IDBCursorWithValue;
     43 class IDBDatabase;
     44 class IDBIndex;
     45 class IDBKeyPath;
     46 class IDBObjectStore;
     47 class IDBTransaction;
     48 
     49 class IDBAny : public RefCounted<IDBAny> {
     50 public:
     51     static PassRefPtr<IDBAny> createUndefined();
     52     static PassRefPtr<IDBAny> createNull();
     53     static PassRefPtr<IDBAny> createString(const String&);
     54     template<typename T>
     55     static PassRefPtr<IDBAny> create(T* idbObject)
     56     {
     57         return adoptRef(new IDBAny(idbObject));
     58     }
     59     template<typename T>
     60     static PassRefPtr<IDBAny> create(const T& idbObject)
     61     {
     62         return adoptRef(new IDBAny(idbObject));
     63     }
     64     template<typename T>
     65     static PassRefPtr<IDBAny> create(PassRefPtr<T> idbObject)
     66     {
     67         return adoptRef(new IDBAny(idbObject));
     68     }
     69     static PassRefPtr<IDBAny> create(int64_t value)
     70     {
     71         return adoptRef(new IDBAny(value));
     72     }
     73     static PassRefPtr<IDBAny> create(PassRefPtr<SharedBuffer> value, PassRefPtr<IDBKey> key, const IDBKeyPath& keyPath)
     74     {
     75         return adoptRef(new IDBAny(value, key, keyPath));
     76     }
     77     ~IDBAny();
     78 
     79     enum Type {
     80         UndefinedType = 0,
     81         NullType,
     82         DOMStringListType,
     83         IDBCursorType,
     84         IDBCursorWithValueType,
     85         IDBDatabaseType,
     86         IDBIndexType,
     87         IDBObjectStoreType,
     88         IDBTransactionType,
     89         BufferType,
     90         IntegerType,
     91         StringType,
     92         KeyPathType,
     93         KeyType,
     94         BufferKeyAndKeyPathType,
     95     };
     96 
     97     Type type() const { return m_type; }
     98     // Use type() to figure out which one of these you're allowed to call.
     99     DOMStringList* domStringList() const;
    100     IDBCursor* idbCursor() const;
    101     IDBCursorWithValue* idbCursorWithValue() const;
    102     IDBDatabase* idbDatabase() const;
    103     IDBIndex* idbIndex() const;
    104     IDBObjectStore* idbObjectStore() const;
    105     IDBTransaction* idbTransaction() const;
    106     SharedBuffer* buffer() const;
    107     int64_t integer() const;
    108     const String& string() const;
    109     const IDBKey* key() const;
    110     const IDBKeyPath& keyPath() const;
    111 
    112 private:
    113     explicit IDBAny(Type);
    114     explicit IDBAny(PassRefPtr<DOMStringList>);
    115     explicit IDBAny(PassRefPtr<IDBCursor>);
    116     explicit IDBAny(PassRefPtr<IDBDatabase>);
    117     explicit IDBAny(PassRefPtr<IDBIndex>);
    118     explicit IDBAny(PassRefPtr<IDBObjectStore>);
    119     explicit IDBAny(PassRefPtr<IDBTransaction>);
    120     explicit IDBAny(PassRefPtr<IDBKey>);
    121     explicit IDBAny(const IDBKeyPath&);
    122     explicit IDBAny(const String&);
    123     explicit IDBAny(PassRefPtr<SharedBuffer>);
    124     explicit IDBAny(PassRefPtr<SharedBuffer>, PassRefPtr<IDBKey>, const IDBKeyPath&);
    125     explicit IDBAny(int64_t);
    126 
    127     const Type m_type;
    128 
    129     // Only one of the following should ever be in use at any given time.
    130     const RefPtr<DOMStringList> m_domStringList;
    131     const RefPtr<IDBCursor> m_idbCursor;
    132     const RefPtr<IDBDatabase> m_idbDatabase;
    133     const RefPtr<IDBIndex> m_idbIndex;
    134     const RefPtr<IDBObjectStore> m_idbObjectStore;
    135     const RefPtr<IDBTransaction> m_idbTransaction;
    136     const RefPtr<IDBKey> m_idbKey;
    137     const IDBKeyPath m_idbKeyPath;
    138     const RefPtr<SharedBuffer> m_buffer;
    139     const String m_string;
    140     const int64_t m_integer;
    141 };
    142 
    143 } // namespace WebCore
    144 
    145 #endif // IDBAny_h
    146