2012-06-24 13:29:47 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-06-21 22:27:13 +00:00
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#include "KeyPath.h"
|
|
|
|
#include "IDBObjectStore.h"
|
|
|
|
#include "Key.h"
|
2014-01-28 00:37:05 +00:00
|
|
|
#include "ReportInternalError.h"
|
2012-06-21 22:27:13 +00:00
|
|
|
|
|
|
|
#include "nsCharSeparatedTokenizer.h"
|
|
|
|
#include "nsJSUtils.h"
|
|
|
|
#include "xpcpublic.h"
|
|
|
|
|
2013-07-31 15:48:36 +00:00
|
|
|
#include "mozilla/dom/BindingDeclarations.h"
|
2014-12-11 18:07:44 +00:00
|
|
|
#include "mozilla/dom/IDBObjectStoreBinding.h"
|
2013-07-31 15:48:36 +00:00
|
|
|
|
2014-09-26 23:21:57 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
namespace indexedDB {
|
2012-06-21 22:27:13 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
inline
|
|
|
|
bool
|
2014-01-04 15:02:17 +00:00
|
|
|
IgnoreWhitespace(char16_t c)
|
2012-06-21 22:27:13 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef nsCharSeparatedTokenizerTemplate<IgnoreWhitespace> KeyPathTokenizer;
|
|
|
|
|
|
|
|
bool
|
2014-12-11 18:07:44 +00:00
|
|
|
IsValidKeyPathString(const nsAString& aKeyPath)
|
2012-06-21 22:27:13 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(!aKeyPath.IsVoid(), "What?");
|
|
|
|
|
|
|
|
KeyPathTokenizer tokenizer(aKeyPath, '.');
|
|
|
|
|
|
|
|
while (tokenizer.hasMoreTokens()) {
|
|
|
|
nsString token(tokenizer.nextToken());
|
|
|
|
|
|
|
|
if (!token.Length()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-12-11 18:07:44 +00:00
|
|
|
if (!JS_IsIdentifier(token.get(), token.Length())) {
|
2012-06-21 22:27:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the very last character was a '.', the tokenizer won't give us an empty
|
|
|
|
// token, but the keyPath is still invalid.
|
|
|
|
if (!aKeyPath.IsEmpty() &&
|
|
|
|
aKeyPath.CharAt(aKeyPath.Length() - 1) == '.') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum KeyExtractionOptions {
|
|
|
|
DoNotCreateProperties,
|
|
|
|
CreateProperties
|
|
|
|
};
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
GetJSValFromKeyPathString(JSContext* aCx,
|
|
|
|
const JS::Value& aValue,
|
|
|
|
const nsAString& aKeyPathString,
|
|
|
|
JS::Value* aKeyJSVal,
|
|
|
|
KeyExtractionOptions aOptions,
|
|
|
|
KeyPath::ExtractOrCreateKeyCallback aCallback,
|
|
|
|
void* aClosure)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aCx, "Null pointer!");
|
2014-12-11 18:07:44 +00:00
|
|
|
NS_ASSERTION(IsValidKeyPathString(aKeyPathString),
|
2012-06-21 22:27:13 +00:00
|
|
|
"This will explode!");
|
|
|
|
NS_ASSERTION(!(aCallback || aClosure) || aOptions == CreateProperties,
|
|
|
|
"This is not allowed!");
|
|
|
|
NS_ASSERTION(aOptions != CreateProperties || aCallback,
|
|
|
|
"If properties are created, there must be a callback!");
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
*aKeyJSVal = aValue;
|
|
|
|
|
|
|
|
KeyPathTokenizer tokenizer(aKeyPathString, '.');
|
|
|
|
|
|
|
|
nsString targetObjectPropName;
|
2013-05-02 09:12:47 +00:00
|
|
|
JS::Rooted<JSObject*> targetObject(aCx, nullptr);
|
|
|
|
JS::Rooted<JSObject*> obj(aCx,
|
2014-04-28 03:27:54 +00:00
|
|
|
aValue.isPrimitive() ? nullptr : aValue.toObjectOrNull());
|
2012-06-21 22:27:13 +00:00
|
|
|
|
|
|
|
while (tokenizer.hasMoreTokens()) {
|
|
|
|
const nsDependentSubstring& token = tokenizer.nextToken();
|
|
|
|
|
|
|
|
NS_ASSERTION(!token.IsEmpty(), "Should be a valid keypath");
|
|
|
|
|
2014-07-22 04:43:21 +00:00
|
|
|
const char16_t* keyPathChars = token.BeginReading();
|
2012-06-21 22:27:13 +00:00
|
|
|
const size_t keyPathLen = token.Length();
|
|
|
|
|
2013-08-08 22:53:04 +00:00
|
|
|
bool hasProp;
|
2012-06-21 22:27:13 +00:00
|
|
|
if (!targetObject) {
|
|
|
|
// We're still walking the chain of existing objects
|
|
|
|
if (!obj) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
|
|
|
|
}
|
|
|
|
|
2013-08-08 22:53:04 +00:00
|
|
|
bool ok = JS_HasUCProperty(aCx, obj, keyPathChars, keyPathLen,
|
|
|
|
&hasProp);
|
2014-01-28 00:37:05 +00:00
|
|
|
IDB_ENSURE_TRUE(ok, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2012-06-21 22:27:13 +00:00
|
|
|
|
|
|
|
if (hasProp) {
|
|
|
|
// Get if the property exists...
|
2013-05-02 09:12:47 +00:00
|
|
|
JS::Rooted<JS::Value> intermediate(aCx);
|
2013-08-08 22:53:04 +00:00
|
|
|
bool ok = JS_GetUCProperty(aCx, obj, keyPathChars, keyPathLen, &intermediate);
|
2014-01-28 00:37:05 +00:00
|
|
|
IDB_ENSURE_TRUE(ok, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2012-06-21 22:27:13 +00:00
|
|
|
|
|
|
|
// Treat explicitly undefined as an error.
|
|
|
|
if (intermediate == JSVAL_VOID) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
|
|
|
|
}
|
|
|
|
if (tokenizer.hasMoreTokens()) {
|
|
|
|
// ...and walk to it if there are more steps...
|
2014-04-28 03:27:54 +00:00
|
|
|
if (intermediate.isPrimitive()) {
|
2012-06-21 22:27:13 +00:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
|
|
|
|
}
|
2014-04-28 02:58:52 +00:00
|
|
|
obj = intermediate.toObjectOrNull();
|
2012-06-21 22:27:13 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// ...otherwise use it as key
|
|
|
|
*aKeyJSVal = intermediate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// If the property doesn't exist, fall into below path of starting
|
|
|
|
// to define properties, if allowed.
|
|
|
|
if (aOptions == DoNotCreateProperties) {
|
2013-01-11 13:32:20 +00:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
|
2012-06-21 22:27:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
targetObject = obj;
|
|
|
|
targetObjectPropName = token;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (targetObject) {
|
|
|
|
// We have started inserting new objects or are about to just insert
|
|
|
|
// the first one.
|
|
|
|
|
|
|
|
*aKeyJSVal = JSVAL_VOID;
|
|
|
|
|
|
|
|
if (tokenizer.hasMoreTokens()) {
|
|
|
|
// If we're not at the end, we need to add a dummy object to the
|
|
|
|
// chain.
|
2014-01-16 17:48:58 +00:00
|
|
|
JS::Rooted<JSObject*> dummy(aCx, JS_NewObject(aCx, nullptr, JS::NullPtr(),
|
|
|
|
JS::NullPtr()));
|
2012-06-21 22:27:13 +00:00
|
|
|
if (!dummy) {
|
2014-01-28 00:37:05 +00:00
|
|
|
IDB_REPORT_INTERNAL_ERR();
|
2012-06-21 22:27:13 +00:00
|
|
|
rv = NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!JS_DefineUCProperty(aCx, obj, token.BeginReading(),
|
2014-04-30 09:10:33 +00:00
|
|
|
token.Length(), dummy, JSPROP_ENUMERATE)) {
|
2014-01-28 00:37:05 +00:00
|
|
|
IDB_REPORT_INTERNAL_ERR();
|
2012-06-21 22:27:13 +00:00
|
|
|
rv = NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
obj = dummy;
|
|
|
|
}
|
|
|
|
else {
|
2014-09-26 23:21:57 +00:00
|
|
|
JS::Rooted<JSObject*> dummy(aCx,
|
|
|
|
JS_NewObject(aCx, IDBObjectStore::DummyPropClass(), JS::NullPtr(),
|
|
|
|
JS::NullPtr()));
|
2012-06-21 22:27:13 +00:00
|
|
|
if (!dummy) {
|
2014-01-28 00:37:05 +00:00
|
|
|
IDB_REPORT_INTERNAL_ERR();
|
2012-06-21 22:27:13 +00:00
|
|
|
rv = NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!JS_DefineUCProperty(aCx, obj, token.BeginReading(),
|
2014-04-30 09:10:33 +00:00
|
|
|
token.Length(), dummy, JSPROP_ENUMERATE)) {
|
2014-01-28 00:37:05 +00:00
|
|
|
IDB_REPORT_INTERNAL_ERR();
|
2012-06-21 22:27:13 +00:00
|
|
|
rv = NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
obj = dummy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We guard on rv being a success because we need to run the property
|
|
|
|
// deletion code below even if we should not be running the callback.
|
|
|
|
if (NS_SUCCEEDED(rv) && aCallback) {
|
|
|
|
rv = (*aCallback)(aCx, aClosure);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (targetObject) {
|
|
|
|
// If this fails, we lose, and the web page sees a magical property
|
|
|
|
// appear on the object :-(
|
2013-08-05 13:01:53 +00:00
|
|
|
bool succeeded;
|
2012-06-21 22:27:13 +00:00
|
|
|
if (!JS_DeleteUCProperty2(aCx, targetObject,
|
|
|
|
targetObjectPropName.get(),
|
2013-05-02 09:12:47 +00:00
|
|
|
targetObjectPropName.Length(),
|
2013-08-02 12:15:39 +00:00
|
|
|
&succeeded)) {
|
2014-01-28 00:37:05 +00:00
|
|
|
IDB_REPORT_INTERNAL_ERR();
|
2012-06-21 22:27:13 +00:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
2014-01-28 00:37:05 +00:00
|
|
|
IDB_ENSURE_TRUE(succeeded, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2012-06-21 22:27:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2013-07-31 15:48:36 +00:00
|
|
|
// static
|
|
|
|
nsresult
|
2014-12-11 18:07:44 +00:00
|
|
|
KeyPath::Parse(const nsAString& aString, KeyPath* aKeyPath)
|
2013-07-31 15:48:36 +00:00
|
|
|
{
|
|
|
|
KeyPath keyPath(0);
|
|
|
|
keyPath.SetType(STRING);
|
|
|
|
|
2014-12-11 18:07:44 +00:00
|
|
|
if (!keyPath.AppendStringWithValidation(aString)) {
|
2013-07-31 15:48:36 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aKeyPath = keyPath;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//static
|
|
|
|
nsresult
|
2014-12-11 18:07:44 +00:00
|
|
|
KeyPath::Parse(const Sequence<nsString>& aStrings, KeyPath* aKeyPath)
|
2013-07-31 15:48:36 +00:00
|
|
|
{
|
|
|
|
KeyPath keyPath(0);
|
|
|
|
keyPath.SetType(ARRAY);
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < aStrings.Length(); ++i) {
|
2014-12-11 18:07:44 +00:00
|
|
|
if (!keyPath.AppendStringWithValidation(aStrings[i])) {
|
2013-07-31 15:48:36 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*aKeyPath = keyPath;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-21 22:27:13 +00:00
|
|
|
// static
|
|
|
|
nsresult
|
2014-12-11 18:07:44 +00:00
|
|
|
KeyPath::Parse(const Nullable<OwningStringOrStringSequence>& aValue, KeyPath* aKeyPath)
|
2012-06-21 22:27:13 +00:00
|
|
|
{
|
|
|
|
KeyPath keyPath(0);
|
|
|
|
|
|
|
|
aKeyPath->SetType(NONEXISTENT);
|
|
|
|
|
2014-12-11 18:07:44 +00:00
|
|
|
if (aValue.IsNull()) {
|
|
|
|
*aKeyPath = keyPath;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-06-21 22:27:13 +00:00
|
|
|
|
2014-12-11 18:07:44 +00:00
|
|
|
if (aValue.Value().IsString()) {
|
|
|
|
return Parse(aValue.Value().GetAsString(), aKeyPath);
|
2012-06-21 22:27:13 +00:00
|
|
|
}
|
|
|
|
|
2014-12-11 18:07:44 +00:00
|
|
|
MOZ_ASSERT(aValue.Value().IsStringSequence());
|
2012-06-21 22:27:13 +00:00
|
|
|
|
2014-12-11 18:07:44 +00:00
|
|
|
const Sequence<nsString>& seq = aValue.Value().GetAsStringSequence();
|
|
|
|
if (seq.Length() == 0) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2012-06-21 22:27:13 +00:00
|
|
|
}
|
2014-12-11 18:07:44 +00:00
|
|
|
return Parse(seq, aKeyPath);
|
2012-06-21 22:27:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
KeyPath::SetType(KeyPathType aType)
|
|
|
|
{
|
|
|
|
mType = aType;
|
|
|
|
mStrings.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-12-11 18:07:44 +00:00
|
|
|
KeyPath::AppendStringWithValidation(const nsAString& aString)
|
2012-06-21 22:27:13 +00:00
|
|
|
{
|
2014-12-11 18:07:44 +00:00
|
|
|
if (!IsValidKeyPathString(aString)) {
|
2012-06-21 22:27:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsString()) {
|
|
|
|
NS_ASSERTION(mStrings.Length() == 0, "Too many strings!");
|
|
|
|
mStrings.AppendElement(aString);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsArray()) {
|
|
|
|
mStrings.AppendElement(aString);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_NOTREACHED("What?!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
KeyPath::ExtractKey(JSContext* aCx, const JS::Value& aValue, Key& aKey) const
|
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t len = mStrings.Length();
|
2013-05-02 09:12:47 +00:00
|
|
|
JS::Rooted<JS::Value> value(aCx);
|
2012-06-21 22:27:13 +00:00
|
|
|
|
|
|
|
aKey.Unset();
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t i = 0; i < len; ++i) {
|
2013-05-02 09:12:47 +00:00
|
|
|
nsresult rv = GetJSValFromKeyPathString(aCx, aValue, mStrings[i],
|
|
|
|
value.address(),
|
2012-07-30 14:20:58 +00:00
|
|
|
DoNotCreateProperties, nullptr,
|
|
|
|
nullptr);
|
2012-06-21 22:27:13 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(aKey.AppendItem(aCx, IsArray() && i == 0, value))) {
|
|
|
|
NS_ASSERTION(aKey.IsUnset(), "Encoding error should unset");
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
aKey.FinishArray();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2012-07-30 14:20:58 +00:00
|
|
|
DoNotCreateProperties, nullptr, nullptr);
|
2012-06-21 22:27:13 +00:00
|
|
|
}
|
2013-05-02 09:12:47 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
const uint32_t len = mStrings.Length();
|
2014-02-12 10:50:46 +00:00
|
|
|
JS::Rooted<JSObject*> arrayObj(aCx, JS_NewArrayObject(aCx, len));
|
2012-06-21 22:27:13 +00:00
|
|
|
if (!arrayObj) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2013-05-02 09:12:47 +00:00
|
|
|
JS::Rooted<JS::Value> value(aCx);
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t i = 0; i < len; ++i) {
|
2013-05-02 09:12:47 +00:00
|
|
|
nsresult rv = GetJSValFromKeyPathString(aCx, aValue, mStrings[i],
|
|
|
|
value.address(),
|
2012-07-30 14:20:58 +00:00
|
|
|
DoNotCreateProperties, nullptr,
|
|
|
|
nullptr);
|
2012-06-21 22:27:13 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-01-25 09:31:17 +00:00
|
|
|
if (!JS_SetElement(aCx, arrayObj, i, value)) {
|
2014-01-28 00:37:05 +00:00
|
|
|
IDB_REPORT_INTERNAL_ERR();
|
2012-06-21 22:27:13 +00:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*aOutVal = OBJECT_TO_JSVAL(arrayObj);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
KeyPath::ExtractOrCreateKey(JSContext* aCx, const JS::Value& aValue,
|
|
|
|
Key& aKey, ExtractOrCreateKeyCallback aCallback,
|
|
|
|
void* aClosure) const
|
|
|
|
{
|
|
|
|
NS_ASSERTION(IsString(), "This doesn't make sense!");
|
|
|
|
|
2013-05-02 09:12:47 +00:00
|
|
|
JS::Rooted<JS::Value> value(aCx);
|
2012-06-21 22:27:13 +00:00
|
|
|
|
|
|
|
aKey.Unset();
|
|
|
|
|
2013-05-02 09:12:47 +00:00
|
|
|
nsresult rv = GetJSValFromKeyPathString(aCx, aValue, mStrings[0],
|
|
|
|
value.address(),
|
2012-06-21 22:27:13 +00:00
|
|
|
CreateProperties, aCallback,
|
|
|
|
aClosure);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(aKey.AppendItem(aCx, false, value))) {
|
|
|
|
NS_ASSERTION(aKey.IsUnset(), "Should be unset");
|
2014-04-28 02:32:05 +00:00
|
|
|
return value.isUndefined() ? NS_OK : NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
|
2012-06-21 22:27:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
aKey.FinishArray();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
KeyPath::SerializeToString(nsAString& aString) const
|
|
|
|
{
|
|
|
|
NS_ASSERTION(IsValid(), "Check to see if I'm valid first!");
|
|
|
|
|
|
|
|
if (IsString()) {
|
|
|
|
aString = mStrings[0];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsArray()) {
|
|
|
|
// We use a comma in the beginning to indicate that it's an array of
|
|
|
|
// key paths. This is to be able to tell a string-keypath from an
|
|
|
|
// array-keypath which contains only one item.
|
|
|
|
// It also makes serializing easier :-)
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t len = mStrings.Length();
|
|
|
|
for (uint32_t i = 0; i < len; ++i) {
|
2014-05-22 03:48:50 +00:00
|
|
|
aString.Append(',');
|
|
|
|
aString.Append(mStrings[i]);
|
2012-06-21 22:27:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_NOTREACHED("What?");
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
KeyPath
|
|
|
|
KeyPath::DeserializeFromString(const nsAString& aString)
|
|
|
|
{
|
|
|
|
KeyPath keyPath(0);
|
|
|
|
|
|
|
|
if (!aString.IsEmpty() && aString.First() == ',') {
|
|
|
|
keyPath.SetType(ARRAY);
|
|
|
|
|
|
|
|
// We use a comma in the beginning to indicate that it's an array of
|
|
|
|
// key paths. This is to be able to tell a string-keypath from an
|
|
|
|
// array-keypath which contains only one item.
|
|
|
|
nsCharSeparatedTokenizerTemplate<IgnoreWhitespace> tokenizer(aString, ',');
|
|
|
|
tokenizer.nextToken();
|
|
|
|
while (tokenizer.hasMoreTokens()) {
|
|
|
|
keyPath.mStrings.AppendElement(tokenizer.nextToken());
|
|
|
|
}
|
|
|
|
|
|
|
|
return keyPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
keyPath.SetType(STRING);
|
|
|
|
keyPath.mStrings.AppendElement(aString);
|
|
|
|
|
|
|
|
return keyPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2013-06-18 10:00:37 +00:00
|
|
|
KeyPath::ToJSVal(JSContext* aCx, JS::MutableHandle<JS::Value> aValue) const
|
2012-06-21 22:27:13 +00:00
|
|
|
{
|
|
|
|
if (IsArray()) {
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t len = mStrings.Length();
|
2014-02-12 10:50:46 +00:00
|
|
|
JS::Rooted<JSObject*> array(aCx, JS_NewArrayObject(aCx, len));
|
2012-06-21 22:27:13 +00:00
|
|
|
if (!array) {
|
2014-01-28 00:37:05 +00:00
|
|
|
IDB_WARNING("Failed to make array!");
|
2012-06-21 22:27:13 +00:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t i = 0; i < len; ++i) {
|
2013-05-02 09:12:47 +00:00
|
|
|
JS::Rooted<JS::Value> val(aCx);
|
2012-06-21 22:27:13 +00:00
|
|
|
nsString tmp(mStrings[i]);
|
2013-10-12 05:02:39 +00:00
|
|
|
if (!xpc::StringToJsval(aCx, tmp, &val)) {
|
2014-01-28 00:37:05 +00:00
|
|
|
IDB_REPORT_INTERNAL_ERR();
|
2012-06-21 22:27:13 +00:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
|
2014-01-25 09:31:17 +00:00
|
|
|
if (!JS_SetElement(aCx, array, i, val)) {
|
2014-01-28 00:37:05 +00:00
|
|
|
IDB_REPORT_INTERNAL_ERR();
|
2012-06-21 22:27:13 +00:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-18 10:00:37 +00:00
|
|
|
aValue.setObject(*array);
|
2012-06-21 22:27:13 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsString()) {
|
|
|
|
nsString tmp(mStrings[0]);
|
2013-10-12 05:02:39 +00:00
|
|
|
if (!xpc::StringToJsval(aCx, tmp, aValue)) {
|
2014-01-28 00:37:05 +00:00
|
|
|
IDB_REPORT_INTERNAL_ERR();
|
2012-06-21 22:27:13 +00:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-06-18 10:00:37 +00:00
|
|
|
aValue.setNull();
|
2012-06-21 22:27:13 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-06-18 10:00:37 +00:00
|
|
|
nsresult
|
|
|
|
KeyPath::ToJSVal(JSContext* aCx, JS::Heap<JS::Value>& aValue) const
|
|
|
|
{
|
|
|
|
JS::Rooted<JS::Value> value(aCx);
|
|
|
|
nsresult rv = ToJSVal(aCx, &value);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
aValue = value;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-06-21 22:27:13 +00:00
|
|
|
bool
|
|
|
|
KeyPath::IsAllowedForObjectStore(bool aAutoIncrement) const
|
|
|
|
{
|
|
|
|
// Any keypath that passed validation is allowed for non-autoIncrement
|
|
|
|
// objectStores.
|
|
|
|
if (!aAutoIncrement) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Array keypaths are not allowed for autoIncrement objectStores.
|
|
|
|
if (IsArray()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Neither are empty strings.
|
|
|
|
if (IsEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Everything else is ok.
|
|
|
|
return true;
|
|
|
|
}
|
2014-09-26 23:21:57 +00:00
|
|
|
|
|
|
|
} // namespace indexedDB
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|