Example #1
0
nsresult KeyPath::ExtractKeyAsJSVal(JSContext* aCx, const JS::Value& aValue,
                                    JS::Value* aOutVal) const {
  NS_ASSERTION(IsValid(), "This doesn't make sense!");

  if (IsString()) {
    return GetJSValFromKeyPathString(aCx, aValue, mStrings[0], aOutVal,
                                     DoNotCreateProperties, nullptr, nullptr);
  }

  const uint32_t len = mStrings.Length();
  JS::Rooted<JSObject*> arrayObj(aCx, JS_NewArrayObject(aCx, len));
  if (!arrayObj) {
    return NS_ERROR_OUT_OF_MEMORY;
  }

  JS::Rooted<JS::Value> value(aCx);
  for (uint32_t i = 0; i < len; ++i) {
    nsresult rv =
        GetJSValFromKeyPathString(aCx, aValue, mStrings[i], value.address(),
                                  DoNotCreateProperties, nullptr, nullptr);
    if (NS_FAILED(rv)) {
      return rv;
    }

    if (!JS_DefineElement(aCx, arrayObj, i, value, JSPROP_ENUMERATE)) {
      IDB_REPORT_INTERNAL_ERR();
      return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
    }
  }

  aOutVal->setObject(*arrayObj);
  return NS_OK;
}
Example #2
0
// static
nsresult
IDBFactory::CreateForMainThreadJSInternal(
    JSContext* aCx,
    JS::Handle<JSObject*> aOwningObject,
    nsAutoPtr<PrincipalInfo>& aPrincipalInfo,
    IDBFactory** aFactory)
{
    MOZ_ASSERT(NS_IsMainThread());
    MOZ_ASSERT(aPrincipalInfo);

    if (aPrincipalInfo->type() != PrincipalInfo::TSystemPrincipalInfo &&
            NS_WARN_IF(!Preferences::GetBool(kPrefIndexedDBEnabled, false))) {
        *aFactory = nullptr;
        return NS_ERROR_DOM_INDEXEDDB_NOT_ALLOWED_ERR;
    }

    IndexedDatabaseManager* mgr = IndexedDatabaseManager::GetOrCreate();
    if (NS_WARN_IF(!mgr)) {
        IDB_REPORT_INTERNAL_ERR();
        return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
    }

    nsresult rv =
        CreateForJSInternal(aCx,
                            aOwningObject,
                            aPrincipalInfo,
                            /* aInnerWindowID */ 0,
                            aFactory);
    if (NS_WARN_IF(NS_FAILED(rv))) {
        return rv;
    }

    return NS_OK;
}
Example #3
0
nsresult
KeyPath::ToJSVal(JSContext* aCx, JS::MutableHandle<JS::Value> aValue) const
{
    if (IsArray()) {
        uint32_t len = mStrings.Length();
        JS::Rooted<JSObject*> array(aCx, JS_NewArrayObject(aCx, len));
        if (!array) {
            IDB_WARNING("Failed to make array!");
            return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
        }

        for (uint32_t i = 0; i < len; ++i) {
            JS::Rooted<JS::Value> val(aCx);
            nsString tmp(mStrings[i]);
            if (!xpc::StringToJsval(aCx, tmp, &val)) {
                IDB_REPORT_INTERNAL_ERR();
                return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
            }

            if (!JS_SetElement(aCx, array, i, val)) {
                IDB_REPORT_INTERNAL_ERR();
                return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
            }
        }

        aValue.setObject(*array);
        return NS_OK;
    }

    if (IsString()) {
        nsString tmp(mStrings[0]);
        if (!xpc::StringToJsval(aCx, tmp, aValue)) {
            IDB_REPORT_INTERNAL_ERR();
            return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
        }
        return NS_OK;
    }

    aValue.setNull();
    return NS_OK;
}
Example #4
0
void
IDBRequest::SetResultCallback(ResultCallback* aCallback)
{
  AssertIsOnOwningThread();
  MOZ_ASSERT(aCallback);
  MOZ_ASSERT(!mHaveResultOrErrorCode);
  MOZ_ASSERT(mResultVal.isUndefined());
  MOZ_ASSERT(!mError);

  // See if our window is still valid.
  if (NS_WARN_IF(NS_FAILED(CheckInnerWindowCorrectness()))) {
    IDB_REPORT_INTERNAL_ERR();
    SetError(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
    return;
  }

  AutoJSAPI autoJS;
  Maybe<JSAutoCompartment> ac;

  if (GetScriptOwner()) {
    // If we have a script owner we want the SafeJSContext and then to enter the
    // script owner's compartment.
    autoJS.Init();
    ac.emplace(autoJS.cx(), GetScriptOwner());
  } else {
    // Otherwise our owner is a window and we use that to initialize.
    MOZ_ASSERT(GetOwner());
    if (!autoJS.InitWithLegacyErrorReporting(GetOwner())) {
      IDB_WARNING("Failed to initialize AutoJSAPI!");
      SetError(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
      return;
    }
  }

  JSContext* cx = autoJS.cx();

  AssertIsRooted();

  JS::Rooted<JS::Value> result(cx);
  nsresult rv = aCallback->GetResult(cx, &result);
  if (NS_WARN_IF(NS_FAILED(rv))) {
    SetError(rv);
    mResultVal.setUndefined();
  } else {
    mError = nullptr;
    mResultVal = result;
  }

  mHaveResultOrErrorCode = true;
}
Example #5
0
already_AddRefed<IDBRequest>
IDBDatabase::CreateMutableFile(JSContext* aCx,
                               const nsAString& aName,
                               const Optional<nsAString>& aType,
                               ErrorResult& aRv)
{
  AssertIsOnOwningThread();

  if (QuotaManager::IsShuttingDown()) {
    IDB_REPORT_INTERNAL_ERR();
    aRv.Throw(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
    return nullptr;
  }

  if (mClosed || mFileHandleDisabled) {
    aRv.Throw(NS_ERROR_DOM_INDEXEDDB_NOT_ALLOWED_ERR);
    return nullptr;
  }

  nsString type;
  if (aType.WasPassed()) {
    type = aType.Value();
  }

  CreateFileParams params(nsString(aName), type);

  RefPtr<IDBRequest> request = IDBRequest::Create(aCx, this, nullptr);
  MOZ_ASSERT(request);

  BackgroundDatabaseRequestChild* actor =
    new BackgroundDatabaseRequestChild(this, request);

  IDB_LOG_MARK("IndexedDB %s: Child  Request[%llu]: "
                 "database(%s).createMutableFile(%s)",
               "IndexedDB %s: C R[%llu]: IDBDatabase.createMutableFile()",
               IDB_LOG_ID_STRING(),
               request->LoggingSerialNumber(),
               IDB_LOG_STRINGIFY(this),
               NS_ConvertUTF16toUTF8(aName).get());

  mBackgroundActor->SendPBackgroundIDBDatabaseRequestConstructor(actor, params);

  return request.forget();
}
Example #6
0
bool
IDBTransaction::
WorkerFeature::Notify(JSContext* aCx, Status aStatus)
{
  MOZ_ASSERT(mWorkerPrivate);
  mWorkerPrivate->AssertIsOnWorkerThread();
  MOZ_ASSERT(aStatus > Running);

  if (mTransaction && aStatus > Terminating) {
    mTransaction->AssertIsOnOwningThread();

    nsRefPtr<IDBTransaction> transaction = Move(mTransaction);

    if (!transaction->IsCommittingOrDone()) {
      IDB_REPORT_INTERNAL_ERR();
      transaction->AbortInternal(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR, nullptr);
    }
  }

  return true;
}
Example #7
0
nsresult
IDBDatabase::Transaction(const StringOrStringSequence& aStoreNames,
                         IDBTransactionMode aMode,
                         IDBTransaction** aTransaction)
{
  AssertIsOnOwningThread();

  if (NS_WARN_IF(aMode == IDBTransactionMode::Readwriteflush &&
                 !IndexedDatabaseManager::ExperimentalFeaturesEnabled())) {
    IDB_REPORT_INTERNAL_ERR();
    return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
  }

  if (QuotaManager::IsShuttingDown()) {
    IDB_REPORT_INTERNAL_ERR();
    return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
  }

  if (mClosed || RunningVersionChangeTransaction()) {
    return NS_ERROR_DOM_INDEXEDDB_NOT_ALLOWED_ERR;
  }

  nsAutoTArray<nsString, 1> stackSequence;

  if (aStoreNames.IsString()) {
    stackSequence.AppendElement(aStoreNames.GetAsString());
  } else {
    MOZ_ASSERT(aStoreNames.IsStringSequence());
    if (aStoreNames.GetAsStringSequence().IsEmpty()) {
      return NS_ERROR_DOM_INVALID_ACCESS_ERR;
    }
  }

  const nsTArray<nsString>& storeNames =
    aStoreNames.IsString() ?
    stackSequence :
    static_cast<const nsTArray<nsString>&>(aStoreNames.GetAsStringSequence());
  MOZ_ASSERT(!storeNames.IsEmpty());

  const nsTArray<ObjectStoreSpec>& objectStores = mSpec->objectStores();
  const uint32_t nameCount = storeNames.Length();

  nsTArray<nsString> sortedStoreNames;
  sortedStoreNames.SetCapacity(nameCount);

  // Check to make sure the object store names we collected actually exist.
  for (uint32_t nameIndex = 0; nameIndex < nameCount; nameIndex++) {
    const nsString& name = storeNames[nameIndex];

    bool found = false;

    for (uint32_t objCount = objectStores.Length(), objIndex = 0;
         objIndex < objCount;
         objIndex++) {
      if (objectStores[objIndex].metadata().name() == name) {
        found = true;
        break;
      }
    }

    if (!found) {
      return NS_ERROR_DOM_INDEXEDDB_NOT_FOUND_ERR;
    }

    sortedStoreNames.InsertElementSorted(name);
  }

  // Remove any duplicates.
  for (uint32_t nameIndex = nameCount - 1; nameIndex > 0; nameIndex--) {
    if (sortedStoreNames[nameIndex] == sortedStoreNames[nameIndex - 1]) {
      sortedStoreNames.RemoveElementAt(nameIndex);
    }
  }

  IDBTransaction::Mode mode;
  switch (aMode) {
    case IDBTransactionMode::Readonly:
      mode = IDBTransaction::READ_ONLY;
      break;
    case IDBTransactionMode::Readwrite:
      mode = IDBTransaction::READ_WRITE;
      break;
    case IDBTransactionMode::Readwriteflush:
      mode = IDBTransaction::READ_WRITE_FLUSH;
      break;
    case IDBTransactionMode::Versionchange:
      return NS_ERROR_DOM_INVALID_ACCESS_ERR;

    default:
      MOZ_CRASH("Unknown mode!");
  }

  RefPtr<IDBTransaction> transaction =
    IDBTransaction::Create(this, sortedStoreNames, mode);
  if (NS_WARN_IF(!transaction)) {
    IDB_REPORT_INTERNAL_ERR();
    return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
  }

  BackgroundTransactionChild* actor =
    new BackgroundTransactionChild(transaction);

  IDB_LOG_MARK("IndexedDB %s: Child  Transaction[%lld]: "
                 "database(%s).transaction(%s)",
               "IndexedDB %s: C T[%lld]: IDBDatabase.transaction()",
               IDB_LOG_ID_STRING(),
               transaction->LoggingSerialNumber(),
               IDB_LOG_STRINGIFY(this),
               IDB_LOG_STRINGIFY(transaction));

  MOZ_ALWAYS_TRUE(
    mBackgroundActor->SendPBackgroundIDBTransactionConstructor(actor,
                                                               sortedStoreNames,
                                                               mode));

  transaction->SetBackgroundActor(actor);

  transaction.forget(aTransaction);
  return NS_OK;
}
Example #8
0
already_AddRefed<IDBOpenDBRequest>
IDBFactory::OpenInternal(JSContext* aCx,
                         nsIPrincipal* aPrincipal,
                         const nsAString& aName,
                         const Optional<uint64_t>& aVersion,
                         const Optional<StorageType>& aStorageType,
                         bool aDeleting,
                         ErrorResult& aRv)
{
    MOZ_ASSERT(mWindow || mOwningObject);
    MOZ_ASSERT_IF(!mWindow, !mPrivateBrowsingMode);

    CommonFactoryRequestParams commonParams;
    commonParams.privateBrowsingMode() = mPrivateBrowsingMode;

    PrincipalInfo& principalInfo = commonParams.principalInfo();

    if (aPrincipal) {
        if (!NS_IsMainThread()) {
            MOZ_CRASH("Figure out security checks for workers!");
        }
        MOZ_ASSERT(nsContentUtils::IsCallerChrome());

        if (NS_WARN_IF(NS_FAILED(PrincipalToPrincipalInfo(aPrincipal,
                                 &principalInfo)))) {
            IDB_REPORT_INTERNAL_ERR();
            aRv.Throw(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
            return nullptr;
        }

        if (principalInfo.type() != PrincipalInfo::TContentPrincipalInfo &&
                principalInfo.type() != PrincipalInfo::TSystemPrincipalInfo) {
            IDB_REPORT_INTERNAL_ERR();
            aRv.Throw(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
            return nullptr;
        }
    } else {
        principalInfo = *mPrincipalInfo;
    }

    uint64_t version = 0;
    if (!aDeleting && aVersion.WasPassed()) {
        if (aVersion.Value() < 1) {
            aRv.ThrowTypeError<MSG_INVALID_VERSION>();
            return nullptr;
        }
        version = aVersion.Value();
    }

    // Nothing can be done here if we have previously failed to create a
    // background actor.
    if (mBackgroundActorFailed) {
        IDB_REPORT_INTERNAL_ERR();
        aRv.Throw(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
        return nullptr;
    }

    PersistenceType persistenceType;

    if (principalInfo.type() == PrincipalInfo::TSystemPrincipalInfo) {
        // Chrome privilege always gets persistent storage.
        persistenceType = PERSISTENCE_TYPE_PERSISTENT;
    } else {
        persistenceType = PersistenceTypeFromStorage(aStorageType);
    }

    DatabaseMetadata& metadata = commonParams.metadata();
    metadata.name() = aName;
    metadata.persistenceType() = persistenceType;

    FactoryRequestParams params;
    if (aDeleting) {
        metadata.version() = 0;
        params = DeleteDatabaseRequestParams(commonParams);
    } else {
        metadata.version() = version;
        params = OpenDatabaseRequestParams(commonParams);
    }

    if (!mBackgroundActor && mPendingRequests.IsEmpty()) {
        BackgroundChildImpl::ThreadLocal* threadLocal =
            BackgroundChildImpl::GetThreadLocalForCurrentThread();

        nsAutoPtr<ThreadLocal> newIDBThreadLocal;
        ThreadLocal* idbThreadLocal;

        if (threadLocal && threadLocal->mIndexedDBThreadLocal) {
            idbThreadLocal = threadLocal->mIndexedDBThreadLocal;
        } else {
            nsCOMPtr<nsIUUIDGenerator> uuidGen =
                do_GetService("@mozilla.org/uuid-generator;1");
            MOZ_ASSERT(uuidGen);

            nsID id;
            MOZ_ALWAYS_SUCCEEDS(uuidGen->GenerateUUIDInPlace(&id));

            newIDBThreadLocal = idbThreadLocal = new ThreadLocal(id);
        }

        if (PBackgroundChild* actor = BackgroundChild::GetForCurrentThread()) {
            BackgroundActorCreated(actor, idbThreadLocal->GetLoggingInfo());
        } else {
            // We need to start the sequence to create a background actor for this
            // thread.
            RefPtr<BackgroundCreateCallback> cb =
                new BackgroundCreateCallback(this, idbThreadLocal->GetLoggingInfo());
            if (NS_WARN_IF(!BackgroundChild::GetOrCreateForCurrentThread(cb))) {
                IDB_REPORT_INTERNAL_ERR();
                aRv.Throw(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
                return nullptr;
            }
        }

        if (newIDBThreadLocal) {
            if (!threadLocal) {
                threadLocal = BackgroundChildImpl::GetThreadLocalForCurrentThread();
            }
            MOZ_ASSERT(threadLocal);
            MOZ_ASSERT(!threadLocal->mIndexedDBThreadLocal);

            threadLocal->mIndexedDBThreadLocal = newIDBThreadLocal.forget();
        }
    }

    RefPtr<IDBOpenDBRequest> request;

    if (mWindow) {
        JS::Rooted<JSObject*> scriptOwner(aCx,
                                          nsGlobalWindow::Cast(mWindow.get())->FastGetGlobalJSObject());
        MOZ_ASSERT(scriptOwner);

        request = IDBOpenDBRequest::CreateForWindow(aCx, this, mWindow, scriptOwner);
    } else {
        JS::Rooted<JSObject*> scriptOwner(aCx, mOwningObject);

        request = IDBOpenDBRequest::CreateForJS(aCx, this, scriptOwner);
        if (!request) {
            MOZ_ASSERT(!NS_IsMainThread());
            aRv.ThrowUncatchableException();
            return nullptr;
        }
    }

    MOZ_ASSERT(request);

    if (aDeleting) {
        IDB_LOG_MARK("IndexedDB %s: Child  Request[%llu]: "
                     "indexedDB.deleteDatabase(\"%s\")",
                     "IndexedDB %s: C R[%llu]: IDBFactory.deleteDatabase()",
                     IDB_LOG_ID_STRING(),
                     request->LoggingSerialNumber(),
                     NS_ConvertUTF16toUTF8(aName).get());
    } else {
        IDB_LOG_MARK("IndexedDB %s: Child  Request[%llu]: "
                     "indexedDB.open(\"%s\", %s)",
                     "IndexedDB %s: C R[%llu]: IDBFactory.open()",
                     IDB_LOG_ID_STRING(),
                     request->LoggingSerialNumber(),
                     NS_ConvertUTF16toUTF8(aName).get(),
                     IDB_LOG_STRINGIFY(aVersion));
    }

    // If we already have a background actor then we can start this request now.
    if (mBackgroundActor) {
        nsresult rv = InitiateRequest(request, params);
        if (NS_WARN_IF(NS_FAILED(rv))) {
            IDB_REPORT_INTERNAL_ERR();
            aRv.Throw(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
            return nullptr;
        }
    } else {
        mPendingRequests.AppendElement(new PendingRequestInfo(request, params));
    }

    return request.forget();
}
Example #9
0
already_AddRefed<IDBOpenDBRequest>
IDBFactory::OpenInternal(JSContext* aCx,
                         nsIPrincipal* aPrincipal,
                         const nsAString& aName,
                         const Optional<uint64_t>& aVersion,
                         const Optional<StorageType>& aStorageType,
                         bool aDeleting,
                         CallerType aCallerType,
                         ErrorResult& aRv)
{
  MOZ_ASSERT(mWindow || mOwningObject);
  MOZ_ASSERT_IF(!mWindow, !mPrivateBrowsingMode);

  CommonFactoryRequestParams commonParams;

  PrincipalInfo& principalInfo = commonParams.principalInfo();

  if (aPrincipal) {
    if (!NS_IsMainThread()) {
      MOZ_CRASH("Figure out security checks for workers!  What's this "
                "aPrincipal we have on a worker thread?");
    }
    MOZ_ASSERT(aCallerType == CallerType::System);
    MOZ_DIAGNOSTIC_ASSERT(mPrivateBrowsingMode == (aPrincipal->GetPrivateBrowsingId() > 0));

    if (NS_WARN_IF(NS_FAILED(PrincipalToPrincipalInfo(aPrincipal,
                                                      &principalInfo)))) {
      IDB_REPORT_INTERNAL_ERR();
      aRv.Throw(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
      return nullptr;
    }

    if (principalInfo.type() != PrincipalInfo::TContentPrincipalInfo &&
        principalInfo.type() != PrincipalInfo::TSystemPrincipalInfo) {
      IDB_REPORT_INTERNAL_ERR();
      aRv.Throw(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
      return nullptr;
    }
  } else {
    principalInfo = *mPrincipalInfo;
  }

  uint64_t version = 0;
  if (!aDeleting && aVersion.WasPassed()) {
    if (aVersion.Value() < 1) {
      aRv.ThrowTypeError<MSG_INVALID_VERSION>();
      return nullptr;
    }
    version = aVersion.Value();
  }

  // Nothing can be done here if we have previously failed to create a
  // background actor.
  if (mBackgroundActorFailed) {
    IDB_REPORT_INTERNAL_ERR();
    aRv.Throw(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
    return nullptr;
  }

  PersistenceType persistenceType;

  bool isInternal = principalInfo.type() == PrincipalInfo::TSystemPrincipalInfo;
  if (!isInternal && principalInfo.type() == PrincipalInfo::TContentPrincipalInfo) {
    nsCString origin = principalInfo.get_ContentPrincipalInfo().originNoSuffix();
    isInternal = QuotaManager::IsOriginInternal(origin);
  }

  // Allow storage attributes for add-ons independent of the pref.
  // This works in the main thread only, workers don't have the principal.
  bool isAddon = false;
  if (NS_IsMainThread()) {
    // aPrincipal is passed inconsistently, so even when we are already on
    // the main thread, we may have been passed a null aPrincipal.
    nsCOMPtr<nsIPrincipal> principal = PrincipalInfoToPrincipal(principalInfo);
    if (principal) {
      nsAutoString addonId;
      Unused << NS_WARN_IF(NS_FAILED(principal->GetAddonId(addonId)));
      isAddon = !addonId.IsEmpty();
    }
  }

  if (isInternal) {
    // Chrome privilege and internal origins always get persistent storage.
    persistenceType = PERSISTENCE_TYPE_PERSISTENT;
  } else if (isAddon || DOMPrefs::IndexedDBStorageOptionsEnabled()) {
    persistenceType = PersistenceTypeFromStorage(aStorageType);
  } else {
    persistenceType = PERSISTENCE_TYPE_DEFAULT;
  }

  DatabaseMetadata& metadata = commonParams.metadata();
  metadata.name() = aName;
  metadata.persistenceType() = persistenceType;

  FactoryRequestParams params;
  if (aDeleting) {
    metadata.version() = 0;
    params = DeleteDatabaseRequestParams(commonParams);
  } else {
    metadata.version() = version;
    params = OpenDatabaseRequestParams(commonParams);
  }

  if (!mBackgroundActor) {
    BackgroundChildImpl::ThreadLocal* threadLocal =
      BackgroundChildImpl::GetThreadLocalForCurrentThread();

    nsAutoPtr<ThreadLocal> newIDBThreadLocal;
    ThreadLocal* idbThreadLocal;

    if (threadLocal && threadLocal->mIndexedDBThreadLocal) {
      idbThreadLocal = threadLocal->mIndexedDBThreadLocal;
    } else {
      nsCOMPtr<nsIUUIDGenerator> uuidGen =
        do_GetService("@mozilla.org/uuid-generator;1");
      MOZ_ASSERT(uuidGen);

      nsID id;
      MOZ_ALWAYS_SUCCEEDS(uuidGen->GenerateUUIDInPlace(&id));

      newIDBThreadLocal = idbThreadLocal = new ThreadLocal(id);
    }

    PBackgroundChild* backgroundActor =
      BackgroundChild::GetOrCreateForCurrentThread();
    if (NS_WARN_IF(!backgroundActor)) {
      IDB_REPORT_INTERNAL_ERR();
      aRv.Throw(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
      return nullptr;
    }

    {
      BackgroundFactoryChild* actor = new BackgroundFactoryChild(this);

      // Set EventTarget for the top-level actor.
      // All child actors created later inherit the same event target.
      backgroundActor->SetEventTargetForActor(actor, EventTarget());
      MOZ_ASSERT(actor->GetActorEventTarget());
      mBackgroundActor =
        static_cast<BackgroundFactoryChild*>(
          backgroundActor->SendPBackgroundIDBFactoryConstructor(actor,
                                                                idbThreadLocal->GetLoggingInfo()));

      if (NS_WARN_IF(!mBackgroundActor)) {
        mBackgroundActorFailed = true;
        IDB_REPORT_INTERNAL_ERR();
        aRv.Throw(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
        return nullptr;
      }
    }

    if (newIDBThreadLocal) {
      if (!threadLocal) {
        threadLocal = BackgroundChildImpl::GetThreadLocalForCurrentThread();
      }
      MOZ_ASSERT(threadLocal);
      MOZ_ASSERT(!threadLocal->mIndexedDBThreadLocal);

      threadLocal->mIndexedDBThreadLocal = newIDBThreadLocal.forget();
    }
  }

  RefPtr<IDBOpenDBRequest> request;

  if (mWindow) {
    JS::Rooted<JSObject*> scriptOwner(aCx,
                                      nsGlobalWindowInner::Cast(mWindow.get())->FastGetGlobalJSObject());
    MOZ_ASSERT(scriptOwner);

    request = IDBOpenDBRequest::CreateForWindow(aCx, this, mWindow, scriptOwner);
  } else {
    JS::Rooted<JSObject*> scriptOwner(aCx, mOwningObject);

    request = IDBOpenDBRequest::CreateForJS(aCx, this, scriptOwner);
    if (!request) {
      MOZ_ASSERT(!NS_IsMainThread());
      aRv.ThrowUncatchableException();
      return nullptr;
    }
  }

  MOZ_ASSERT(request);

  if (aDeleting) {
    IDB_LOG_MARK("IndexedDB %s: Child  Request[%llu]: "
                   "indexedDB.deleteDatabase(\"%s\")",
                 "IndexedDB %s: C R[%llu]: IDBFactory.deleteDatabase()",
                 IDB_LOG_ID_STRING(),
                 request->LoggingSerialNumber(),
                 NS_ConvertUTF16toUTF8(aName).get());
  } else {
    IDB_LOG_MARK("IndexedDB %s: Child  Request[%llu]: "
                   "indexedDB.open(\"%s\", %s)",
                 "IndexedDB %s: C R[%llu]: IDBFactory.open()",
                 IDB_LOG_ID_STRING(),
                 request->LoggingSerialNumber(),
                 NS_ConvertUTF16toUTF8(aName).get(),
                 IDB_LOG_STRINGIFY(aVersion));
  }

  nsresult rv = InitiateRequest(request, params);
  if (NS_WARN_IF(NS_FAILED(rv))) {
    IDB_REPORT_INTERNAL_ERR();
    aRv.Throw(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
    return nullptr;
  }

  return request.forget();
}