Ejemplo n.º 1
0
// static
already_AddRefed<IDBLocaleAwareKeyRange>
IDBLocaleAwareKeyRange::Bound(const GlobalObject& aGlobal,
                              JS::Handle<JS::Value> aLower,
                              JS::Handle<JS::Value> aUpper,
                              bool aLowerOpen,
                              bool aUpperOpen,
                              ErrorResult& aRv)
{
  RefPtr<IDBLocaleAwareKeyRange> keyRange =
    new IDBLocaleAwareKeyRange(aGlobal.GetAsSupports(), aLowerOpen, aUpperOpen, false);

  aRv = GetKeyFromJSVal(aGlobal.Context(), aLower, keyRange->Lower());
  if (aRv.Failed()) {
    return nullptr;
  }

  aRv = GetKeyFromJSVal(aGlobal.Context(), aUpper, keyRange->Upper());
  if (aRv.Failed()) {
    return nullptr;
  }

  if (keyRange->Lower() == keyRange->Upper() && (aLowerOpen || aUpperOpen)) {
    aRv.Throw(NS_ERROR_DOM_INDEXEDDB_DATA_ERR);
    return nullptr;
  }

  return keyRange.forget();
}
Ejemplo n.º 2
0
// static
already_AddRefed<IDBKeyRange>
IDBKeyRange::Only(const GlobalObject& aGlobal,
                  JS::Handle<JS::Value> aValue,
                  ErrorResult& aRv)
{
  RefPtr<IDBKeyRange> keyRange =
    new IDBKeyRange(aGlobal.GetAsSupports(), false, false, true);

  aRv = GetKeyFromJSVal(aGlobal.Context(), aValue, keyRange->Lower());
  if (aRv.Failed()) {
    return nullptr;
  }

  return keyRange.forget();
}
Ejemplo n.º 3
0
bool
IDBKeyRange::Includes(JSContext* aCx,
                      JS::Handle<JS::Value> aValue,
                      ErrorResult& aRv) const
{
  Key key;
  aRv = GetKeyFromJSVal(aCx, aValue, key);
  if (aRv.Failed()) {
    return false;
  }

  switch (Key::CompareKeys(Lower(), key)) {
  case 1:
    return false;
  case 0:
    // Identical keys.
    if (LowerOpen()) {
      return false;
    }
    break;
  case -1:
    if (IsOnly()) {
      return false;
    }
    break;
  default:
    MOZ_CRASH();
  }

  if (!IsOnly()) {
    switch (Key::CompareKeys(key, Upper())) {
    case 1:
      return false;
    case 0:
      // Identical keys.
      if (UpperOpen()) {
        return false;
      }
      break;
    case -1:
      break;
    }
  } else {
    MOZ_ASSERT(key == Lower());
  }

  return true;
}
Ejemplo n.º 4
0
bool
IDBKeyRange::Includes(JSContext* aCx,
                      JS::Handle<JS::Value> aValue,
                      ErrorResult& aRv) const
{
  Key key;
  aRv = GetKeyFromJSVal(aCx, aValue, key);
  if (aRv.Failed()) {
    return false;
  }

  MOZ_ASSERT(!(Lower().IsUnset() && Upper().IsUnset()));
  MOZ_ASSERT_IF(IsOnly(),
    !Lower().IsUnset() && !LowerOpen() &&
    Lower() == Upper() && LowerOpen() == UpperOpen());

  if (!Lower().IsUnset()) {
    switch (Key::CompareKeys(Lower(), key)) {
    case 1:
      return false;
    case 0:
      // Identical keys.
      return !LowerOpen();
    case -1:
      if (IsOnly()) {
        return false;
      }
      break;
    default:
      MOZ_CRASH();
    }
  }

  if (!Upper().IsUnset()) {
    switch (Key::CompareKeys(key, Upper())) {
    case 1:
      return false;
    case 0:
      // Identical keys.
      return !UpperOpen();
    case -1:
      break;
    }
  }

  return true;
}
Ejemplo n.º 5
0
// static
nsresult
IDBKeyRange::FromJSVal(JSContext* aCx,
                       JS::Handle<JS::Value> aVal,
                       IDBKeyRange** aKeyRange)
{
  MOZ_ASSERT_IF(!aCx, aVal.isUndefined());

  RefPtr<IDBKeyRange> keyRange;

  if (aVal.isNullOrUndefined()) {
    // undefined and null returns no IDBKeyRange.
    keyRange.forget(aKeyRange);
    return NS_OK;
  }

  JS::Rooted<JSObject*> obj(aCx, aVal.isObject() ? &aVal.toObject() : nullptr);
  bool isValidKey = aVal.isPrimitive();
  if (!isValidKey) {
    js::ESClass cls;
    if (!js::GetBuiltinClass(aCx, obj, &cls)) {
      return NS_ERROR_UNEXPECTED;
    }
    isValidKey = cls == js::ESClass::Array || cls == js::ESClass::Date;
  }
  if (isValidKey) {
    // A valid key returns an 'only' IDBKeyRange.
    keyRange = new IDBKeyRange(nullptr, false, false, true);

    nsresult rv = GetKeyFromJSVal(aCx, aVal, keyRange->Lower());
    if (NS_FAILED(rv)) {
      return rv;
    }
  }
  else {
    MOZ_ASSERT(aVal.isObject());
    // An object is not permitted unless it's another IDBKeyRange.
    if (NS_FAILED(UNWRAP_OBJECT(IDBKeyRange, obj, keyRange))) {
      return NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
    }
  }

  keyRange.forget(aKeyRange);
  return NS_OK;
}
Ejemplo n.º 6
0
// static
nsresult
IDBKeyRange::FromJSVal(JSContext* aCx,
                       const jsval& aVal,
                       IDBKeyRange** aKeyRange)
{
  nsresult rv;
  nsRefPtr<IDBKeyRange> keyRange;

  if (JSVAL_IS_VOID(aVal) || JSVAL_IS_NULL(aVal)) {
    // undefined and null returns no IDBKeyRange.
  }
  else if (JSVAL_IS_PRIMITIVE(aVal) ||
           JS_IsArrayObject(aCx, JSVAL_TO_OBJECT(aVal)) ||
           JS_ObjectIsDate(aCx, JSVAL_TO_OBJECT(aVal))) {
    // A valid key returns an 'only' IDBKeyRange.
    keyRange = new IDBKeyRange(false, false, true);

    rv = GetKeyFromJSVal(aCx, aVal, keyRange->Lower());
    if (NS_FAILED(rv)) {
      return rv;
    }
  }
  else {
    // An object is not permitted unless it's another IDBKeyRange.
    nsIXPConnect* xpc = nsContentUtils::XPConnect();
    NS_ASSERTION(xpc, "This should never be null!");

    nsCOMPtr<nsIXPConnectWrappedNative> wrapper;
    rv = xpc->GetWrappedNativeOfJSObject(aCx, JSVAL_TO_OBJECT(aVal),
                                         getter_AddRefs(wrapper));
    NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);

    nsCOMPtr<nsIIDBKeyRange> iface;
    if (!wrapper || !(iface = do_QueryInterface(wrapper->Native()))) {
      // Some random JS object?
      return NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
    }

    keyRange = static_cast<IDBKeyRange*>(iface.get());
  }

  keyRange.forget(aKeyRange);
  return NS_OK;
}