2010-06-23 12:46:08 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2012-06-01 10:21:12 -07:00
|
|
|
#include "base/basictypes.h"
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
#include "IDBKeyRange.h"
|
|
|
|
|
2010-11-10 15:26:00 -08:00
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
|
|
|
|
#include "nsJSUtils.h"
|
2010-06-23 12:46:08 -07:00
|
|
|
#include "nsThreadUtils.h"
|
2010-11-10 15:26:00 -08:00
|
|
|
#include "nsContentUtils.h"
|
2012-07-20 09:42:08 -07:00
|
|
|
#include "nsDOMClassInfoID.h"
|
2010-11-10 15:26:00 -08:00
|
|
|
#include "Key.h"
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2012-06-01 10:21:12 -07:00
|
|
|
#include "mozilla/dom/indexedDB/PIndexedDBIndex.h"
|
|
|
|
#include "mozilla/dom/indexedDB/PIndexedDBObjectStore.h"
|
2013-08-02 00:09:15 -07:00
|
|
|
#include "mozilla/dom/IDBKeyRangeBinding.h"
|
2012-06-01 10:21:12 -07:00
|
|
|
|
2013-08-02 00:09:15 -07:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
2010-06-23 12:46:08 -07:00
|
|
|
USING_INDEXEDDB_NAMESPACE
|
2012-06-10 16:44:50 -07:00
|
|
|
using namespace mozilla::dom::indexedDB::ipc;
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2013-08-02 00:09:15 -07:00
|
|
|
static inline nsresult
|
2011-11-03 08:57:30 -07:00
|
|
|
GetKeyFromJSVal(JSContext* aCx,
|
2013-08-02 00:09:15 -07:00
|
|
|
JS::Handle<JS::Value> aVal,
|
2011-11-03 08:57:30 -07:00
|
|
|
Key& aKey,
|
|
|
|
bool aAllowUnset = false)
|
|
|
|
{
|
|
|
|
nsresult rv = aKey.SetFromJSVal(aCx, aVal);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_ASSERTION(NS_ERROR_GET_MODULE(rv) == NS_ERROR_MODULE_DOM_INDEXEDDB,
|
|
|
|
"Bad error code!");
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aKey.IsUnset() && !aAllowUnset) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-08-02 00:09:15 -07:00
|
|
|
JSObject*
|
|
|
|
IDBKeyRange::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
|
2011-11-03 08:57:30 -07:00
|
|
|
{
|
2013-08-02 00:09:15 -07:00
|
|
|
return IDBKeyRangeBinding::Wrap(aCx, aScope, this);
|
2011-11-03 08:57:30 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:09:15 -07:00
|
|
|
/* static */ already_AddRefed<IDBKeyRange>
|
|
|
|
IDBKeyRange::Only(const GlobalObject& aGlobal, JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue, ErrorResult& aRv)
|
2011-11-03 08:57:30 -07:00
|
|
|
{
|
2013-08-02 00:09:15 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
|
2010-11-10 15:26:00 -08:00
|
|
|
|
2013-08-02 00:09:15 -07:00
|
|
|
nsRefPtr<IDBKeyRange> keyRange = new IDBKeyRange(aGlobal.Get(), false, false, true);
|
2010-11-10 15:26:00 -08:00
|
|
|
|
2013-08-02 00:09:15 -07:00
|
|
|
aRv = GetKeyFromJSVal(aCx, aValue, keyRange->Lower());
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
2010-11-10 15:26:00 -08:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:09:15 -07:00
|
|
|
return keyRange.forget();
|
2010-11-10 15:26:00 -08:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:09:15 -07:00
|
|
|
/* static */ already_AddRefed<IDBKeyRange>
|
|
|
|
IDBKeyRange::LowerBound(const GlobalObject& aGlobal, JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue, bool aOpen,
|
|
|
|
ErrorResult& aRv)
|
2010-11-10 15:26:00 -08:00
|
|
|
{
|
2013-08-02 00:09:15 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
|
2010-11-10 15:26:00 -08:00
|
|
|
|
2013-08-02 00:09:15 -07:00
|
|
|
nsRefPtr<IDBKeyRange> keyRange = new IDBKeyRange(aGlobal.Get(), aOpen, true, false);
|
2010-11-10 15:26:00 -08:00
|
|
|
|
2013-08-02 00:09:15 -07:00
|
|
|
aRv = GetKeyFromJSVal(aCx, aValue, keyRange->Lower());
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
2010-11-10 15:26:00 -08:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:09:15 -07:00
|
|
|
return keyRange.forget();
|
2010-11-10 15:26:00 -08:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:09:15 -07:00
|
|
|
/* static */ already_AddRefed<IDBKeyRange>
|
|
|
|
IDBKeyRange::UpperBound(const GlobalObject& aGlobal, JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue, bool aOpen,
|
|
|
|
ErrorResult& aRv)
|
2010-11-10 15:26:00 -08:00
|
|
|
{
|
2013-08-02 00:09:15 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
|
2010-11-10 15:26:00 -08:00
|
|
|
|
2013-08-02 00:09:15 -07:00
|
|
|
nsRefPtr<IDBKeyRange> keyRange = new IDBKeyRange(aGlobal.Get(), true, aOpen, false);
|
2010-11-10 15:26:00 -08:00
|
|
|
|
2013-08-02 00:09:15 -07:00
|
|
|
aRv = GetKeyFromJSVal(aCx, aValue, keyRange->Upper());
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
2010-11-10 15:26:00 -08:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:09:15 -07:00
|
|
|
return keyRange.forget();
|
2010-11-10 15:26:00 -08:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:09:15 -07:00
|
|
|
/* static */ already_AddRefed<IDBKeyRange>
|
|
|
|
IDBKeyRange::Bound(const GlobalObject& aGlobal, JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aLower, JS::Handle<JS::Value> aUpper,
|
|
|
|
bool aLowerOpen, bool aUpperOpen, ErrorResult& aRv)
|
2010-11-10 15:26:00 -08:00
|
|
|
{
|
2013-08-02 00:09:15 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
|
2010-11-10 15:26:00 -08:00
|
|
|
|
2013-08-02 00:09:15 -07:00
|
|
|
nsRefPtr<IDBKeyRange> keyRange = new IDBKeyRange(aGlobal.Get(), aLowerOpen,
|
|
|
|
aUpperOpen, false);
|
2010-11-10 15:26:00 -08:00
|
|
|
|
2013-08-02 00:09:15 -07:00
|
|
|
aRv = GetKeyFromJSVal(aCx, aLower, keyRange->Lower());
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
2010-11-10 15:26:00 -08:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:09:15 -07:00
|
|
|
aRv = GetKeyFromJSVal(aCx, aUpper, keyRange->Upper());
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
2010-11-10 15:26:00 -08:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:09:15 -07:00
|
|
|
if (keyRange->Lower() > keyRange->Upper() ||
|
|
|
|
(keyRange->Lower() == keyRange->Upper() && (aLowerOpen || aUpperOpen))) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INDEXEDDB_DATA_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2010-11-10 15:26:00 -08:00
|
|
|
|
2013-08-02 00:09:15 -07:00
|
|
|
return keyRange.forget();
|
2010-11-10 15:26:00 -08:00
|
|
|
}
|
|
|
|
|
2010-06-23 12:46:08 -07:00
|
|
|
// static
|
2011-11-03 08:57:30 -07:00
|
|
|
nsresult
|
|
|
|
IDBKeyRange::FromJSVal(JSContext* aCx,
|
2013-08-02 00:09:15 -07:00
|
|
|
const jsval& aValue,
|
2011-11-03 08:57:30 -07:00
|
|
|
IDBKeyRange** aKeyRange)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
2013-08-02 00:09:15 -07:00
|
|
|
JS::Rooted<JS::Value> value(aCx, aValue);
|
2011-11-03 08:57:30 -07:00
|
|
|
nsRefPtr<IDBKeyRange> keyRange;
|
|
|
|
|
2013-08-02 00:09:15 -07:00
|
|
|
if (value.isNullOrUndefined()) {
|
2011-11-03 08:57:30 -07:00
|
|
|
// undefined and null returns no IDBKeyRange.
|
|
|
|
}
|
2013-08-02 00:09:15 -07:00
|
|
|
else if (value.isPrimitive() ||
|
|
|
|
JS_IsArrayObject(aCx, &value.toObject()) ||
|
|
|
|
JS_ObjectIsDate(aCx, &value.toObject())) {
|
2011-11-03 08:57:30 -07:00
|
|
|
// A valid key returns an 'only' IDBKeyRange.
|
2013-08-02 00:09:15 -07:00
|
|
|
keyRange = new IDBKeyRange(nullptr, false, false, true);
|
2011-11-03 08:57:30 -07:00
|
|
|
|
2013-08-02 00:09:15 -07:00
|
|
|
nsresult rv = GetKeyFromJSVal(aCx, value, keyRange->Lower());
|
2011-11-03 08:57:30 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2013-08-02 00:09:15 -07:00
|
|
|
MOZ_ASSERT(value.isObject());
|
2011-11-03 08:57:30 -07:00
|
|
|
// An object is not permitted unless it's another IDBKeyRange.
|
2013-08-02 00:09:15 -07:00
|
|
|
if (NS_FAILED(UnwrapObject<IDBKeyRange>(aCx, &value.toObject(),
|
|
|
|
keyRange))) {
|
2011-11-03 08:57:30 -07:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
keyRange.forget(aKeyRange);
|
|
|
|
return NS_OK;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2012-06-01 10:21:12 -07:00
|
|
|
// static
|
|
|
|
template <class T>
|
|
|
|
already_AddRefed<IDBKeyRange>
|
|
|
|
IDBKeyRange::FromSerializedKeyRange(const T& aKeyRange)
|
|
|
|
{
|
|
|
|
nsRefPtr<IDBKeyRange> keyRange =
|
2013-08-02 00:09:15 -07:00
|
|
|
new IDBKeyRange(nullptr, aKeyRange.lowerOpen(), aKeyRange.upperOpen(),
|
2012-06-01 10:21:12 -07:00
|
|
|
aKeyRange.isOnly());
|
|
|
|
keyRange->Lower() = aKeyRange.lower();
|
|
|
|
if (!keyRange->IsOnly()) {
|
|
|
|
keyRange->Upper() = aKeyRange.upper();
|
|
|
|
}
|
|
|
|
return keyRange.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
void
|
|
|
|
IDBKeyRange::ToSerializedKeyRange(T& aKeyRange)
|
|
|
|
{
|
|
|
|
aKeyRange.lowerOpen() = IsLowerOpen();
|
|
|
|
aKeyRange.upperOpen() = IsUpperOpen();
|
|
|
|
aKeyRange.isOnly() = IsOnly();
|
|
|
|
|
|
|
|
aKeyRange.lower() = Lower();
|
|
|
|
if (!IsOnly()) {
|
|
|
|
aKeyRange.upper() = Upper();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-02 00:09:15 -07:00
|
|
|
|
2011-11-03 08:57:30 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(IDBKeyRange)
|
2013-08-02 00:09:15 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mGlobal)
|
2011-11-03 08:57:30 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(IDBKeyRange)
|
2012-06-10 16:44:50 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JSVAL_MEMBER_CALLBACK(mCachedLowerVal)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JSVAL_MEMBER_CALLBACK(mCachedUpperVal)
|
2011-11-03 08:57:30 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(IDBKeyRange)
|
2013-08-02 00:09:15 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mGlobal)
|
2012-11-27 17:37:57 -08:00
|
|
|
tmp->DropJSObjects();
|
2011-11-03 08:57:30 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2011-11-03 08:57:30 -07:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(IDBKeyRange)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
2010-06-23 12:46:08 -07:00
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2011-11-03 08:57:30 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(IDBKeyRange)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(IDBKeyRange)
|
|
|
|
|
2012-11-27 17:37:57 -08:00
|
|
|
void
|
|
|
|
IDBKeyRange::DropJSObjects()
|
2012-03-28 07:40:52 -07:00
|
|
|
{
|
2012-11-27 17:37:57 -08:00
|
|
|
if (!mRooted) {
|
|
|
|
return;
|
2012-03-28 07:40:52 -07:00
|
|
|
}
|
2012-11-27 17:37:57 -08:00
|
|
|
mCachedLowerVal = JSVAL_VOID;
|
|
|
|
mCachedUpperVal = JSVAL_VOID;
|
|
|
|
mHaveCachedLowerVal = false;
|
|
|
|
mHaveCachedUpperVal = false;
|
|
|
|
mRooted = false;
|
|
|
|
NS_DROP_JS_OBJECTS(this, IDBKeyRange);
|
|
|
|
}
|
|
|
|
|
|
|
|
IDBKeyRange::~IDBKeyRange()
|
|
|
|
{
|
|
|
|
DropJSObjects();
|
2012-03-28 07:40:52 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:09:15 -07:00
|
|
|
JS::Value
|
|
|
|
IDBKeyRange::GetLower(JSContext* aCx, ErrorResult& aRv)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
2013-08-02 00:09:15 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2011-11-03 08:57:30 -07:00
|
|
|
if (!mHaveCachedLowerVal) {
|
|
|
|
if (!mRooted) {
|
|
|
|
NS_HOLD_JS_OBJECTS(this, IDBKeyRange);
|
|
|
|
mRooted = true;
|
|
|
|
}
|
|
|
|
|
2013-08-02 00:09:15 -07:00
|
|
|
aRv = Lower().ToJSVal(aCx, mCachedLowerVal);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return JS::UndefinedValue();
|
|
|
|
}
|
2011-11-03 08:57:30 -07:00
|
|
|
|
|
|
|
mHaveCachedLowerVal = true;
|
|
|
|
}
|
|
|
|
|
2013-08-02 00:09:15 -07:00
|
|
|
return mCachedLowerVal;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:09:15 -07:00
|
|
|
JS::Value
|
|
|
|
IDBKeyRange::GetUpper(JSContext* aCx, ErrorResult& aRv)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
2013-08-02 00:09:15 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
|
2010-06-23 12:46:08 -07:00
|
|
|
|
2011-11-03 08:57:30 -07:00
|
|
|
if (!mHaveCachedUpperVal) {
|
|
|
|
if (!mRooted) {
|
|
|
|
NS_HOLD_JS_OBJECTS(this, IDBKeyRange);
|
|
|
|
mRooted = true;
|
|
|
|
}
|
|
|
|
|
2013-08-02 00:09:15 -07:00
|
|
|
aRv = Upper().ToJSVal(aCx, mCachedUpperVal);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return JS::UndefinedValue();
|
|
|
|
}
|
2011-11-03 08:57:30 -07:00
|
|
|
|
|
|
|
mHaveCachedUpperVal = true;
|
|
|
|
}
|
|
|
|
|
2013-08-02 00:09:15 -07:00
|
|
|
return mCachedUpperVal;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
2012-06-01 10:21:12 -07:00
|
|
|
|
|
|
|
// Explicitly instantiate for all our key range types... Grumble.
|
|
|
|
template already_AddRefed<IDBKeyRange>
|
2012-06-10 16:44:50 -07:00
|
|
|
IDBKeyRange::FromSerializedKeyRange<FIXME_Bug_521898_objectstore::KeyRange>
|
|
|
|
(const FIXME_Bug_521898_objectstore::KeyRange& aKeyRange);
|
2012-06-01 10:21:12 -07:00
|
|
|
|
|
|
|
template already_AddRefed<IDBKeyRange>
|
2012-06-10 16:44:50 -07:00
|
|
|
IDBKeyRange::FromSerializedKeyRange<FIXME_Bug_521898_index::KeyRange>
|
|
|
|
(const FIXME_Bug_521898_index::KeyRange& aKeyRange);
|
2012-06-01 10:21:12 -07:00
|
|
|
|
|
|
|
template void
|
2012-06-10 16:44:50 -07:00
|
|
|
IDBKeyRange::ToSerializedKeyRange<FIXME_Bug_521898_objectstore::KeyRange>
|
|
|
|
(FIXME_Bug_521898_objectstore::KeyRange& aKeyRange);
|
2012-06-01 10:21:12 -07:00
|
|
|
|
|
|
|
template void
|
2012-06-10 16:44:50 -07:00
|
|
|
IDBKeyRange::ToSerializedKeyRange<FIXME_Bug_521898_index::KeyRange>
|
|
|
|
(FIXME_Bug_521898_index::KeyRange& aKeyRange);
|