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 IDBCursor_h 27 #define IDBCursor_h 28 29 #include "bindings/v8/ScriptValue.h" 30 #include "bindings/v8/ScriptWrappable.h" 31 #include "modules/indexeddb/IDBKey.h" 32 #include "modules/indexeddb/IDBRequest.h" 33 #include "modules/indexeddb/IndexedDB.h" 34 #include "public/platform/WebIDBCursor.h" 35 #include "wtf/PassRefPtr.h" 36 #include "wtf/RefCounted.h" 37 #include "wtf/RefPtr.h" 38 39 namespace WebCore { 40 41 class DOMRequestState; 42 class ExceptionState; 43 class IDBAny; 44 class IDBTransaction; 45 class ExecutionContext; 46 class SharedBuffer; 47 48 class IDBCursor : public ScriptWrappable, public WTF::RefCountedBase { 49 public: 50 static const AtomicString& directionNext(); 51 static const AtomicString& directionNextUnique(); 52 static const AtomicString& directionPrev(); 53 static const AtomicString& directionPrevUnique(); 54 55 static IndexedDB::CursorDirection stringToDirection(const String& modeString, ExceptionState&); 56 static const AtomicString& directionToString(unsigned short mode); 57 58 static PassRefPtr<IDBCursor> create(PassOwnPtr<blink::WebIDBCursor>, IndexedDB::CursorDirection, IDBRequest*, IDBAny* source, IDBTransaction*); 59 virtual ~IDBCursor(); 60 61 // Implement the IDL 62 const String& direction() const { return directionToString(m_direction); } 63 ScriptValue key(ExecutionContext*); 64 ScriptValue primaryKey(ExecutionContext*); 65 ScriptValue value(ExecutionContext*); 66 ScriptValue source(ExecutionContext*) const; 67 68 PassRefPtr<IDBRequest> update(ScriptState*, ScriptValue&, ExceptionState&); 69 void advance(unsigned long, ExceptionState&); 70 void continueFunction(ExecutionContext*, const ScriptValue& key, ExceptionState&); 71 void continuePrimaryKey(ExecutionContext*, const ScriptValue& key, const ScriptValue& primaryKey, ExceptionState&); 72 PassRefPtr<IDBRequest> deleteFunction(ExecutionContext*, ExceptionState&); 73 74 bool isKeyDirty() const { return m_keyDirty; } 75 bool isPrimaryKeyDirty() const { return m_primaryKeyDirty; } 76 bool isValueDirty() const { return m_valueDirty; } 77 78 void continueFunction(PassRefPtr<IDBKey>, PassRefPtr<IDBKey> primaryKey, ExceptionState&); 79 void postSuccessHandlerCallback(); 80 void close(); 81 void setValueReady(PassRefPtr<IDBKey>, PassRefPtr<IDBKey> primaryKey, PassRefPtr<SharedBuffer> value); 82 PassRefPtr<IDBKey> idbPrimaryKey() const { return m_primaryKey; } 83 IDBRequest* request() const { return m_request.get(); } 84 virtual bool isKeyCursor() const { return true; } 85 virtual bool isCursorWithValue() const { return false; } 86 87 void deref() 88 { 89 if (derefBase()) 90 delete this; 91 else if (hasOneRef()) 92 checkForReferenceCycle(); 93 } 94 95 protected: 96 IDBCursor(PassOwnPtr<blink::WebIDBCursor>, IndexedDB::CursorDirection, IDBRequest*, IDBAny* source, IDBTransaction*); 97 98 private: 99 PassRefPtr<IDBObjectStore> effectiveObjectStore() const; 100 101 void checkForReferenceCycle(); 102 bool isDeleted() const; 103 104 OwnPtr<blink::WebIDBCursor> m_backend; 105 RefPtr<IDBRequest> m_request; 106 const IndexedDB::CursorDirection m_direction; 107 RefPtr<IDBAny> m_source; 108 RefPtr<IDBTransaction> m_transaction; 109 bool m_gotValue; 110 bool m_keyDirty; 111 bool m_primaryKeyDirty; 112 bool m_valueDirty; 113 RefPtr<IDBKey> m_key; 114 RefPtr<IDBKey> m_primaryKey; 115 RefPtr<SharedBuffer> m_value; 116 }; 117 118 } // namespace WebCore 119 120 #endif // IDBCursor_h 121