2010-03-24 10:32:40 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
|
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-03-24 10:32:40 +03:00
|
|
|
|
|
|
|
#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"
|
|
|
|
|
2016-12-14 00:33:46 +03:00
|
|
|
#include "xpc_make_class.h"
|
|
|
|
|
2010-03-24 10:32:40 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace storage {
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// AsyncStatementJSHelper
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
AsyncStatementJSHelper::getParams(AsyncStatement *aStatement,
|
|
|
|
JSContext *aCtx,
|
|
|
|
JSObject *aScopeObj,
|
2015-07-18 22:45:35 +03:00
|
|
|
JS::Value *_params)
|
2010-03-24 10:32:40 +03:00
|
|
|
{
|
2015-03-12 19:55:56 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2010-03-24 10:32:40 +03:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t state;
|
2010-03-24 10:32:40 +03:00
|
|
|
(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);
|
|
|
|
|
2013-05-12 17:10:39 +04:00
|
|
|
JS::RootedObject scope(aCtx, aScopeObj);
|
2015-03-12 19:55:56 +03:00
|
|
|
nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
|
2010-03-24 10:32:40 +03:00
|
|
|
nsCOMPtr<nsIXPConnect> xpc(Service::getXPConnect());
|
2015-07-01 21:17:17 +03:00
|
|
|
rv = xpc->WrapNativeHolder(
|
2010-03-24 10:32:40 +03:00
|
|
|
aCtx,
|
2013-05-12 17:10:39 +04:00
|
|
|
::JS_GetGlobalForObject(aCtx, scope),
|
2010-03-24 10:32:40 +03:00
|
|
|
params,
|
|
|
|
NS_GET_IID(mozIStorageStatementParams),
|
2015-03-12 19:55:56 +03:00
|
|
|
getter_AddRefs(holder)
|
2010-03-24 10:32:40 +03:00
|
|
|
);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<AsyncStatementParamsHolder> paramsHolder =
|
2015-03-12 19:55:56 +03:00
|
|
|
new AsyncStatementParamsHolder(holder);
|
|
|
|
aStatement->mStatementParamsHolder =
|
|
|
|
new nsMainThreadPtrHolder<nsIXPConnectJSObjectHolder>(paramsHolder);
|
2010-03-24 10:32:40 +03:00
|
|
|
}
|
|
|
|
|
2013-05-03 06:02:40 +04:00
|
|
|
JS::Rooted<JSObject*> obj(aCtx);
|
2013-05-20 16:44:18 +04:00
|
|
|
obj = aStatement->mStatementParamsHolder->GetJSObject();
|
|
|
|
NS_ENSURE_STATE(obj);
|
2010-03-24 10:32:40 +03:00
|
|
|
|
2015-07-01 07:09:46 +03:00
|
|
|
_params->setObject(*obj);
|
2010-03-24 10:32:40 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-03-28 00:38:33 +04:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType) AsyncStatementJSHelper::AddRef() { return 2; }
|
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType) AsyncStatementJSHelper::Release() { return 1; }
|
2010-03-24 10:32:40 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN(AsyncStatementJSHelper)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIXPCScriptable)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// nsIXPCScriptable
|
|
|
|
|
2017-01-23 05:33:58 +03:00
|
|
|
#define XPC_MAP_CLASSNAME AsyncStatementJSHelper
|
2010-03-24 10:32:40 +03:00
|
|
|
#define XPC_MAP_QUOTED_CLASSNAME "AsyncStatementJSHelper"
|
2017-01-23 05:33:58 +03:00
|
|
|
#define XPC_MAP_FLAGS (XPC_SCRIPTABLE_WANT_GETPROPERTY | \
|
|
|
|
XPC_SCRIPTABLE_ALLOW_PROP_MODS_DURING_RESOLVE)
|
2010-03-24 10:32:40 +03:00
|
|
|
#include "xpc_map_end.h"
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
AsyncStatementJSHelper::GetProperty(nsIXPConnectWrappedNative *aWrapper,
|
|
|
|
JSContext *aCtx,
|
|
|
|
JSObject *aScopeObj,
|
2010-07-15 10:19:36 +04:00
|
|
|
jsid aId,
|
2015-07-18 22:45:35 +03:00
|
|
|
JS::Value *_result,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool *_retval)
|
2010-03-24 10:32:40 +03:00
|
|
|
{
|
2010-07-15 10:19:36 +04:00
|
|
|
if (!JSID_IS_STRING(aId))
|
2010-03-24 10:32:40 +03:00
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
// Cast to async via mozI* since direct from nsISupports is ambiguous.
|
2013-05-12 17:10:39 +04:00
|
|
|
JS::RootedObject scope(aCtx, aScopeObj);
|
|
|
|
JS::RootedId id(aCtx, aId);
|
2010-03-24 10:32:40 +03:00
|
|
|
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
|
|
|
|
|
2013-05-12 17:10:39 +04:00
|
|
|
if (::JS_FlatStringEqualsAscii(JSID_TO_FLAT_STRING(id), "params"))
|
|
|
|
return getParams(stmt, aCtx, scope, _result);
|
2010-03-24 10:32:40 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-12 19:55:56 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// 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;
|
|
|
|
}
|
|
|
|
|
2010-03-24 10:32:40 +03:00
|
|
|
} // namespace storage
|
|
|
|
} // namespace mozilla
|