Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / storage / src / mozStorageStatementJSHelper.cpp
blob3b93ae70be8cf7e8ce70baa53451811e5a23f5d3
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: sw=2 ts=2 sts=2 expandtab
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is mozStorage code.
18 * The Initial Developer of the Original Code is
19 * Mozilla Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 2008
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
24 * Shawn Wilsher <me@shawnwilsher.com> (Original Author)
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 #include "nsIXPConnect.h"
41 #include "mozStorageStatement.h"
42 #include "mozStorageService.h"
44 #include "nsMemory.h"
45 #include "nsString.h"
46 #include "nsServiceManagerUtils.h"
48 #include "mozStorageStatementJSHelper.h"
50 #include "mozStorageStatementRow.h"
51 #include "mozStorageStatementParams.h"
53 #include "jsapi.h"
55 static
56 JSBool
57 stepFunc(JSContext *aCtx, PRUint32, jsval *_vp)
59 nsCOMPtr<nsIXPConnectWrappedNative> wrapper;
60 nsresult rv = mozStorageService::XPConnect()->GetWrappedNativeOfJSObject(
61 aCtx, JS_THIS_OBJECT(aCtx, _vp), getter_AddRefs(wrapper)
63 if (NS_FAILED(rv)) {
64 JS_ReportError(aCtx, "mozIStorageStatement::step() could not obtain native statement");
65 return JS_FALSE;
68 mozStorageStatement *stmt =
69 static_cast<mozStorageStatement *>(wrapper->Native());
71 #ifdef DEBUG
73 nsCOMPtr<mozIStorageStatement> isStatement(do_QueryInterface(stmt));
74 NS_ASSERTION(isStatement, "How is this not a statement?!");
76 #endif
78 PRBool hasMore = PR_FALSE;
79 rv = stmt->ExecuteStep(&hasMore);
80 if (NS_SUCCEEDED(rv) && !hasMore) {
81 *_vp = JSVAL_FALSE;
82 (void)stmt->Reset();
83 return JS_TRUE;
86 if (NS_FAILED(rv)) {
87 JS_ReportError(aCtx, "mozIStorageStatement::step() returned an error");
88 return JS_FALSE;
91 *_vp = BOOLEAN_TO_JSVAL(hasMore);
92 return JS_TRUE;
95 nsresult
96 mozStorageStatementJSHelper::getRow(mozStorageStatement *aStatement,
97 JSContext *aCtx, JSObject *aScopeObj,
98 jsval *_row)
100 nsresult rv;
102 PRInt32 state;
103 (void)aStatement->GetState(&state);
104 if (state != mozIStorageStatement::MOZ_STORAGE_STATEMENT_EXECUTING)
105 return NS_ERROR_UNEXPECTED;
107 if (!aStatement->mStatementRowHolder) {
108 nsCOMPtr<mozIStorageStatementRow> row =
109 new mozStorageStatementRow(aStatement);
110 NS_ENSURE_TRUE(row, NS_ERROR_OUT_OF_MEMORY);
112 rv = mozStorageService::XPConnect()->WrapNative(
113 aCtx,
114 ::JS_GetGlobalForObject(aCtx, aScopeObj),
115 row,
116 NS_GET_IID(mozIStorageStatementRow),
117 getter_AddRefs(aStatement->mStatementRowHolder)
119 NS_ENSURE_SUCCESS(rv, rv);
122 JSObject *obj = nsnull;
123 rv = aStatement->mStatementRowHolder->GetJSObject(&obj);
124 NS_ENSURE_SUCCESS(rv, rv);
126 *_row = OBJECT_TO_JSVAL(obj);
127 return NS_OK;
130 nsresult
131 mozStorageStatementJSHelper::getParams(mozStorageStatement *aStatement,
132 JSContext *aCtx, JSObject *aScopeObj,
133 jsval *_params)
135 nsresult rv;
137 PRInt32 state;
138 (void)aStatement->GetState(&state);
139 if (state != mozIStorageStatement::MOZ_STORAGE_STATEMENT_READY)
140 return NS_ERROR_UNEXPECTED;
142 if (!aStatement->mStatementParamsHolder) {
143 nsCOMPtr<mozIStorageStatementParams> params =
144 new mozStorageStatementParams(aStatement);
145 NS_ENSURE_TRUE(params, NS_ERROR_OUT_OF_MEMORY);
147 rv = mozStorageService::XPConnect()->WrapNative(
148 aCtx,
149 ::JS_GetGlobalForObject(aCtx, aScopeObj),
150 params,
151 NS_GET_IID(mozIStorageStatementParams),
152 getter_AddRefs(aStatement->mStatementParamsHolder)
154 NS_ENSURE_SUCCESS(rv, rv);
157 JSObject *obj = nsnull;
158 rv = aStatement->mStatementParamsHolder->GetJSObject(&obj);
159 NS_ENSURE_SUCCESS(rv, rv);
161 *_params = OBJECT_TO_JSVAL(obj);
162 return NS_OK;
165 NS_IMETHODIMP_(nsrefcnt) mozStorageStatementJSHelper::AddRef() { return 2; }
166 NS_IMETHODIMP_(nsrefcnt) mozStorageStatementJSHelper::Release() { return 1; }
167 NS_INTERFACE_MAP_BEGIN(mozStorageStatementJSHelper)
168 NS_INTERFACE_MAP_ENTRY(nsIXPCScriptable)
169 NS_INTERFACE_MAP_ENTRY(nsISupports)
170 NS_INTERFACE_MAP_END
172 ////////////////////////////////////////////////////////////////////////////////
173 //// nsIXPCScriptable
175 #define XPC_MAP_CLASSNAME mozStorageStatementJSHelper
176 #define XPC_MAP_QUOTED_CLASSNAME "mozStorageStatementJSHelper"
177 #define XPC_MAP_WANT_GETPROPERTY
178 #define XPC_MAP_WANT_NEWRESOLVE
179 #define XPC_MAP_FLAGS nsIXPCScriptable::ALLOW_PROP_MODS_DURING_RESOLVE
180 #include "xpc_map_end.h"
182 NS_IMETHODIMP
183 mozStorageStatementJSHelper::GetProperty(nsIXPConnectWrappedNative *aWrapper,
184 JSContext *aCtx, JSObject *aScopeObj,
185 jsval aId, jsval *_result,
186 PRBool *_retval)
188 if (!JSVAL_IS_STRING(aId))
189 return NS_OK;
191 mozStorageStatement *stmt =
192 static_cast<mozStorageStatement *>(aWrapper->Native());
194 #ifdef DEBUG
196 nsCOMPtr<mozIStorageStatement> isStatement(do_QueryInterface(stmt));
197 NS_ASSERTION(isStatement, "How is this not a statement?!");
199 #endif
201 const char *propName = JS_GetStringBytes(JSVAL_TO_STRING(aId));
202 if (strcmp(propName, "row") == 0)
203 return getRow(stmt, aCtx, aScopeObj, _result);
205 if (strcmp(propName, "params") == 0)
206 return getParams(stmt, aCtx, aScopeObj, _result);
208 return NS_OK;
212 NS_IMETHODIMP
213 mozStorageStatementJSHelper::NewResolve(nsIXPConnectWrappedNative *aWrapper,
214 JSContext *aCtx, JSObject *aScopeObj,
215 jsval aId, PRUint32 aFlags,
216 JSObject **_objp, PRBool *_retval)
218 if (!JSVAL_IS_STRING(aId))
219 return NS_OK;
221 const char *name = JS_GetStringBytes(JSVAL_TO_STRING(aId));
222 if (strcmp(name, "step") == 0) {
223 *_retval = JS_DefineFunction(aCtx, aScopeObj, "step", (JSNative)stepFunc, 0,
224 JSFUN_FAST_NATIVE) != nsnull;
225 *_objp = aScopeObj;
226 return NS_OK;
228 return NS_OK;