Bug 662930: Consolidate IDB structured clone serializations/deserializations. r=bent

This commit is contained in:
Kyle Huey 2011-06-08 19:21:53 -07:00
Родитель 858825860d
Коммит f8960cdcf7
6 изменённых файлов: 57 добавлений и 49 удалений

Просмотреть файл

@ -103,7 +103,7 @@ ConvertCloneBuffersToArrayInternal(
JSAutoStructuredCloneBuffer& buffer = aBuffers[index];
jsval val;
if (!buffer.read(&val, aCx)) {
if (!IDBObjectStore::DeserializeValue(aCx, buffer, &val)) {
NS_WARNING("Failed to decode!");
return NS_ERROR_DOM_DATA_CLONE_ERR;
}
@ -491,35 +491,6 @@ AsyncConnectionHelper::WrapNative(JSContext* aCx,
return NS_OK;
}
// static
nsresult
AsyncConnectionHelper::ConvertCloneBufferToJSVal(
JSContext* aCx,
JSAutoStructuredCloneBuffer& aBuffer,
jsval* aResult)
{
NS_ASSERTION(aCx, "Null context!");
NS_ASSERTION(aResult, "Null pointer!");
JSAutoRequest ar(aCx);
if (aBuffer.data()) {
JSBool ok = aBuffer.read(aResult, aCx);
aBuffer.clear(aCx);
if (!ok) {
NS_ERROR("Failed to decode!");
return NS_ERROR_DOM_DATA_CLONE_ERR;
}
}
else {
*aResult = JSVAL_VOID;
}
return NS_OK;
}
// static
nsresult
AsyncConnectionHelper::ConvertCloneBuffersToArray(

Просмотреть файл

@ -165,14 +165,6 @@ protected:
nsISupports* aNative,
jsval* aResult);
/**
* Helper to decode a clone buffer to a jsval.
*/
static nsresult ConvertCloneBufferToJSVal(
JSContext* aCx,
JSAutoStructuredCloneBuffer& aBuffer,
jsval* aResult);
/**
* Helper to make a JS array object out of an array of clone buffers.
*/

Просмотреть файл

@ -470,9 +470,7 @@ IDBCursor::GetValue(JSContext* aCx,
mRooted = true;
}
JSAutoRequest ar(aCx);
if (!mCloneBuffer.read(&mCachedValue, aCx)) {
if (!IDBObjectStore::DeserializeValue(aCx, mCloneBuffer, &mCachedValue)) {
mCachedValue = JSVAL_VOID;
return NS_ERROR_DOM_DATA_CLONE_ERR;
}

Просмотреть файл

@ -780,11 +780,11 @@ nsresult
GetHelper::GetSuccessResult(JSContext* aCx,
jsval* aVal)
{
nsresult rv = ConvertCloneBufferToJSVal(aCx, mCloneBuffer, aVal);
bool result = IDBObjectStore::DeserializeValue(aCx, mCloneBuffer, aVal);
mCloneBuffer.clear(aCx);
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_TRUE(result, NS_ERROR_FAILURE);
return NS_OK;
}

Просмотреть файл

@ -834,6 +834,43 @@ IDBObjectStore::ClearStructuredCloneBuffer(JSAutoStructuredCloneBuffer& aBuffer)
}
}
// static
bool
IDBObjectStore::DeserializeValue(JSContext* aCx,
JSAutoStructuredCloneBuffer& aBuffer,
jsval* aValue)
{
/*
* This function can be called on multiple threads! Be careful!
*/
NS_ASSERTION(aCx, "A JSContext is required!");
if (!aBuffer.data()) {
*aValue = JSVAL_VOID;
return true;
}
JSAutoRequest ar(aCx);
return aBuffer.read(aValue, aCx, nsnull);
}
// static
bool
IDBObjectStore::SerializeValue(JSContext* aCx,
JSAutoStructuredCloneBuffer& aBuffer,
jsval aValue)
{
/*
* This function can be called on multiple threads! Be careful!
*/
NS_ASSERTION(aCx, "A JSContext is required!");
JSAutoRequest ar(aCx);
return aBuffer.write(aCx, aValue, nsnull);
}
IDBObjectStore::IDBObjectStore()
: mId(LL_MININT),
mAutoIncrement(PR_FALSE)
@ -895,7 +932,7 @@ IDBObjectStore::GetAddInfo(JSContext* aCx,
rv = GetIndexUpdateInfo(info, aCx, aValue, aUpdateInfoArray);
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
if (!aCloneBuffer.write(aCx, aValue)) {
if (!IDBObjectStore::SerializeValue(aCx, aCloneBuffer, aValue)) {
return NS_ERROR_DOM_DATA_CLONE_ERR;
}
@ -1750,14 +1787,14 @@ AddHelper::ModifyValueForNewKey()
const nsString& keyPath = mObjectStore->KeyPath();
JSContext* cx;
JSContext* cx = nsnull;
nsresult rv = nsContentUtils::ThreadJSContextStack()->GetSafeJSContext(&cx);
NS_ENSURE_SUCCESS(rv, rv);
JSAutoRequest ar(cx);
jsval clone;
if (!mCloneBuffer.read(&clone, cx)) {
if (!IDBObjectStore::DeserializeValue(cx, mCloneBuffer, &clone)) {
return NS_ERROR_DOM_DATA_CLONE_ERR;
}
@ -1785,7 +1822,7 @@ AddHelper::ModifyValueForNewKey()
nsnull, JSPROP_ENUMERATE);
NS_ENSURE_TRUE(ok, NS_ERROR_FAILURE);
if (!mCloneBuffer.write(cx, OBJECT_TO_JSVAL(obj))) {
if (!IDBObjectStore::SerializeValue(cx, mCloneBuffer, OBJECT_TO_JSVAL(obj))) {
return NS_ERROR_DOM_DATA_CLONE_ERR;
}
@ -1840,11 +1877,11 @@ nsresult
GetHelper::GetSuccessResult(JSContext* aCx,
jsval* aVal)
{
nsresult rv = ConvertCloneBufferToJSVal(aCx, mCloneBuffer, aVal);
bool result = IDBObjectStore::DeserializeValue(aCx, mCloneBuffer, aVal);
mCloneBuffer.clear(aCx);
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_TRUE(result, NS_ERROR_FAILURE);
return NS_OK;
}

Просмотреть файл

@ -116,6 +116,16 @@ public:
static void
ClearStructuredCloneBuffer(JSAutoStructuredCloneBuffer& aBuffer);
static bool
DeserializeValue(JSContext* aCx,
JSAutoStructuredCloneBuffer& aBuffer,
jsval* aValue);
static bool
SerializeValue(JSContext* aCx,
JSAutoStructuredCloneBuffer& aBuffer,
jsval aValue);
const nsString& Name() const
{
return mName;