dom/indexedDB/IDBIndex.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/indexedDB/IDBIndex.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,2582 @@
     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 +#include "base/basictypes.h"
    1.11 +
    1.12 +#include "IDBIndex.h"
    1.13 +
    1.14 +#include <algorithm>
    1.15 +#include "mozilla/dom/ContentChild.h"
    1.16 +#include "mozilla/dom/ContentParent.h"
    1.17 +#include "mozilla/dom/ipc/Blob.h"
    1.18 +#include "mozilla/storage.h"
    1.19 +#include "nsThreadUtils.h"
    1.20 +#include "xpcpublic.h"
    1.21 +
    1.22 +#include "AsyncConnectionHelper.h"
    1.23 +#include "DatabaseInfo.h"
    1.24 +#include "IDBCursor.h"
    1.25 +#include "IDBEvents.h"
    1.26 +#include "IDBKeyRange.h"
    1.27 +#include "IDBObjectStore.h"
    1.28 +#include "IDBTransaction.h"
    1.29 +#include "ProfilerHelpers.h"
    1.30 +#include "ReportInternalError.h"
    1.31 +
    1.32 +#include "ipc/IndexedDBChild.h"
    1.33 +#include "ipc/IndexedDBParent.h"
    1.34 +
    1.35 +#include "IndexedDatabaseInlines.h"
    1.36 +
    1.37 +USING_INDEXEDDB_NAMESPACE
    1.38 +using namespace mozilla::dom;
    1.39 +using namespace mozilla::dom::indexedDB::ipc;
    1.40 +using mozilla::ErrorResult;
    1.41 +using mozilla::Move;
    1.42 +
    1.43 +namespace {
    1.44 +
    1.45 +class IndexHelper : public AsyncConnectionHelper
    1.46 +{
    1.47 +public:
    1.48 +  IndexHelper(IDBTransaction* aTransaction,
    1.49 +              IDBRequest* aRequest,
    1.50 +              IDBIndex* aIndex)
    1.51 +  : AsyncConnectionHelper(aTransaction, aRequest), mIndex(aIndex),
    1.52 +    mActor(nullptr)
    1.53 +  {
    1.54 +    NS_ASSERTION(aTransaction, "Null transaction!");
    1.55 +    NS_ASSERTION(aRequest, "Null request!");
    1.56 +    NS_ASSERTION(aIndex, "Null index!");
    1.57 +  }
    1.58 +
    1.59 +  virtual void ReleaseMainThreadObjects() MOZ_OVERRIDE;
    1.60 +
    1.61 +  virtual nsresult Dispatch(nsIEventTarget* aDatabaseThread) MOZ_OVERRIDE;
    1.62 +
    1.63 +  virtual nsresult
    1.64 +  PackArgumentsForParentProcess(IndexRequestParams& aParams) = 0;
    1.65 +
    1.66 +  virtual nsresult
    1.67 +  UnpackResponseFromParentProcess(const ResponseValue& aResponseValue) = 0;
    1.68 +
    1.69 +protected:
    1.70 +  nsRefPtr<IDBIndex> mIndex;
    1.71 +
    1.72 +private:
    1.73 +  IndexedDBIndexRequestChild* mActor;
    1.74 +};
    1.75 +
    1.76 +class GetKeyHelper : public IndexHelper
    1.77 +{
    1.78 +public:
    1.79 +  GetKeyHelper(IDBTransaction* aTransaction,
    1.80 +               IDBRequest* aRequest,
    1.81 +               IDBIndex* aIndex,
    1.82 +               IDBKeyRange* aKeyRange)
    1.83 +  : IndexHelper(aTransaction, aRequest, aIndex), mKeyRange(aKeyRange)
    1.84 +  { }
    1.85 +
    1.86 +  virtual nsresult DoDatabaseWork(mozIStorageConnection* aConnection)
    1.87 +                                  MOZ_OVERRIDE;
    1.88 +
    1.89 +  virtual nsresult GetSuccessResult(JSContext* aCx,
    1.90 +                                    JS::MutableHandle<JS::Value> aVal) MOZ_OVERRIDE;
    1.91 +
    1.92 +  virtual void ReleaseMainThreadObjects() MOZ_OVERRIDE;
    1.93 +
    1.94 +  virtual nsresult
    1.95 +  PackArgumentsForParentProcess(IndexRequestParams& aParams) MOZ_OVERRIDE;
    1.96 +
    1.97 +  virtual ChildProcessSendResult
    1.98 +  SendResponseToChildProcess(nsresult aResultCode) MOZ_OVERRIDE;
    1.99 +
   1.100 +  virtual nsresult
   1.101 +  UnpackResponseFromParentProcess(const ResponseValue& aResponseValue)
   1.102 +                                  MOZ_OVERRIDE;
   1.103 +
   1.104 +protected:
   1.105 +  // In-params.
   1.106 +  nsRefPtr<IDBKeyRange> mKeyRange;
   1.107 +
   1.108 +  // Out-params.
   1.109 +  Key mKey;
   1.110 +};
   1.111 +
   1.112 +class GetHelper : public GetKeyHelper
   1.113 +{
   1.114 +public:
   1.115 +  GetHelper(IDBTransaction* aTransaction,
   1.116 +            IDBRequest* aRequest,
   1.117 +            IDBIndex* aIndex,
   1.118 +            IDBKeyRange* aKeyRange)
   1.119 +  : GetKeyHelper(aTransaction, aRequest, aIndex, aKeyRange)
   1.120 +  { }
   1.121 +
   1.122 +  ~GetHelper()
   1.123 +  {
   1.124 +    IDBObjectStore::ClearCloneReadInfo(mCloneReadInfo);
   1.125 +  }
   1.126 +
   1.127 +  virtual nsresult DoDatabaseWork(mozIStorageConnection* aConnection)
   1.128 +                                  MOZ_OVERRIDE;
   1.129 +
   1.130 +  virtual nsresult GetSuccessResult(JSContext* aCx,
   1.131 +                                    JS::MutableHandle<JS::Value> aVal) MOZ_OVERRIDE;
   1.132 +
   1.133 +  virtual void ReleaseMainThreadObjects() MOZ_OVERRIDE;
   1.134 +
   1.135 +  virtual nsresult
   1.136 +  PackArgumentsForParentProcess(IndexRequestParams& aParams) MOZ_OVERRIDE;
   1.137 +
   1.138 +  virtual ChildProcessSendResult
   1.139 +  SendResponseToChildProcess(nsresult aResultCode) MOZ_OVERRIDE;
   1.140 +
   1.141 +  virtual nsresult
   1.142 +  UnpackResponseFromParentProcess(const ResponseValue& aResponseValue)
   1.143 +                                  MOZ_OVERRIDE;
   1.144 +
   1.145 +protected:
   1.146 +  StructuredCloneReadInfo mCloneReadInfo;
   1.147 +};
   1.148 +
   1.149 +class GetAllKeysHelper : public GetKeyHelper
   1.150 +{
   1.151 +public:
   1.152 +  GetAllKeysHelper(IDBTransaction* aTransaction,
   1.153 +                   IDBRequest* aRequest,
   1.154 +                   IDBIndex* aIndex,
   1.155 +                   IDBKeyRange* aKeyRange,
   1.156 +                   const uint32_t aLimit)
   1.157 +  : GetKeyHelper(aTransaction, aRequest, aIndex, aKeyRange), mLimit(aLimit)
   1.158 +  { }
   1.159 +
   1.160 +  virtual nsresult DoDatabaseWork(mozIStorageConnection* aConnection)
   1.161 +                                  MOZ_OVERRIDE;
   1.162 +
   1.163 +  virtual nsresult GetSuccessResult(JSContext* aCx,
   1.164 +                                    JS::MutableHandle<JS::Value> aVal) MOZ_OVERRIDE;
   1.165 +
   1.166 +  virtual nsresult
   1.167 +  PackArgumentsForParentProcess(IndexRequestParams& aParams) MOZ_OVERRIDE;
   1.168 +
   1.169 +  virtual ChildProcessSendResult
   1.170 +  SendResponseToChildProcess(nsresult aResultCode) MOZ_OVERRIDE;
   1.171 +
   1.172 +  virtual nsresult
   1.173 +  UnpackResponseFromParentProcess(const ResponseValue& aResponseValue)
   1.174 +                                  MOZ_OVERRIDE;
   1.175 +
   1.176 +protected:
   1.177 +  const uint32_t mLimit;
   1.178 +  nsTArray<Key> mKeys;
   1.179 +};
   1.180 +
   1.181 +class GetAllHelper : public GetKeyHelper
   1.182 +{
   1.183 +public:
   1.184 +  GetAllHelper(IDBTransaction* aTransaction,
   1.185 +               IDBRequest* aRequest,
   1.186 +               IDBIndex* aIndex,
   1.187 +               IDBKeyRange* aKeyRange,
   1.188 +               const uint32_t aLimit)
   1.189 +  : GetKeyHelper(aTransaction, aRequest, aIndex, aKeyRange), mLimit(aLimit)
   1.190 +  { }
   1.191 +
   1.192 +  ~GetAllHelper()
   1.193 +  {
   1.194 +    for (uint32_t index = 0; index < mCloneReadInfos.Length(); index++) {
   1.195 +      IDBObjectStore::ClearCloneReadInfo(mCloneReadInfos[index]);
   1.196 +    }
   1.197 +  }
   1.198 +
   1.199 +  virtual nsresult DoDatabaseWork(mozIStorageConnection* aConnection)
   1.200 +                                  MOZ_OVERRIDE;
   1.201 +
   1.202 +  virtual nsresult GetSuccessResult(JSContext* aCx,
   1.203 +                                    JS::MutableHandle<JS::Value> aVal) MOZ_OVERRIDE;
   1.204 +
   1.205 +  virtual void ReleaseMainThreadObjects() MOZ_OVERRIDE;
   1.206 +
   1.207 +  virtual nsresult
   1.208 +  PackArgumentsForParentProcess(IndexRequestParams& aParams) MOZ_OVERRIDE;
   1.209 +
   1.210 +  virtual ChildProcessSendResult
   1.211 +  SendResponseToChildProcess(nsresult aResultCode) MOZ_OVERRIDE;
   1.212 +
   1.213 +  virtual nsresult
   1.214 +  UnpackResponseFromParentProcess(const ResponseValue& aResponseValue)
   1.215 +                                  MOZ_OVERRIDE;
   1.216 +
   1.217 +protected:
   1.218 +  const uint32_t mLimit;
   1.219 +  nsTArray<StructuredCloneReadInfo> mCloneReadInfos;
   1.220 +};
   1.221 +
   1.222 +class OpenKeyCursorHelper : public IndexHelper
   1.223 +{
   1.224 +public:
   1.225 +  OpenKeyCursorHelper(IDBTransaction* aTransaction,
   1.226 +                      IDBRequest* aRequest,
   1.227 +                      IDBIndex* aIndex,
   1.228 +                      IDBKeyRange* aKeyRange,
   1.229 +                      IDBCursor::Direction aDirection)
   1.230 +  : IndexHelper(aTransaction, aRequest, aIndex), mKeyRange(aKeyRange),
   1.231 +    mDirection(aDirection)
   1.232 +  { }
   1.233 +
   1.234 +  ~OpenKeyCursorHelper()
   1.235 +  {
   1.236 +    NS_ASSERTION(true, "bas");
   1.237 +  }
   1.238 +
   1.239 +  virtual nsresult DoDatabaseWork(mozIStorageConnection* aConnection)
   1.240 +                                  MOZ_OVERRIDE;
   1.241 +
   1.242 +  virtual nsresult GetSuccessResult(JSContext* aCx,
   1.243 +                                    JS::MutableHandle<JS::Value> aVal) MOZ_OVERRIDE;
   1.244 +
   1.245 +  virtual void ReleaseMainThreadObjects() MOZ_OVERRIDE;
   1.246 +
   1.247 +  virtual nsresult
   1.248 +  PackArgumentsForParentProcess(IndexRequestParams& aParams) MOZ_OVERRIDE;
   1.249 +
   1.250 +  virtual ChildProcessSendResult
   1.251 +  SendResponseToChildProcess(nsresult aResultCode) MOZ_OVERRIDE;
   1.252 +
   1.253 +  virtual nsresult
   1.254 +  UnpackResponseFromParentProcess(const ResponseValue& aResponseValue)
   1.255 +                                  MOZ_OVERRIDE;
   1.256 +
   1.257 +protected:
   1.258 +  virtual nsresult EnsureCursor();
   1.259 +
   1.260 +  // In-params.
   1.261 +  nsRefPtr<IDBKeyRange> mKeyRange;
   1.262 +  const IDBCursor::Direction mDirection;
   1.263 +
   1.264 +  // Out-params.
   1.265 +  Key mKey;
   1.266 +  Key mObjectKey;
   1.267 +  nsCString mContinueQuery;
   1.268 +  nsCString mContinueToQuery;
   1.269 +  Key mRangeKey;
   1.270 +
   1.271 +  // Only used in the parent process.
   1.272 +  nsRefPtr<IDBCursor> mCursor;
   1.273 +};
   1.274 +
   1.275 +class OpenCursorHelper : public OpenKeyCursorHelper
   1.276 +{
   1.277 +public:
   1.278 +  OpenCursorHelper(IDBTransaction* aTransaction,
   1.279 +                   IDBRequest* aRequest,
   1.280 +                   IDBIndex* aIndex,
   1.281 +                   IDBKeyRange* aKeyRange,
   1.282 +                   IDBCursor::Direction aDirection)
   1.283 +  : OpenKeyCursorHelper(aTransaction, aRequest, aIndex, aKeyRange, aDirection)
   1.284 +  { }
   1.285 +
   1.286 +  ~OpenCursorHelper()
   1.287 +  {
   1.288 +    IDBObjectStore::ClearCloneReadInfo(mCloneReadInfo);
   1.289 +  }
   1.290 +
   1.291 +  virtual nsresult DoDatabaseWork(mozIStorageConnection* aConnection)
   1.292 +                                  MOZ_OVERRIDE;
   1.293 +
   1.294 +  virtual void ReleaseMainThreadObjects() MOZ_OVERRIDE;
   1.295 +
   1.296 +  virtual nsresult
   1.297 +  PackArgumentsForParentProcess(IndexRequestParams& aParams) MOZ_OVERRIDE;
   1.298 +
   1.299 +  virtual ChildProcessSendResult
   1.300 +  SendResponseToChildProcess(nsresult aResultCode) MOZ_OVERRIDE;
   1.301 +
   1.302 +private:
   1.303 +  virtual nsresult EnsureCursor();
   1.304 +
   1.305 +  StructuredCloneReadInfo mCloneReadInfo;
   1.306 +
   1.307 +  // Only used in the parent process.
   1.308 +  SerializedStructuredCloneReadInfo mSerializedCloneReadInfo;
   1.309 +};
   1.310 +
   1.311 +class CountHelper : public IndexHelper
   1.312 +{
   1.313 +public:
   1.314 +  CountHelper(IDBTransaction* aTransaction,
   1.315 +              IDBRequest* aRequest,
   1.316 +              IDBIndex* aIndex,
   1.317 +              IDBKeyRange* aKeyRange)
   1.318 +  : IndexHelper(aTransaction, aRequest, aIndex), mKeyRange(aKeyRange), mCount(0)
   1.319 +  { }
   1.320 +
   1.321 +  virtual nsresult DoDatabaseWork(mozIStorageConnection* aConnection)
   1.322 +                                  MOZ_OVERRIDE;
   1.323 +
   1.324 +  virtual nsresult GetSuccessResult(JSContext* aCx,
   1.325 +                                    JS::MutableHandle<JS::Value> aVal) MOZ_OVERRIDE;
   1.326 +
   1.327 +  virtual void ReleaseMainThreadObjects() MOZ_OVERRIDE;
   1.328 +
   1.329 +  virtual nsresult
   1.330 +  PackArgumentsForParentProcess(IndexRequestParams& aParams) MOZ_OVERRIDE;
   1.331 +
   1.332 +  virtual ChildProcessSendResult
   1.333 +  SendResponseToChildProcess(nsresult aResultCode) MOZ_OVERRIDE;
   1.334 +
   1.335 +  virtual nsresult
   1.336 +  UnpackResponseFromParentProcess(const ResponseValue& aResponseValue)
   1.337 +                                  MOZ_OVERRIDE;
   1.338 +
   1.339 +private:
   1.340 +  nsRefPtr<IDBKeyRange> mKeyRange;
   1.341 +  uint64_t mCount;
   1.342 +};
   1.343 +
   1.344 +inline
   1.345 +already_AddRefed<IDBRequest>
   1.346 +GenerateRequest(IDBIndex* aIndex)
   1.347 +{
   1.348 +  NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
   1.349 +  IDBTransaction* transaction = aIndex->ObjectStore()->Transaction();
   1.350 +  IDBDatabase* database = transaction->Database();
   1.351 +  return IDBRequest::Create(aIndex, database, transaction);
   1.352 +}
   1.353 +
   1.354 +} // anonymous namespace
   1.355 +
   1.356 +// static
   1.357 +already_AddRefed<IDBIndex>
   1.358 +IDBIndex::Create(IDBObjectStore* aObjectStore,
   1.359 +                 const IndexInfo* aIndexInfo,
   1.360 +                 bool aCreating)
   1.361 +{
   1.362 +  NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
   1.363 +  NS_ASSERTION(aObjectStore, "Null pointer!");
   1.364 +  NS_ASSERTION(aIndexInfo, "Null pointer!");
   1.365 +
   1.366 +  nsRefPtr<IDBIndex> index = new IDBIndex();
   1.367 +
   1.368 +  index->mObjectStore = aObjectStore;
   1.369 +  index->mId = aIndexInfo->id;
   1.370 +  index->mName = aIndexInfo->name;
   1.371 +  index->mKeyPath = aIndexInfo->keyPath;
   1.372 +  index->mUnique = aIndexInfo->unique;
   1.373 +  index->mMultiEntry = aIndexInfo->multiEntry;
   1.374 +
   1.375 +  if (!IndexedDatabaseManager::IsMainProcess()) {
   1.376 +    IndexedDBObjectStoreChild* objectStoreActor = aObjectStore->GetActorChild();
   1.377 +    NS_ASSERTION(objectStoreActor, "Must have an actor here!");
   1.378 +
   1.379 +    nsAutoPtr<IndexedDBIndexChild> actor(new IndexedDBIndexChild(index));
   1.380 +
   1.381 +    IndexConstructorParams params;
   1.382 +
   1.383 +    if (aCreating) {
   1.384 +      CreateIndexParams createParams;
   1.385 +      createParams.info() = *aIndexInfo;
   1.386 +      params = createParams;
   1.387 +    }
   1.388 +    else {
   1.389 +      GetIndexParams getParams;
   1.390 +      getParams.name() = aIndexInfo->name;
   1.391 +      params = getParams;
   1.392 +    }
   1.393 +
   1.394 +    objectStoreActor->SendPIndexedDBIndexConstructor(actor.forget(), params);
   1.395 +  }
   1.396 +
   1.397 +  return index.forget();
   1.398 +}
   1.399 +
   1.400 +IDBIndex::IDBIndex()
   1.401 +: mId(INT64_MIN),
   1.402 +  mKeyPath(0),
   1.403 +  mCachedKeyPath(JSVAL_VOID),
   1.404 +  mActorChild(nullptr),
   1.405 +  mActorParent(nullptr),
   1.406 +  mUnique(false),
   1.407 +  mMultiEntry(false),
   1.408 +  mRooted(false)
   1.409 +{
   1.410 +  NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
   1.411 +
   1.412 +  SetIsDOMBinding();
   1.413 +}
   1.414 +
   1.415 +IDBIndex::~IDBIndex()
   1.416 +{
   1.417 +  NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
   1.418 +  NS_ASSERTION(!mActorParent, "Actor parent owns us, how can we be dying?!");
   1.419 +
   1.420 +  if (mRooted) {
   1.421 +    mCachedKeyPath = JSVAL_VOID;
   1.422 +    mozilla::DropJSObjects(this);
   1.423 +  }
   1.424 +
   1.425 +  if (mActorChild) {
   1.426 +    NS_ASSERTION(!IndexedDatabaseManager::IsMainProcess(), "Wrong process!");
   1.427 +    mActorChild->Send__delete__(mActorChild);
   1.428 +    NS_ASSERTION(!mActorChild, "Should have cleared in Send__delete__!");
   1.429 +  }
   1.430 +}
   1.431 +
   1.432 +already_AddRefed<IDBRequest>
   1.433 +IDBIndex::GetInternal(IDBKeyRange* aKeyRange, ErrorResult& aRv)
   1.434 +{
   1.435 +  NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
   1.436 +
   1.437 +  IDBTransaction* transaction = mObjectStore->Transaction();
   1.438 +  if (!transaction->IsOpen()) {
   1.439 +    aRv.Throw(NS_ERROR_DOM_INDEXEDDB_TRANSACTION_INACTIVE_ERR);
   1.440 +    return nullptr;
   1.441 +  }
   1.442 +
   1.443 +  nsRefPtr<IDBRequest> request = GenerateRequest(this);
   1.444 +  if (!request) {
   1.445 +    IDB_WARNING("Failed to generate request!");
   1.446 +    aRv.Throw(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
   1.447 +    return nullptr;
   1.448 +  }
   1.449 +
   1.450 +  nsRefPtr<GetHelper> helper =
   1.451 +    new GetHelper(transaction, request, this, aKeyRange);
   1.452 +
   1.453 +  nsresult rv = helper->DispatchToTransactionPool();
   1.454 +  if (NS_FAILED(rv)) {
   1.455 +    IDB_WARNING("Failed to dispatch!");
   1.456 +    aRv.Throw(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
   1.457 +    return nullptr;
   1.458 +  }
   1.459 +
   1.460 +  IDB_PROFILER_MARK("IndexedDB Request %llu: "
   1.461 +                    "database(%s).transaction(%s).objectStore(%s).index(%s)."
   1.462 +                    "get(%s)",
   1.463 +                    "IDBRequest[%llu] MT IDBIndex.get()",
   1.464 +                    request->GetSerialNumber(),
   1.465 +                    IDB_PROFILER_STRING(ObjectStore()->Transaction()->
   1.466 +                                        Database()),
   1.467 +                    IDB_PROFILER_STRING(ObjectStore()->Transaction()),
   1.468 +                    IDB_PROFILER_STRING(ObjectStore()),
   1.469 +                    IDB_PROFILER_STRING(this), IDB_PROFILER_STRING(aKeyRange));
   1.470 +
   1.471 +  return request.forget();
   1.472 +}
   1.473 +
   1.474 +already_AddRefed<IDBRequest>
   1.475 +IDBIndex::GetKeyInternal(IDBKeyRange* aKeyRange, ErrorResult& aRv)
   1.476 +{
   1.477 +  NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
   1.478 +
   1.479 +  IDBTransaction* transaction = mObjectStore->Transaction();
   1.480 +  if (!transaction->IsOpen()) {
   1.481 +    aRv.Throw(NS_ERROR_DOM_INDEXEDDB_TRANSACTION_INACTIVE_ERR);
   1.482 +    return nullptr;
   1.483 +  }
   1.484 +
   1.485 +  nsRefPtr<IDBRequest> request = GenerateRequest(this);
   1.486 +  if (!request) {
   1.487 +    IDB_WARNING("Failed to generate request!");
   1.488 +    aRv.Throw(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
   1.489 +    return nullptr;
   1.490 +  }
   1.491 +
   1.492 +  nsRefPtr<GetKeyHelper> helper =
   1.493 +    new GetKeyHelper(transaction, request, this, aKeyRange);
   1.494 +
   1.495 +  nsresult rv = helper->DispatchToTransactionPool();
   1.496 +  if (NS_FAILED(rv)) {
   1.497 +    IDB_WARNING("Failed to dispatch!");
   1.498 +    aRv.Throw(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
   1.499 +    return nullptr;
   1.500 +  }
   1.501 +
   1.502 +  IDB_PROFILER_MARK("IndexedDB Request %llu: "
   1.503 +                    "database(%s).transaction(%s).objectStore(%s).index(%s)."
   1.504 +                    "getKey(%s)",
   1.505 +                    "IDBRequest[%llu] MT IDBIndex.getKey()",
   1.506 +                    request->GetSerialNumber(),
   1.507 +                    IDB_PROFILER_STRING(ObjectStore()->Transaction()->
   1.508 +                                        Database()),
   1.509 +                    IDB_PROFILER_STRING(ObjectStore()->Transaction()),
   1.510 +                    IDB_PROFILER_STRING(ObjectStore()),
   1.511 +                    IDB_PROFILER_STRING(this), IDB_PROFILER_STRING(aKeyRange));
   1.512 +
   1.513 +  return request.forget();
   1.514 +}
   1.515 +
   1.516 +already_AddRefed<IDBRequest>
   1.517 +IDBIndex::GetAllInternal(IDBKeyRange* aKeyRange, uint32_t aLimit,
   1.518 +                         ErrorResult& aRv)
   1.519 +{
   1.520 +  NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
   1.521 +
   1.522 +  IDBTransaction* transaction = mObjectStore->Transaction();
   1.523 +  if (!transaction->IsOpen()) {
   1.524 +    aRv.Throw(NS_ERROR_DOM_INDEXEDDB_TRANSACTION_INACTIVE_ERR);
   1.525 +    return nullptr;
   1.526 +  }
   1.527 +
   1.528 +  nsRefPtr<IDBRequest> request = GenerateRequest(this);
   1.529 +  if (!request) {
   1.530 +    IDB_WARNING("Failed to generate request!");
   1.531 +    aRv.Throw(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
   1.532 +    return nullptr;
   1.533 +  }
   1.534 +
   1.535 +  nsRefPtr<GetAllHelper> helper =
   1.536 +    new GetAllHelper(transaction, request, this, aKeyRange, aLimit);
   1.537 +
   1.538 +  nsresult rv = helper->DispatchToTransactionPool();
   1.539 +  if (NS_FAILED(rv)) {
   1.540 +    IDB_WARNING("Failed to dispatch!");
   1.541 +    aRv.Throw(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
   1.542 +    return nullptr;
   1.543 +  }
   1.544 +
   1.545 +  IDB_PROFILER_MARK("IndexedDB Request %llu: "
   1.546 +                    "database(%s).transaction(%s).objectStore(%s).index(%s)."
   1.547 +                    "getAll(%s, %lu)",
   1.548 +                    "IDBRequest[%llu] MT IDBIndex.getAll()",
   1.549 +                    request->GetSerialNumber(),
   1.550 +                    IDB_PROFILER_STRING(ObjectStore()->Transaction()->
   1.551 +                                        Database()),
   1.552 +                    IDB_PROFILER_STRING(ObjectStore()->Transaction()),
   1.553 +                    IDB_PROFILER_STRING(ObjectStore()),
   1.554 +                    IDB_PROFILER_STRING(this),
   1.555 +                    IDB_PROFILER_STRING(aKeyRange), aLimit);
   1.556 +
   1.557 +  return request.forget();
   1.558 +}
   1.559 +
   1.560 +already_AddRefed<IDBRequest>
   1.561 +IDBIndex::GetAllKeysInternal(IDBKeyRange* aKeyRange, uint32_t aLimit,
   1.562 +                             ErrorResult& aRv)
   1.563 +{
   1.564 +  NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
   1.565 +
   1.566 +  IDBTransaction* transaction = mObjectStore->Transaction();
   1.567 +  if (!transaction->IsOpen()) {
   1.568 +    aRv.Throw(NS_ERROR_DOM_INDEXEDDB_TRANSACTION_INACTIVE_ERR);
   1.569 +    return nullptr;
   1.570 +  }
   1.571 +
   1.572 +  nsRefPtr<IDBRequest> request = GenerateRequest(this);
   1.573 +  if (!request) {
   1.574 +    IDB_WARNING("Failed to generate request!");
   1.575 +    aRv.Throw(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
   1.576 +    return nullptr;
   1.577 +  }
   1.578 +
   1.579 +  nsRefPtr<GetAllKeysHelper> helper =
   1.580 +    new GetAllKeysHelper(transaction, request, this, aKeyRange, aLimit);
   1.581 +
   1.582 +  nsresult rv = helper->DispatchToTransactionPool();
   1.583 +  if (NS_FAILED(rv)) {
   1.584 +    IDB_WARNING("Failed to dispatch!");
   1.585 +    aRv.Throw(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
   1.586 +    return nullptr;
   1.587 +  }
   1.588 +
   1.589 +  IDB_PROFILER_MARK("IndexedDB Request %llu: "
   1.590 +                    "database(%s).transaction(%s).objectStore(%s).index(%s)."
   1.591 +                    "getAllKeys(%s, %lu)",
   1.592 +                    "IDBRequest[%llu] MT IDBIndex.getAllKeys()",
   1.593 +                    request->GetSerialNumber(),
   1.594 +                    IDB_PROFILER_STRING(ObjectStore()->Transaction()->
   1.595 +                                        Database()),
   1.596 +                    IDB_PROFILER_STRING(ObjectStore()->Transaction()),
   1.597 +                    IDB_PROFILER_STRING(ObjectStore()),
   1.598 +                    IDB_PROFILER_STRING(this), IDB_PROFILER_STRING(aKeyRange),
   1.599 +                    aLimit);
   1.600 +
   1.601 +  return request.forget();
   1.602 +}
   1.603 +
   1.604 +already_AddRefed<IDBRequest>
   1.605 +IDBIndex::CountInternal(IDBKeyRange* aKeyRange, ErrorResult& aRv)
   1.606 +{
   1.607 +  NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
   1.608 +
   1.609 +  IDBTransaction* transaction = mObjectStore->Transaction();
   1.610 +  if (!transaction->IsOpen()) {
   1.611 +    aRv.Throw(NS_ERROR_DOM_INDEXEDDB_TRANSACTION_INACTIVE_ERR);
   1.612 +    return nullptr;
   1.613 +  }
   1.614 +
   1.615 +  nsRefPtr<IDBRequest> request = GenerateRequest(this);
   1.616 +  if (!request) {
   1.617 +    IDB_WARNING("Failed to generate request!");
   1.618 +    aRv.Throw(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
   1.619 +    return nullptr;
   1.620 +  }
   1.621 +
   1.622 +  nsRefPtr<CountHelper> helper =
   1.623 +    new CountHelper(transaction, request, this, aKeyRange);
   1.624 +
   1.625 +  nsresult rv = helper->DispatchToTransactionPool();
   1.626 +  if (NS_FAILED(rv)) {
   1.627 +    IDB_WARNING("Failed to dispatch!");
   1.628 +    aRv.Throw(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
   1.629 +    return nullptr;
   1.630 +  }
   1.631 +
   1.632 +  IDB_PROFILER_MARK("IndexedDB Request %llu: "
   1.633 +                    "database(%s).transaction(%s).objectStore(%s).index(%s)."
   1.634 +                    "count(%s)",
   1.635 +                    "IDBRequest[%llu] MT IDBIndex.count()",
   1.636 +                    request->GetSerialNumber(),
   1.637 +                    IDB_PROFILER_STRING(ObjectStore()->Transaction()->
   1.638 +                                        Database()),
   1.639 +                    IDB_PROFILER_STRING(ObjectStore()->Transaction()),
   1.640 +                    IDB_PROFILER_STRING(ObjectStore()),
   1.641 +                    IDB_PROFILER_STRING(this), IDB_PROFILER_STRING(aKeyRange));
   1.642 +
   1.643 +  return request.forget();
   1.644 +}
   1.645 +
   1.646 +already_AddRefed<IDBRequest>
   1.647 +IDBIndex::OpenKeyCursorInternal(IDBKeyRange* aKeyRange, size_t aDirection,
   1.648 +                                ErrorResult& aRv)
   1.649 +{
   1.650 +  NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
   1.651 +
   1.652 +  IDBTransaction* transaction = mObjectStore->Transaction();
   1.653 +  if (!transaction->IsOpen()) {
   1.654 +    aRv.Throw(NS_ERROR_DOM_INDEXEDDB_TRANSACTION_INACTIVE_ERR);
   1.655 +    return nullptr;
   1.656 +  }
   1.657 +
   1.658 +  IDBCursor::Direction direction =
   1.659 +    static_cast<IDBCursor::Direction>(aDirection);
   1.660 +
   1.661 +  nsRefPtr<IDBRequest> request = GenerateRequest(this);
   1.662 +  if (!request) {
   1.663 +    IDB_WARNING("Failed to generate request!");
   1.664 +    aRv.Throw(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
   1.665 +    return nullptr;
   1.666 +  }
   1.667 +
   1.668 +  nsRefPtr<OpenKeyCursorHelper> helper =
   1.669 +    new OpenKeyCursorHelper(transaction, request, this, aKeyRange, direction);
   1.670 +
   1.671 +  nsresult rv = helper->DispatchToTransactionPool();
   1.672 +  if (NS_FAILED(rv)) {
   1.673 +    IDB_WARNING("Failed to dispatch!");
   1.674 +    aRv.Throw(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
   1.675 +    return nullptr;
   1.676 +  }
   1.677 +
   1.678 +  IDB_PROFILER_MARK("IndexedDB Request %llu: "
   1.679 +                    "database(%s).transaction(%s).objectStore(%s).index(%s)."
   1.680 +                    "openKeyCursor(%s)",
   1.681 +                    "IDBRequest[%llu] MT IDBIndex.openKeyCursor()",
   1.682 +                    request->GetSerialNumber(),
   1.683 +                    IDB_PROFILER_STRING(ObjectStore()->Transaction()->
   1.684 +                                        Database()),
   1.685 +                    IDB_PROFILER_STRING(ObjectStore()->Transaction()),
   1.686 +                    IDB_PROFILER_STRING(ObjectStore()),
   1.687 +                    IDB_PROFILER_STRING(this), IDB_PROFILER_STRING(aKeyRange),
   1.688 +                    IDB_PROFILER_STRING(direction));
   1.689 +
   1.690 +  return request.forget();
   1.691 +}
   1.692 +
   1.693 +nsresult
   1.694 +IDBIndex::OpenCursorInternal(IDBKeyRange* aKeyRange,
   1.695 +                             size_t aDirection,
   1.696 +                             IDBRequest** _retval)
   1.697 +{
   1.698 +  NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
   1.699 +
   1.700 +  IDBTransaction* transaction = mObjectStore->Transaction();
   1.701 +  if (!transaction->IsOpen()) {
   1.702 +    return NS_ERROR_DOM_INDEXEDDB_TRANSACTION_INACTIVE_ERR;
   1.703 +  }
   1.704 +
   1.705 +  IDBCursor::Direction direction =
   1.706 +    static_cast<IDBCursor::Direction>(aDirection);
   1.707 +
   1.708 +  nsRefPtr<IDBRequest> request = GenerateRequest(this);
   1.709 +  IDB_ENSURE_TRUE(request, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
   1.710 +
   1.711 +  nsRefPtr<OpenCursorHelper> helper =
   1.712 +    new OpenCursorHelper(transaction, request, this, aKeyRange, direction);
   1.713 +
   1.714 +  nsresult rv = helper->DispatchToTransactionPool();
   1.715 +  IDB_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
   1.716 +
   1.717 +  IDB_PROFILER_MARK("IndexedDB Request %llu: "
   1.718 +                    "database(%s).transaction(%s).objectStore(%s).index(%s)."
   1.719 +                    "openCursor(%s)",
   1.720 +                    "IDBRequest[%llu] MT IDBIndex.openCursor()",
   1.721 +                    request->GetSerialNumber(),
   1.722 +                    IDB_PROFILER_STRING(ObjectStore()->Transaction()->
   1.723 +                                        Database()),
   1.724 +                    IDB_PROFILER_STRING(ObjectStore()->Transaction()),
   1.725 +                    IDB_PROFILER_STRING(ObjectStore()),
   1.726 +                    IDB_PROFILER_STRING(this), IDB_PROFILER_STRING(aKeyRange),
   1.727 +                    IDB_PROFILER_STRING(direction));
   1.728 +
   1.729 +  request.forget(_retval);
   1.730 +  return NS_OK;
   1.731 +}
   1.732 +
   1.733 +nsresult
   1.734 +IDBIndex::OpenCursorFromChildProcess(IDBRequest* aRequest,
   1.735 +                                     size_t aDirection,
   1.736 +                                     const Key& aKey,
   1.737 +                                     const Key& aObjectKey,
   1.738 +                                     IDBCursor** _retval)
   1.739 +{
   1.740 +  NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
   1.741 +
   1.742 +  IDBCursor::Direction direction =
   1.743 +    static_cast<IDBCursor::Direction>(aDirection);
   1.744 +
   1.745 +  nsRefPtr<IDBCursor> cursor =
   1.746 +    IDBCursor::Create(aRequest, mObjectStore->Transaction(), this, direction,
   1.747 +                      Key(), EmptyCString(), EmptyCString(), aKey, aObjectKey);
   1.748 +  IDB_ENSURE_TRUE(cursor, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
   1.749 +
   1.750 +  cursor.forget(_retval);
   1.751 +  return NS_OK;
   1.752 +}
   1.753 +
   1.754 +nsresult
   1.755 +IDBIndex::OpenCursorFromChildProcess(
   1.756 +                            IDBRequest* aRequest,
   1.757 +                            size_t aDirection,
   1.758 +                            const Key& aKey,
   1.759 +                            const Key& aObjectKey,
   1.760 +                            const SerializedStructuredCloneReadInfo& aCloneInfo,
   1.761 +                            nsTArray<StructuredCloneFile>& aBlobs,
   1.762 +                            IDBCursor** _retval)
   1.763 +{
   1.764 +  NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
   1.765 +  NS_ASSERTION((!aCloneInfo.dataLength && !aCloneInfo.data) ||
   1.766 +               (aCloneInfo.dataLength && aCloneInfo.data),
   1.767 +               "Inconsistent clone info!");
   1.768 +
   1.769 +  IDBCursor::Direction direction =
   1.770 +    static_cast<IDBCursor::Direction>(aDirection);
   1.771 +
   1.772 +  StructuredCloneReadInfo cloneInfo;
   1.773 +
   1.774 +  if (!cloneInfo.SetFromSerialized(aCloneInfo)) {
   1.775 +    IDB_WARNING("Failed to copy clone buffer!");
   1.776 +    return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
   1.777 +  }
   1.778 +
   1.779 +  cloneInfo.mFiles.SwapElements(aBlobs);
   1.780 +
   1.781 +  nsRefPtr<IDBCursor> cursor =
   1.782 +    IDBCursor::Create(aRequest, mObjectStore->Transaction(), this, direction,
   1.783 +                      Key(), EmptyCString(), EmptyCString(), aKey, aObjectKey,
   1.784 +                      Move(cloneInfo));
   1.785 +  IDB_ENSURE_TRUE(cursor, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
   1.786 +
   1.787 +  NS_ASSERTION(!cloneInfo.mCloneBuffer.data(), "Should have swapped!");
   1.788 +
   1.789 +  cursor.forget(_retval);
   1.790 +  return NS_OK;
   1.791 +}
   1.792 +
   1.793 +NS_IMPL_CYCLE_COLLECTION_CLASS(IDBIndex)
   1.794 +
   1.795 +NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(IDBIndex)
   1.796 +  NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
   1.797 +  NS_IMPL_CYCLE_COLLECTION_TRACE_JSVAL_MEMBER_CALLBACK(mCachedKeyPath)
   1.798 +NS_IMPL_CYCLE_COLLECTION_TRACE_END
   1.799 +
   1.800 +NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(IDBIndex)
   1.801 +  NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
   1.802 +  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mObjectStore)
   1.803 +NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
   1.804 +
   1.805 +NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(IDBIndex)
   1.806 +  NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
   1.807 +
   1.808 +  // Don't unlink mObjectStore!
   1.809 +
   1.810 +  tmp->mCachedKeyPath = JSVAL_VOID;
   1.811 +
   1.812 +  if (tmp->mRooted) {
   1.813 +    mozilla::DropJSObjects(tmp);
   1.814 +    tmp->mRooted = false;
   1.815 +  }
   1.816 +NS_IMPL_CYCLE_COLLECTION_UNLINK_END
   1.817 +
   1.818 +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(IDBIndex)
   1.819 +  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
   1.820 +  NS_INTERFACE_MAP_ENTRY(nsISupports)
   1.821 +NS_INTERFACE_MAP_END
   1.822 +
   1.823 +NS_IMPL_CYCLE_COLLECTING_ADDREF(IDBIndex)
   1.824 +NS_IMPL_CYCLE_COLLECTING_RELEASE(IDBIndex)
   1.825 +
   1.826 +JSObject*
   1.827 +IDBIndex::WrapObject(JSContext* aCx)
   1.828 +{
   1.829 +  return IDBIndexBinding::Wrap(aCx, this);
   1.830 +}
   1.831 +
   1.832 +void
   1.833 +IDBIndex::GetKeyPath(JSContext* aCx, JS::MutableHandle<JS::Value> aResult,
   1.834 +                     ErrorResult& aRv)
   1.835 +{
   1.836 +  NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
   1.837 +
   1.838 +  if (!JSVAL_IS_VOID(mCachedKeyPath)) {
   1.839 +    JS::ExposeValueToActiveJS(mCachedKeyPath);
   1.840 +    aResult.set(mCachedKeyPath);
   1.841 +    return;
   1.842 +  }
   1.843 +
   1.844 +  aRv = GetKeyPath().ToJSVal(aCx, mCachedKeyPath);
   1.845 +  if (NS_WARN_IF(aRv.Failed())) {
   1.846 +    return;
   1.847 +  }
   1.848 +
   1.849 +  if (JSVAL_IS_GCTHING(mCachedKeyPath)) {
   1.850 +    mozilla::HoldJSObjects(this);
   1.851 +    mRooted = true;
   1.852 +  }
   1.853 +
   1.854 +  JS::ExposeValueToActiveJS(mCachedKeyPath);
   1.855 +  aResult.set(mCachedKeyPath);
   1.856 +}
   1.857 +
   1.858 +already_AddRefed<IDBRequest>
   1.859 +IDBIndex::Get(JSContext* aCx, JS::Handle<JS::Value> aKey, ErrorResult& aRv)
   1.860 +{
   1.861 +  NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
   1.862 +
   1.863 +  IDBTransaction* transaction = mObjectStore->Transaction();
   1.864 +  if (!transaction->IsOpen()) {
   1.865 +    aRv.Throw(NS_ERROR_DOM_INDEXEDDB_TRANSACTION_INACTIVE_ERR);
   1.866 +    return nullptr;
   1.867 +  }
   1.868 +
   1.869 +  nsRefPtr<IDBKeyRange> keyRange;
   1.870 +  aRv = IDBKeyRange::FromJSVal(aCx, aKey, getter_AddRefs(keyRange));
   1.871 +  ENSURE_SUCCESS(aRv, nullptr);
   1.872 +
   1.873 +  if (!keyRange) {
   1.874 +    // Must specify a key or keyRange for getKey().
   1.875 +    aRv.Throw(NS_ERROR_DOM_INDEXEDDB_DATA_ERR);
   1.876 +    return nullptr;
   1.877 +  }
   1.878 +
   1.879 +  return GetInternal(keyRange, aRv);
   1.880 +}
   1.881 +
   1.882 +already_AddRefed<IDBRequest>
   1.883 +IDBIndex::GetKey(JSContext* aCx, JS::Handle<JS::Value> aKey, ErrorResult& aRv)
   1.884 +{
   1.885 +  NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
   1.886 +
   1.887 +  IDBTransaction* transaction = mObjectStore->Transaction();
   1.888 +  if (!transaction->IsOpen()) {
   1.889 +    aRv.Throw(NS_ERROR_DOM_INDEXEDDB_TRANSACTION_INACTIVE_ERR);
   1.890 +    return nullptr;
   1.891 +  }
   1.892 +
   1.893 +  nsRefPtr<IDBKeyRange> keyRange;
   1.894 +  aRv = IDBKeyRange::FromJSVal(aCx, aKey, getter_AddRefs(keyRange));
   1.895 +  ENSURE_SUCCESS(aRv, nullptr);
   1.896 +
   1.897 +  if (!keyRange) {
   1.898 +    // Must specify a key or keyRange for get().
   1.899 +    aRv.Throw(NS_ERROR_DOM_INDEXEDDB_DATA_ERR);
   1.900 +    return nullptr;
   1.901 +  }
   1.902 +
   1.903 +  return GetKeyInternal(keyRange, aRv);
   1.904 +}
   1.905 +
   1.906 +already_AddRefed<IDBRequest>
   1.907 +IDBIndex::GetAll(JSContext* aCx, JS::Handle<JS::Value> aKey,
   1.908 +                 const Optional<uint32_t>& aLimit, ErrorResult& aRv)
   1.909 +{
   1.910 +  NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
   1.911 +
   1.912 +  IDBTransaction* transaction = mObjectStore->Transaction();
   1.913 +  if (!transaction->IsOpen()) {
   1.914 +    aRv.Throw(NS_ERROR_DOM_INDEXEDDB_TRANSACTION_INACTIVE_ERR);
   1.915 +    return nullptr;
   1.916 +  }
   1.917 +
   1.918 +  nsRefPtr<IDBKeyRange> keyRange;
   1.919 +  aRv = IDBKeyRange::FromJSVal(aCx, aKey, getter_AddRefs(keyRange));
   1.920 +  ENSURE_SUCCESS(aRv, nullptr);
   1.921 +
   1.922 +  uint32_t limit = UINT32_MAX;
   1.923 +  if (aLimit.WasPassed() && aLimit.Value() > 0) {
   1.924 +    limit = aLimit.Value();
   1.925 +  }
   1.926 +
   1.927 +  return GetAllInternal(keyRange, limit, aRv);
   1.928 +}
   1.929 +
   1.930 +already_AddRefed<IDBRequest>
   1.931 +IDBIndex::GetAllKeys(JSContext* aCx,
   1.932 +                     JS::Handle<JS::Value> aKey,
   1.933 +                     const Optional<uint32_t>& aLimit, ErrorResult& aRv)
   1.934 +{
   1.935 +  NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
   1.936 +
   1.937 +  IDBTransaction* transaction = mObjectStore->Transaction();
   1.938 +  if (!transaction->IsOpen()) {
   1.939 +    aRv.Throw(NS_ERROR_DOM_INDEXEDDB_TRANSACTION_INACTIVE_ERR);
   1.940 +    return nullptr;
   1.941 +  }
   1.942 +
   1.943 +  nsRefPtr<IDBKeyRange> keyRange;
   1.944 +  aRv = IDBKeyRange::FromJSVal(aCx, aKey, getter_AddRefs(keyRange));
   1.945 +  ENSURE_SUCCESS(aRv, nullptr);
   1.946 +
   1.947 +  uint32_t limit = UINT32_MAX;
   1.948 +  if (aLimit.WasPassed() && aLimit.Value() > 0) {
   1.949 +    limit = aLimit.Value();
   1.950 +  }
   1.951 +
   1.952 +  return GetAllKeysInternal(keyRange, limit, aRv);
   1.953 +}
   1.954 +
   1.955 +already_AddRefed<IDBRequest>
   1.956 +IDBIndex::OpenCursor(JSContext* aCx,
   1.957 +                     JS::Handle<JS::Value> aRange,
   1.958 +                     IDBCursorDirection aDirection, ErrorResult& aRv)
   1.959 +{
   1.960 +  NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
   1.961 +
   1.962 +  IDBTransaction* transaction = mObjectStore->Transaction();
   1.963 +  if (!transaction->IsOpen()) {
   1.964 +    aRv.Throw(NS_ERROR_DOM_INDEXEDDB_TRANSACTION_INACTIVE_ERR);
   1.965 +    return nullptr;
   1.966 +  }
   1.967 +
   1.968 +  nsRefPtr<IDBKeyRange> keyRange;
   1.969 +  aRv = IDBKeyRange::FromJSVal(aCx, aRange, getter_AddRefs(keyRange));
   1.970 +  ENSURE_SUCCESS(aRv, nullptr);
   1.971 +
   1.972 +  IDBCursor::Direction direction = IDBCursor::ConvertDirection(aDirection);
   1.973 +
   1.974 +  nsRefPtr<IDBRequest> request = GenerateRequest(this);
   1.975 +  if (!request) {
   1.976 +    IDB_WARNING("Failed to generate request!");
   1.977 +    aRv.Throw(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
   1.978 +    return nullptr;
   1.979 +  }
   1.980 +
   1.981 +  nsRefPtr<OpenCursorHelper> helper =
   1.982 +    new OpenCursorHelper(transaction, request, this, keyRange, direction);
   1.983 +
   1.984 +  nsresult rv = helper->DispatchToTransactionPool();
   1.985 +  if (NS_FAILED(rv)) {
   1.986 +    IDB_WARNING("Failed to dispatch!");
   1.987 +    aRv.Throw(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
   1.988 +    return nullptr;
   1.989 +  }
   1.990 +
   1.991 +  return request.forget();
   1.992 +}
   1.993 +
   1.994 +already_AddRefed<IDBRequest>
   1.995 +IDBIndex::OpenKeyCursor(JSContext* aCx,
   1.996 +                        JS::Handle<JS::Value> aRange,
   1.997 +                        IDBCursorDirection aDirection, ErrorResult& aRv)
   1.998 +{
   1.999 +  NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
  1.1000 +
  1.1001 +  IDBTransaction* transaction = mObjectStore->Transaction();
  1.1002 +  if (!transaction->IsOpen()) {
  1.1003 +    aRv.Throw(NS_ERROR_DOM_INDEXEDDB_TRANSACTION_INACTIVE_ERR);
  1.1004 +    return nullptr;
  1.1005 +  }
  1.1006 +
  1.1007 +  nsRefPtr<IDBKeyRange> keyRange;
  1.1008 +  aRv = IDBKeyRange::FromJSVal(aCx, aRange, getter_AddRefs(keyRange));
  1.1009 +  ENSURE_SUCCESS(aRv, nullptr);
  1.1010 +
  1.1011 +  IDBCursor::Direction direction = IDBCursor::ConvertDirection(aDirection);
  1.1012 +
  1.1013 +  return OpenKeyCursorInternal(keyRange, direction, aRv);
  1.1014 +}
  1.1015 +
  1.1016 +already_AddRefed<IDBRequest>
  1.1017 +IDBIndex::Count(JSContext* aCx, JS::Handle<JS::Value> aKey,
  1.1018 +                ErrorResult& aRv)
  1.1019 +{
  1.1020 +  IDBTransaction* transaction = mObjectStore->Transaction();
  1.1021 +  if (!transaction->IsOpen()) {
  1.1022 +    aRv.Throw(NS_ERROR_DOM_INDEXEDDB_TRANSACTION_INACTIVE_ERR);
  1.1023 +    return nullptr;
  1.1024 +  }
  1.1025 +
  1.1026 +  nsRefPtr<IDBKeyRange> keyRange;
  1.1027 +  aRv = IDBKeyRange::FromJSVal(aCx, aKey, getter_AddRefs(keyRange));
  1.1028 +  ENSURE_SUCCESS(aRv, nullptr);
  1.1029 +
  1.1030 +  return CountInternal(keyRange, aRv);
  1.1031 +}
  1.1032 +
  1.1033 +void
  1.1034 +IndexHelper::ReleaseMainThreadObjects()
  1.1035 +{
  1.1036 +  mIndex = nullptr;
  1.1037 +  AsyncConnectionHelper::ReleaseMainThreadObjects();
  1.1038 +}
  1.1039 +
  1.1040 +nsresult
  1.1041 +IndexHelper::Dispatch(nsIEventTarget* aDatabaseThread)
  1.1042 +{
  1.1043 +  NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
  1.1044 +
  1.1045 +  PROFILER_MAIN_THREAD_LABEL("IndexedDB", "IndexHelper::Dispatch");
  1.1046 +
  1.1047 +  if (IndexedDatabaseManager::IsMainProcess()) {
  1.1048 +    return AsyncConnectionHelper::Dispatch(aDatabaseThread);
  1.1049 +  }
  1.1050 +
  1.1051 +  // If we've been invalidated then there's no point sending anything to the
  1.1052 +  // parent process.
  1.1053 +  if (mIndex->ObjectStore()->Transaction()->Database()->IsInvalidated()) {
  1.1054 +    IDB_REPORT_INTERNAL_ERR();
  1.1055 +    return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
  1.1056 +  }
  1.1057 +
  1.1058 +  IndexedDBIndexChild* indexActor = mIndex->GetActorChild();
  1.1059 +  NS_ASSERTION(indexActor, "Must have an actor here!");
  1.1060 +
  1.1061 +  IndexRequestParams params;
  1.1062 +  nsresult rv = PackArgumentsForParentProcess(params);
  1.1063 +  IDB_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
  1.1064 +
  1.1065 +  NoDispatchEventTarget target;
  1.1066 +  rv = AsyncConnectionHelper::Dispatch(&target);
  1.1067 +  IDB_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
  1.1068 +
  1.1069 +  mActor = new IndexedDBIndexRequestChild(this, mIndex, params.type());
  1.1070 +  indexActor->SendPIndexedDBRequestConstructor(mActor, params);
  1.1071 +
  1.1072 +  return NS_OK;
  1.1073 +}
  1.1074 +
  1.1075 +nsresult
  1.1076 +GetKeyHelper::DoDatabaseWork(mozIStorageConnection* /* aConnection */)
  1.1077 +{
  1.1078 +  NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
  1.1079 +  NS_ASSERTION(IndexedDatabaseManager::IsMainProcess(), "Wrong process!");
  1.1080 +  NS_ASSERTION(mKeyRange, "Must have a key range here!");
  1.1081 +
  1.1082 +  PROFILER_LABEL("IndexedDB", "GetKeyHelper::DoDatabaseWork");
  1.1083 +
  1.1084 +  nsCString indexTable;
  1.1085 +  if (mIndex->IsUnique()) {
  1.1086 +    indexTable.AssignLiteral("unique_index_data");
  1.1087 +  }
  1.1088 +  else {
  1.1089 +    indexTable.AssignLiteral("index_data");
  1.1090 +  }
  1.1091 +
  1.1092 +  nsCString keyRangeClause;
  1.1093 +  mKeyRange->GetBindingClause(NS_LITERAL_CSTRING("value"), keyRangeClause);
  1.1094 +
  1.1095 +  NS_ASSERTION(!keyRangeClause.IsEmpty(), "Huh?!");
  1.1096 +
  1.1097 +  nsCString query = NS_LITERAL_CSTRING("SELECT object_data_key FROM ") +
  1.1098 +                    indexTable +
  1.1099 +                    NS_LITERAL_CSTRING(" WHERE index_id = :index_id") +
  1.1100 +                    keyRangeClause +
  1.1101 +                    NS_LITERAL_CSTRING(" LIMIT 1");
  1.1102 +
  1.1103 +  nsCOMPtr<mozIStorageStatement> stmt = mTransaction->GetCachedStatement(query);
  1.1104 +  IDB_ENSURE_TRUE(stmt, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
  1.1105 +
  1.1106 +  mozStorageStatementScoper scoper(stmt);
  1.1107 +
  1.1108 +  nsresult rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("index_id"),
  1.1109 +                                      mIndex->Id());
  1.1110 +  IDB_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
  1.1111 +
  1.1112 +  rv = mKeyRange->BindToStatement(stmt);
  1.1113 +  NS_ENSURE_SUCCESS(rv, rv);
  1.1114 +
  1.1115 +  bool hasResult;
  1.1116 +  rv = stmt->ExecuteStep(&hasResult);
  1.1117 +  IDB_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
  1.1118 +
  1.1119 +  if (hasResult) {
  1.1120 +    rv = mKey.SetFromStatement(stmt, 0);
  1.1121 +    NS_ENSURE_SUCCESS(rv, rv);
  1.1122 +  }
  1.1123 +
  1.1124 +  return NS_OK;
  1.1125 +}
  1.1126 +
  1.1127 +nsresult
  1.1128 +GetKeyHelper::GetSuccessResult(JSContext* aCx,
  1.1129 +                               JS::MutableHandle<JS::Value> aVal)
  1.1130 +{
  1.1131 +  return mKey.ToJSVal(aCx, aVal);
  1.1132 +}
  1.1133 +
  1.1134 +void
  1.1135 +GetKeyHelper::ReleaseMainThreadObjects()
  1.1136 +{
  1.1137 +  mKeyRange = nullptr;
  1.1138 +  IndexHelper::ReleaseMainThreadObjects();
  1.1139 +}
  1.1140 +
  1.1141 +nsresult
  1.1142 +GetKeyHelper::PackArgumentsForParentProcess(IndexRequestParams& aParams)
  1.1143 +{
  1.1144 +  NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
  1.1145 +  NS_ASSERTION(!IndexedDatabaseManager::IsMainProcess(), "Wrong process!");
  1.1146 +  NS_ASSERTION(mKeyRange, "This should never be null!");
  1.1147 +
  1.1148 +  PROFILER_MAIN_THREAD_LABEL("IndexedDB",
  1.1149 +                             "GetKeyHelper::PackArgumentsForParentProcess");
  1.1150 +
  1.1151 +  GetKeyParams params;
  1.1152 +
  1.1153 +  mKeyRange->ToSerializedKeyRange(params.keyRange());
  1.1154 +
  1.1155 +  aParams = params;
  1.1156 +  return NS_OK;
  1.1157 +}
  1.1158 +
  1.1159 +AsyncConnectionHelper::ChildProcessSendResult
  1.1160 +GetKeyHelper::SendResponseToChildProcess(nsresult aResultCode)
  1.1161 +{
  1.1162 +  NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
  1.1163 +  NS_ASSERTION(IndexedDatabaseManager::IsMainProcess(), "Wrong process!");
  1.1164 +
  1.1165 +  PROFILER_MAIN_THREAD_LABEL("IndexedDB",
  1.1166 +                             "GetKeyHelper::SendResponseToChildProcess");
  1.1167 +
  1.1168 +  IndexedDBRequestParentBase* actor = mRequest->GetActorParent();
  1.1169 +  NS_ASSERTION(actor, "How did we get this far without an actor?");
  1.1170 +
  1.1171 +  ResponseValue response;
  1.1172 +  if (NS_FAILED(aResultCode)) {
  1.1173 +    response = aResultCode;
  1.1174 +  }
  1.1175 +  else {
  1.1176 +    GetKeyResponse getKeyResponse;
  1.1177 +    getKeyResponse.key() = mKey;
  1.1178 +    response = getKeyResponse;
  1.1179 +  }
  1.1180 +
  1.1181 +  if (!actor->SendResponse(response)) {
  1.1182 +    return Error;
  1.1183 +  }
  1.1184 +
  1.1185 +  return Success_Sent;
  1.1186 +}
  1.1187 +
  1.1188 +nsresult
  1.1189 +GetKeyHelper::UnpackResponseFromParentProcess(
  1.1190 +                                            const ResponseValue& aResponseValue)
  1.1191 +{
  1.1192 +  NS_ASSERTION(aResponseValue.type() == ResponseValue::TGetKeyResponse,
  1.1193 +               "Bad response type!");
  1.1194 +
  1.1195 +  mKey = aResponseValue.get_GetKeyResponse().key();
  1.1196 +  return NS_OK;
  1.1197 +}
  1.1198 +
  1.1199 +nsresult
  1.1200 +GetHelper::DoDatabaseWork(mozIStorageConnection* /* aConnection */)
  1.1201 +{
  1.1202 +  NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
  1.1203 +  NS_ASSERTION(IndexedDatabaseManager::IsMainProcess(), "Wrong process!");
  1.1204 +  NS_ASSERTION(mKeyRange, "Must have a key range here!");
  1.1205 +
  1.1206 +  PROFILER_LABEL("IndexedDB", "GetHelper::DoDatabaseWork [IDBIndex.cpp]");
  1.1207 +
  1.1208 +  nsCString indexTable;
  1.1209 +  if (mIndex->IsUnique()) {
  1.1210 +    indexTable.AssignLiteral("unique_index_data");
  1.1211 +  }
  1.1212 +  else {
  1.1213 +    indexTable.AssignLiteral("index_data");
  1.1214 +  }
  1.1215 +
  1.1216 +  nsCString keyRangeClause;
  1.1217 +  mKeyRange->GetBindingClause(NS_LITERAL_CSTRING("value"), keyRangeClause);
  1.1218 +
  1.1219 +  NS_ASSERTION(!keyRangeClause.IsEmpty(), "Huh?!");
  1.1220 +
  1.1221 +  nsCString query = NS_LITERAL_CSTRING("SELECT data, file_ids FROM object_data "
  1.1222 +                                       "INNER JOIN ") + indexTable +
  1.1223 +                    NS_LITERAL_CSTRING(" AS index_table ON object_data.id = ") +
  1.1224 +                    NS_LITERAL_CSTRING("index_table.object_data_id WHERE "
  1.1225 +                                       "index_id = :index_id") +
  1.1226 +                    keyRangeClause +
  1.1227 +                    NS_LITERAL_CSTRING(" LIMIT 1");
  1.1228 +
  1.1229 +  nsCOMPtr<mozIStorageStatement> stmt = mTransaction->GetCachedStatement(query);
  1.1230 +  IDB_ENSURE_TRUE(stmt, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
  1.1231 +
  1.1232 +  mozStorageStatementScoper scoper(stmt);
  1.1233 +
  1.1234 +  nsresult rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("index_id"),
  1.1235 +                                      mIndex->Id());
  1.1236 +  IDB_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
  1.1237 +
  1.1238 +  rv = mKeyRange->BindToStatement(stmt);
  1.1239 +  NS_ENSURE_SUCCESS(rv, rv);
  1.1240 +
  1.1241 +  bool hasResult;
  1.1242 +  rv = stmt->ExecuteStep(&hasResult);
  1.1243 +  IDB_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
  1.1244 +
  1.1245 +  if (hasResult) {
  1.1246 +    rv = IDBObjectStore::GetStructuredCloneReadInfoFromStatement(stmt, 0, 1,
  1.1247 +      mDatabase, mCloneReadInfo);
  1.1248 +    NS_ENSURE_SUCCESS(rv, rv);
  1.1249 +  }
  1.1250 +
  1.1251 +  return NS_OK;
  1.1252 +}
  1.1253 +
  1.1254 +nsresult
  1.1255 +GetHelper::GetSuccessResult(JSContext* aCx,
  1.1256 +                            JS::MutableHandle<JS::Value> aVal)
  1.1257 +{
  1.1258 +  bool result = IDBObjectStore::DeserializeValue(aCx, mCloneReadInfo, aVal);
  1.1259 +
  1.1260 +  mCloneReadInfo.mCloneBuffer.clear();
  1.1261 +
  1.1262 +  NS_ENSURE_TRUE(result, NS_ERROR_FAILURE);
  1.1263 +  return NS_OK;
  1.1264 +}
  1.1265 +
  1.1266 +void
  1.1267 +GetHelper::ReleaseMainThreadObjects()
  1.1268 +{
  1.1269 +  IDBObjectStore::ClearCloneReadInfo(mCloneReadInfo);
  1.1270 +  GetKeyHelper::ReleaseMainThreadObjects();
  1.1271 +}
  1.1272 +
  1.1273 +nsresult
  1.1274 +GetHelper::PackArgumentsForParentProcess(IndexRequestParams& aParams)
  1.1275 +{
  1.1276 +  NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
  1.1277 +  NS_ASSERTION(!IndexedDatabaseManager::IsMainProcess(), "Wrong process!");
  1.1278 +  NS_ASSERTION(mKeyRange, "This should never be null!");
  1.1279 +
  1.1280 +  PROFILER_MAIN_THREAD_LABEL("IndexedDB",
  1.1281 +                             "GetHelper::PackArgumentsForParentProcess "
  1.1282 +                             "[IDBIndex.cpp]");
  1.1283 +
  1.1284 +  GetParams params;
  1.1285 +
  1.1286 +  mKeyRange->ToSerializedKeyRange(params.keyRange());
  1.1287 +
  1.1288 +  aParams = params;
  1.1289 +  return NS_OK;
  1.1290 +}
  1.1291 +
  1.1292 +AsyncConnectionHelper::ChildProcessSendResult
  1.1293 +GetHelper::SendResponseToChildProcess(nsresult aResultCode)
  1.1294 +{
  1.1295 +  NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
  1.1296 +  NS_ASSERTION(IndexedDatabaseManager::IsMainProcess(), "Wrong process!");
  1.1297 +
  1.1298 +  PROFILER_MAIN_THREAD_LABEL("IndexedDB",
  1.1299 +                             "GetHelper::SendResponseToChildProcess "
  1.1300 +                             "[IDBIndex.cpp]");
  1.1301 +
  1.1302 +  IndexedDBRequestParentBase* actor = mRequest->GetActorParent();
  1.1303 +  NS_ASSERTION(actor, "How did we get this far without an actor?");
  1.1304 +
  1.1305 +  InfallibleTArray<PBlobParent*> blobsParent;
  1.1306 +
  1.1307 +  if (NS_SUCCEEDED(aResultCode)) {
  1.1308 +    IDBDatabase* database = mIndex->ObjectStore()->Transaction()->Database();
  1.1309 +    NS_ASSERTION(database, "This should never be null!");
  1.1310 +
  1.1311 +    ContentParent* contentParent = database->GetContentParent();
  1.1312 +    NS_ASSERTION(contentParent, "This should never be null!");
  1.1313 +
  1.1314 +    FileManager* fileManager = database->Manager();
  1.1315 +    NS_ASSERTION(fileManager, "This should never be null!");
  1.1316 +
  1.1317 +    const nsTArray<StructuredCloneFile>& files = mCloneReadInfo.mFiles;
  1.1318 +
  1.1319 +    aResultCode =
  1.1320 +      IDBObjectStore::ConvertBlobsToActors(contentParent, fileManager, files,
  1.1321 +                                           blobsParent);
  1.1322 +    if (NS_FAILED(aResultCode)) {
  1.1323 +      NS_WARNING("ConvertBlobActors failed!");
  1.1324 +    }
  1.1325 +  }
  1.1326 +
  1.1327 +  ResponseValue response;
  1.1328 +  if (NS_FAILED(aResultCode)) {
  1.1329 +    response = aResultCode;
  1.1330 +  }
  1.1331 +  else {
  1.1332 +    GetResponse getResponse;
  1.1333 +    getResponse.cloneInfo() = mCloneReadInfo;
  1.1334 +    getResponse.blobsParent().SwapElements(blobsParent);
  1.1335 +    response = getResponse;
  1.1336 +  }
  1.1337 +
  1.1338 +  if (!actor->SendResponse(response)) {
  1.1339 +    return Error;
  1.1340 +  }
  1.1341 +
  1.1342 +  return Success_Sent;
  1.1343 +}
  1.1344 +
  1.1345 +nsresult
  1.1346 +GetHelper::UnpackResponseFromParentProcess(const ResponseValue& aResponseValue)
  1.1347 +{
  1.1348 +  NS_ASSERTION(aResponseValue.type() == ResponseValue::TGetResponse,
  1.1349 +               "Bad response type!");
  1.1350 +
  1.1351 +  const GetResponse& getResponse = aResponseValue.get_GetResponse();
  1.1352 +  const SerializedStructuredCloneReadInfo& cloneInfo = getResponse.cloneInfo();
  1.1353 +
  1.1354 +  NS_ASSERTION((!cloneInfo.dataLength && !cloneInfo.data) ||
  1.1355 +               (cloneInfo.dataLength && cloneInfo.data),
  1.1356 +               "Inconsistent clone info!");
  1.1357 +
  1.1358 +  if (!mCloneReadInfo.SetFromSerialized(cloneInfo)) {
  1.1359 +    IDB_WARNING("Failed to copy clone buffer!");
  1.1360 +    return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
  1.1361 +  }
  1.1362 +
  1.1363 +  IDBObjectStore::ConvertActorsToBlobs(getResponse.blobsChild(),
  1.1364 +                                       mCloneReadInfo.mFiles);
  1.1365 +  return NS_OK;
  1.1366 +}
  1.1367 +
  1.1368 +nsresult
  1.1369 +GetAllKeysHelper::DoDatabaseWork(mozIStorageConnection* /* aConnection */)
  1.1370 +{
  1.1371 +  MOZ_ASSERT(!NS_IsMainThread());
  1.1372 +  MOZ_ASSERT(IndexedDatabaseManager::IsMainProcess());
  1.1373 +
  1.1374 +  PROFILER_LABEL("IndexedDB",
  1.1375 +                 "GetAllKeysHelper::DoDatabaseWork [IDBIndex.cpp]");
  1.1376 +
  1.1377 +  nsCString tableName;
  1.1378 +  if (mIndex->IsUnique()) {
  1.1379 +    tableName.AssignLiteral("unique_index_data");
  1.1380 +  }
  1.1381 +  else {
  1.1382 +    tableName.AssignLiteral("index_data");
  1.1383 +  }
  1.1384 +
  1.1385 +  nsCString keyRangeClause;
  1.1386 +  if (mKeyRange) {
  1.1387 +    mKeyRange->GetBindingClause(NS_LITERAL_CSTRING("value"), keyRangeClause);
  1.1388 +  }
  1.1389 +
  1.1390 +  nsCString limitClause;
  1.1391 +  if (mLimit != UINT32_MAX) {
  1.1392 +    limitClause = NS_LITERAL_CSTRING(" LIMIT ");
  1.1393 +    limitClause.AppendInt(mLimit);
  1.1394 +  }
  1.1395 +
  1.1396 +  nsCString query = NS_LITERAL_CSTRING("SELECT object_data_key FROM ") +
  1.1397 +                    tableName +
  1.1398 +                    NS_LITERAL_CSTRING(" WHERE index_id = :index_id") +
  1.1399 +                    keyRangeClause + limitClause;
  1.1400 +
  1.1401 +  nsCOMPtr<mozIStorageStatement> stmt = mTransaction->GetCachedStatement(query);
  1.1402 +  IDB_ENSURE_TRUE(stmt, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
  1.1403 +
  1.1404 +  mozStorageStatementScoper scoper(stmt);
  1.1405 +
  1.1406 +  nsresult rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("index_id"),
  1.1407 +                                      mIndex->Id());
  1.1408 +  IDB_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
  1.1409 +
  1.1410 +  if (mKeyRange) {
  1.1411 +    rv = mKeyRange->BindToStatement(stmt);
  1.1412 +    NS_ENSURE_SUCCESS(rv, rv);
  1.1413 +  }
  1.1414 +
  1.1415 +  mKeys.SetCapacity(std::min<uint32_t>(50, mLimit));
  1.1416 +
  1.1417 +  bool hasResult;
  1.1418 +  while(NS_SUCCEEDED((rv = stmt->ExecuteStep(&hasResult))) && hasResult) {
  1.1419 +    if (mKeys.Capacity() == mKeys.Length()) {
  1.1420 +      mKeys.SetCapacity(mKeys.Capacity() * 2);
  1.1421 +    }
  1.1422 +
  1.1423 +    Key* key = mKeys.AppendElement();
  1.1424 +    NS_ASSERTION(key, "This shouldn't fail!");
  1.1425 +
  1.1426 +    rv = key->SetFromStatement(stmt, 0);
  1.1427 +    NS_ENSURE_SUCCESS(rv, rv);
  1.1428 +  }
  1.1429 +  IDB_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
  1.1430 +
  1.1431 +  return NS_OK;
  1.1432 +}
  1.1433 +
  1.1434 +nsresult
  1.1435 +GetAllKeysHelper::GetSuccessResult(JSContext* aCx,
  1.1436 +                                   JS::MutableHandle<JS::Value> aVal)
  1.1437 +{
  1.1438 +  MOZ_ASSERT(NS_IsMainThread());
  1.1439 +  MOZ_ASSERT(mKeys.Length() <= mLimit);
  1.1440 +
  1.1441 +  PROFILER_MAIN_THREAD_LABEL("IndexedDB",
  1.1442 +                             "GetAllKeysHelper::GetSuccessResult "
  1.1443 +                             "[IDBIndex.cpp]");
  1.1444 +
  1.1445 +  nsTArray<Key> keys;
  1.1446 +  mKeys.SwapElements(keys);
  1.1447 +
  1.1448 +  JS::Rooted<JSObject*> array(aCx, JS_NewArrayObject(aCx, 0));
  1.1449 +  if (!array) {
  1.1450 +    IDB_WARNING("Failed to make array!");
  1.1451 +    return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
  1.1452 +  }
  1.1453 +
  1.1454 +  if (!keys.IsEmpty()) {
  1.1455 +    if (!JS_SetArrayLength(aCx, array, uint32_t(keys.Length()))) {
  1.1456 +      IDB_WARNING("Failed to set array length!");
  1.1457 +      return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
  1.1458 +    }
  1.1459 +
  1.1460 +    for (uint32_t index = 0, count = keys.Length(); index < count; index++) {
  1.1461 +      const Key& key = keys[index];
  1.1462 +      NS_ASSERTION(!key.IsUnset(), "Bad key!");
  1.1463 +
  1.1464 +      JS::Rooted<JS::Value> value(aCx);
  1.1465 +      nsresult rv = key.ToJSVal(aCx, &value);
  1.1466 +      if (NS_FAILED(rv)) {
  1.1467 +        NS_WARNING("Failed to get jsval for key!");
  1.1468 +        return rv;
  1.1469 +      }
  1.1470 +
  1.1471 +      if (!JS_SetElement(aCx, array, index, value)) {
  1.1472 +        IDB_WARNING("Failed to set array element!");
  1.1473 +        return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
  1.1474 +      }
  1.1475 +    }
  1.1476 +  }
  1.1477 +
  1.1478 +  aVal.setObject(*array);
  1.1479 +  return NS_OK;
  1.1480 +}
  1.1481 +
  1.1482 +nsresult
  1.1483 +GetAllKeysHelper::PackArgumentsForParentProcess(IndexRequestParams& aParams)
  1.1484 +{
  1.1485 +  MOZ_ASSERT(NS_IsMainThread());
  1.1486 +  MOZ_ASSERT(!IndexedDatabaseManager::IsMainProcess());
  1.1487 +
  1.1488 +  PROFILER_MAIN_THREAD_LABEL("IndexedDB",
  1.1489 +                             "GetAllKeysHelper::PackArgumentsForParentProcess "
  1.1490 +                             "[IDBIndex.cpp]");
  1.1491 +
  1.1492 +  GetAllKeysParams params;
  1.1493 +
  1.1494 +  if (mKeyRange) {
  1.1495 +    KeyRange keyRange;
  1.1496 +    mKeyRange->ToSerializedKeyRange(keyRange);
  1.1497 +    params.optionalKeyRange() = keyRange;
  1.1498 +  }
  1.1499 +  else {
  1.1500 +    params.optionalKeyRange() = mozilla::void_t();
  1.1501 +  }
  1.1502 +
  1.1503 +  params.limit() = mLimit;
  1.1504 +
  1.1505 +  aParams = params;
  1.1506 +  return NS_OK;
  1.1507 +}
  1.1508 +
  1.1509 +AsyncConnectionHelper::ChildProcessSendResult
  1.1510 +GetAllKeysHelper::SendResponseToChildProcess(nsresult aResultCode)
  1.1511 +{
  1.1512 +  MOZ_ASSERT(NS_IsMainThread());
  1.1513 +  MOZ_ASSERT(IndexedDatabaseManager::IsMainProcess());
  1.1514 +
  1.1515 +  PROFILER_MAIN_THREAD_LABEL("IndexedDB",
  1.1516 +                             "GetAllKeysHelper::SendResponseToChildProcess "
  1.1517 +                             "[IDBIndex.cpp]");
  1.1518 +
  1.1519 +  IndexedDBRequestParentBase* actor = mRequest->GetActorParent();
  1.1520 +  NS_ASSERTION(actor, "How did we get this far without an actor?");
  1.1521 +
  1.1522 +  ResponseValue response;
  1.1523 +  if (NS_FAILED(aResultCode)) {
  1.1524 +    response = aResultCode;
  1.1525 +  }
  1.1526 +  else {
  1.1527 +    GetAllKeysResponse getAllKeysResponse;
  1.1528 +    getAllKeysResponse.keys().AppendElements(mKeys);
  1.1529 +    response = getAllKeysResponse;
  1.1530 +  }
  1.1531 +
  1.1532 +  if (!actor->SendResponse(response)) {
  1.1533 +    return Error;
  1.1534 +  }
  1.1535 +
  1.1536 +  return Success_Sent;
  1.1537 +}
  1.1538 +
  1.1539 +nsresult
  1.1540 +GetAllKeysHelper::UnpackResponseFromParentProcess(
  1.1541 +                                            const ResponseValue& aResponseValue)
  1.1542 +{
  1.1543 +  MOZ_ASSERT(NS_IsMainThread());
  1.1544 +  MOZ_ASSERT(!IndexedDatabaseManager::IsMainProcess());
  1.1545 +  MOZ_ASSERT(aResponseValue.type() == ResponseValue::TGetAllKeysResponse);
  1.1546 +
  1.1547 +  mKeys.AppendElements(aResponseValue.get_GetAllKeysResponse().keys());
  1.1548 +  return NS_OK;
  1.1549 +}
  1.1550 +
  1.1551 +nsresult
  1.1552 +GetAllHelper::DoDatabaseWork(mozIStorageConnection* /* aConnection */)
  1.1553 +{
  1.1554 +  NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
  1.1555 +  NS_ASSERTION(IndexedDatabaseManager::IsMainProcess(), "Wrong process!");
  1.1556 +
  1.1557 +  PROFILER_LABEL("IndexedDB", "GetAllHelper::DoDatabaseWork [IDBIndex.cpp]");
  1.1558 +
  1.1559 +  nsCString indexTable;
  1.1560 +  if (mIndex->IsUnique()) {
  1.1561 +    indexTable.AssignLiteral("unique_index_data");
  1.1562 +  }
  1.1563 +  else {
  1.1564 +    indexTable.AssignLiteral("index_data");
  1.1565 +  }
  1.1566 +
  1.1567 +  nsCString keyRangeClause;
  1.1568 +  if (mKeyRange) {
  1.1569 +    mKeyRange->GetBindingClause(NS_LITERAL_CSTRING("value"), keyRangeClause);
  1.1570 +  }
  1.1571 +
  1.1572 +  nsCString limitClause;
  1.1573 +  if (mLimit != UINT32_MAX) {
  1.1574 +    limitClause = NS_LITERAL_CSTRING(" LIMIT ");
  1.1575 +    limitClause.AppendInt(mLimit);
  1.1576 +  }
  1.1577 +
  1.1578 +  nsCString query = NS_LITERAL_CSTRING("SELECT data, file_ids FROM object_data "
  1.1579 +                                       "INNER JOIN ") + indexTable +
  1.1580 +                    NS_LITERAL_CSTRING(" AS index_table ON object_data.id = "
  1.1581 +                                       "index_table.object_data_id "
  1.1582 +                                       "WHERE index_id = :index_id") +
  1.1583 +                    keyRangeClause + limitClause;
  1.1584 +
  1.1585 +  nsCOMPtr<mozIStorageStatement> stmt = mTransaction->GetCachedStatement(query);
  1.1586 +  IDB_ENSURE_TRUE(stmt, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
  1.1587 +
  1.1588 +  mozStorageStatementScoper scoper(stmt);
  1.1589 +
  1.1590 +  nsresult rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("index_id"),
  1.1591 +                                      mIndex->Id());
  1.1592 +  IDB_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
  1.1593 +
  1.1594 +  if (mKeyRange) {
  1.1595 +    rv = mKeyRange->BindToStatement(stmt);
  1.1596 +    NS_ENSURE_SUCCESS(rv, rv);
  1.1597 +  }
  1.1598 +
  1.1599 +  mCloneReadInfos.SetCapacity(50);
  1.1600 +
  1.1601 +  bool hasResult;
  1.1602 +  while(NS_SUCCEEDED((rv = stmt->ExecuteStep(&hasResult))) && hasResult) {
  1.1603 +    if (mCloneReadInfos.Capacity() == mCloneReadInfos.Length()) {
  1.1604 +      mCloneReadInfos.SetCapacity(mCloneReadInfos.Capacity() * 2);
  1.1605 +    }
  1.1606 +
  1.1607 +    StructuredCloneReadInfo* readInfo = mCloneReadInfos.AppendElement();
  1.1608 +    NS_ASSERTION(readInfo, "This shouldn't fail!");
  1.1609 +
  1.1610 +    rv = IDBObjectStore::GetStructuredCloneReadInfoFromStatement(stmt, 0, 1,
  1.1611 +      mDatabase, *readInfo);
  1.1612 +    NS_ENSURE_SUCCESS(rv, rv);
  1.1613 +  }
  1.1614 +  IDB_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
  1.1615 +
  1.1616 +  return NS_OK;
  1.1617 +}
  1.1618 +
  1.1619 +nsresult
  1.1620 +GetAllHelper::GetSuccessResult(JSContext* aCx,
  1.1621 +                               JS::MutableHandle<JS::Value> aVal)
  1.1622 +{
  1.1623 +  NS_ASSERTION(mCloneReadInfos.Length() <= mLimit, "Too many results!");
  1.1624 +
  1.1625 +  nsresult rv = ConvertToArrayAndCleanup(aCx, mCloneReadInfos, aVal);
  1.1626 +
  1.1627 +  NS_ASSERTION(mCloneReadInfos.IsEmpty(),
  1.1628 +               "Should have cleared in ConvertToArrayAndCleanup");
  1.1629 +  NS_ENSURE_SUCCESS(rv, rv);
  1.1630 +
  1.1631 +  return NS_OK;
  1.1632 +}
  1.1633 +
  1.1634 +void
  1.1635 +GetAllHelper::ReleaseMainThreadObjects()
  1.1636 +{
  1.1637 +  for (uint32_t index = 0; index < mCloneReadInfos.Length(); index++) {
  1.1638 +    IDBObjectStore::ClearCloneReadInfo(mCloneReadInfos[index]);
  1.1639 +  }
  1.1640 +  GetKeyHelper::ReleaseMainThreadObjects();
  1.1641 +}
  1.1642 +
  1.1643 +nsresult
  1.1644 +GetAllHelper::PackArgumentsForParentProcess(IndexRequestParams& aParams)
  1.1645 +{
  1.1646 +  NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
  1.1647 +  NS_ASSERTION(!IndexedDatabaseManager::IsMainProcess(), "Wrong process!");
  1.1648 +
  1.1649 +  PROFILER_MAIN_THREAD_LABEL("IndexedDB",
  1.1650 +                             "GetAllHelper::PackArgumentsForParentProcess "
  1.1651 +                             "[IDBIndex.cpp]");
  1.1652 +
  1.1653 +  GetAllParams params;
  1.1654 +
  1.1655 +  if (mKeyRange) {
  1.1656 +    KeyRange keyRange;
  1.1657 +    mKeyRange->ToSerializedKeyRange(keyRange);
  1.1658 +    params.optionalKeyRange() = keyRange;
  1.1659 +  }
  1.1660 +  else {
  1.1661 +    params.optionalKeyRange() = mozilla::void_t();
  1.1662 +  }
  1.1663 +
  1.1664 +  params.limit() = mLimit;
  1.1665 +
  1.1666 +  aParams = params;
  1.1667 +  return NS_OK;
  1.1668 +}
  1.1669 +
  1.1670 +AsyncConnectionHelper::ChildProcessSendResult
  1.1671 +GetAllHelper::SendResponseToChildProcess(nsresult aResultCode)
  1.1672 +{
  1.1673 +  NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
  1.1674 +  NS_ASSERTION(IndexedDatabaseManager::IsMainProcess(), "Wrong process!");
  1.1675 +
  1.1676 +  PROFILER_MAIN_THREAD_LABEL("IndexedDB",
  1.1677 +                             "GetAllHelper::SendResponseToChildProcess "
  1.1678 +                             "[IDBIndex.cpp]");
  1.1679 +
  1.1680 +  IndexedDBRequestParentBase* actor = mRequest->GetActorParent();
  1.1681 +  NS_ASSERTION(actor, "How did we get this far without an actor?");
  1.1682 +
  1.1683 +  GetAllResponse getAllResponse;
  1.1684 +
  1.1685 +  if (NS_SUCCEEDED(aResultCode) && !mCloneReadInfos.IsEmpty()) {
  1.1686 +    IDBDatabase* database = mIndex->ObjectStore()->Transaction()->Database();
  1.1687 +    NS_ASSERTION(database, "This should never be null!");
  1.1688 +
  1.1689 +    ContentParent* contentParent = database->GetContentParent();
  1.1690 +    NS_ASSERTION(contentParent, "This should never be null!");
  1.1691 +
  1.1692 +    FileManager* fileManager = database->Manager();
  1.1693 +    NS_ASSERTION(fileManager, "This should never be null!");
  1.1694 +
  1.1695 +    uint32_t length = mCloneReadInfos.Length();
  1.1696 +
  1.1697 +    InfallibleTArray<SerializedStructuredCloneReadInfo>& infos =
  1.1698 +      getAllResponse.cloneInfos();
  1.1699 +    infos.SetCapacity(length);
  1.1700 +
  1.1701 +    InfallibleTArray<BlobArray>& blobArrays = getAllResponse.blobs();
  1.1702 +    blobArrays.SetCapacity(length);
  1.1703 +
  1.1704 +    for (uint32_t index = 0;
  1.1705 +         NS_SUCCEEDED(aResultCode) && index < length;
  1.1706 +         index++) {
  1.1707 +      const StructuredCloneReadInfo& clone = mCloneReadInfos[index];
  1.1708 +
  1.1709 +      // Append the structured clone data.
  1.1710 +      SerializedStructuredCloneReadInfo* info = infos.AppendElement();
  1.1711 +      *info = clone;
  1.1712 +
  1.1713 +      const nsTArray<StructuredCloneFile>& files = clone.mFiles;
  1.1714 +
  1.1715 +      // Now take care of the files.
  1.1716 +      BlobArray* blobArray = blobArrays.AppendElement();
  1.1717 +
  1.1718 +      InfallibleTArray<PBlobParent*>& blobs = blobArray->blobsParent();
  1.1719 +
  1.1720 +      aResultCode =
  1.1721 +        IDBObjectStore::ConvertBlobsToActors(contentParent, fileManager, files,
  1.1722 +                                             blobs);
  1.1723 +      if (NS_FAILED(aResultCode)) {
  1.1724 +        NS_WARNING("ConvertBlobsToActors failed!");
  1.1725 +        break;
  1.1726 +      }
  1.1727 +    }
  1.1728 +  }
  1.1729 +
  1.1730 +  ResponseValue response;
  1.1731 +  if (NS_FAILED(aResultCode)) {
  1.1732 +    response = aResultCode;
  1.1733 +  }
  1.1734 +  else {
  1.1735 +    response = getAllResponse;
  1.1736 +  }
  1.1737 +
  1.1738 +  if (!actor->SendResponse(response)) {
  1.1739 +    return Error;
  1.1740 +  }
  1.1741 +
  1.1742 +  return Success_Sent;
  1.1743 +}
  1.1744 +
  1.1745 +nsresult
  1.1746 +GetAllHelper::UnpackResponseFromParentProcess(
  1.1747 +                                            const ResponseValue& aResponseValue)
  1.1748 +{
  1.1749 +  NS_ASSERTION(aResponseValue.type() == ResponseValue::TGetAllResponse,
  1.1750 +               "Bad response type!");
  1.1751 +
  1.1752 +  const GetAllResponse& getAllResponse = aResponseValue.get_GetAllResponse();
  1.1753 +  const InfallibleTArray<SerializedStructuredCloneReadInfo>& cloneInfos =
  1.1754 +    getAllResponse.cloneInfos();
  1.1755 +  const InfallibleTArray<BlobArray>& blobArrays = getAllResponse.blobs();
  1.1756 +
  1.1757 +  mCloneReadInfos.SetCapacity(cloneInfos.Length());
  1.1758 +
  1.1759 +  for (uint32_t index = 0; index < cloneInfos.Length(); index++) {
  1.1760 +    const SerializedStructuredCloneReadInfo srcInfo = cloneInfos[index];
  1.1761 +    const InfallibleTArray<PBlobChild*>& blobs = blobArrays[index].blobsChild();
  1.1762 +
  1.1763 +    StructuredCloneReadInfo* destInfo = mCloneReadInfos.AppendElement();
  1.1764 +    if (!destInfo->SetFromSerialized(srcInfo)) {
  1.1765 +      IDB_WARNING("Failed to copy clone buffer!");
  1.1766 +      return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
  1.1767 +    }
  1.1768 +
  1.1769 +    IDBObjectStore::ConvertActorsToBlobs(blobs, destInfo->mFiles);
  1.1770 +  }
  1.1771 +
  1.1772 +  return NS_OK;
  1.1773 +}
  1.1774 +
  1.1775 +nsresult
  1.1776 +OpenKeyCursorHelper::DoDatabaseWork(mozIStorageConnection* aConnection)
  1.1777 +{
  1.1778 +  NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
  1.1779 +  NS_ASSERTION(IndexedDatabaseManager::IsMainProcess(), "Wrong process!");
  1.1780 +  NS_ASSERTION(aConnection, "Passed a null connection!");
  1.1781 +
  1.1782 +  PROFILER_LABEL("IndexedDB", "OpenKeyCursorHelper::DoDatabaseWork");
  1.1783 +
  1.1784 +  nsCString table;
  1.1785 +  if (mIndex->IsUnique()) {
  1.1786 +    table.AssignLiteral("unique_index_data");
  1.1787 +  }
  1.1788 +  else {
  1.1789 +    table.AssignLiteral("index_data");
  1.1790 +  }
  1.1791 +
  1.1792 +  NS_NAMED_LITERAL_CSTRING(value, "value");
  1.1793 +
  1.1794 +  nsCString keyRangeClause;
  1.1795 +  if (mKeyRange) {
  1.1796 +    mKeyRange->GetBindingClause(value, keyRangeClause);
  1.1797 +  }
  1.1798 +
  1.1799 +  nsAutoCString directionClause(" ORDER BY value ");
  1.1800 +  switch (mDirection) {
  1.1801 +    case IDBCursor::NEXT:
  1.1802 +    case IDBCursor::NEXT_UNIQUE:
  1.1803 +      directionClause += NS_LITERAL_CSTRING("ASC, object_data_key ASC");
  1.1804 +      break;
  1.1805 +
  1.1806 +    case IDBCursor::PREV:
  1.1807 +      directionClause += NS_LITERAL_CSTRING("DESC, object_data_key DESC");
  1.1808 +      break;
  1.1809 +
  1.1810 +    case IDBCursor::PREV_UNIQUE:
  1.1811 +      directionClause += NS_LITERAL_CSTRING("DESC, object_data_key ASC");
  1.1812 +      break;
  1.1813 +
  1.1814 +    default:
  1.1815 +      NS_NOTREACHED("Unknown direction!");
  1.1816 +  }
  1.1817 +  nsCString firstQuery = NS_LITERAL_CSTRING("SELECT value, object_data_key "
  1.1818 +                                            "FROM ") + table +
  1.1819 +                         NS_LITERAL_CSTRING(" WHERE index_id = :index_id") +
  1.1820 +                         keyRangeClause + directionClause +
  1.1821 +                         NS_LITERAL_CSTRING(" LIMIT 1");
  1.1822 +
  1.1823 +  nsCOMPtr<mozIStorageStatement> stmt =
  1.1824 +    mTransaction->GetCachedStatement(firstQuery);
  1.1825 +  IDB_ENSURE_TRUE(stmt, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
  1.1826 +
  1.1827 +  mozStorageStatementScoper scoper(stmt);
  1.1828 +
  1.1829 +  nsresult rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("index_id"),
  1.1830 +                                      mIndex->Id());
  1.1831 +  IDB_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
  1.1832 +
  1.1833 +  if (mKeyRange) {
  1.1834 +    rv = mKeyRange->BindToStatement(stmt);
  1.1835 +    NS_ENSURE_SUCCESS(rv, rv);
  1.1836 +  }
  1.1837 +
  1.1838 +  bool hasResult;
  1.1839 +  rv = stmt->ExecuteStep(&hasResult);
  1.1840 +  IDB_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
  1.1841 +
  1.1842 +  if (!hasResult) {
  1.1843 +    mKey.Unset();
  1.1844 +    return NS_OK;
  1.1845 +  }
  1.1846 +
  1.1847 +  rv = mKey.SetFromStatement(stmt, 0);
  1.1848 +  NS_ENSURE_SUCCESS(rv, rv);
  1.1849 +
  1.1850 +  rv = mObjectKey.SetFromStatement(stmt, 1);
  1.1851 +  NS_ENSURE_SUCCESS(rv, rv);
  1.1852 +
  1.1853 +  // Now we need to make the query to get the next match.
  1.1854 +  nsAutoCString queryStart = NS_LITERAL_CSTRING("SELECT value, object_data_key"
  1.1855 +                                                " FROM ") + table +
  1.1856 +                             NS_LITERAL_CSTRING(" WHERE index_id = :id");
  1.1857 +
  1.1858 +  NS_NAMED_LITERAL_CSTRING(rangeKey, "range_key");
  1.1859 +
  1.1860 +  switch (mDirection) {
  1.1861 +    case IDBCursor::NEXT:
  1.1862 +      if (mKeyRange && !mKeyRange->Upper().IsUnset()) {
  1.1863 +        AppendConditionClause(value, rangeKey, true, !mKeyRange->IsUpperOpen(),
  1.1864 +                              queryStart);
  1.1865 +        mRangeKey = mKeyRange->Upper();
  1.1866 +      }
  1.1867 +      mContinueQuery =
  1.1868 +        queryStart +
  1.1869 +        NS_LITERAL_CSTRING(" AND value >= :current_key AND "
  1.1870 +                           "( value > :current_key OR "
  1.1871 +                           "  object_data_key > :object_key )") +
  1.1872 +        directionClause +
  1.1873 +        NS_LITERAL_CSTRING(" LIMIT ");
  1.1874 +      mContinueToQuery =
  1.1875 +        queryStart +
  1.1876 +        NS_LITERAL_CSTRING(" AND value >= :current_key ") +
  1.1877 +        directionClause +
  1.1878 +        NS_LITERAL_CSTRING(" LIMIT ");
  1.1879 +      break;
  1.1880 +
  1.1881 +    case IDBCursor::NEXT_UNIQUE:
  1.1882 +      if (mKeyRange && !mKeyRange->Upper().IsUnset()) {
  1.1883 +        AppendConditionClause(value, rangeKey, true, !mKeyRange->IsUpperOpen(),
  1.1884 +                              queryStart);
  1.1885 +        mRangeKey = mKeyRange->Upper();
  1.1886 +      }
  1.1887 +      mContinueQuery =
  1.1888 +        queryStart + NS_LITERAL_CSTRING(" AND value > :current_key") +
  1.1889 +        directionClause +
  1.1890 +        NS_LITERAL_CSTRING(" LIMIT ");
  1.1891 +      mContinueToQuery =
  1.1892 +        queryStart + NS_LITERAL_CSTRING(" AND value >= :current_key") +
  1.1893 +        directionClause +
  1.1894 +        NS_LITERAL_CSTRING(" LIMIT ");
  1.1895 +      break;
  1.1896 +
  1.1897 +    case IDBCursor::PREV:
  1.1898 +      if (mKeyRange && !mKeyRange->Lower().IsUnset()) {
  1.1899 +        AppendConditionClause(value, rangeKey, false, !mKeyRange->IsLowerOpen(),
  1.1900 +                              queryStart);
  1.1901 +        mRangeKey = mKeyRange->Lower();
  1.1902 +      }
  1.1903 +
  1.1904 +      mContinueQuery =
  1.1905 +        queryStart +
  1.1906 +        NS_LITERAL_CSTRING(" AND value <= :current_key AND "
  1.1907 +                           "( value < :current_key OR "
  1.1908 +                           "  object_data_key < :object_key )") +
  1.1909 +        directionClause +
  1.1910 +        NS_LITERAL_CSTRING(" LIMIT ");
  1.1911 +      mContinueToQuery =
  1.1912 +        queryStart +
  1.1913 +        NS_LITERAL_CSTRING(" AND value <= :current_key ") +
  1.1914 +        directionClause +
  1.1915 +        NS_LITERAL_CSTRING(" LIMIT ");
  1.1916 +      break;
  1.1917 +
  1.1918 +    case IDBCursor::PREV_UNIQUE:
  1.1919 +      if (mKeyRange && !mKeyRange->Lower().IsUnset()) {
  1.1920 +        AppendConditionClause(value, rangeKey, false, !mKeyRange->IsLowerOpen(),
  1.1921 +                              queryStart);
  1.1922 +        mRangeKey = mKeyRange->Lower();
  1.1923 +      }
  1.1924 +      mContinueQuery =
  1.1925 +        queryStart +
  1.1926 +        NS_LITERAL_CSTRING(" AND value < :current_key") +
  1.1927 +        directionClause +
  1.1928 +        NS_LITERAL_CSTRING(" LIMIT ");
  1.1929 +      mContinueToQuery =
  1.1930 +        queryStart +
  1.1931 +        NS_LITERAL_CSTRING(" AND value <= :current_key") +
  1.1932 +        directionClause +
  1.1933 +        NS_LITERAL_CSTRING(" LIMIT ");
  1.1934 +      break;
  1.1935 +
  1.1936 +    default:
  1.1937 +      NS_NOTREACHED("Unknown direction type!");
  1.1938 +  }
  1.1939 +
  1.1940 +  return NS_OK;
  1.1941 +}
  1.1942 +
  1.1943 +nsresult
  1.1944 +OpenKeyCursorHelper::EnsureCursor()
  1.1945 +{
  1.1946 +  if (mCursor || mKey.IsUnset()) {
  1.1947 +    return NS_OK;
  1.1948 +  }
  1.1949 +
  1.1950 +  nsRefPtr<IDBCursor> cursor =
  1.1951 +    IDBCursor::Create(mRequest, mTransaction, mIndex, mDirection, mRangeKey,
  1.1952 +                      mContinueQuery, mContinueToQuery, mKey, mObjectKey);
  1.1953 +  IDB_ENSURE_TRUE(cursor, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
  1.1954 +
  1.1955 +  mCursor.swap(cursor);
  1.1956 +  return NS_OK;
  1.1957 +}
  1.1958 +
  1.1959 +nsresult
  1.1960 +OpenKeyCursorHelper::GetSuccessResult(JSContext* aCx,
  1.1961 +                                      JS::MutableHandle<JS::Value> aVal)
  1.1962 +{
  1.1963 +  nsresult rv = EnsureCursor();
  1.1964 +  NS_ENSURE_SUCCESS(rv, rv);
  1.1965 +
  1.1966 +  if (mCursor) {
  1.1967 +    rv = WrapNative(aCx, mCursor, aVal);
  1.1968 +    IDB_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
  1.1969 +  }
  1.1970 +  else {
  1.1971 +    aVal.setUndefined();
  1.1972 +  }
  1.1973 +
  1.1974 +  return NS_OK;
  1.1975 +}
  1.1976 +
  1.1977 +void
  1.1978 +OpenKeyCursorHelper::ReleaseMainThreadObjects()
  1.1979 +{
  1.1980 +  mKeyRange = nullptr;
  1.1981 +  mCursor = nullptr;
  1.1982 +  IndexHelper::ReleaseMainThreadObjects();
  1.1983 +}
  1.1984 +
  1.1985 +nsresult
  1.1986 +OpenKeyCursorHelper::PackArgumentsForParentProcess(IndexRequestParams& aParams)
  1.1987 +{
  1.1988 +  NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
  1.1989 +  NS_ASSERTION(!IndexedDatabaseManager::IsMainProcess(), "Wrong process!");
  1.1990 +
  1.1991 +  PROFILER_MAIN_THREAD_LABEL("IndexedDB",
  1.1992 +                             "OpenKeyCursorHelper::"
  1.1993 +                             "PackArgumentsForParentProcess [IDBIndex.cpp]");
  1.1994 +
  1.1995 +  OpenKeyCursorParams params;
  1.1996 +
  1.1997 +  if (mKeyRange) {
  1.1998 +    KeyRange keyRange;
  1.1999 +    mKeyRange->ToSerializedKeyRange(keyRange);
  1.2000 +    params.optionalKeyRange() = keyRange;
  1.2001 +  }
  1.2002 +  else {
  1.2003 +    params.optionalKeyRange() = mozilla::void_t();
  1.2004 +  }
  1.2005 +
  1.2006 +  params.direction() = mDirection;
  1.2007 +
  1.2008 +  aParams = params;
  1.2009 +  return NS_OK;
  1.2010 +}
  1.2011 +
  1.2012 +AsyncConnectionHelper::ChildProcessSendResult
  1.2013 +OpenKeyCursorHelper::SendResponseToChildProcess(nsresult aResultCode)
  1.2014 +{
  1.2015 +  NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
  1.2016 +  NS_ASSERTION(IndexedDatabaseManager::IsMainProcess(), "Wrong process!");
  1.2017 +  NS_ASSERTION(!mCursor, "Shouldn't have this yet!");
  1.2018 +
  1.2019 +  PROFILER_MAIN_THREAD_LABEL("IndexedDB",
  1.2020 +                             "OpenKeyCursorHelper::SendResponseToChildProcess");
  1.2021 +
  1.2022 +  IndexedDBRequestParentBase* actor = mRequest->GetActorParent();
  1.2023 +  NS_ASSERTION(actor, "How did we get this far without an actor?");
  1.2024 +
  1.2025 +  if (NS_SUCCEEDED(aResultCode)) {
  1.2026 +    nsresult rv = EnsureCursor();
  1.2027 +    if (NS_FAILED(rv)) {
  1.2028 +      NS_WARNING("EnsureCursor failed!");
  1.2029 +      aResultCode = rv;
  1.2030 +    }
  1.2031 +  }
  1.2032 +
  1.2033 +  ResponseValue response;
  1.2034 +  if (NS_FAILED(aResultCode)) {
  1.2035 +    response = aResultCode;
  1.2036 +  }
  1.2037 +  else {
  1.2038 +    OpenCursorResponse openCursorResponse;
  1.2039 +
  1.2040 +    if (!mCursor) {
  1.2041 +      openCursorResponse = mozilla::void_t();
  1.2042 +    }
  1.2043 +    else {
  1.2044 +      IndexedDBIndexParent* indexActor = mIndex->GetActorParent();
  1.2045 +      NS_ASSERTION(indexActor, "Must have an actor here!");
  1.2046 +
  1.2047 +      IndexedDBRequestParentBase* requestActor = mRequest->GetActorParent();
  1.2048 +      NS_ASSERTION(requestActor, "Must have an actor here!");
  1.2049 +
  1.2050 +      IndexCursorConstructorParams params;
  1.2051 +      params.requestParent() = requestActor;
  1.2052 +      params.direction() = mDirection;
  1.2053 +      params.key() = mKey;
  1.2054 +      params.objectKey() = mObjectKey;
  1.2055 +      params.optionalCloneInfo() = mozilla::void_t();
  1.2056 +
  1.2057 +      if (!indexActor->OpenCursor(mCursor, params, openCursorResponse)) {
  1.2058 +        return Error;
  1.2059 +      }
  1.2060 +    }
  1.2061 +
  1.2062 +    response = openCursorResponse;
  1.2063 +  }
  1.2064 +
  1.2065 +  if (!actor->SendResponse(response)) {
  1.2066 +    return Error;
  1.2067 +  }
  1.2068 +
  1.2069 +  return Success_Sent;
  1.2070 +}
  1.2071 +
  1.2072 +nsresult
  1.2073 +OpenKeyCursorHelper::UnpackResponseFromParentProcess(
  1.2074 +                                            const ResponseValue& aResponseValue)
  1.2075 +{
  1.2076 +  NS_ASSERTION(aResponseValue.type() == ResponseValue::TOpenCursorResponse,
  1.2077 +               "Bad response type!");
  1.2078 +  NS_ASSERTION(aResponseValue.get_OpenCursorResponse().type() ==
  1.2079 +               OpenCursorResponse::Tvoid_t ||
  1.2080 +               aResponseValue.get_OpenCursorResponse().type() ==
  1.2081 +               OpenCursorResponse::TPIndexedDBCursorChild,
  1.2082 +               "Bad response union type!");
  1.2083 +  NS_ASSERTION(!mCursor, "Shouldn't have this yet!");
  1.2084 +
  1.2085 +  const OpenCursorResponse& response =
  1.2086 +    aResponseValue.get_OpenCursorResponse();
  1.2087 +
  1.2088 +  switch (response.type()) {
  1.2089 +    case OpenCursorResponse::Tvoid_t:
  1.2090 +      break;
  1.2091 +
  1.2092 +    case OpenCursorResponse::TPIndexedDBCursorChild: {
  1.2093 +      IndexedDBCursorChild* actor =
  1.2094 +        static_cast<IndexedDBCursorChild*>(
  1.2095 +          response.get_PIndexedDBCursorChild());
  1.2096 +
  1.2097 +      mCursor = actor->ForgetStrongCursor();
  1.2098 +      NS_ASSERTION(mCursor, "This should never be null!");
  1.2099 +
  1.2100 +    } break;
  1.2101 +
  1.2102 +    default:
  1.2103 +      MOZ_CRASH();
  1.2104 +  }
  1.2105 +
  1.2106 +  return NS_OK;
  1.2107 +}
  1.2108 +
  1.2109 +nsresult
  1.2110 +OpenCursorHelper::DoDatabaseWork(mozIStorageConnection* aConnection)
  1.2111 +{
  1.2112 +  NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
  1.2113 +  NS_ASSERTION(IndexedDatabaseManager::IsMainProcess(), "Wrong process!");
  1.2114 +  NS_ASSERTION(aConnection, "Passed a null connection!");
  1.2115 +
  1.2116 +  PROFILER_LABEL("IndexedDB",
  1.2117 +                 "OpenCursorHelper::DoDatabaseWork [IDBIndex.cpp]");
  1.2118 +
  1.2119 +  nsCString indexTable;
  1.2120 +  if (mIndex->IsUnique()) {
  1.2121 +    indexTable.AssignLiteral("unique_index_data");
  1.2122 +  }
  1.2123 +  else {
  1.2124 +    indexTable.AssignLiteral("index_data");
  1.2125 +  }
  1.2126 +
  1.2127 +  NS_NAMED_LITERAL_CSTRING(value, "index_table.value");
  1.2128 +
  1.2129 +  nsCString keyRangeClause;
  1.2130 +  if (mKeyRange) {
  1.2131 +    mKeyRange->GetBindingClause(value, keyRangeClause);
  1.2132 +  }
  1.2133 +
  1.2134 +  nsAutoCString directionClause(" ORDER BY index_table.value ");
  1.2135 +  switch (mDirection) {
  1.2136 +    case IDBCursor::NEXT:
  1.2137 +    case IDBCursor::NEXT_UNIQUE:
  1.2138 +      directionClause +=
  1.2139 +        NS_LITERAL_CSTRING("ASC, index_table.object_data_key ASC");
  1.2140 +      break;
  1.2141 +
  1.2142 +    case IDBCursor::PREV:
  1.2143 +      directionClause +=
  1.2144 +        NS_LITERAL_CSTRING("DESC, index_table.object_data_key DESC");
  1.2145 +      break;
  1.2146 +
  1.2147 +    case IDBCursor::PREV_UNIQUE:
  1.2148 +      directionClause +=
  1.2149 +        NS_LITERAL_CSTRING("DESC, index_table.object_data_key ASC");
  1.2150 +      break;
  1.2151 +
  1.2152 +    default:
  1.2153 +      NS_NOTREACHED("Unknown direction!");
  1.2154 +  }
  1.2155 +
  1.2156 +  nsCString firstQuery =
  1.2157 +    NS_LITERAL_CSTRING("SELECT index_table.value, "
  1.2158 +                       "index_table.object_data_key, object_data.data, "
  1.2159 +                       "object_data.file_ids FROM ") +
  1.2160 +    indexTable +
  1.2161 +    NS_LITERAL_CSTRING(" AS index_table INNER JOIN object_data ON "
  1.2162 +                       "index_table.object_data_id = object_data.id "
  1.2163 +                       "WHERE index_table.index_id = :id") +
  1.2164 +    keyRangeClause + directionClause +
  1.2165 +    NS_LITERAL_CSTRING(" LIMIT 1");
  1.2166 +
  1.2167 +  nsCOMPtr<mozIStorageStatement> stmt =
  1.2168 +    mTransaction->GetCachedStatement(firstQuery);
  1.2169 +  IDB_ENSURE_TRUE(stmt, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
  1.2170 +
  1.2171 +  mozStorageStatementScoper scoper(stmt);
  1.2172 +
  1.2173 +  nsresult rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("id"), mIndex->Id());
  1.2174 +  IDB_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
  1.2175 +
  1.2176 +  if (mKeyRange) {
  1.2177 +    rv = mKeyRange->BindToStatement(stmt);
  1.2178 +    NS_ENSURE_SUCCESS(rv, rv);
  1.2179 +  }
  1.2180 +
  1.2181 +  bool hasResult;
  1.2182 +  rv = stmt->ExecuteStep(&hasResult);
  1.2183 +  IDB_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
  1.2184 +
  1.2185 +  if (!hasResult) {
  1.2186 +    mKey.Unset();
  1.2187 +    return NS_OK;
  1.2188 +  }
  1.2189 +
  1.2190 +  rv = mKey.SetFromStatement(stmt, 0);
  1.2191 +  NS_ENSURE_SUCCESS(rv, rv);
  1.2192 +
  1.2193 +  rv = mObjectKey.SetFromStatement(stmt, 1);
  1.2194 +  NS_ENSURE_SUCCESS(rv, rv);
  1.2195 +
  1.2196 +  rv = IDBObjectStore::GetStructuredCloneReadInfoFromStatement(stmt, 2, 3,
  1.2197 +    mDatabase, mCloneReadInfo);
  1.2198 +  NS_ENSURE_SUCCESS(rv, rv);
  1.2199 +
  1.2200 +  // Now we need to make the query to get the next match.
  1.2201 +  nsAutoCString queryStart =
  1.2202 +    NS_LITERAL_CSTRING("SELECT index_table.value, "
  1.2203 +                       "index_table.object_data_key, object_data.data, "
  1.2204 +                       "object_data.file_ids FROM ") +
  1.2205 +    indexTable +
  1.2206 +    NS_LITERAL_CSTRING(" AS index_table INNER JOIN object_data ON "
  1.2207 +                       "index_table.object_data_id = object_data.id "
  1.2208 +                       "WHERE index_table.index_id = :id");
  1.2209 +
  1.2210 +  NS_NAMED_LITERAL_CSTRING(rangeKey, "range_key");
  1.2211 +
  1.2212 +  NS_NAMED_LITERAL_CSTRING(limit, " LIMIT ");
  1.2213 +
  1.2214 +  switch (mDirection) {
  1.2215 +    case IDBCursor::NEXT:
  1.2216 +      if (mKeyRange && !mKeyRange->Upper().IsUnset()) {
  1.2217 +        AppendConditionClause(value, rangeKey, true, !mKeyRange->IsUpperOpen(),
  1.2218 +                              queryStart);
  1.2219 +        mRangeKey = mKeyRange->Upper();
  1.2220 +      }
  1.2221 +      mContinueQuery =
  1.2222 +        queryStart +
  1.2223 +        NS_LITERAL_CSTRING(" AND index_table.value >= :current_key AND "
  1.2224 +                           "( index_table.value > :current_key OR "
  1.2225 +                           "  index_table.object_data_key > :object_key ) ") +
  1.2226 +        directionClause + limit;
  1.2227 +      mContinueToQuery =
  1.2228 +        queryStart +
  1.2229 +        NS_LITERAL_CSTRING(" AND index_table.value >= :current_key") +
  1.2230 +        directionClause + limit;
  1.2231 +      break;
  1.2232 +
  1.2233 +    case IDBCursor::NEXT_UNIQUE:
  1.2234 +      if (mKeyRange && !mKeyRange->Upper().IsUnset()) {
  1.2235 +        AppendConditionClause(value, rangeKey, true, !mKeyRange->IsUpperOpen(),
  1.2236 +                              queryStart);
  1.2237 +        mRangeKey = mKeyRange->Upper();
  1.2238 +      }
  1.2239 +      mContinueQuery =
  1.2240 +        queryStart +
  1.2241 +        NS_LITERAL_CSTRING(" AND index_table.value > :current_key") +
  1.2242 +        directionClause + limit;
  1.2243 +      mContinueToQuery =
  1.2244 +        queryStart +
  1.2245 +        NS_LITERAL_CSTRING(" AND index_table.value >= :current_key") +
  1.2246 +        directionClause + limit;
  1.2247 +      break;
  1.2248 +
  1.2249 +    case IDBCursor::PREV:
  1.2250 +      if (mKeyRange && !mKeyRange->Lower().IsUnset()) {
  1.2251 +        AppendConditionClause(value, rangeKey, false, !mKeyRange->IsLowerOpen(),
  1.2252 +                              queryStart);
  1.2253 +        mRangeKey = mKeyRange->Lower();
  1.2254 +      }
  1.2255 +      mContinueQuery =
  1.2256 +        queryStart +
  1.2257 +        NS_LITERAL_CSTRING(" AND index_table.value <= :current_key AND "
  1.2258 +                           "( index_table.value < :current_key OR "
  1.2259 +                           "  index_table.object_data_key < :object_key ) ") +
  1.2260 +        directionClause + limit;
  1.2261 +      mContinueToQuery =
  1.2262 +        queryStart +
  1.2263 +        NS_LITERAL_CSTRING(" AND index_table.value <= :current_key") +
  1.2264 +        directionClause + limit;
  1.2265 +      break;
  1.2266 +
  1.2267 +    case IDBCursor::PREV_UNIQUE:
  1.2268 +      if (mKeyRange && !mKeyRange->Lower().IsUnset()) {
  1.2269 +        AppendConditionClause(value, rangeKey, false, !mKeyRange->IsLowerOpen(),
  1.2270 +                              queryStart);
  1.2271 +        mRangeKey = mKeyRange->Lower();
  1.2272 +      }
  1.2273 +      mContinueQuery =
  1.2274 +        queryStart +
  1.2275 +        NS_LITERAL_CSTRING(" AND index_table.value < :current_key") +
  1.2276 +        directionClause + limit;
  1.2277 +      mContinueToQuery =
  1.2278 +        queryStart +
  1.2279 +        NS_LITERAL_CSTRING(" AND index_table.value <= :current_key") +
  1.2280 +        directionClause + limit;
  1.2281 +      break;
  1.2282 +
  1.2283 +    default:
  1.2284 +      NS_NOTREACHED("Unknown direction type!");
  1.2285 +  }
  1.2286 +
  1.2287 +  return NS_OK;
  1.2288 +}
  1.2289 +
  1.2290 +nsresult
  1.2291 +OpenCursorHelper::EnsureCursor()
  1.2292 +{
  1.2293 +  if (mCursor || mKey.IsUnset()) {
  1.2294 +    return NS_OK;
  1.2295 +  }
  1.2296 +
  1.2297 +  mSerializedCloneReadInfo = mCloneReadInfo;
  1.2298 +
  1.2299 +  NS_ASSERTION(mSerializedCloneReadInfo.data &&
  1.2300 +               mSerializedCloneReadInfo.dataLength,
  1.2301 +               "Shouldn't be possible!");
  1.2302 +
  1.2303 +  nsRefPtr<IDBCursor> cursor =
  1.2304 +    IDBCursor::Create(mRequest, mTransaction, mIndex, mDirection, mRangeKey,
  1.2305 +                      mContinueQuery, mContinueToQuery, mKey, mObjectKey,
  1.2306 +                      Move(mCloneReadInfo));
  1.2307 +  IDB_ENSURE_TRUE(cursor, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
  1.2308 +
  1.2309 +  NS_ASSERTION(!mCloneReadInfo.mCloneBuffer.data(), "Should have swapped!");
  1.2310 +
  1.2311 +  mCursor.swap(cursor);
  1.2312 +  return NS_OK;
  1.2313 +}
  1.2314 +
  1.2315 +void
  1.2316 +OpenCursorHelper::ReleaseMainThreadObjects()
  1.2317 +{
  1.2318 +  IDBObjectStore::ClearCloneReadInfo(mCloneReadInfo);
  1.2319 +
  1.2320 +  // These don't need to be released on the main thread but they're only valid
  1.2321 +  // as long as mCursor is set.
  1.2322 +  mSerializedCloneReadInfo.data = nullptr;
  1.2323 +  mSerializedCloneReadInfo.dataLength = 0;
  1.2324 +
  1.2325 +  OpenKeyCursorHelper::ReleaseMainThreadObjects();
  1.2326 +}
  1.2327 +
  1.2328 +nsresult
  1.2329 +OpenCursorHelper::PackArgumentsForParentProcess(IndexRequestParams& aParams)
  1.2330 +{
  1.2331 +  NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
  1.2332 +  NS_ASSERTION(!IndexedDatabaseManager::IsMainProcess(), "Wrong process!");
  1.2333 +
  1.2334 +  PROFILER_MAIN_THREAD_LABEL("IndexedDB",
  1.2335 +                             "OpenCursorHelper::PackArgumentsForParentProcess "
  1.2336 +                             "[IDBIndex.cpp]");
  1.2337 +
  1.2338 +  OpenCursorParams params;
  1.2339 +
  1.2340 +  if (mKeyRange) {
  1.2341 +    KeyRange keyRange;
  1.2342 +    mKeyRange->ToSerializedKeyRange(keyRange);
  1.2343 +    params.optionalKeyRange() = keyRange;
  1.2344 +  }
  1.2345 +  else {
  1.2346 +    params.optionalKeyRange() = mozilla::void_t();
  1.2347 +  }
  1.2348 +
  1.2349 +  params.direction() = mDirection;
  1.2350 +
  1.2351 +  aParams = params;
  1.2352 +  return NS_OK;
  1.2353 +}
  1.2354 +
  1.2355 +AsyncConnectionHelper::ChildProcessSendResult
  1.2356 +OpenCursorHelper::SendResponseToChildProcess(nsresult aResultCode)
  1.2357 +{
  1.2358 +  NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
  1.2359 +  NS_ASSERTION(IndexedDatabaseManager::IsMainProcess(), "Wrong process!");
  1.2360 +  NS_ASSERTION(!mCursor, "Shouldn't have this yet!");
  1.2361 +
  1.2362 +  PROFILER_MAIN_THREAD_LABEL("IndexedDB",
  1.2363 +                             "OpenCursorHelper::SendResponseToChildProcess "
  1.2364 +                             "[IDBIndex.cpp]");
  1.2365 +
  1.2366 +  IndexedDBRequestParentBase* actor = mRequest->GetActorParent();
  1.2367 +  NS_ASSERTION(actor, "How did we get this far without an actor?");
  1.2368 +
  1.2369 +  InfallibleTArray<PBlobParent*> blobsParent;
  1.2370 +
  1.2371 +  if (NS_SUCCEEDED(aResultCode)) {
  1.2372 +    IDBDatabase* database = mIndex->ObjectStore()->Transaction()->Database();
  1.2373 +    NS_ASSERTION(database, "This should never be null!");
  1.2374 +
  1.2375 +    ContentParent* contentParent = database->GetContentParent();
  1.2376 +    NS_ASSERTION(contentParent, "This should never be null!");
  1.2377 +
  1.2378 +    FileManager* fileManager = database->Manager();
  1.2379 +    NS_ASSERTION(fileManager, "This should never be null!");
  1.2380 +
  1.2381 +    const nsTArray<StructuredCloneFile>& files = mCloneReadInfo.mFiles;
  1.2382 +
  1.2383 +    aResultCode =
  1.2384 +      IDBObjectStore::ConvertBlobsToActors(contentParent, fileManager, files,
  1.2385 +                                           blobsParent);
  1.2386 +    if (NS_FAILED(aResultCode)) {
  1.2387 +      NS_WARNING("ConvertBlobsToActors failed!");
  1.2388 +    }
  1.2389 +  }
  1.2390 +
  1.2391 +  if (NS_SUCCEEDED(aResultCode)) {
  1.2392 +    nsresult rv = EnsureCursor();
  1.2393 +    if (NS_FAILED(rv)) {
  1.2394 +      NS_WARNING("EnsureCursor failed!");
  1.2395 +      aResultCode = rv;
  1.2396 +    }
  1.2397 +  }
  1.2398 +
  1.2399 +  ResponseValue response;
  1.2400 +  if (NS_FAILED(aResultCode)) {
  1.2401 +    response = aResultCode;
  1.2402 +  }
  1.2403 +  else {
  1.2404 +    OpenCursorResponse openCursorResponse;
  1.2405 +
  1.2406 +    if (!mCursor) {
  1.2407 +      openCursorResponse = mozilla::void_t();
  1.2408 +    }
  1.2409 +    else {
  1.2410 +      IndexedDBIndexParent* indexActor = mIndex->GetActorParent();
  1.2411 +      NS_ASSERTION(indexActor, "Must have an actor here!");
  1.2412 +
  1.2413 +      IndexedDBRequestParentBase* requestActor = mRequest->GetActorParent();
  1.2414 +      NS_ASSERTION(requestActor, "Must have an actor here!");
  1.2415 +
  1.2416 +      NS_ASSERTION(mSerializedCloneReadInfo.data &&
  1.2417 +                   mSerializedCloneReadInfo.dataLength,
  1.2418 +                   "Shouldn't be possible!");
  1.2419 +
  1.2420 +      IndexCursorConstructorParams params;
  1.2421 +      params.requestParent() = requestActor;
  1.2422 +      params.direction() = mDirection;
  1.2423 +      params.key() = mKey;
  1.2424 +      params.objectKey() = mObjectKey;
  1.2425 +      params.optionalCloneInfo() = mSerializedCloneReadInfo;
  1.2426 +      params.blobsParent().SwapElements(blobsParent);
  1.2427 +
  1.2428 +      if (!indexActor->OpenCursor(mCursor, params, openCursorResponse)) {
  1.2429 +        return Error;
  1.2430 +      }
  1.2431 +    }
  1.2432 +
  1.2433 +    response = openCursorResponse;
  1.2434 +  }
  1.2435 +
  1.2436 +  if (!actor->SendResponse(response)) {
  1.2437 +    return Error;
  1.2438 +  }
  1.2439 +
  1.2440 +  return Success_Sent;
  1.2441 +}
  1.2442 +
  1.2443 +nsresult
  1.2444 +CountHelper::DoDatabaseWork(mozIStorageConnection* aConnection)
  1.2445 +{
  1.2446 +  NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
  1.2447 +  NS_ASSERTION(IndexedDatabaseManager::IsMainProcess(), "Wrong process!");
  1.2448 +
  1.2449 +  PROFILER_LABEL("IndexedDB", "CountHelper::DoDatabaseWork [IDBIndex.cpp]");
  1.2450 +
  1.2451 +  nsCString table;
  1.2452 +  if (mIndex->IsUnique()) {
  1.2453 +    table.AssignLiteral("unique_index_data");
  1.2454 +  }
  1.2455 +  else {
  1.2456 +    table.AssignLiteral("index_data");
  1.2457 +  }
  1.2458 +
  1.2459 +  NS_NAMED_LITERAL_CSTRING(lowerKeyName, "lower_key");
  1.2460 +  NS_NAMED_LITERAL_CSTRING(upperKeyName, "upper_key");
  1.2461 +  NS_NAMED_LITERAL_CSTRING(value, "value");
  1.2462 +
  1.2463 +  nsAutoCString keyRangeClause;
  1.2464 +  if (mKeyRange) {
  1.2465 +    if (!mKeyRange->Lower().IsUnset()) {
  1.2466 +      AppendConditionClause(value, lowerKeyName, false,
  1.2467 +                            !mKeyRange->IsLowerOpen(), keyRangeClause);
  1.2468 +    }
  1.2469 +    if (!mKeyRange->Upper().IsUnset()) {
  1.2470 +      AppendConditionClause(value, upperKeyName, true,
  1.2471 +                            !mKeyRange->IsUpperOpen(), keyRangeClause);
  1.2472 +    }
  1.2473 +  }
  1.2474 +
  1.2475 +  nsCString query = NS_LITERAL_CSTRING("SELECT count(*) FROM ") + table +
  1.2476 +                    NS_LITERAL_CSTRING(" WHERE index_id = :id") +
  1.2477 +                    keyRangeClause;
  1.2478 +
  1.2479 +  nsCOMPtr<mozIStorageStatement> stmt = mTransaction->GetCachedStatement(query);
  1.2480 +  IDB_ENSURE_TRUE(stmt, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
  1.2481 +
  1.2482 +  mozStorageStatementScoper scoper(stmt);
  1.2483 +
  1.2484 +  nsresult rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("id"), mIndex->Id());
  1.2485 +  IDB_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
  1.2486 +
  1.2487 +  if (mKeyRange) {
  1.2488 +    if (!mKeyRange->Lower().IsUnset()) {
  1.2489 +      rv = mKeyRange->Lower().BindToStatement(stmt, lowerKeyName);
  1.2490 +      NS_ENSURE_SUCCESS(rv, rv);
  1.2491 +    }
  1.2492 +    if (!mKeyRange->Upper().IsUnset()) {
  1.2493 +      rv = mKeyRange->Upper().BindToStatement(stmt, upperKeyName);
  1.2494 +      NS_ENSURE_SUCCESS(rv, rv);
  1.2495 +    }
  1.2496 +  }
  1.2497 +
  1.2498 +  bool hasResult;
  1.2499 +  rv = stmt->ExecuteStep(&hasResult);
  1.2500 +  IDB_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
  1.2501 +  IDB_ENSURE_TRUE(hasResult, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
  1.2502 +
  1.2503 +  mCount = stmt->AsInt64(0);
  1.2504 +  return NS_OK;
  1.2505 +}
  1.2506 +
  1.2507 +nsresult
  1.2508 +CountHelper::GetSuccessResult(JSContext* aCx,
  1.2509 +                              JS::MutableHandle<JS::Value> aVal)
  1.2510 +{
  1.2511 +  aVal.setNumber(static_cast<double>(mCount));
  1.2512 +  return NS_OK;
  1.2513 +}
  1.2514 +
  1.2515 +void
  1.2516 +CountHelper::ReleaseMainThreadObjects()
  1.2517 +{
  1.2518 +  mKeyRange = nullptr;
  1.2519 +  IndexHelper::ReleaseMainThreadObjects();
  1.2520 +}
  1.2521 +
  1.2522 +nsresult
  1.2523 +CountHelper::PackArgumentsForParentProcess(IndexRequestParams& aParams)
  1.2524 +{
  1.2525 +  NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
  1.2526 +  NS_ASSERTION(!IndexedDatabaseManager::IsMainProcess(), "Wrong process!");
  1.2527 +
  1.2528 +  PROFILER_MAIN_THREAD_LABEL("IndexedDB",
  1.2529 +                             "CountHelper::PackArgumentsForParentProcess "
  1.2530 +                             "[IDBIndex.cpp]");
  1.2531 +
  1.2532 +  CountParams params;
  1.2533 +
  1.2534 +  if (mKeyRange) {
  1.2535 +    KeyRange keyRange;
  1.2536 +    mKeyRange->ToSerializedKeyRange(keyRange);
  1.2537 +    params.optionalKeyRange() = keyRange;
  1.2538 +  }
  1.2539 +  else {
  1.2540 +    params.optionalKeyRange() = mozilla::void_t();
  1.2541 +  }
  1.2542 +
  1.2543 +  aParams = params;
  1.2544 +  return NS_OK;
  1.2545 +}
  1.2546 +
  1.2547 +AsyncConnectionHelper::ChildProcessSendResult
  1.2548 +CountHelper::SendResponseToChildProcess(nsresult aResultCode)
  1.2549 +{
  1.2550 +  NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
  1.2551 +  NS_ASSERTION(IndexedDatabaseManager::IsMainProcess(), "Wrong process!");
  1.2552 +
  1.2553 +  PROFILER_MAIN_THREAD_LABEL("IndexedDB",
  1.2554 +                             "CountHelper::SendResponseToChildProcess "
  1.2555 +                             "[IDBIndex.cpp]");
  1.2556 +
  1.2557 +  IndexedDBRequestParentBase* actor = mRequest->GetActorParent();
  1.2558 +  NS_ASSERTION(actor, "How did we get this far without an actor?");
  1.2559 +
  1.2560 +  ResponseValue response;
  1.2561 +  if (NS_FAILED(aResultCode)) {
  1.2562 +    response = aResultCode;
  1.2563 +  }
  1.2564 +  else {
  1.2565 +    CountResponse countResponse = mCount;
  1.2566 +    response = countResponse;
  1.2567 +  }
  1.2568 +
  1.2569 +  if (!actor->SendResponse(response)) {
  1.2570 +    return Error;
  1.2571 +  }
  1.2572 +
  1.2573 +  return Success_Sent;
  1.2574 +}
  1.2575 +
  1.2576 +nsresult
  1.2577 +CountHelper::UnpackResponseFromParentProcess(
  1.2578 +                                            const ResponseValue& aResponseValue)
  1.2579 +{
  1.2580 +  NS_ASSERTION(aResponseValue.type() == ResponseValue::TCountResponse,
  1.2581 +               "Bad response type!");
  1.2582 +
  1.2583 +  mCount = aResponseValue.get_CountResponse().count();
  1.2584 +  return NS_OK;
  1.2585 +}

mercurial