2008-09-13 01:30:41 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2009-04-13 20:29:41 +04:00
|
|
|
* 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/. */
|
2008-09-13 01:30:41 +04:00
|
|
|
|
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
#include "mozStorageStatement.h"
|
2008-11-19 09:11:30 +03:00
|
|
|
#include "mozStorageService.h"
|
2008-09-13 01:30:41 +04:00
|
|
|
|
|
|
|
#include "nsMemory.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
|
|
|
|
#include "mozStorageStatementJSHelper.h"
|
|
|
|
|
|
|
|
#include "mozStorageStatementRow.h"
|
|
|
|
#include "mozStorageStatementParams.h"
|
|
|
|
|
|
|
|
#include "jsapi.h"
|
|
|
|
|
2016-12-14 00:33:46 +03:00
|
|
|
#include "xpc_make_class.h"
|
|
|
|
|
2009-04-08 20:24:25 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace storage {
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// Global Functions
|
2009-03-31 18:26:16 +04:00
|
|
|
|
2018-03-22 18:42:03 +03:00
|
|
|
static bool stepFunc(JSContext *aCtx, uint32_t argc, JS::Value *_vp) {
|
|
|
|
JS::CallArgs args = CallArgsFromVp(argc, _vp);
|
|
|
|
|
2018-09-06 17:01:58 +03:00
|
|
|
nsCOMPtr<nsIXPConnect> xpc(nsIXPConnect::XPConnect());
|
2008-09-13 01:30:41 +04:00
|
|
|
nsCOMPtr<nsIXPConnectWrappedNative> wrapper;
|
2018-03-22 18:42:03 +03:00
|
|
|
|
|
|
|
if (!args.thisv().isObject()) {
|
|
|
|
::JS_ReportErrorASCII(aCtx, "mozIStorageStatement::step() requires object");
|
2013-08-02 11:41:57 +04:00
|
|
|
return false;
|
2011-12-02 01:30:28 +04:00
|
|
|
}
|
|
|
|
|
2018-03-22 18:42:03 +03:00
|
|
|
JSObject *obj = &args.thisv().toObject();
|
2011-12-02 01:30:28 +04:00
|
|
|
nsresult rv =
|
|
|
|
xpc->GetWrappedNativeOfJSObject(aCtx, obj, getter_AddRefs(wrapper));
|
2008-09-13 01:30:41 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2016-08-14 14:39:28 +03:00
|
|
|
::JS_ReportErrorASCII(
|
|
|
|
aCtx, "mozIStorageStatement::step() could not obtain native statement");
|
2013-08-02 11:41:57 +04:00
|
|
|
return false;
|
2008-09-13 01:30:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
2010-03-24 10:32:40 +03:00
|
|
|
nsCOMPtr<mozIStorageStatement> isStatement(
|
|
|
|
do_QueryInterface(wrapper->Native()));
|
2008-09-13 01:30:41 +04:00
|
|
|
NS_ASSERTION(isStatement, "How is this not a statement?!");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-03-24 10:32:40 +03:00
|
|
|
Statement *stmt = static_cast<Statement *>(
|
|
|
|
static_cast<mozIStorageStatement *>(wrapper->Native()));
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool hasMore = false;
|
2008-09-13 01:30:41 +04:00
|
|
|
rv = stmt->ExecuteStep(&hasMore);
|
|
|
|
if (NS_SUCCEEDED(rv) && !hasMore) {
|
2018-03-22 18:42:03 +03:00
|
|
|
args.rval().setBoolean(false);
|
2008-09-13 01:30:41 +04:00
|
|
|
(void)stmt->Reset();
|
2013-08-02 11:41:57 +04:00
|
|
|
return true;
|
2008-09-13 01:30:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
2016-08-14 14:39:28 +03:00
|
|
|
::JS_ReportErrorASCII(aCtx,
|
|
|
|
"mozIStorageStatement::step() returned an error");
|
2013-08-02 11:41:57 +04:00
|
|
|
return false;
|
2008-09-13 01:30:41 +04:00
|
|
|
}
|
|
|
|
|
2018-03-22 18:42:03 +03:00
|
|
|
args.rval().setBoolean(hasMore);
|
2013-08-02 11:41:57 +04:00
|
|
|
return true;
|
2008-09-13 01:30:41 +04:00
|
|
|
}
|
|
|
|
|
2009-04-08 20:24:25 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// StatementJSHelper
|
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
nsresult StatementJSHelper::getRow(Statement *aStatement, JSContext *aCtx,
|
2009-04-08 20:24:25 +04:00
|
|
|
JSObject *aScopeObj, JS::Value *_row) {
|
2015-03-12 19:55:56 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2008-09-13 01:30:41 +04:00
|
|
|
|
2009-06-12 00:18:58 +04:00
|
|
|
#ifdef DEBUG
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t state;
|
2008-09-13 01:30:41 +04:00
|
|
|
(void)aStatement->GetState(&state);
|
2009-06-12 00:18:58 +04:00
|
|
|
NS_ASSERTION(state == mozIStorageStatement::MOZ_STORAGE_STATEMENT_EXECUTING,
|
|
|
|
"Invalid state to get the row object - all calls will fail!");
|
|
|
|
#endif
|
2008-09-13 01:30:41 +04:00
|
|
|
|
2017-08-22 10:25:37 +03:00
|
|
|
JS::RootedObject scope(aCtx, aScopeObj);
|
|
|
|
|
2008-09-13 01:30:41 +04:00
|
|
|
if (!aStatement->mStatementRowHolder) {
|
2017-08-22 10:25:37 +03:00
|
|
|
dom::GlobalObject global(aCtx, scope);
|
|
|
|
if (global.Failed()) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsPIDOMWindowInner> window =
|
|
|
|
do_QueryInterface(global.GetAsSupports());
|
|
|
|
|
|
|
|
RefPtr<StatementRow> row(new StatementRow(window, aStatement));
|
2008-09-13 01:30:41 +04:00
|
|
|
NS_ENSURE_TRUE(row, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
2017-08-22 10:25:37 +03:00
|
|
|
RefPtr<StatementRowHolder> rowHolder = new StatementRowHolder(row);
|
|
|
|
NS_ENSURE_TRUE(rowHolder, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
2015-03-12 19:55:56 +03:00
|
|
|
aStatement->mStatementRowHolder =
|
2017-08-22 10:25:37 +03:00
|
|
|
new nsMainThreadPtrHolder<StatementRowHolder>(
|
2017-06-14 04:27:17 +03:00
|
|
|
"Statement::mStatementRowHolder", rowHolder);
|
2008-09-13 01:30:41 +04:00
|
|
|
}
|
|
|
|
|
2017-08-22 10:25:37 +03:00
|
|
|
RefPtr<StatementRow> row(aStatement->mStatementRowHolder->Get());
|
|
|
|
JSObject *obj = row->WrapObject(aCtx, nullptr);
|
|
|
|
if (!obj) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2008-09-13 01:30:41 +04:00
|
|
|
|
2015-07-01 07:09:46 +03:00
|
|
|
_row->setObject(*obj);
|
2008-09-13 01:30:41 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
nsresult StatementJSHelper::getParams(Statement *aStatement, JSContext *aCtx,
|
2009-04-08 20:24:25 +04:00
|
|
|
JSObject *aScopeObj, JS::Value *_params) {
|
2015-03-12 19:55:56 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2008-09-13 01:30:41 +04:00
|
|
|
|
2009-06-12 00:18:58 +04:00
|
|
|
#ifdef DEBUG
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t state;
|
2008-09-13 01:30:41 +04:00
|
|
|
(void)aStatement->GetState(&state);
|
2009-06-12 00:18:58 +04:00
|
|
|
NS_ASSERTION(state == mozIStorageStatement::MOZ_STORAGE_STATEMENT_READY,
|
|
|
|
"Invalid state to get the params object - all calls will fail!");
|
|
|
|
#endif
|
2008-09-13 01:30:41 +04:00
|
|
|
|
2017-08-24 11:52:52 +03:00
|
|
|
JS::RootedObject scope(aCtx, aScopeObj);
|
|
|
|
|
2008-09-13 01:30:41 +04:00
|
|
|
if (!aStatement->mStatementParamsHolder) {
|
2017-08-24 11:52:52 +03:00
|
|
|
dom::GlobalObject global(aCtx, scope);
|
|
|
|
if (global.Failed()) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsPIDOMWindowInner> window =
|
|
|
|
do_QueryInterface(global.GetAsSupports());
|
|
|
|
|
|
|
|
RefPtr<StatementParams> params(new StatementParams(window, aStatement));
|
2008-09-13 01:30:41 +04:00
|
|
|
NS_ENSURE_TRUE(params, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
2017-08-24 11:52:52 +03:00
|
|
|
RefPtr<StatementParamsHolder> paramsHolder =
|
|
|
|
new StatementParamsHolder(params);
|
|
|
|
NS_ENSURE_TRUE(paramsHolder, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
2015-03-12 19:55:56 +03:00
|
|
|
aStatement->mStatementParamsHolder =
|
2017-08-24 11:52:52 +03:00
|
|
|
new nsMainThreadPtrHolder<StatementParamsHolder>(
|
2017-06-14 04:27:17 +03:00
|
|
|
"Statement::mStatementParamsHolder", paramsHolder);
|
2008-09-13 01:30:41 +04:00
|
|
|
}
|
|
|
|
|
2017-08-24 11:52:52 +03:00
|
|
|
RefPtr<StatementParams> params(aStatement->mStatementParamsHolder->Get());
|
|
|
|
JSObject *obj = params->WrapObject(aCtx, nullptr);
|
|
|
|
if (!obj) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2008-09-13 01:30:41 +04:00
|
|
|
|
2015-07-01 07:09:46 +03:00
|
|
|
_params->setObject(*obj);
|
2008-09-13 01:30:41 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-03-28 00:38:33 +04:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType) StatementJSHelper::AddRef() {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType) StatementJSHelper::Release() {
|
|
|
|
return 1;
|
|
|
|
}
|
2009-04-08 20:24:25 +04:00
|
|
|
NS_INTERFACE_MAP_BEGIN(StatementJSHelper)
|
2008-09-13 01:30:41 +04:00
|
|
|
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 StatementJSHelper
|
2009-04-08 20:24:25 +04:00
|
|
|
#define XPC_MAP_QUOTED_CLASSNAME "StatementJSHelper"
|
2017-08-15 17:16:07 +03:00
|
|
|
#define XPC_MAP_FLAGS \
|
2017-01-23 05:33:58 +03:00
|
|
|
(XPC_SCRIPTABLE_WANT_RESOLVE | XPC_SCRIPTABLE_ALLOW_PROP_MODS_DURING_RESOLVE)
|
2008-09-13 01:30:41 +04:00
|
|
|
#include "xpc_map_end.h"
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2017-08-15 17:16:07 +03:00
|
|
|
StatementJSHelper::Resolve(nsIXPConnectWrappedNative *aWrapper, JSContext *aCtx,
|
|
|
|
JSObject *aScopeObj, jsid aId, bool *aResolvedp,
|
|
|
|
bool *_retval) {
|
2010-07-15 10:19:36 +04:00
|
|
|
if (!JSID_IS_STRING(aId)) return NS_OK;
|
2008-09-13 01:30:41 +04:00
|
|
|
|
2013-05-13 02:32:13 +04:00
|
|
|
JS::Rooted<JSObject *> scope(aCtx, aScopeObj);
|
|
|
|
JS::Rooted<jsid> id(aCtx, aId);
|
|
|
|
|
2008-09-13 01:30:41 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
2010-03-24 10:32:40 +03:00
|
|
|
nsCOMPtr<mozIStorageStatement> isStatement(
|
|
|
|
do_QueryInterface(aWrapper->Native()));
|
2008-09-13 01:30:41 +04:00
|
|
|
NS_ASSERTION(isStatement, "How is this not a statement?!");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-03-24 10:32:40 +03:00
|
|
|
Statement *stmt = static_cast<Statement *>(
|
|
|
|
static_cast<mozIStorageStatement *>(aWrapper->Native()));
|
|
|
|
|
2017-08-15 17:16:07 +03:00
|
|
|
JSFlatString *str = JSID_TO_FLAT_STRING(id);
|
|
|
|
if (::JS_FlatStringEqualsAscii(str, "step")) {
|
|
|
|
*_retval = ::JS_DefineFunction(aCtx, scope, "step", stepFunc, 0,
|
|
|
|
JSPROP_RESOLVING) != nullptr;
|
|
|
|
*aResolvedp = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2008-09-13 01:30:41 +04:00
|
|
|
|
2017-08-15 17:16:07 +03:00
|
|
|
JS::RootedValue val(aCtx);
|
2008-09-13 01:30:41 +04:00
|
|
|
|
2017-08-15 17:16:07 +03:00
|
|
|
if (::JS_FlatStringEqualsAscii(str, "row")) {
|
|
|
|
nsresult rv = getRow(stmt, aCtx, scope, val.address());
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
*_retval = ::JS_DefinePropertyById(aCtx, scope, id, val, JSPROP_RESOLVING);
|
|
|
|
*aResolvedp = true;
|
2008-09-13 01:30:41 +04:00
|
|
|
return NS_OK;
|
2017-08-15 17:16:07 +03:00
|
|
|
}
|
2008-09-13 01:30:41 +04:00
|
|
|
|
2017-08-15 17:16:07 +03:00
|
|
|
if (::JS_FlatStringEqualsAscii(str, "params")) {
|
|
|
|
nsresult rv = getParams(stmt, aCtx, scope, val.address());
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
*_retval = ::JS_DefinePropertyById(aCtx, scope, id, val, JSPROP_RESOLVING);
|
2014-11-08 03:07:12 +03:00
|
|
|
*aResolvedp = true;
|
2008-09-13 01:30:41 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2017-08-15 17:16:07 +03:00
|
|
|
|
2008-09-13 01:30:41 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2009-04-08 20:24:25 +04:00
|
|
|
|
2017-08-24 11:52:52 +03:00
|
|
|
NS_IMPL_ISUPPORTS0(StatementParamsHolder);
|
2015-03-12 19:55:56 +03:00
|
|
|
|
|
|
|
StatementParamsHolder::~StatementParamsHolder() {
|
|
|
|
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.
|
2017-08-24 11:52:52 +03:00
|
|
|
mParams->mStatement = nullptr;
|
2015-03-12 19:55:56 +03:00
|
|
|
}
|
|
|
|
|
2017-08-22 10:25:37 +03:00
|
|
|
NS_IMPL_ISUPPORTS0(StatementRowHolder);
|
|
|
|
|
2015-03-12 19:55:56 +03:00
|
|
|
StatementRowHolder::~StatementRowHolder() {
|
|
|
|
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.
|
2017-08-22 10:25:37 +03:00
|
|
|
mRow->mStatement = nullptr;
|
2015-03-12 19:55:56 +03:00
|
|
|
}
|
|
|
|
|
2009-04-08 20:24:25 +04:00
|
|
|
} // namespace storage
|
|
|
|
} // namespace mozilla
|