1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/indexedDB/IDBObjectStore.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,417 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim: set ts=2 et sw=2 tw=80: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#ifndef mozilla_dom_indexeddb_idbobjectstore_h__ 1.11 +#define mozilla_dom_indexeddb_idbobjectstore_h__ 1.12 + 1.13 +#include "mozilla/dom/indexedDB/IndexedDatabase.h" 1.14 + 1.15 +#include "js/TypeDecls.h" 1.16 +#include "mozilla/dom/IDBCursorBinding.h" 1.17 +#include "mozilla/dom/IDBIndexBinding.h" 1.18 +#include "mozilla/dom/IDBObjectStoreBinding.h" 1.19 +#include "nsCycleCollectionParticipant.h" 1.20 +#include "MainThreadUtils.h" 1.21 + 1.22 +#include "mozilla/dom/indexedDB/IDBRequest.h" 1.23 +#include "mozilla/dom/indexedDB/IDBTransaction.h" 1.24 +#include "mozilla/dom/indexedDB/KeyPath.h" 1.25 + 1.26 +class nsIDOMBlob; 1.27 +class nsIScriptContext; 1.28 +class nsPIDOMWindow; 1.29 + 1.30 +namespace mozilla { 1.31 +namespace dom { 1.32 +class ContentParent; 1.33 +class PBlobChild; 1.34 +class PBlobParent; 1.35 +} 1.36 +} 1.37 + 1.38 +BEGIN_INDEXEDDB_NAMESPACE 1.39 + 1.40 +class AsyncConnectionHelper; 1.41 +class FileManager; 1.42 +class IDBCursor; 1.43 +class IDBKeyRange; 1.44 +class IDBRequest; 1.45 +class IndexedDBObjectStoreChild; 1.46 +class IndexedDBObjectStoreParent; 1.47 +class Key; 1.48 + 1.49 +struct IndexInfo; 1.50 +struct IndexUpdateInfo; 1.51 +struct ObjectStoreInfo; 1.52 + 1.53 +struct FileHandleData; 1.54 +struct BlobOrFileData; 1.55 + 1.56 +class IDBObjectStore MOZ_FINAL : public nsISupports, 1.57 + public nsWrapperCache 1.58 +{ 1.59 +public: 1.60 + NS_DECL_CYCLE_COLLECTING_ISUPPORTS 1.61 + NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(IDBObjectStore) 1.62 + 1.63 + static already_AddRefed<IDBObjectStore> 1.64 + Create(IDBTransaction* aTransaction, 1.65 + ObjectStoreInfo* aInfo, 1.66 + const nsACString& aDatabaseId, 1.67 + bool aCreating); 1.68 + 1.69 + static nsresult 1.70 + AppendIndexUpdateInfo(int64_t aIndexID, 1.71 + const KeyPath& aKeyPath, 1.72 + bool aUnique, 1.73 + bool aMultiEntry, 1.74 + JSContext* aCx, 1.75 + JS::Handle<JS::Value> aObject, 1.76 + nsTArray<IndexUpdateInfo>& aUpdateInfoArray); 1.77 + 1.78 + static nsresult 1.79 + UpdateIndexes(IDBTransaction* aTransaction, 1.80 + int64_t aObjectStoreId, 1.81 + const Key& aObjectStoreKey, 1.82 + bool aOverwrite, 1.83 + int64_t aObjectDataId, 1.84 + const nsTArray<IndexUpdateInfo>& aUpdateInfoArray); 1.85 + 1.86 + static nsresult 1.87 + GetStructuredCloneReadInfoFromStatement(mozIStorageStatement* aStatement, 1.88 + uint32_t aDataIndex, 1.89 + uint32_t aFileIdsIndex, 1.90 + IDBDatabase* aDatabase, 1.91 + StructuredCloneReadInfo& aInfo); 1.92 + 1.93 + static void 1.94 + ClearCloneReadInfo(StructuredCloneReadInfo& aReadInfo); 1.95 + 1.96 + static void 1.97 + ClearCloneWriteInfo(StructuredCloneWriteInfo& aWriteInfo); 1.98 + 1.99 + static bool 1.100 + DeserializeValue(JSContext* aCx, 1.101 + StructuredCloneReadInfo& aCloneReadInfo, 1.102 + JS::MutableHandle<JS::Value> aValue); 1.103 + 1.104 + static bool 1.105 + SerializeValue(JSContext* aCx, 1.106 + StructuredCloneWriteInfo& aCloneWriteInfo, 1.107 + JS::Handle<JS::Value> aValue); 1.108 + 1.109 + template <class DeserializationTraits> 1.110 + static JSObject* 1.111 + StructuredCloneReadCallback(JSContext* aCx, 1.112 + JSStructuredCloneReader* aReader, 1.113 + uint32_t aTag, 1.114 + uint32_t aData, 1.115 + void* aClosure); 1.116 + static bool 1.117 + StructuredCloneWriteCallback(JSContext* aCx, 1.118 + JSStructuredCloneWriter* aWriter, 1.119 + JS::Handle<JSObject*> aObj, 1.120 + void* aClosure); 1.121 + 1.122 + static nsresult 1.123 + ConvertFileIdsToArray(const nsAString& aFileIds, 1.124 + nsTArray<int64_t>& aResult); 1.125 + 1.126 + // Called only in the main process. 1.127 + static nsresult 1.128 + ConvertBlobsToActors(ContentParent* aContentParent, 1.129 + FileManager* aFileManager, 1.130 + const nsTArray<StructuredCloneFile>& aFiles, 1.131 + InfallibleTArray<PBlobParent*>& aActors); 1.132 + 1.133 + // Called only in the child process. 1.134 + static void 1.135 + ConvertActorsToBlobs(const InfallibleTArray<PBlobChild*>& aActors, 1.136 + nsTArray<StructuredCloneFile>& aFiles); 1.137 + 1.138 + const nsString& Name() const 1.139 + { 1.140 + return mName; 1.141 + } 1.142 + 1.143 + bool IsAutoIncrement() const 1.144 + { 1.145 + return mAutoIncrement; 1.146 + } 1.147 + 1.148 + bool IsWriteAllowed() const 1.149 + { 1.150 + return mTransaction->IsWriteAllowed(); 1.151 + } 1.152 + 1.153 + int64_t Id() const 1.154 + { 1.155 + NS_ASSERTION(mId != INT64_MIN, "Don't ask for this yet!"); 1.156 + return mId; 1.157 + } 1.158 + 1.159 + const KeyPath& GetKeyPath() const 1.160 + { 1.161 + return mKeyPath; 1.162 + } 1.163 + 1.164 + const bool HasValidKeyPath() const 1.165 + { 1.166 + return mKeyPath.IsValid(); 1.167 + } 1.168 + 1.169 + IDBTransaction* Transaction() 1.170 + { 1.171 + return mTransaction; 1.172 + } 1.173 + 1.174 + ObjectStoreInfo* Info() 1.175 + { 1.176 + return mInfo; 1.177 + } 1.178 + 1.179 + void 1.180 + SetActor(IndexedDBObjectStoreChild* aActorChild) 1.181 + { 1.182 + NS_ASSERTION(!aActorChild || !mActorChild, "Shouldn't have more than one!"); 1.183 + mActorChild = aActorChild; 1.184 + } 1.185 + 1.186 + void 1.187 + SetActor(IndexedDBObjectStoreParent* aActorParent) 1.188 + { 1.189 + NS_ASSERTION(!aActorParent || !mActorParent, 1.190 + "Shouldn't have more than one!"); 1.191 + mActorParent = aActorParent; 1.192 + } 1.193 + 1.194 + IndexedDBObjectStoreChild* 1.195 + GetActorChild() const 1.196 + { 1.197 + return mActorChild; 1.198 + } 1.199 + 1.200 + IndexedDBObjectStoreParent* 1.201 + GetActorParent() const 1.202 + { 1.203 + return mActorParent; 1.204 + } 1.205 + 1.206 + already_AddRefed<IDBIndex> 1.207 + CreateIndexInternal(const IndexInfo& aInfo, 1.208 + ErrorResult& aRv); 1.209 + 1.210 + nsresult AddOrPutInternal( 1.211 + const SerializedStructuredCloneWriteInfo& aCloneWriteInfo, 1.212 + const Key& aKey, 1.213 + const InfallibleTArray<IndexUpdateInfo>& aUpdateInfoArray, 1.214 + const nsTArray<nsCOMPtr<nsIDOMBlob> >& aBlobs, 1.215 + bool aOverwrite, 1.216 + IDBRequest** _retval); 1.217 + 1.218 + already_AddRefed<IDBRequest> 1.219 + GetInternal(IDBKeyRange* aKeyRange, 1.220 + ErrorResult& aRv); 1.221 + 1.222 + already_AddRefed<IDBRequest> 1.223 + GetAllInternal(IDBKeyRange* aKeyRange, 1.224 + uint32_t aLimit, 1.225 + ErrorResult& aRv); 1.226 + 1.227 + already_AddRefed<IDBRequest> 1.228 + GetAllKeysInternal(IDBKeyRange* aKeyRange, 1.229 + uint32_t aLimit, 1.230 + ErrorResult& aRv); 1.231 + 1.232 + already_AddRefed<IDBRequest> 1.233 + DeleteInternal(IDBKeyRange* aKeyRange, 1.234 + ErrorResult& aRv); 1.235 + 1.236 + already_AddRefed<IDBRequest> 1.237 + CountInternal(IDBKeyRange* aKeyRange, 1.238 + ErrorResult& aRv); 1.239 + 1.240 + already_AddRefed<IDBRequest> 1.241 + OpenCursorInternal(IDBKeyRange* aKeyRange, 1.242 + size_t aDirection, 1.243 + ErrorResult& aRv); 1.244 + 1.245 + already_AddRefed<IDBRequest> 1.246 + OpenKeyCursorInternal(IDBKeyRange* aKeyRange, 1.247 + size_t aDirection, 1.248 + ErrorResult& aRv); 1.249 + 1.250 + nsresult 1.251 + OpenCursorFromChildProcess( 1.252 + IDBRequest* aRequest, 1.253 + size_t aDirection, 1.254 + const Key& aKey, 1.255 + const SerializedStructuredCloneReadInfo& aCloneInfo, 1.256 + nsTArray<StructuredCloneFile>& aBlobs, 1.257 + IDBCursor** _retval); 1.258 + 1.259 + nsresult 1.260 + OpenCursorFromChildProcess(IDBRequest* aRequest, 1.261 + size_t aDirection, 1.262 + const Key& aKey, 1.263 + IDBCursor** _retval); 1.264 + 1.265 + void 1.266 + SetInfo(ObjectStoreInfo* aInfo); 1.267 + 1.268 + static const JSClass sDummyPropJSClass; 1.269 + 1.270 + // nsWrapperCache 1.271 + virtual JSObject* 1.272 + WrapObject(JSContext* aCx) MOZ_OVERRIDE; 1.273 + 1.274 + // WebIDL 1.275 + IDBTransaction* 1.276 + GetParentObject() const 1.277 + { 1.278 + return mTransaction; 1.279 + } 1.280 + 1.281 + void 1.282 + GetName(nsString& aName) const 1.283 + { 1.284 + NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); 1.285 + aName.Assign(mName); 1.286 + } 1.287 + 1.288 + void 1.289 + GetKeyPath(JSContext* aCx, JS::MutableHandle<JS::Value> aResult, 1.290 + ErrorResult& aRv); 1.291 + 1.292 + already_AddRefed<DOMStringList> 1.293 + GetIndexNames(ErrorResult& aRv); 1.294 + 1.295 + IDBTransaction* 1.296 + Transaction() const 1.297 + { 1.298 + NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); 1.299 + return mTransaction; 1.300 + } 1.301 + 1.302 + bool 1.303 + AutoIncrement() const 1.304 + { 1.305 + NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); 1.306 + return mAutoIncrement; 1.307 + } 1.308 + 1.309 + already_AddRefed<IDBRequest> 1.310 + Put(JSContext* aCx, JS::Handle<JS::Value> aValue, 1.311 + JS::Handle<JS::Value> aKey, ErrorResult& aRv) 1.312 + { 1.313 + NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); 1.314 + return AddOrPut(aCx, aValue, aKey, true, aRv); 1.315 + } 1.316 + 1.317 + already_AddRefed<IDBRequest> 1.318 + Add(JSContext* aCx, JS::Handle<JS::Value> aValue, 1.319 + JS::Handle<JS::Value> aKey, ErrorResult& aRv) 1.320 + { 1.321 + NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); 1.322 + return AddOrPut(aCx, aValue, aKey, false, aRv); 1.323 + } 1.324 + 1.325 + already_AddRefed<IDBRequest> 1.326 + Delete(JSContext* aCx, JS::Handle<JS::Value> aKey, ErrorResult& aRv); 1.327 + 1.328 + already_AddRefed<IDBRequest> 1.329 + Get(JSContext* aCx, JS::Handle<JS::Value> aKey, ErrorResult& aRv); 1.330 + 1.331 + already_AddRefed<IDBRequest> 1.332 + Clear(ErrorResult& aRv); 1.333 + 1.334 + already_AddRefed<IDBRequest> 1.335 + OpenCursor(JSContext* aCx, JS::Handle<JS::Value> aRange, 1.336 + IDBCursorDirection aDirection, ErrorResult& aRv); 1.337 + 1.338 + already_AddRefed<IDBIndex> 1.339 + CreateIndex(JSContext* aCx, const nsAString& aName, const nsAString& aKeyPath, 1.340 + const IDBIndexParameters& aOptionalParameters, ErrorResult& aRv); 1.341 + 1.342 + already_AddRefed<IDBIndex> 1.343 + CreateIndex(JSContext* aCx, const nsAString& aName, 1.344 + const Sequence<nsString>& aKeyPath, 1.345 + const IDBIndexParameters& aOptionalParameters, ErrorResult& aRv); 1.346 + 1.347 + already_AddRefed<IDBIndex> 1.348 + Index(const nsAString& aName, ErrorResult &aRv); 1.349 + 1.350 + void 1.351 + DeleteIndex(const nsAString& aIndexName, ErrorResult& aRv); 1.352 + 1.353 + already_AddRefed<IDBRequest> 1.354 + Count(JSContext* aCx, JS::Handle<JS::Value> aKey, 1.355 + ErrorResult& aRv); 1.356 + 1.357 + already_AddRefed<IDBRequest> 1.358 + GetAll(JSContext* aCx, JS::Handle<JS::Value> aKey, 1.359 + const Optional<uint32_t>& aLimit, ErrorResult& aRv); 1.360 + 1.361 + already_AddRefed<IDBRequest> 1.362 + GetAllKeys(JSContext* aCx, JS::Handle<JS::Value> aKey, 1.363 + const Optional<uint32_t>& aLimit, ErrorResult& aRv); 1.364 + 1.365 + already_AddRefed<IDBRequest> 1.366 + OpenKeyCursor(JSContext* aCx, JS::Handle<JS::Value> aRange, 1.367 + IDBCursorDirection aDirection, ErrorResult& aRv); 1.368 + 1.369 +protected: 1.370 + IDBObjectStore(); 1.371 + ~IDBObjectStore(); 1.372 + 1.373 + nsresult GetAddInfo(JSContext* aCx, 1.374 + JS::Handle<JS::Value> aValue, 1.375 + JS::Handle<JS::Value> aKeyVal, 1.376 + StructuredCloneWriteInfo& aCloneWriteInfo, 1.377 + Key& aKey, 1.378 + nsTArray<IndexUpdateInfo>& aUpdateInfoArray); 1.379 + 1.380 + already_AddRefed<IDBRequest> 1.381 + AddOrPut(JSContext* aCx, JS::Handle<JS::Value> aValue, 1.382 + JS::Handle<JS::Value> aKey, bool aOverwrite, 1.383 + ErrorResult& aRv); 1.384 + 1.385 + already_AddRefed<IDBIndex> 1.386 + CreateIndex(JSContext* aCx, const nsAString& aName, KeyPath& aKeyPath, 1.387 + const IDBIndexParameters& aOptionalParameters, ErrorResult& aRv); 1.388 + 1.389 + static void 1.390 + ClearStructuredCloneBuffer(JSAutoStructuredCloneBuffer& aBuffer); 1.391 + 1.392 + static bool 1.393 + ReadFileHandle(JSStructuredCloneReader* aReader, 1.394 + FileHandleData* aRetval); 1.395 + 1.396 + static bool 1.397 + ReadBlobOrFile(JSStructuredCloneReader* aReader, 1.398 + uint32_t aTag, 1.399 + BlobOrFileData* aRetval); 1.400 +private: 1.401 + nsRefPtr<IDBTransaction> mTransaction; 1.402 + 1.403 + int64_t mId; 1.404 + nsString mName; 1.405 + KeyPath mKeyPath; 1.406 + JS::Heap<JS::Value> mCachedKeyPath; 1.407 + bool mRooted; 1.408 + bool mAutoIncrement; 1.409 + nsCString mDatabaseId; 1.410 + nsRefPtr<ObjectStoreInfo> mInfo; 1.411 + 1.412 + nsTArray<nsRefPtr<IDBIndex> > mCreatedIndexes; 1.413 + 1.414 + IndexedDBObjectStoreChild* mActorChild; 1.415 + IndexedDBObjectStoreParent* mActorParent; 1.416 +}; 1.417 + 1.418 +END_INDEXEDDB_NAMESPACE 1.419 + 1.420 +#endif // mozilla_dom_indexeddb_idbobjectstore_h__