Files
palemoon27/storage/mozStorageAsyncStatementJSHelper.cpp
T
roytam1 07bb947214 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1164463 - Clean up MediaManager shutdown to be reliable and avoid holding locks while Joining a thread. r=jib (636e2e5dc)
- missing part of Bug 1154389 - Stop leaking DeviceSuccessCallbackRunnable objects (2f8906119)
- Bug 1169665 - Have enumerateDevices return empty array on zero devices instead of fail. r=jesup (f83fcb269)
- Bug 1162720 - enumerateDevices visits main thread for profileDir. r=jesup (20687dcb7)
- Bug 1173255 - Cleanup MediaManager e10s code in prep for deviceId constraint. r=jesup (43496fe28)
- Bug 1136110 - Define OS.Constants.Sys.bits. r=yoric (384b01680)
- Bug 1177892 part 4 - Remove INT_TO_JSVAL. r=evilpie (70fc1c3e7)
- Bug 1177892 part 5 - Remove DOUBLE_TO_JSVAL. r=evilpie (a6943687b)
- Bug 1177892 part 6 - Remove UINT_TO_JSVAL. r=evilpie (10fa41862)
- Bug 1184564 part 1 - Use Value instead of jsval in XPConnect. r=bholley (a6e63ec42)
- Bug 1184564 part 2 - Use Value instead of jsval in dom/ and storage/. r=bz (e50a374cf)
- Bug 1184564 part 3 - Use Value instead of jsval in CTypes. r=arai (7f426bbdd)
- Bug 1155618 - Add better support for testing OOM behaviour r=terrence (382c1005e)
- fix mispatch of Bug 1092544 - Use assertRecoveredOnBailout in the test suite. (eba837c73)
- Bug 1138265 - TraceLogger: Throw more errors, r=bbouvier ON CLOSED TREE (02edf22d4)
- Bug 1184564 part 4 - Use Value instead of jsval in SpiderMonkey. r=evilpie (52f751286)
- Bug 1184564 part 5 - Remove jsval typedef. r=jorendorff (f08006461)
2021-04-06 11:38:48 +08:00

152 lines
4.9 KiB
C++

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
* 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 "nsIXPConnect.h"
#include "mozStorageAsyncStatement.h"
#include "mozStorageService.h"
#include "nsMemory.h"
#include "nsString.h"
#include "nsServiceManagerUtils.h"
#include "mozStorageAsyncStatementJSHelper.h"
#include "mozStorageAsyncStatementParams.h"
#include "jsapi.h"
namespace mozilla {
namespace storage {
////////////////////////////////////////////////////////////////////////////////
//// AsyncStatementJSHelper
nsresult
AsyncStatementJSHelper::getParams(AsyncStatement *aStatement,
JSContext *aCtx,
JSObject *aScopeObj,
JS::Value *_params)
{
MOZ_ASSERT(NS_IsMainThread());
nsresult rv;
#ifdef DEBUG
int32_t state;
(void)aStatement->GetState(&state);
NS_ASSERTION(state == mozIStorageAsyncStatement::MOZ_STORAGE_STATEMENT_READY,
"Invalid state to get the params object - all calls will fail!");
#endif
if (!aStatement->mStatementParamsHolder) {
nsCOMPtr<mozIStorageStatementParams> params =
new AsyncStatementParams(aStatement);
NS_ENSURE_TRUE(params, NS_ERROR_OUT_OF_MEMORY);
JS::RootedObject scope(aCtx, aScopeObj);
nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
nsCOMPtr<nsIXPConnect> xpc(Service::getXPConnect());
rv = xpc->WrapNativeHolder(
aCtx,
::JS_GetGlobalForObject(aCtx, scope),
params,
NS_GET_IID(mozIStorageStatementParams),
getter_AddRefs(holder)
);
NS_ENSURE_SUCCESS(rv, rv);
nsRefPtr<AsyncStatementParamsHolder> paramsHolder =
new AsyncStatementParamsHolder(holder);
aStatement->mStatementParamsHolder =
new nsMainThreadPtrHolder<nsIXPConnectJSObjectHolder>(paramsHolder);
}
JS::Rooted<JSObject*> obj(aCtx);
obj = aStatement->mStatementParamsHolder->GetJSObject();
NS_ENSURE_STATE(obj);
_params->setObject(*obj);
return NS_OK;
}
NS_IMETHODIMP_(MozExternalRefCountType) AsyncStatementJSHelper::AddRef() { return 2; }
NS_IMETHODIMP_(MozExternalRefCountType) AsyncStatementJSHelper::Release() { return 1; }
NS_INTERFACE_MAP_BEGIN(AsyncStatementJSHelper)
NS_INTERFACE_MAP_ENTRY(nsIXPCScriptable)
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END
////////////////////////////////////////////////////////////////////////////////
//// nsIXPCScriptable
#define XPC_MAP_CLASSNAME AsyncStatementJSHelper
#define XPC_MAP_QUOTED_CLASSNAME "AsyncStatementJSHelper"
#define XPC_MAP_WANT_GETPROPERTY
#define XPC_MAP_FLAGS nsIXPCScriptable::ALLOW_PROP_MODS_DURING_RESOLVE
#include "xpc_map_end.h"
NS_IMETHODIMP
AsyncStatementJSHelper::GetProperty(nsIXPConnectWrappedNative *aWrapper,
JSContext *aCtx,
JSObject *aScopeObj,
jsid aId,
JS::Value *_result,
bool *_retval)
{
if (!JSID_IS_STRING(aId))
return NS_OK;
// Cast to async via mozI* since direct from nsISupports is ambiguous.
JS::RootedObject scope(aCtx, aScopeObj);
JS::RootedId id(aCtx, aId);
mozIStorageAsyncStatement *iAsyncStmt =
static_cast<mozIStorageAsyncStatement *>(aWrapper->Native());
AsyncStatement *stmt = static_cast<AsyncStatement *>(iAsyncStmt);
#ifdef DEBUG
{
nsISupports *supp = aWrapper->Native();
nsCOMPtr<mozIStorageAsyncStatement> isStatement(do_QueryInterface(supp));
NS_ASSERTION(isStatement, "How is this not an async statement?!");
}
#endif
if (::JS_FlatStringEqualsAscii(JSID_TO_FLAT_STRING(id), "params"))
return getParams(stmt, aCtx, scope, _result);
return NS_OK;
}
////////////////////////////////////////////////////////////////////////////////
//// AsyncStatementParamsHolder
NS_IMPL_ISUPPORTS(AsyncStatementParamsHolder, nsIXPConnectJSObjectHolder);
JSObject*
AsyncStatementParamsHolder::GetJSObject()
{
return mHolder->GetJSObject();
}
AsyncStatementParamsHolder::AsyncStatementParamsHolder(nsIXPConnectJSObjectHolder* aHolder)
: mHolder(aHolder)
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(mHolder);
}
AsyncStatementParamsHolder::~AsyncStatementParamsHolder()
{
MOZ_ASSERT(NS_IsMainThread());
// We are considered dead at this point, so any wrappers for row or params
// need to lose their reference to the statement.
nsCOMPtr<nsIXPConnectWrappedNative> wrapper = do_QueryInterface(mHolder);
nsCOMPtr<mozIStorageStatementParams> iObj = do_QueryWrappedNative(wrapper);
AsyncStatementParams *obj = static_cast<AsyncStatementParams *>(iObj.get());
obj->mStatement = nullptr;
}
} // namespace storage
} // namespace mozilla