no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / storage / mozStorageAsyncStatementJSHelper.cpp
blob73366f6604b44b86671def0c8d32b9f30a179fd7
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsIXPConnect.h"
8 #include "mozStorageAsyncStatement.h"
9 #include "mozStorageService.h"
11 #include "nsString.h"
12 #include "nsServiceManagerUtils.h"
14 #include "mozStorageAsyncStatementJSHelper.h"
16 #include "mozStorageAsyncStatementParams.h"
18 #include "jsapi.h"
19 #include "js/PropertyAndElement.h" // JS_DefineProperty, JS_DefinePropertyById
21 #include "xpc_make_class.h"
23 namespace mozilla {
24 namespace storage {
26 ////////////////////////////////////////////////////////////////////////////////
27 //// AsyncStatementJSHelper
29 nsresult AsyncStatementJSHelper::getParams(AsyncStatement* aStatement,
30 JSContext* aCtx, JSObject* aScopeObj,
31 JS::Value* _params) {
32 MOZ_ASSERT(NS_IsMainThread());
34 #ifdef DEBUG
35 int32_t state;
36 (void)aStatement->GetState(&state);
37 NS_ASSERTION(state == mozIStorageAsyncStatement::MOZ_STORAGE_STATEMENT_READY,
38 "Invalid state to get the params object - all calls will fail!");
39 #endif
41 JS::Rooted<JSObject*> scope(aCtx, aScopeObj);
43 if (!aStatement->mStatementParamsHolder) {
44 dom::GlobalObject global(aCtx, scope);
45 if (global.Failed()) {
46 return NS_ERROR_UNEXPECTED;
49 nsCOMPtr<nsPIDOMWindowInner> window =
50 do_QueryInterface(global.GetAsSupports());
52 RefPtr<AsyncStatementParams> params(
53 new AsyncStatementParams(window, aStatement));
54 NS_ENSURE_TRUE(params, NS_ERROR_OUT_OF_MEMORY);
56 RefPtr<AsyncStatementParamsHolder> paramsHolder =
57 new AsyncStatementParamsHolder(params);
58 NS_ENSURE_TRUE(paramsHolder, NS_ERROR_OUT_OF_MEMORY);
60 aStatement->mStatementParamsHolder =
61 new nsMainThreadPtrHolder<AsyncStatementParamsHolder>(
62 "Statement::mStatementParamsHolder", paramsHolder);
65 RefPtr<AsyncStatementParams> params(
66 aStatement->mStatementParamsHolder->Get());
67 JSObject* obj = params->WrapObject(aCtx, nullptr);
68 if (!obj) {
69 return NS_ERROR_UNEXPECTED;
72 _params->setObject(*obj);
73 return NS_OK;
76 NS_IMETHODIMP_(MozExternalRefCountType) AsyncStatementJSHelper::AddRef() {
77 return 2;
79 NS_IMETHODIMP_(MozExternalRefCountType) AsyncStatementJSHelper::Release() {
80 return 1;
82 NS_INTERFACE_MAP_BEGIN(AsyncStatementJSHelper)
83 NS_INTERFACE_MAP_ENTRY(nsIXPCScriptable)
84 NS_INTERFACE_MAP_ENTRY(nsISupports)
85 NS_INTERFACE_MAP_END
87 ////////////////////////////////////////////////////////////////////////////////
88 //// nsIXPCScriptable
90 #define XPC_MAP_CLASSNAME AsyncStatementJSHelper
91 #define XPC_MAP_QUOTED_CLASSNAME "AsyncStatementJSHelper"
92 #define XPC_MAP_FLAGS \
93 (XPC_SCRIPTABLE_WANT_RESOLVE | XPC_SCRIPTABLE_ALLOW_PROP_MODS_DURING_RESOLVE)
94 #include "xpc_map_end.h"
96 NS_IMETHODIMP
97 AsyncStatementJSHelper::Resolve(nsIXPConnectWrappedNative* aWrapper,
98 JSContext* aCtx, JSObject* aScopeObj, jsid aId,
99 bool* resolvedp, bool* _retval) {
100 if (!aId.isString()) return NS_OK;
102 // Cast to async via mozI* since direct from nsISupports is ambiguous.
103 JS::Rooted<JSObject*> scope(aCtx, aScopeObj);
104 JS::Rooted<JS::PropertyKey> id(aCtx, aId);
105 mozIStorageAsyncStatement* iAsyncStmt =
106 static_cast<mozIStorageAsyncStatement*>(aWrapper->Native());
107 AsyncStatement* stmt = static_cast<AsyncStatement*>(iAsyncStmt);
109 #ifdef DEBUG
111 nsISupports* supp = aWrapper->Native();
112 nsCOMPtr<mozIStorageAsyncStatement> isStatement(do_QueryInterface(supp));
113 NS_ASSERTION(isStatement, "How is this not an async statement?!");
115 #endif
117 if (::JS_LinearStringEqualsLiteral(id.toLinearString(), "params")) {
118 JS::Rooted<JS::Value> val(aCtx);
119 nsresult rv = getParams(stmt, aCtx, scope, val.address());
120 NS_ENSURE_SUCCESS(rv, rv);
121 *_retval = ::JS_DefinePropertyById(aCtx, scope, id, val, JSPROP_RESOLVING);
122 *resolvedp = true;
123 return NS_OK;
126 return NS_OK;
129 ////////////////////////////////////////////////////////////////////////////////
130 //// AsyncStatementParamsHolder
132 NS_IMPL_ISUPPORTS0(AsyncStatementParamsHolder);
134 AsyncStatementParamsHolder::~AsyncStatementParamsHolder() {
135 MOZ_ASSERT(NS_IsMainThread());
136 // We are considered dead at this point, so any wrappers for row or params
137 // need to lose their reference to the statement.
138 mParams->mStatement = nullptr;
141 } // namespace storage
142 } // namespace mozilla