Bumping gaia.json for 2 gaia revision(s) a=gaia-bump
[gecko.git] / storage / src / mozStorageStatementJSHelper.cpp
blob5c3caefdd1898cd746cd25268cd20f368bc36b4c
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 "mozStorageStatement.h"
9 #include "mozStorageService.h"
11 #include "nsMemory.h"
12 #include "nsString.h"
13 #include "nsServiceManagerUtils.h"
15 #include "mozStorageStatementJSHelper.h"
17 #include "mozStorageStatementRow.h"
18 #include "mozStorageStatementParams.h"
20 #include "jsapi.h"
22 namespace mozilla {
23 namespace storage {
25 ////////////////////////////////////////////////////////////////////////////////
26 //// Global Functions
28 static
29 bool
30 stepFunc(JSContext *aCtx,
31 uint32_t,
32 jsval *_vp)
34 nsCOMPtr<nsIXPConnect> xpc(Service::getXPConnect());
35 nsCOMPtr<nsIXPConnectWrappedNative> wrapper;
36 JSObject *obj = JS_THIS_OBJECT(aCtx, _vp);
37 if (!obj) {
38 return false;
41 nsresult rv =
42 xpc->GetWrappedNativeOfJSObject(aCtx, obj, getter_AddRefs(wrapper));
43 if (NS_FAILED(rv)) {
44 ::JS_ReportError(aCtx, "mozIStorageStatement::step() could not obtain native statement");
45 return false;
48 #ifdef DEBUG
50 nsCOMPtr<mozIStorageStatement> isStatement(
51 do_QueryInterface(wrapper->Native())
53 NS_ASSERTION(isStatement, "How is this not a statement?!");
55 #endif
57 Statement *stmt = static_cast<Statement *>(
58 static_cast<mozIStorageStatement *>(wrapper->Native())
61 bool hasMore = false;
62 rv = stmt->ExecuteStep(&hasMore);
63 if (NS_SUCCEEDED(rv) && !hasMore) {
64 *_vp = JSVAL_FALSE;
65 (void)stmt->Reset();
66 return true;
69 if (NS_FAILED(rv)) {
70 ::JS_ReportError(aCtx, "mozIStorageStatement::step() returned an error");
71 return false;
74 *_vp = BOOLEAN_TO_JSVAL(hasMore);
75 return true;
78 ////////////////////////////////////////////////////////////////////////////////
79 //// StatementJSHelper
81 nsresult
82 StatementJSHelper::getRow(Statement *aStatement,
83 JSContext *aCtx,
84 JSObject *aScopeObj,
85 jsval *_row)
87 nsresult rv;
89 #ifdef DEBUG
90 int32_t state;
91 (void)aStatement->GetState(&state);
92 NS_ASSERTION(state == mozIStorageStatement::MOZ_STORAGE_STATEMENT_EXECUTING,
93 "Invalid state to get the row object - all calls will fail!");
94 #endif
96 if (!aStatement->mStatementRowHolder) {
97 JS::RootedObject scope(aCtx, aScopeObj);
98 nsCOMPtr<mozIStorageStatementRow> row(new StatementRow(aStatement));
99 NS_ENSURE_TRUE(row, NS_ERROR_OUT_OF_MEMORY);
101 nsCOMPtr<nsIXPConnect> xpc(Service::getXPConnect());
102 rv = xpc->WrapNative(
103 aCtx,
104 ::JS_GetGlobalForObject(aCtx, scope),
105 row,
106 NS_GET_IID(mozIStorageStatementRow),
107 getter_AddRefs(aStatement->mStatementRowHolder)
109 NS_ENSURE_SUCCESS(rv, rv);
112 JS::Rooted<JSObject*> obj(aCtx);
113 obj = aStatement->mStatementRowHolder->GetJSObject();
114 NS_ENSURE_STATE(obj);
116 *_row = OBJECT_TO_JSVAL(obj);
117 return NS_OK;
120 nsresult
121 StatementJSHelper::getParams(Statement *aStatement,
122 JSContext *aCtx,
123 JSObject *aScopeObj,
124 jsval *_params)
126 nsresult rv;
128 #ifdef DEBUG
129 int32_t state;
130 (void)aStatement->GetState(&state);
131 NS_ASSERTION(state == mozIStorageStatement::MOZ_STORAGE_STATEMENT_READY,
132 "Invalid state to get the params object - all calls will fail!");
133 #endif
135 if (!aStatement->mStatementParamsHolder) {
136 JS::RootedObject scope(aCtx, aScopeObj);
137 nsCOMPtr<mozIStorageStatementParams> params =
138 new StatementParams(aStatement);
139 NS_ENSURE_TRUE(params, NS_ERROR_OUT_OF_MEMORY);
141 nsCOMPtr<nsIXPConnect> xpc(Service::getXPConnect());
142 rv = xpc->WrapNative(
143 aCtx,
144 ::JS_GetGlobalForObject(aCtx, scope),
145 params,
146 NS_GET_IID(mozIStorageStatementParams),
147 getter_AddRefs(aStatement->mStatementParamsHolder)
149 NS_ENSURE_SUCCESS(rv, rv);
152 JS::Rooted<JSObject*> obj(aCtx);
153 obj = aStatement->mStatementParamsHolder->GetJSObject();
154 NS_ENSURE_STATE(obj);
156 *_params = OBJECT_TO_JSVAL(obj);
157 return NS_OK;
160 NS_IMETHODIMP_(MozExternalRefCountType) StatementJSHelper::AddRef() { return 2; }
161 NS_IMETHODIMP_(MozExternalRefCountType) StatementJSHelper::Release() { return 1; }
162 NS_INTERFACE_MAP_BEGIN(StatementJSHelper)
163 NS_INTERFACE_MAP_ENTRY(nsIXPCScriptable)
164 NS_INTERFACE_MAP_ENTRY(nsISupports)
165 NS_INTERFACE_MAP_END
167 ////////////////////////////////////////////////////////////////////////////////
168 //// nsIXPCScriptable
170 #define XPC_MAP_CLASSNAME StatementJSHelper
171 #define XPC_MAP_QUOTED_CLASSNAME "StatementJSHelper"
172 #define XPC_MAP_WANT_GETPROPERTY
173 #define XPC_MAP_WANT_NEWRESOLVE
174 #define XPC_MAP_FLAGS nsIXPCScriptable::ALLOW_PROP_MODS_DURING_RESOLVE
175 #include "xpc_map_end.h"
177 NS_IMETHODIMP
178 StatementJSHelper::GetProperty(nsIXPConnectWrappedNative *aWrapper,
179 JSContext *aCtx,
180 JSObject *aScopeObj,
181 jsid aId,
182 jsval *_result,
183 bool *_retval)
185 if (!JSID_IS_STRING(aId))
186 return NS_OK;
188 JS::Rooted<JSObject*> scope(aCtx, aScopeObj);
189 JS::Rooted<jsid> id(aCtx, aId);
191 #ifdef DEBUG
193 nsCOMPtr<mozIStorageStatement> isStatement(
194 do_QueryInterface(aWrapper->Native()));
195 NS_ASSERTION(isStatement, "How is this not a statement?!");
197 #endif
199 Statement *stmt = static_cast<Statement *>(
200 static_cast<mozIStorageStatement *>(aWrapper->Native())
203 JSFlatString *str = JSID_TO_FLAT_STRING(id);
204 if (::JS_FlatStringEqualsAscii(str, "row"))
205 return getRow(stmt, aCtx, scope, _result);
207 if (::JS_FlatStringEqualsAscii(str, "params"))
208 return getParams(stmt, aCtx, scope, _result);
210 return NS_OK;
214 NS_IMETHODIMP
215 StatementJSHelper::NewResolve(nsIXPConnectWrappedNative *aWrapper,
216 JSContext *aCtx,
217 JSObject *aScopeObj,
218 jsid aId,
219 JSObject **_objp,
220 bool *_retval)
222 if (!JSID_IS_STRING(aId))
223 return NS_OK;
225 JS::RootedObject scope(aCtx, aScopeObj);
226 if (::JS_FlatStringEqualsAscii(JSID_TO_FLAT_STRING(aId), "step")) {
227 *_retval = ::JS_DefineFunction(aCtx, scope, "step", stepFunc,
228 0, 0) != nullptr;
229 *_objp = scope.get();
230 return NS_OK;
232 return NS_OK;
235 } // namespace storage
236 } // namespace mozilla