2010-11-11 02:26:00 +03: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 15:12:37 +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/. */
|
2010-11-11 02:26:00 +03:00
|
|
|
|
|
|
|
#ifndef mozilla_dom_indexeddb_key_h__
|
|
|
|
#define mozilla_dom_indexeddb_key_h__
|
|
|
|
|
|
|
|
#include "mozilla/dom/indexedDB/IndexedDatabase.h"
|
|
|
|
|
2011-11-03 19:57:30 +04:00
|
|
|
#include "mozIStorageStatement.h"
|
|
|
|
|
2013-08-20 10:43:47 +04:00
|
|
|
#include "js/Value.h"
|
|
|
|
|
2012-06-01 21:21:12 +04:00
|
|
|
namespace IPC {
|
|
|
|
template <typename T> struct ParamTraits;
|
|
|
|
} // namespace IPC
|
|
|
|
|
2010-11-11 02:26:00 +03:00
|
|
|
BEGIN_INDEXEDDB_NAMESPACE
|
|
|
|
|
|
|
|
class Key
|
|
|
|
{
|
2012-06-01 21:21:12 +04:00
|
|
|
friend struct IPC::ParamTraits<Key>;
|
|
|
|
|
2010-11-11 02:26:00 +03:00
|
|
|
public:
|
|
|
|
Key()
|
|
|
|
{
|
2011-11-03 19:57:30 +04:00
|
|
|
Unset();
|
2010-11-11 02:26:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Key& operator=(const nsAString& aString)
|
|
|
|
{
|
2011-11-03 19:57:30 +04:00
|
|
|
SetFromString(aString);
|
2010-11-11 02:26:00 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
Key& operator=(int64_t aInt)
|
2010-11-11 02:26:00 +03:00
|
|
|
{
|
2011-11-03 19:57:30 +04:00
|
|
|
SetFromInteger(aInt);
|
2010-11-11 02:26:00 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const Key& aOther) const
|
|
|
|
{
|
2011-12-20 14:58:44 +04:00
|
|
|
NS_ASSERTION(!mBuffer.IsVoid() && !aOther.mBuffer.IsVoid(),
|
2011-11-03 19:57:30 +04:00
|
|
|
"Don't compare unset keys!");
|
|
|
|
|
2011-12-20 14:58:44 +04:00
|
|
|
return mBuffer.Equals(aOther.mBuffer);
|
2010-11-11 02:26:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const Key& aOther) const
|
|
|
|
{
|
2011-12-20 14:58:44 +04:00
|
|
|
NS_ASSERTION(!mBuffer.IsVoid() && !aOther.mBuffer.IsVoid(),
|
|
|
|
"Don't compare unset keys!");
|
|
|
|
|
|
|
|
return !mBuffer.Equals(aOther.mBuffer);
|
2010-11-11 02:26:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<(const Key& aOther) const
|
|
|
|
{
|
2011-12-20 14:58:44 +04:00
|
|
|
NS_ASSERTION(!mBuffer.IsVoid() && !aOther.mBuffer.IsVoid(),
|
2011-11-03 19:57:30 +04:00
|
|
|
"Don't compare unset keys!");
|
2011-11-03 05:57:48 +04:00
|
|
|
|
2011-12-20 14:58:44 +04:00
|
|
|
return Compare(mBuffer, aOther.mBuffer) < 0;
|
2010-11-11 02:26:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator>(const Key& aOther) const
|
|
|
|
{
|
2011-12-20 14:58:44 +04:00
|
|
|
NS_ASSERTION(!mBuffer.IsVoid() && !aOther.mBuffer.IsVoid(),
|
|
|
|
"Don't compare unset keys!");
|
|
|
|
|
|
|
|
return Compare(mBuffer, aOther.mBuffer) > 0;
|
2010-11-11 02:26:00 +03:00
|
|
|
}
|
|
|
|
|
2010-12-10 05:15:00 +03:00
|
|
|
bool operator<=(const Key& aOther) const
|
|
|
|
{
|
2011-12-20 14:58:44 +04:00
|
|
|
NS_ASSERTION(!mBuffer.IsVoid() && !aOther.mBuffer.IsVoid(),
|
|
|
|
"Don't compare unset keys!");
|
|
|
|
|
|
|
|
return Compare(mBuffer, aOther.mBuffer) <= 0;
|
2010-12-10 05:15:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator>=(const Key& aOther) const
|
|
|
|
{
|
2011-12-20 14:58:44 +04:00
|
|
|
NS_ASSERTION(!mBuffer.IsVoid() && !aOther.mBuffer.IsVoid(),
|
|
|
|
"Don't compare unset keys!");
|
|
|
|
|
|
|
|
return Compare(mBuffer, aOther.mBuffer) >= 0;
|
2010-12-10 05:15:00 +03:00
|
|
|
}
|
|
|
|
|
2011-11-03 19:57:30 +04:00
|
|
|
void
|
|
|
|
Unset()
|
|
|
|
{
|
2011-12-20 14:58:44 +04:00
|
|
|
mBuffer.SetIsVoid(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsUnset() const
|
|
|
|
{
|
|
|
|
return mBuffer.IsVoid();
|
2011-11-03 19:57:30 +04:00
|
|
|
}
|
|
|
|
|
2011-12-20 14:58:44 +04:00
|
|
|
bool IsFloat() const
|
|
|
|
{
|
2013-03-16 10:58:50 +04:00
|
|
|
return !IsUnset() && mBuffer.First() == eFloat;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsDate() const
|
|
|
|
{
|
|
|
|
return !IsUnset() && mBuffer.First() == eDate;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsString() const
|
|
|
|
{
|
|
|
|
return !IsUnset() && mBuffer.First() == eString;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsArray() const
|
|
|
|
{
|
|
|
|
return !IsUnset() && mBuffer.First() >= eArray;
|
2011-12-20 14:58:44 +04:00
|
|
|
}
|
2011-11-03 19:57:30 +04:00
|
|
|
|
2011-12-20 14:58:44 +04:00
|
|
|
double ToFloat() const
|
2011-11-03 19:57:30 +04:00
|
|
|
{
|
2011-12-20 14:58:44 +04:00
|
|
|
NS_ASSERTION(IsFloat(), "Why'd you call this?");
|
|
|
|
const unsigned char* pos = BufferStart();
|
|
|
|
double res = DecodeNumber(pos, BufferEnd());
|
|
|
|
NS_ASSERTION(pos >= BufferEnd(), "Should consume whole buffer");
|
|
|
|
return res;
|
2011-11-03 19:57:30 +04:00
|
|
|
}
|
|
|
|
|
2013-03-16 10:58:50 +04:00
|
|
|
double ToDateMsec() const
|
|
|
|
{
|
|
|
|
NS_ASSERTION(IsDate(), "Why'd you call this?");
|
|
|
|
const unsigned char* pos = BufferStart();
|
|
|
|
double res = DecodeNumber(pos, BufferEnd());
|
|
|
|
NS_ASSERTION(pos >= BufferEnd(), "Should consume whole buffer");
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToString(nsString& aString) const
|
|
|
|
{
|
|
|
|
NS_ASSERTION(IsString(), "Why'd you call this?");
|
|
|
|
const unsigned char* pos = BufferStart();
|
|
|
|
DecodeString(pos, BufferEnd(), aString);
|
|
|
|
NS_ASSERTION(pos >= BufferEnd(), "Should consume whole buffer");
|
|
|
|
}
|
|
|
|
|
2011-12-20 14:58:44 +04:00
|
|
|
void SetFromString(const nsAString& aString)
|
2011-11-03 19:57:30 +04:00
|
|
|
{
|
2011-12-20 14:58:44 +04:00
|
|
|
mBuffer.Truncate();
|
|
|
|
EncodeString(aString, 0);
|
|
|
|
TrimBuffer();
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
void SetFromInteger(int64_t aInt)
|
2011-12-20 14:58:44 +04:00
|
|
|
{
|
|
|
|
mBuffer.Truncate();
|
|
|
|
EncodeNumber(double(aInt), eFloat);
|
|
|
|
TrimBuffer();
|
2011-11-03 19:57:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult SetFromJSVal(JSContext* aCx,
|
2014-01-23 23:49:40 +04:00
|
|
|
JS::Handle<JS::Value> aVal)
|
2011-11-03 19:57:30 +04:00
|
|
|
{
|
2011-12-20 14:58:44 +04:00
|
|
|
mBuffer.Truncate();
|
2011-11-03 19:57:30 +04:00
|
|
|
|
2014-01-23 23:49:40 +04:00
|
|
|
if (aVal.isNull() || aVal.isUndefined()) {
|
2011-12-20 14:58:44 +04:00
|
|
|
Unset();
|
2011-11-03 19:57:30 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-12-20 14:58:44 +04:00
|
|
|
nsresult rv = EncodeJSVal(aCx, aVal, 0);
|
|
|
|
if (NS_FAILED(rv)) {
|
2011-11-03 19:57:30 +04:00
|
|
|
Unset();
|
2011-12-20 14:58:44 +04:00
|
|
|
return rv;
|
2011-11-03 19:57:30 +04:00
|
|
|
}
|
2011-12-20 14:58:44 +04:00
|
|
|
TrimBuffer();
|
2011-11-03 19:57:30 +04:00
|
|
|
|
2011-12-20 14:58:44 +04:00
|
|
|
return NS_OK;
|
2011-11-03 19:57:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult ToJSVal(JSContext* aCx,
|
2013-06-18 14:00:37 +04:00
|
|
|
JS::MutableHandle<JS::Value> aVal) const
|
2011-11-03 19:57:30 +04:00
|
|
|
{
|
2011-12-20 14:58:44 +04:00
|
|
|
if (IsUnset()) {
|
2014-01-23 23:49:40 +04:00
|
|
|
aVal.setUndefined();
|
2011-12-20 14:58:44 +04:00
|
|
|
return NS_OK;
|
2011-11-03 19:57:30 +04:00
|
|
|
}
|
2010-11-11 02:26:00 +03:00
|
|
|
|
2011-12-20 14:58:44 +04:00
|
|
|
const unsigned char* pos = BufferStart();
|
|
|
|
nsresult rv = DecodeJSVal(pos, BufferEnd(), aCx, 0, aVal);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
NS_ASSERTION(pos >= BufferEnd(),
|
|
|
|
"Didn't consume whole buffer");
|
|
|
|
|
|
|
|
return NS_OK;
|
2010-11-11 02:26:00 +03:00
|
|
|
}
|
|
|
|
|
2013-06-18 14:00:37 +04:00
|
|
|
nsresult ToJSVal(JSContext* aCx,
|
|
|
|
JS::Heap<JS::Value>& aVal) const
|
|
|
|
{
|
|
|
|
JS::Rooted<JS::Value> value(aCx);
|
|
|
|
nsresult rv = ToJSVal(aCx, &value);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
aVal = value;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-06-22 02:27:13 +04:00
|
|
|
nsresult AppendItem(JSContext* aCx,
|
|
|
|
bool aFirstOfArray,
|
2014-01-23 23:49:40 +04:00
|
|
|
JS::Handle<JS::Value> aVal)
|
2011-12-20 14:59:07 +04:00
|
|
|
{
|
2012-06-22 02:27:13 +04:00
|
|
|
nsresult rv = EncodeJSVal(aCx, aVal, aFirstOfArray ? eMaxType : 0);
|
2011-12-20 14:59:07 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
Unset();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FinishArray()
|
|
|
|
{
|
|
|
|
TrimBuffer();
|
|
|
|
}
|
|
|
|
|
2011-12-20 14:58:44 +04:00
|
|
|
const nsCString& GetBuffer() const
|
2011-11-03 19:57:30 +04:00
|
|
|
{
|
2011-12-20 14:58:44 +04:00
|
|
|
return mBuffer;
|
2010-11-11 02:26:00 +03:00
|
|
|
}
|
|
|
|
|
2011-11-03 19:57:30 +04:00
|
|
|
nsresult BindToStatement(mozIStorageStatement* aStatement,
|
|
|
|
const nsACString& aParamName) const
|
|
|
|
{
|
2011-12-20 14:58:44 +04:00
|
|
|
nsresult rv = aStatement->BindBlobByName(aParamName,
|
2012-08-22 19:56:38 +04:00
|
|
|
reinterpret_cast<const uint8_t*>(mBuffer.get()), mBuffer.Length());
|
2011-11-03 19:57:30 +04:00
|
|
|
|
|
|
|
return NS_SUCCEEDED(rv) ? NS_OK : NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2010-11-11 02:26:00 +03:00
|
|
|
}
|
|
|
|
|
2011-11-03 19:57:30 +04:00
|
|
|
nsresult SetFromStatement(mozIStorageStatement* aStatement,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aIndex)
|
2011-11-03 19:57:30 +04:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t* data;
|
|
|
|
uint32_t dataLength = 0;
|
2011-12-20 14:58:44 +04:00
|
|
|
|
|
|
|
nsresult rv = aStatement->GetBlob(aIndex, &dataLength, &data);
|
2011-11-03 19:57:30 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
|
|
|
|
|
2011-12-20 14:58:44 +04:00
|
|
|
mBuffer.Adopt(
|
2012-08-22 19:56:38 +04:00
|
|
|
reinterpret_cast<char*>(const_cast<uint8_t*>(data)), dataLength);
|
2011-11-03 19:57:30 +04:00
|
|
|
|
2011-12-20 14:58:44 +04:00
|
|
|
return NS_OK;
|
2011-11-03 19:57:30 +04:00
|
|
|
}
|
|
|
|
|
2011-12-20 14:58:44 +04:00
|
|
|
static
|
2012-08-22 19:56:38 +04:00
|
|
|
int16_t CompareKeys(Key& aFirst, Key& aSecond)
|
2011-11-03 19:57:30 +04:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t result = Compare(aFirst.mBuffer, aSecond.mBuffer);
|
2011-11-03 19:57:30 +04:00
|
|
|
|
2011-12-20 14:58:44 +04:00
|
|
|
if (result < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2011-11-03 19:57:30 +04:00
|
|
|
|
2011-12-20 14:58:44 +04:00
|
|
|
if (result > 0) {
|
|
|
|
return 1;
|
2011-11-03 19:57:30 +04:00
|
|
|
}
|
|
|
|
|
2011-12-20 14:58:44 +04:00
|
|
|
return 0;
|
2010-11-11 02:26:00 +03:00
|
|
|
}
|
|
|
|
|
2011-12-20 14:58:44 +04:00
|
|
|
private:
|
|
|
|
const unsigned char* BufferStart() const
|
2011-11-03 19:57:30 +04:00
|
|
|
{
|
2011-12-20 14:58:44 +04:00
|
|
|
return reinterpret_cast<const unsigned char*>(mBuffer.BeginReading());
|
2010-11-11 02:26:00 +03:00
|
|
|
}
|
|
|
|
|
2011-12-20 14:58:44 +04:00
|
|
|
const unsigned char* BufferEnd() const
|
|
|
|
{
|
|
|
|
return reinterpret_cast<const unsigned char*>(mBuffer.EndReading());
|
|
|
|
}
|
|
|
|
|
|
|
|
enum {
|
|
|
|
eTerminator = 0,
|
|
|
|
eFloat = 1,
|
|
|
|
eDate = 2,
|
|
|
|
eString = 3,
|
|
|
|
eArray = 4,
|
|
|
|
eMaxType = eArray
|
2011-11-03 19:57:30 +04:00
|
|
|
};
|
|
|
|
|
2011-12-20 14:58:44 +04:00
|
|
|
// Encoding helper. Trims trailing zeros off of mBuffer as a post-processing
|
|
|
|
// step.
|
|
|
|
void TrimBuffer()
|
|
|
|
{
|
|
|
|
const char* end = mBuffer.EndReading() - 1;
|
|
|
|
while (!*end) {
|
|
|
|
--end;
|
|
|
|
}
|
2011-11-03 19:57:30 +04:00
|
|
|
|
2011-12-20 14:58:44 +04:00
|
|
|
mBuffer.Truncate(end + 1 - mBuffer.BeginReading());
|
|
|
|
}
|
2011-11-03 19:57:30 +04:00
|
|
|
|
2011-12-20 14:58:44 +04:00
|
|
|
// Encoding functions. These append the encoded value to the end of mBuffer
|
2014-01-23 23:49:40 +04:00
|
|
|
inline nsresult EncodeJSVal(JSContext* aCx, JS::Handle<JS::Value> aVal,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t aTypeOffset)
|
2012-05-11 21:47:27 +04:00
|
|
|
{
|
|
|
|
return EncodeJSValInternal(aCx, aVal, aTypeOffset, 0);
|
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
void EncodeString(const nsAString& aString, uint8_t aTypeOffset);
|
|
|
|
void EncodeNumber(double aFloat, uint8_t aType);
|
2011-12-20 14:58:44 +04:00
|
|
|
|
|
|
|
// Decoding functions. aPos points into mBuffer and is adjusted to point
|
|
|
|
// past the consumed value.
|
2012-05-11 21:47:27 +04:00
|
|
|
static inline nsresult DecodeJSVal(const unsigned char*& aPos,
|
|
|
|
const unsigned char* aEnd, JSContext* aCx,
|
2013-06-18 14:00:37 +04:00
|
|
|
uint8_t aTypeOffset, JS::MutableHandle<JS::Value> aVal)
|
2012-05-11 21:47:27 +04:00
|
|
|
{
|
|
|
|
return DecodeJSValInternal(aPos, aEnd, aCx, aTypeOffset, aVal, 0);
|
|
|
|
}
|
|
|
|
|
2011-12-20 14:58:44 +04:00
|
|
|
static void DecodeString(const unsigned char*& aPos,
|
|
|
|
const unsigned char* aEnd,
|
|
|
|
nsString& aString);
|
|
|
|
static double DecodeNumber(const unsigned char*& aPos,
|
|
|
|
const unsigned char* aEnd);
|
|
|
|
|
|
|
|
nsCString mBuffer;
|
2012-05-11 21:47:27 +04:00
|
|
|
|
|
|
|
private:
|
2014-01-23 23:49:40 +04:00
|
|
|
nsresult EncodeJSValInternal(JSContext* aCx, JS::Handle<JS::Value> aVal,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t aTypeOffset, uint16_t aRecursionDepth);
|
2012-05-11 21:47:27 +04:00
|
|
|
|
|
|
|
static nsresult DecodeJSValInternal(const unsigned char*& aPos,
|
|
|
|
const unsigned char* aEnd,
|
2012-08-22 19:56:38 +04:00
|
|
|
JSContext* aCx, uint8_t aTypeOffset,
|
2013-06-18 14:00:37 +04:00
|
|
|
JS::MutableHandle<JS::Value> aVal, uint16_t aRecursionDepth);
|
2010-11-11 02:26:00 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
END_INDEXEDDB_NAMESPACE
|
|
|
|
|
|
|
|
#endif /* mozilla_dom_indexeddb_key_h__ */
|