michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ : michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsIXPConnect.h" michael@0: #include "mozStorageStatement.h" michael@0: #include "mozStorageService.h" michael@0: michael@0: #include "nsMemory.h" michael@0: #include "nsString.h" michael@0: #include "nsServiceManagerUtils.h" michael@0: michael@0: #include "mozStorageStatementJSHelper.h" michael@0: michael@0: #include "mozStorageStatementRow.h" michael@0: #include "mozStorageStatementParams.h" michael@0: michael@0: #include "jsapi.h" michael@0: michael@0: namespace mozilla { michael@0: namespace storage { michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: //// Global Functions michael@0: michael@0: static michael@0: bool michael@0: stepFunc(JSContext *aCtx, michael@0: uint32_t, michael@0: jsval *_vp) michael@0: { michael@0: nsCOMPtr xpc(Service::getXPConnect()); michael@0: nsCOMPtr wrapper; michael@0: JSObject *obj = JS_THIS_OBJECT(aCtx, _vp); michael@0: if (!obj) { michael@0: return false; michael@0: } michael@0: michael@0: nsresult rv = michael@0: xpc->GetWrappedNativeOfJSObject(aCtx, obj, getter_AddRefs(wrapper)); michael@0: if (NS_FAILED(rv)) { michael@0: ::JS_ReportError(aCtx, "mozIStorageStatement::step() could not obtain native statement"); michael@0: return false; michael@0: } michael@0: michael@0: #ifdef DEBUG michael@0: { michael@0: nsCOMPtr isStatement( michael@0: do_QueryInterface(wrapper->Native()) michael@0: ); michael@0: NS_ASSERTION(isStatement, "How is this not a statement?!"); michael@0: } michael@0: #endif michael@0: michael@0: Statement *stmt = static_cast( michael@0: static_cast(wrapper->Native()) michael@0: ); michael@0: michael@0: bool hasMore = false; michael@0: rv = stmt->ExecuteStep(&hasMore); michael@0: if (NS_SUCCEEDED(rv) && !hasMore) { michael@0: *_vp = JSVAL_FALSE; michael@0: (void)stmt->Reset(); michael@0: return true; michael@0: } michael@0: michael@0: if (NS_FAILED(rv)) { michael@0: ::JS_ReportError(aCtx, "mozIStorageStatement::step() returned an error"); michael@0: return false; michael@0: } michael@0: michael@0: *_vp = BOOLEAN_TO_JSVAL(hasMore); michael@0: return true; michael@0: } michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: //// StatementJSHelper michael@0: michael@0: nsresult michael@0: StatementJSHelper::getRow(Statement *aStatement, michael@0: JSContext *aCtx, michael@0: JSObject *aScopeObj, michael@0: jsval *_row) michael@0: { michael@0: nsresult rv; michael@0: michael@0: #ifdef DEBUG michael@0: int32_t state; michael@0: (void)aStatement->GetState(&state); michael@0: NS_ASSERTION(state == mozIStorageStatement::MOZ_STORAGE_STATEMENT_EXECUTING, michael@0: "Invalid state to get the row object - all calls will fail!"); michael@0: #endif michael@0: michael@0: if (!aStatement->mStatementRowHolder) { michael@0: JS::RootedObject scope(aCtx, aScopeObj); michael@0: nsCOMPtr row(new StatementRow(aStatement)); michael@0: NS_ENSURE_TRUE(row, NS_ERROR_OUT_OF_MEMORY); michael@0: michael@0: nsCOMPtr xpc(Service::getXPConnect()); michael@0: rv = xpc->WrapNative( michael@0: aCtx, michael@0: ::JS_GetGlobalForObject(aCtx, scope), michael@0: row, michael@0: NS_GET_IID(mozIStorageStatementRow), michael@0: getter_AddRefs(aStatement->mStatementRowHolder) michael@0: ); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: michael@0: JS::Rooted obj(aCtx); michael@0: obj = aStatement->mStatementRowHolder->GetJSObject(); michael@0: NS_ENSURE_STATE(obj); michael@0: michael@0: *_row = OBJECT_TO_JSVAL(obj); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: StatementJSHelper::getParams(Statement *aStatement, michael@0: JSContext *aCtx, michael@0: JSObject *aScopeObj, michael@0: jsval *_params) michael@0: { michael@0: nsresult rv; michael@0: michael@0: #ifdef DEBUG michael@0: int32_t state; michael@0: (void)aStatement->GetState(&state); michael@0: NS_ASSERTION(state == mozIStorageStatement::MOZ_STORAGE_STATEMENT_READY, michael@0: "Invalid state to get the params object - all calls will fail!"); michael@0: #endif michael@0: michael@0: if (!aStatement->mStatementParamsHolder) { michael@0: JS::RootedObject scope(aCtx, aScopeObj); michael@0: nsCOMPtr params = michael@0: new StatementParams(aStatement); michael@0: NS_ENSURE_TRUE(params, NS_ERROR_OUT_OF_MEMORY); michael@0: michael@0: nsCOMPtr xpc(Service::getXPConnect()); michael@0: rv = xpc->WrapNative( michael@0: aCtx, michael@0: ::JS_GetGlobalForObject(aCtx, scope), michael@0: params, michael@0: NS_GET_IID(mozIStorageStatementParams), michael@0: getter_AddRefs(aStatement->mStatementParamsHolder) michael@0: ); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: michael@0: JS::Rooted obj(aCtx); michael@0: obj = aStatement->mStatementParamsHolder->GetJSObject(); michael@0: NS_ENSURE_STATE(obj); michael@0: michael@0: *_params = OBJECT_TO_JSVAL(obj); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP_(MozExternalRefCountType) StatementJSHelper::AddRef() { return 2; } michael@0: NS_IMETHODIMP_(MozExternalRefCountType) StatementJSHelper::Release() { return 1; } michael@0: NS_INTERFACE_MAP_BEGIN(StatementJSHelper) michael@0: NS_INTERFACE_MAP_ENTRY(nsIXPCScriptable) michael@0: NS_INTERFACE_MAP_ENTRY(nsISupports) michael@0: NS_INTERFACE_MAP_END michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: //// nsIXPCScriptable michael@0: michael@0: #define XPC_MAP_CLASSNAME StatementJSHelper michael@0: #define XPC_MAP_QUOTED_CLASSNAME "StatementJSHelper" michael@0: #define XPC_MAP_WANT_GETPROPERTY michael@0: #define XPC_MAP_WANT_NEWRESOLVE michael@0: #define XPC_MAP_FLAGS nsIXPCScriptable::ALLOW_PROP_MODS_DURING_RESOLVE michael@0: #include "xpc_map_end.h" michael@0: michael@0: NS_IMETHODIMP michael@0: StatementJSHelper::GetProperty(nsIXPConnectWrappedNative *aWrapper, michael@0: JSContext *aCtx, michael@0: JSObject *aScopeObj, michael@0: jsid aId, michael@0: jsval *_result, michael@0: bool *_retval) michael@0: { michael@0: if (!JSID_IS_STRING(aId)) michael@0: return NS_OK; michael@0: michael@0: JS::Rooted scope(aCtx, aScopeObj); michael@0: JS::Rooted id(aCtx, aId); michael@0: michael@0: #ifdef DEBUG michael@0: { michael@0: nsCOMPtr isStatement( michael@0: do_QueryInterface(aWrapper->Native())); michael@0: NS_ASSERTION(isStatement, "How is this not a statement?!"); michael@0: } michael@0: #endif michael@0: michael@0: Statement *stmt = static_cast( michael@0: static_cast(aWrapper->Native()) michael@0: ); michael@0: michael@0: JSFlatString *str = JSID_TO_FLAT_STRING(id); michael@0: if (::JS_FlatStringEqualsAscii(str, "row")) michael@0: return getRow(stmt, aCtx, scope, _result); michael@0: michael@0: if (::JS_FlatStringEqualsAscii(str, "params")) michael@0: return getParams(stmt, aCtx, scope, _result); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: NS_IMETHODIMP michael@0: StatementJSHelper::NewResolve(nsIXPConnectWrappedNative *aWrapper, michael@0: JSContext *aCtx, michael@0: JSObject *aScopeObj, michael@0: jsid aId, michael@0: JSObject **_objp, michael@0: bool *_retval) michael@0: { michael@0: if (!JSID_IS_STRING(aId)) michael@0: return NS_OK; michael@0: michael@0: JS::RootedObject scope(aCtx, aScopeObj); michael@0: if (::JS_FlatStringEqualsAscii(JSID_TO_FLAT_STRING(aId), "step")) { michael@0: *_retval = ::JS_DefineFunction(aCtx, scope, "step", stepFunc, michael@0: 0, 0) != nullptr; michael@0: *_objp = scope.get(); michael@0: return NS_OK; michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: } // namespace storage michael@0: } // namespace mozilla