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 "mozStorageAsyncStatement.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 "mozStorageAsyncStatementJSHelper.h" michael@0: michael@0: #include "mozStorageAsyncStatementParams.h" michael@0: michael@0: #include "jsapi.h" michael@0: michael@0: namespace mozilla { michael@0: namespace storage { michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: //// AsyncStatementJSHelper michael@0: michael@0: nsresult michael@0: AsyncStatementJSHelper::getParams(AsyncStatement *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 == mozIStorageAsyncStatement::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: nsCOMPtr params = michael@0: new AsyncStatementParams(aStatement); michael@0: NS_ENSURE_TRUE(params, NS_ERROR_OUT_OF_MEMORY); michael@0: michael@0: JS::RootedObject scope(aCtx, aScopeObj); 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) AsyncStatementJSHelper::AddRef() { return 2; } michael@0: NS_IMETHODIMP_(MozExternalRefCountType) AsyncStatementJSHelper::Release() { return 1; } michael@0: NS_INTERFACE_MAP_BEGIN(AsyncStatementJSHelper) 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 AsyncStatementJSHelper michael@0: #define XPC_MAP_QUOTED_CLASSNAME "AsyncStatementJSHelper" michael@0: #define XPC_MAP_WANT_GETPROPERTY 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: AsyncStatementJSHelper::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: // Cast to async via mozI* since direct from nsISupports is ambiguous. michael@0: JS::RootedObject scope(aCtx, aScopeObj); michael@0: JS::RootedId id(aCtx, aId); michael@0: mozIStorageAsyncStatement *iAsyncStmt = michael@0: static_cast(aWrapper->Native()); michael@0: AsyncStatement *stmt = static_cast(iAsyncStmt); michael@0: michael@0: #ifdef DEBUG michael@0: { michael@0: nsISupports *supp = aWrapper->Native(); michael@0: nsCOMPtr isStatement(do_QueryInterface(supp)); michael@0: NS_ASSERTION(isStatement, "How is this not an async statement?!"); michael@0: } michael@0: #endif michael@0: michael@0: if (::JS_FlatStringEqualsAscii(JSID_TO_FLAT_STRING(id), "params")) michael@0: return getParams(stmt, aCtx, scope, _result); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: } // namespace storage michael@0: } // namespace mozilla