2012-06-24 17:29:47 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2015-05-03 22:32:37 +03:00
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-06-22 02:27:13 +04: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/. */
|
|
|
|
|
|
|
|
#include "KeyPath.h"
|
|
|
|
#include "IDBObjectStore.h"
|
|
|
|
#include "Key.h"
|
2014-01-28 04:37:05 +04:00
|
|
|
#include "ReportInternalError.h"
|
2012-06-22 02:27:13 +04:00
|
|
|
|
|
|
|
#include "nsCharSeparatedTokenizer.h"
|
|
|
|
#include "nsJSUtils.h"
|
2017-06-07 13:36:20 +03:00
|
|
|
#include "nsPrintfCString.h"
|
2012-06-22 02:27:13 +04:00
|
|
|
#include "xpcpublic.h"
|
|
|
|
|
2013-07-31 19:48:36 +04:00
|
|
|
#include "mozilla/dom/BindingDeclarations.h"
|
2019-03-29 21:05:11 +03:00
|
|
|
#include "mozilla/dom/Blob.h"
|
2018-09-25 12:53:49 +03:00
|
|
|
#include "mozilla/dom/BlobBinding.h"
|
2019-03-29 21:05:11 +03:00
|
|
|
#include "mozilla/dom/File.h"
|
2014-12-11 21:07:44 +03:00
|
|
|
#include "mozilla/dom/IDBObjectStoreBinding.h"
|
2013-07-31 19:48:36 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
namespace indexedDB {
|
2012-06-22 02:27:13 +04:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
inline bool IgnoreWhitespace(char16_t c) { return false; }
|
2012-06-22 02:27:13 +04:00
|
|
|
|
|
|
|
typedef nsCharSeparatedTokenizerTemplate<IgnoreWhitespace> KeyPathTokenizer;
|
|
|
|
|
2014-12-11 21:07:44 +03:00
|
|
|
bool IsValidKeyPathString(const nsAString& aKeyPath) {
|
2012-06-22 02:27:13 +04:00
|
|
|
NS_ASSERTION(!aKeyPath.IsVoid(), "What?");
|
|
|
|
|
|
|
|
KeyPathTokenizer tokenizer(aKeyPath, '.');
|
|
|
|
|
|
|
|
while (tokenizer.hasMoreTokens()) {
|
|
|
|
nsString token(tokenizer.nextToken());
|
|
|
|
|
|
|
|
if (!token.Length()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-12-11 21:07:44 +03:00
|
|
|
if (!JS_IsIdentifier(token.get(), token.Length())) {
|
2012-06-22 02:27:13 +04: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.
|
2019-11-08 16:22:28 +03:00
|
|
|
return aKeyPath.IsEmpty() || aKeyPath.CharAt(aKeyPath.Length() - 1) != '.';
|
2012-06-22 02:27:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
enum KeyExtractionOptions { DoNotCreateProperties, CreateProperties };
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-06-22 02:27:13 +04:00
|
|
|
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 21:07:44 +03:00
|
|
|
NS_ASSERTION(IsValidKeyPathString(aKeyPathString), "This will explode!");
|
2012-06-22 02:27:13 +04:00
|
|
|
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 13:12:47 +04:00
|
|
|
JS::Rooted<JSObject*> targetObject(aCx, nullptr);
|
2015-07-01 07:15:30 +03:00
|
|
|
JS::Rooted<JS::Value> currentVal(aCx, aValue);
|
|
|
|
JS::Rooted<JSObject*> obj(aCx);
|
2012-06-22 02:27:13 +04:00
|
|
|
|
|
|
|
while (tokenizer.hasMoreTokens()) {
|
|
|
|
const nsDependentSubstring& token = tokenizer.nextToken();
|
|
|
|
|
|
|
|
NS_ASSERTION(!token.IsEmpty(), "Should be a valid keypath");
|
|
|
|
|
2014-07-22 08:43:21 +04:00
|
|
|
const char16_t* keyPathChars = token.BeginReading();
|
2012-06-22 02:27:13 +04:00
|
|
|
const size_t keyPathLen = token.Length();
|
|
|
|
|
|
|
|
if (!targetObject) {
|
|
|
|
// We're still walking the chain of existing objects
|
2017-09-19 10:52:48 +03:00
|
|
|
// http://w3c.github.io/IndexedDB/#evaluate-a-key-path-on-a-value
|
2015-07-01 07:15:30 +03:00
|
|
|
// step 4 substep 1: check for .length on a String value.
|
|
|
|
if (currentVal.isString() && !tokenizer.hasMoreTokens() &&
|
2017-09-19 10:52:48 +03:00
|
|
|
token.EqualsLiteral("length")) {
|
2015-07-01 07:15:30 +03:00
|
|
|
aKeyJSVal->setNumber(double(JS_GetStringLength(currentVal.toString())));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!currentVal.isObject()) {
|
2012-06-22 02:27:13 +04:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
|
|
|
|
}
|
2015-07-01 07:15:30 +03:00
|
|
|
obj = ¤tVal.toObject();
|
2012-06-22 02:27:13 +04:00
|
|
|
|
2018-09-25 12:53:49 +03:00
|
|
|
// We call JS_GetOwnUCPropertyDescriptor on purpose (as opposed to
|
|
|
|
// JS_GetUCPropertyDescriptor) to avoid searching the prototype chain.
|
|
|
|
JS::Rooted<JS::PropertyDescriptor> desc(aCx);
|
|
|
|
bool ok = JS_GetOwnUCPropertyDescriptor(aCx, obj, keyPathChars,
|
|
|
|
keyPathLen, &desc);
|
2014-01-28 04:37:05 +04:00
|
|
|
IDB_ENSURE_TRUE(ok, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2012-06-22 02:27:13 +04:00
|
|
|
|
2018-09-25 12:53:49 +03:00
|
|
|
JS::Rooted<JS::Value> intermediate(aCx);
|
|
|
|
bool hasProp = false;
|
|
|
|
|
|
|
|
if (desc.object()) {
|
|
|
|
intermediate = desc.value();
|
|
|
|
hasProp = true;
|
|
|
|
} else {
|
|
|
|
// If we get here it means the object doesn't have the property or the
|
|
|
|
// property is available throuch a getter. We don't want to call any
|
|
|
|
// getters to avoid potential re-entrancy.
|
|
|
|
// The blob object is special since its properties are available
|
|
|
|
// only through getters but we still want to support them for key
|
|
|
|
// extraction. So they need to be handled manually.
|
|
|
|
Blob* blob;
|
|
|
|
if (NS_SUCCEEDED(UNWRAP_OBJECT(Blob, &obj, blob))) {
|
|
|
|
if (token.EqualsLiteral("size")) {
|
|
|
|
ErrorResult rv;
|
|
|
|
uint64_t size = blob->GetSize(rv);
|
|
|
|
MOZ_ALWAYS_TRUE(!rv.Failed());
|
|
|
|
|
|
|
|
intermediate = JS_NumberValue(size);
|
|
|
|
hasProp = true;
|
|
|
|
} else if (token.EqualsLiteral("type")) {
|
|
|
|
nsString type;
|
|
|
|
blob->GetType(type);
|
|
|
|
|
|
|
|
JSString* string =
|
|
|
|
JS_NewUCStringCopyN(aCx, type.get(), type.Length());
|
|
|
|
|
|
|
|
intermediate = JS::StringValue(string);
|
|
|
|
hasProp = true;
|
|
|
|
} else {
|
|
|
|
RefPtr<File> file = blob->ToFile();
|
|
|
|
if (file) {
|
|
|
|
if (token.EqualsLiteral("name")) {
|
|
|
|
nsString name;
|
|
|
|
file->GetName(name);
|
|
|
|
|
|
|
|
JSString* string =
|
|
|
|
JS_NewUCStringCopyN(aCx, name.get(), name.Length());
|
|
|
|
|
|
|
|
intermediate = JS::StringValue(string);
|
|
|
|
hasProp = true;
|
|
|
|
} else if (token.EqualsLiteral("lastModified")) {
|
|
|
|
ErrorResult rv;
|
|
|
|
int64_t lastModifiedDate = file->GetLastModified(rv);
|
|
|
|
MOZ_ALWAYS_TRUE(!rv.Failed());
|
|
|
|
|
|
|
|
intermediate = JS_NumberValue(lastModifiedDate);
|
|
|
|
hasProp = true;
|
|
|
|
}
|
|
|
|
// The spec also lists "lastModifiedDate", but we deprecated and
|
|
|
|
// removed support for it.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-22 02:27:13 +04:00
|
|
|
|
2018-09-25 12:53:49 +03:00
|
|
|
if (hasProp) {
|
2012-06-22 02:27:13 +04:00
|
|
|
// Treat explicitly undefined as an error.
|
2015-01-14 10:59:06 +03:00
|
|
|
if (intermediate.isUndefined()) {
|
2012-06-22 02:27:13 +04:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
|
|
|
|
}
|
|
|
|
if (tokenizer.hasMoreTokens()) {
|
|
|
|
// ...and walk to it if there are more steps...
|
2015-07-01 07:15:30 +03:00
|
|
|
currentVal = intermediate;
|
2012-06-22 02:27:13 +04: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 17:32:20 +04:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
|
2012-06-22 02:27:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
targetObject = obj;
|
|
|
|
targetObjectPropName = token;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (targetObject) {
|
|
|
|
// We have started inserting new objects or are about to just insert
|
|
|
|
// the first one.
|
|
|
|
|
2015-01-14 10:59:06 +03:00
|
|
|
aKeyJSVal->setUndefined();
|
2012-06-22 02:27:13 +04:00
|
|
|
|
|
|
|
if (tokenizer.hasMoreTokens()) {
|
|
|
|
// If we're not at the end, we need to add a dummy object to the
|
|
|
|
// chain.
|
2015-01-24 18:38:08 +03:00
|
|
|
JS::Rooted<JSObject*> dummy(aCx, JS_NewPlainObject(aCx));
|
2012-06-22 02:27:13 +04:00
|
|
|
if (!dummy) {
|
2014-01-28 04:37:05 +04:00
|
|
|
IDB_REPORT_INTERNAL_ERR();
|
2012-06-22 02:27:13 +04:00
|
|
|
rv = NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!JS_DefineUCProperty(aCx, obj, token.BeginReading(), token.Length(),
|
2014-04-30 13:10:33 +04:00
|
|
|
dummy, JSPROP_ENUMERATE)) {
|
2014-01-28 04:37:05 +04:00
|
|
|
IDB_REPORT_INTERNAL_ERR();
|
2012-06-22 02:27:13 +04:00
|
|
|
rv = NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
obj = dummy;
|
|
|
|
} else {
|
2014-09-27 03:21:57 +04:00
|
|
|
JS::Rooted<JSObject*> dummy(
|
2015-01-31 18:18:51 +03:00
|
|
|
aCx, JS_NewObject(aCx, IDBObjectStore::DummyPropClass()));
|
2012-06-22 02:27:13 +04:00
|
|
|
if (!dummy) {
|
2014-01-28 04:37:05 +04:00
|
|
|
IDB_REPORT_INTERNAL_ERR();
|
2012-06-22 02:27:13 +04:00
|
|
|
rv = NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!JS_DefineUCProperty(aCx, obj, token.BeginReading(), token.Length(),
|
2014-04-30 13:10:33 +04:00
|
|
|
dummy, JSPROP_ENUMERATE)) {
|
2014-01-28 04:37:05 +04:00
|
|
|
IDB_REPORT_INTERNAL_ERR();
|
2012-06-22 02:27:13 +04: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 :-(
|
2015-02-04 19:20:04 +03:00
|
|
|
JS::ObjectOpResult succeeded;
|
|
|
|
if (!JS_DeleteUCProperty(aCx, targetObject, targetObjectPropName.get(),
|
|
|
|
targetObjectPropName.Length(), succeeded)) {
|
2014-01-28 04:37:05 +04:00
|
|
|
IDB_REPORT_INTERNAL_ERR();
|
2012-06-22 02:27:13 +04:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
2014-01-28 04:37:05 +04:00
|
|
|
IDB_ENSURE_TRUE(succeeded, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
2012-06-22 02:27:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2012-06-22 02:27:13 +04:00
|
|
|
|
2013-07-31 19:48:36 +04:00
|
|
|
// static
|
2014-12-11 21:07:44 +03:00
|
|
|
nsresult KeyPath::Parse(const nsAString& aString, KeyPath* aKeyPath) {
|
2013-07-31 19:48:36 +04:00
|
|
|
KeyPath keyPath(0);
|
|
|
|
keyPath.SetType(STRING);
|
|
|
|
|
2014-12-11 21:07:44 +03:00
|
|
|
if (!keyPath.AppendStringWithValidation(aString)) {
|
2013-07-31 19:48:36 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aKeyPath = keyPath;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2014-12-11 21:07:44 +03:00
|
|
|
nsresult KeyPath::Parse(const Sequence<nsString>& aStrings, KeyPath* aKeyPath) {
|
2013-07-31 19:48:36 +04:00
|
|
|
KeyPath keyPath(0);
|
|
|
|
keyPath.SetType(ARRAY);
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < aStrings.Length(); ++i) {
|
2014-12-11 21:07:44 +03:00
|
|
|
if (!keyPath.AppendStringWithValidation(aStrings[i])) {
|
2013-07-31 19:48:36 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*aKeyPath = keyPath;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-22 02:27:13 +04:00
|
|
|
// static
|
2014-12-11 21:07:44 +03:00
|
|
|
nsresult KeyPath::Parse(const Nullable<OwningStringOrStringSequence>& aValue,
|
|
|
|
KeyPath* aKeyPath) {
|
2012-06-22 02:27:13 +04:00
|
|
|
KeyPath keyPath(0);
|
|
|
|
|
|
|
|
aKeyPath->SetType(NONEXISTENT);
|
|
|
|
|
2014-12-11 21:07:44 +03:00
|
|
|
if (aValue.IsNull()) {
|
|
|
|
*aKeyPath = keyPath;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-06-22 02:27:13 +04:00
|
|
|
|
2014-12-11 21:07:44 +03:00
|
|
|
if (aValue.Value().IsString()) {
|
|
|
|
return Parse(aValue.Value().GetAsString(), aKeyPath);
|
2012-06-22 02:27:13 +04:00
|
|
|
}
|
|
|
|
|
2014-12-11 21:07:44 +03:00
|
|
|
MOZ_ASSERT(aValue.Value().IsStringSequence());
|
2012-06-22 02:27:13 +04:00
|
|
|
|
2014-12-11 21:07:44 +03:00
|
|
|
const Sequence<nsString>& seq = aValue.Value().GetAsStringSequence();
|
|
|
|
if (seq.Length() == 0) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2012-06-22 02:27:13 +04:00
|
|
|
}
|
2014-12-11 21:07:44 +03:00
|
|
|
return Parse(seq, aKeyPath);
|
2012-06-22 02:27:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void KeyPath::SetType(KeyPathType aType) {
|
|
|
|
mType = aType;
|
|
|
|
mStrings.Clear();
|
|
|
|
}
|
|
|
|
|
2014-12-11 21:07:44 +03:00
|
|
|
bool KeyPath::AppendStringWithValidation(const nsAString& aString) {
|
|
|
|
if (!IsValidKeyPathString(aString)) {
|
2012-06-22 02:27:13 +04: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;
|
|
|
|
}
|
|
|
|
|
2018-06-18 08:43:11 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("What?!");
|
2012-06-22 02:27:13 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult KeyPath::ExtractKey(JSContext* aCx, const JS::Value& aValue,
|
|
|
|
Key& aKey) const {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t len = mStrings.Length();
|
2013-05-02 13:12:47 +04:00
|
|
|
JS::Rooted<JS::Value> value(aCx);
|
2012-06-22 02:27:13 +04:00
|
|
|
|
|
|
|
aKey.Unset();
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < len; ++i) {
|
2013-05-02 13:12:47 +04:00
|
|
|
nsresult rv =
|
|
|
|
GetJSValFromKeyPathString(aCx, aValue, mStrings[i], value.address(),
|
2012-07-30 18:20:58 +04:00
|
|
|
DoNotCreateProperties, nullptr, nullptr);
|
2012-06-22 02:27:13 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2019-07-29 12:05:44 +03:00
|
|
|
ErrorResult errorResult;
|
|
|
|
auto result = aKey.AppendItem(aCx, IsArray() && i == 0, value, errorResult);
|
|
|
|
if (!result.Is(Ok, errorResult)) {
|
2012-06-22 02:27:13 +04:00
|
|
|
NS_ASSERTION(aKey.IsUnset(), "Encoding error should unset");
|
2019-07-29 12:05:44 +03:00
|
|
|
errorResult.SuppressException();
|
2012-06-22 02:27:13 +04:00
|
|
|
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 18:20:58 +04:00
|
|
|
DoNotCreateProperties, nullptr, nullptr);
|
2012-06-22 02:27:13 +04:00
|
|
|
}
|
2013-05-02 13:12:47 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
const uint32_t len = mStrings.Length();
|
2014-02-12 14:50:46 +04:00
|
|
|
JS::Rooted<JSObject*> arrayObj(aCx, JS_NewArrayObject(aCx, len));
|
2012-06-22 02:27:13 +04:00
|
|
|
if (!arrayObj) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2013-05-02 13:12:47 +04:00
|
|
|
JS::Rooted<JS::Value> value(aCx);
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < len; ++i) {
|
2013-05-02 13:12:47 +04:00
|
|
|
nsresult rv =
|
|
|
|
GetJSValFromKeyPathString(aCx, aValue, mStrings[i], value.address(),
|
2012-07-30 18:20:58 +04:00
|
|
|
DoNotCreateProperties, nullptr, nullptr);
|
2012-06-22 02:27:13 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-03-02 16:31:15 +03:00
|
|
|
if (!JS_DefineElement(aCx, arrayObj, i, value, JSPROP_ENUMERATE)) {
|
2014-01-28 04:37:05 +04:00
|
|
|
IDB_REPORT_INTERNAL_ERR();
|
2012-06-22 02:27:13 +04:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-01 07:09:46 +03:00
|
|
|
aOutVal->setObject(*arrayObj);
|
2012-06-22 02:27:13 +04:00
|
|
|
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 13:12:47 +04:00
|
|
|
JS::Rooted<JS::Value> value(aCx);
|
2012-06-22 02:27:13 +04:00
|
|
|
|
|
|
|
aKey.Unset();
|
|
|
|
|
2013-05-02 13:12:47 +04:00
|
|
|
nsresult rv =
|
|
|
|
GetJSValFromKeyPathString(aCx, aValue, mStrings[0], value.address(),
|
2012-06-22 02:27:13 +04:00
|
|
|
CreateProperties, aCallback, aClosure);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2019-07-29 12:05:44 +03:00
|
|
|
ErrorResult errorResult;
|
|
|
|
auto result = aKey.AppendItem(aCx, false, value, errorResult);
|
|
|
|
if (!result.Is(Ok, errorResult)) {
|
2012-06-22 02:27:13 +04:00
|
|
|
NS_ASSERTION(aKey.IsUnset(), "Should be unset");
|
2019-07-29 12:05:44 +03:00
|
|
|
errorResult.SuppressException();
|
2014-04-28 06:32:05 +04:00
|
|
|
return value.isUndefined() ? NS_OK : NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
|
2012-06-22 02:27:13 +04: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 19:56:38 +04:00
|
|
|
uint32_t len = mStrings.Length();
|
|
|
|
for (uint32_t i = 0; i < len; ++i) {
|
2014-05-22 07:48:50 +04:00
|
|
|
aString.Append(',');
|
|
|
|
aString.Append(mStrings[i]);
|
2012-06-22 02:27:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-18 08:43:11 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("What?");
|
2012-06-22 02:27:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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());
|
|
|
|
}
|
|
|
|
|
2019-04-17 15:52:46 +03:00
|
|
|
if (tokenizer.separatorAfterCurrentToken()) {
|
|
|
|
// There is a trailing comma, indicating the original KeyPath has
|
|
|
|
// a trailing empty string, i.e. [..., '']. We should append this
|
|
|
|
// empty string.
|
|
|
|
keyPath.mStrings.AppendElement(nsString{});
|
|
|
|
}
|
|
|
|
|
2012-06-22 02:27:13 +04:00
|
|
|
return keyPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
keyPath.SetType(STRING);
|
|
|
|
keyPath.mStrings.AppendElement(aString);
|
|
|
|
|
|
|
|
return keyPath;
|
|
|
|
}
|
|
|
|
|
2013-06-18 14:00:37 +04:00
|
|
|
nsresult KeyPath::ToJSVal(JSContext* aCx,
|
|
|
|
JS::MutableHandle<JS::Value> aValue) const {
|
2012-06-22 02:27:13 +04:00
|
|
|
if (IsArray()) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t len = mStrings.Length();
|
2014-02-12 14:50:46 +04:00
|
|
|
JS::Rooted<JSObject*> array(aCx, JS_NewArrayObject(aCx, len));
|
2012-06-22 02:27:13 +04:00
|
|
|
if (!array) {
|
2014-01-28 04:37:05 +04:00
|
|
|
IDB_WARNING("Failed to make array!");
|
2012-06-22 02:27:13 +04:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < len; ++i) {
|
2013-05-02 13:12:47 +04:00
|
|
|
JS::Rooted<JS::Value> val(aCx);
|
2012-06-22 02:27:13 +04:00
|
|
|
nsString tmp(mStrings[i]);
|
2013-10-12 09:02:39 +04:00
|
|
|
if (!xpc::StringToJsval(aCx, tmp, &val)) {
|
2014-01-28 04:37:05 +04:00
|
|
|
IDB_REPORT_INTERNAL_ERR();
|
2012-06-22 02:27:13 +04:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
|
2015-03-02 16:31:15 +03:00
|
|
|
if (!JS_DefineElement(aCx, array, i, val, JSPROP_ENUMERATE)) {
|
2014-01-28 04:37:05 +04:00
|
|
|
IDB_REPORT_INTERNAL_ERR();
|
2012-06-22 02:27:13 +04:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-18 14:00:37 +04:00
|
|
|
aValue.setObject(*array);
|
2012-06-22 02:27:13 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsString()) {
|
|
|
|
nsString tmp(mStrings[0]);
|
2013-10-12 09:02:39 +04:00
|
|
|
if (!xpc::StringToJsval(aCx, tmp, aValue)) {
|
2014-01-28 04:37:05 +04:00
|
|
|
IDB_REPORT_INTERNAL_ERR();
|
2012-06-22 02:27:13 +04:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-06-18 14:00:37 +04:00
|
|
|
aValue.setNull();
|
2012-06-22 02:27:13 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-06-18 14:00:37 +04: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-22 02:27:13 +04: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-27 03:21:57 +04:00
|
|
|
|
|
|
|
} // namespace indexedDB
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|