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
|
|
|
|
2013-09-28 04:25:46 -07:00
|
|
|
#include "mozilla/dom/IDBKeyRangeBinding.h"
|
2012-06-01 10:21:12 -07:00
|
|
|
#include "mozilla/dom/indexedDB/PIndexedDBIndex.h"
|
|
|
|
#include "mozilla/dom/indexedDB/PIndexedDBObjectStore.h"
|
|
|
|
|
2013-09-28 04:25:46 -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 02:38:49 -07:00
|
|
|
namespace {
|
|
|
|
|
2013-09-28 04:25:46 -07:00
|
|
|
inline nsresult
|
2011-11-03 08:57:30 -07:00
|
|
|
GetKeyFromJSVal(JSContext* aCx,
|
2013-08-02 02:38:49 -07:00
|
|
|
jsval 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 02:38:49 -07:00
|
|
|
} // anonymous namespace
|
|
|
|
|
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 02:38:49 -07:00
|
|
|
const jsval& aVal,
|
2011-11-03 08:57:30 -07:00
|
|
|
IDBKeyRange** aKeyRange)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
2011-11-03 08:57:30 -07:00
|
|
|
nsRefPtr<IDBKeyRange> keyRange;
|
|
|
|
|
2013-09-28 04:25:46 -07:00
|
|
|
if (aVal.isNullOrUndefined()) {
|
2011-11-03 08:57:30 -07:00
|
|
|
// undefined and null returns no IDBKeyRange.
|
2013-09-28 04:25:46 -07:00
|
|
|
keyRange.forget(aKeyRange);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-08-02 02:38:49 -07:00
|
|
|
|
2013-11-11 00:04:41 -08:00
|
|
|
JS::Rooted<JSObject*> obj(aCx, aVal.isObject() ? &aVal.toObject() : nullptr);
|
2013-09-28 04:25:46 -07:00
|
|
|
if (aVal.isPrimitive() || JS_IsArrayObject(aCx, obj) ||
|
|
|
|
JS_ObjectIsDate(aCx, obj)) {
|
|
|
|
// A valid key returns an 'only' IDBKeyRange.
|
|
|
|
keyRange = new IDBKeyRange(nullptr, false, false, true);
|
2013-08-02 02:38:49 -07:00
|
|
|
|
2013-09-28 04:25:46 -07:00
|
|
|
nsresult rv = GetKeyFromJSVal(aCx, aVal, keyRange->Lower());
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
2013-09-19 00:54:01 -07:00
|
|
|
}
|
2013-09-28 04:25:46 -07:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
MOZ_ASSERT(aVal.isObject());
|
|
|
|
// An object is not permitted unless it's another IDBKeyRange.
|
2013-11-21 04:51:16 -08:00
|
|
|
if (NS_FAILED(UNWRAP_OBJECT(IDBKeyRange, obj, keyRange))) {
|
2013-09-28 04:25:46 -07:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
|
2013-09-19 00:54:01 -07:00
|
|
|
}
|
2011-11-03 08:57:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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-09-28 04:25:46 -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-01 18:29:05 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(IDBKeyRange)
|
2013-08-02 00:09:15 -07:00
|
|
|
|
2011-11-03 08:57:30 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(IDBKeyRange)
|
2013-09-28 04:25:46 -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-09-28 04:25:46 -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;
|
2013-08-16 13:10:17 -07:00
|
|
|
mozilla::DropJSObjects(this);
|
2012-11-27 17:37:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
IDBKeyRange::~IDBKeyRange()
|
|
|
|
{
|
|
|
|
DropJSObjects();
|
2012-03-28 07:40:52 -07:00
|
|
|
}
|
|
|
|
|
2013-09-28 04:25:46 -07:00
|
|
|
JSObject*
|
2013-11-11 00:04:41 -08:00
|
|
|
IDBKeyRange::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
|
2013-09-28 04:25:46 -07:00
|
|
|
{
|
|
|
|
return IDBKeyRangeBinding::Wrap(aCx, aScope, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Value
|
|
|
|
IDBKeyRange::GetLower(JSContext* aCx, ErrorResult& aRv)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
2013-09-28 04:25:46 -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) {
|
2013-08-16 13:10:17 -07:00
|
|
|
mozilla::HoldJSObjects(this);
|
2011-11-03 08:57:30 -07:00
|
|
|
mRooted = true;
|
|
|
|
}
|
|
|
|
|
2013-09-28 04:25:46 -07:00
|
|
|
aRv = Lower().ToJSVal(aCx, mCachedLowerVal);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return JS::UndefinedValue();
|
|
|
|
}
|
2011-11-03 08:57:30 -07:00
|
|
|
|
|
|
|
mHaveCachedLowerVal = true;
|
|
|
|
}
|
|
|
|
|
2013-09-28 04:25:46 -07:00
|
|
|
return mCachedLowerVal;
|
2010-06-23 12:46:08 -07:00
|
|
|
}
|
|
|
|
|
2013-09-28 04:25:46 -07:00
|
|
|
JS::Value
|
|
|
|
IDBKeyRange::GetUpper(JSContext* aCx, ErrorResult& aRv)
|
2010-06-23 12:46:08 -07:00
|
|
|
{
|
2013-09-28 04:25:46 -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) {
|
2013-08-16 13:10:17 -07:00
|
|
|
mozilla::HoldJSObjects(this);
|
2011-11-03 08:57:30 -07:00
|
|
|
mRooted = true;
|
|
|
|
}
|
|
|
|
|
2013-09-28 04:25:46 -07:00
|
|
|
aRv = Upper().ToJSVal(aCx, mCachedUpperVal);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return JS::UndefinedValue();
|
|
|
|
}
|
2011-11-03 08:57:30 -07:00
|
|
|
|
|
|
|
mHaveCachedUpperVal = true;
|
|
|
|
}
|
|
|
|
|
2013-09-28 04:25:46 -07:00
|
|
|
return mCachedUpperVal;
|
2013-08-02 02:38:49 -07:00
|
|
|
}
|
|
|
|
|
2013-09-28 04:25:46 -07:00
|
|
|
// static
|
|
|
|
already_AddRefed<IDBKeyRange>
|
|
|
|
IDBKeyRange::Only(const GlobalObject& aGlobal, JSContext* aCx,
|
2013-11-11 00:04:41 -08:00
|
|
|
JS::Handle<JS::Value> aValue, ErrorResult& aRv)
|
2013-08-02 02:38:49 -07:00
|
|
|
{
|
2013-09-28 04:25:46 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
|
2013-08-02 02:38:49 -07:00
|
|
|
|
2013-09-28 04:25:46 -07:00
|
|
|
nsRefPtr<IDBKeyRange> keyRange =
|
|
|
|
new IDBKeyRange(aGlobal.GetAsSupports(), false, false, true);
|
|
|
|
|
|
|
|
aRv = GetKeyFromJSVal(aCx, aValue, keyRange->Lower());
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return keyRange.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<IDBKeyRange>
|
|
|
|
IDBKeyRange::LowerBound(const GlobalObject& aGlobal, JSContext* aCx,
|
2013-11-11 00:04:41 -08:00
|
|
|
JS::Handle<JS::Value> aValue, bool aOpen,
|
|
|
|
ErrorResult& aRv)
|
2013-09-28 04:25:46 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
nsRefPtr<IDBKeyRange> keyRange =
|
|
|
|
new IDBKeyRange(aGlobal.GetAsSupports(), aOpen, true, false);
|
|
|
|
|
|
|
|
aRv = GetKeyFromJSVal(aCx, aValue, keyRange->Lower());
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return keyRange.forget();
|
2013-08-02 02:38:49 -07:00
|
|
|
}
|
|
|
|
|
2013-09-28 04:25:46 -07:00
|
|
|
// static
|
|
|
|
already_AddRefed<IDBKeyRange>
|
|
|
|
IDBKeyRange::UpperBound(const GlobalObject& aGlobal, JSContext* aCx,
|
2013-11-11 00:04:41 -08:00
|
|
|
JS::Handle<JS::Value> aValue, bool aOpen,
|
|
|
|
ErrorResult& aRv)
|
2013-09-28 04:25:46 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
nsRefPtr<IDBKeyRange> keyRange =
|
|
|
|
new IDBKeyRange(aGlobal.GetAsSupports(), true, aOpen, false);
|
2013-08-02 02:38:49 -07:00
|
|
|
|
2013-09-28 04:25:46 -07:00
|
|
|
aRv = GetKeyFromJSVal(aCx, aValue, keyRange->Upper());
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return keyRange.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<IDBKeyRange>
|
|
|
|
IDBKeyRange::Bound(const GlobalObject& aGlobal, JSContext* aCx,
|
2013-11-11 00:04:41 -08:00
|
|
|
JS::Handle<JS::Value> aLower, JS::Handle<JS::Value> aUpper,
|
2013-09-28 04:25:46 -07:00
|
|
|
bool aLowerOpen, bool aUpperOpen, ErrorResult& aRv)
|
2013-08-02 02:38:49 -07:00
|
|
|
{
|
2013-09-28 04:25:46 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
|
2013-08-02 02:38:49 -07:00
|
|
|
|
2013-09-28 04:25:46 -07:00
|
|
|
nsRefPtr<IDBKeyRange> keyRange =
|
|
|
|
new IDBKeyRange(aGlobal.GetAsSupports(), aLowerOpen, aUpperOpen, false);
|
|
|
|
|
|
|
|
aRv = GetKeyFromJSVal(aCx, aLower, keyRange->Lower());
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
aRv = GetKeyFromJSVal(aCx, aUpper, keyRange->Upper());
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (keyRange->Lower() > keyRange->Upper() ||
|
|
|
|
(keyRange->Lower() == keyRange->Upper() && (aLowerOpen || aUpperOpen))) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INDEXEDDB_DATA_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return keyRange.forget();
|
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>
|
2013-08-06 05:44:01 -07:00
|
|
|
IDBKeyRange::FromSerializedKeyRange<KeyRange> (const KeyRange& aKeyRange);
|
2012-06-01 10:21:12 -07:00
|
|
|
|
|
|
|
template void
|
2013-08-06 05:44:01 -07:00
|
|
|
IDBKeyRange::ToSerializedKeyRange<KeyRange> (KeyRange& aKeyRange);
|