Bug 460546 - nsBrowserGlue should use a smart getter for the pref service. r=gavin
[wine-gecko.git] / storage / src / mozStorageStatementJSHelper.cpp
blob1267c9e455ee721ac0aecf93c00e10a1befba598
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"
43 #include "nsMemory.h"
44 #include "nsString.h"
45 #include "nsServiceManagerUtils.h"
47 #include "mozStorageStatementJSHelper.h"
49 #include "mozStorageStatementRow.h"
50 #include "mozStorageStatementParams.h"
52 #include "jsapi.h"
54 static nsIXPConnect *sXPConnect = nsnull;
55 static inline
56 nsIXPConnect *
57 XPConnect()
59 if (!sXPConnect) {
60 (void)CallGetService(nsIXPConnect::GetCID(), &sXPConnect);
61 NS_ASSERTION(sXPConnect, "Could not get XPConnect!");
63 return sXPConnect;
66 static
67 JSBool
68 stepFunc(JSContext *aCtx, PRUint32, jsval *_vp)
70 nsCOMPtr<nsIXPConnectWrappedNative> wrapper;
71 nsresult rv = XPConnect()->GetWrappedNativeOfJSObject(
72 aCtx, JS_THIS_OBJECT(aCtx, _vp), getter_AddRefs(wrapper)
74 if (NS_FAILED(rv)) {
75 JS_ReportError(aCtx, "mozIStorageStatement::step() could not obtain native statement");
76 return JS_FALSE;
79 mozStorageStatement *stmt =
80 static_cast<mozStorageStatement *>(wrapper->Native());
82 #ifdef DEBUG
84 nsCOMPtr<mozIStorageStatement> isStatement(do_QueryInterface(stmt));
85 NS_ASSERTION(isStatement, "How is this not a statement?!");
87 #endif
89 PRBool hasMore = PR_FALSE;
90 rv = stmt->ExecuteStep(&hasMore);
91 if (NS_SUCCEEDED(rv) && !hasMore) {
92 *_vp = JSVAL_FALSE;
93 (void)stmt->Reset();
94 return JS_TRUE;
97 if (NS_FAILED(rv)) {
98 JS_ReportError(aCtx, "mozIStorageStatement::step() returned an error");
99 return JS_FALSE;
102 *_vp = BOOLEAN_TO_JSVAL(hasMore);
103 return JS_TRUE;
106 nsresult
107 mozStorageStatementJSHelper::getRow(mozStorageStatement *aStatement,
108 JSContext *aCtx, JSObject *aScopeObj,
109 jsval *_row)
111 nsresult rv;
113 PRInt32 state;
114 (void)aStatement->GetState(&state);
115 if (state != mozIStorageStatement::MOZ_STORAGE_STATEMENT_EXECUTING)
116 return NS_ERROR_UNEXPECTED;
118 if (!aStatement->mStatementRowHolder) {
119 nsCOMPtr<mozIStorageStatementRow> row =
120 new mozStorageStatementRow(aStatement);
121 NS_ENSURE_TRUE(row, NS_ERROR_OUT_OF_MEMORY);
123 rv = XPConnect()->WrapNative(aCtx, ::JS_GetGlobalForObject(aCtx, aScopeObj),
124 row, NS_GET_IID(mozIStorageStatementRow),
125 getter_AddRefs(aStatement->mStatementRowHolder));
126 NS_ENSURE_SUCCESS(rv, rv);
129 JSObject *obj = nsnull;
130 rv = aStatement->mStatementRowHolder->GetJSObject(&obj);
131 NS_ENSURE_SUCCESS(rv, rv);
133 *_row = OBJECT_TO_JSVAL(obj);
134 return NS_OK;
137 nsresult
138 mozStorageStatementJSHelper::getParams(mozStorageStatement *aStatement,
139 JSContext *aCtx, JSObject *aScopeObj,
140 jsval *_params)
142 nsresult rv;
144 PRInt32 state;
145 (void)aStatement->GetState(&state);
146 if (state != mozIStorageStatement::MOZ_STORAGE_STATEMENT_READY)
147 return NS_ERROR_UNEXPECTED;
149 if (!aStatement->mStatementParamsHolder) {
150 nsCOMPtr<mozIStorageStatementParams> params =
151 new mozStorageStatementParams(aStatement);
152 NS_ENSURE_TRUE(params, NS_ERROR_OUT_OF_MEMORY);
154 rv = XPConnect()->WrapNative(aCtx, ::JS_GetGlobalForObject(aCtx, aScopeObj),
155 params, NS_GET_IID(mozIStorageStatementParams),
156 getter_AddRefs(aStatement->mStatementParamsHolder));
157 NS_ENSURE_SUCCESS(rv, rv);
160 JSObject *obj = nsnull;
161 rv = aStatement->mStatementParamsHolder->GetJSObject(&obj);
162 NS_ENSURE_SUCCESS(rv, rv);
164 *_params = OBJECT_TO_JSVAL(obj);
165 return NS_OK;
168 NS_IMETHODIMP_(nsrefcnt) mozStorageStatementJSHelper::AddRef() { return 2; }
169 NS_IMETHODIMP_(nsrefcnt) mozStorageStatementJSHelper::Release() { return 1; }
170 NS_INTERFACE_MAP_BEGIN(mozStorageStatementJSHelper)
171 NS_INTERFACE_MAP_ENTRY(nsIXPCScriptable)
172 NS_INTERFACE_MAP_ENTRY(nsISupports)
173 NS_INTERFACE_MAP_END
175 ////////////////////////////////////////////////////////////////////////////////
176 //// nsIXPCScriptable
178 #define XPC_MAP_CLASSNAME mozStorageStatementJSHelper
179 #define XPC_MAP_QUOTED_CLASSNAME "mozStorageStatementJSHelper"
180 #define XPC_MAP_WANT_GETPROPERTY
181 #define XPC_MAP_WANT_NEWRESOLVE
182 #define XPC_MAP_FLAGS nsIXPCScriptable::ALLOW_PROP_MODS_DURING_RESOLVE
183 #include "xpc_map_end.h"
185 NS_IMETHODIMP
186 mozStorageStatementJSHelper::GetProperty(nsIXPConnectWrappedNative *aWrapper,
187 JSContext *aCtx, JSObject *aScopeObj,
188 jsval aId, jsval *_result,
189 PRBool *_retval)
191 if (!JSVAL_IS_STRING(aId))
192 return NS_OK;
194 mozStorageStatement *stmt =
195 static_cast<mozStorageStatement *>(aWrapper->Native());
197 #ifdef DEBUG
199 nsCOMPtr<mozIStorageStatement> isStatement(do_QueryInterface(stmt));
200 NS_ASSERTION(isStatement, "How is this not a statement?!");
202 #endif
204 const char *propName = JS_GetStringBytes(JSVAL_TO_STRING(aId));
205 if (strcmp(propName, "row") == 0)
206 return getRow(stmt, aCtx, aScopeObj, _result);
208 if (strcmp(propName, "params") == 0)
209 return getParams(stmt, aCtx, aScopeObj, _result);
211 return NS_OK;
215 NS_IMETHODIMP
216 mozStorageStatementJSHelper::NewResolve(nsIXPConnectWrappedNative *aWrapper,
217 JSContext *aCtx, JSObject *aScopeObj,
218 jsval aId, PRUint32 aFlags,
219 JSObject **_objp, PRBool *_retval)
221 if (!JSVAL_IS_STRING(aId))
222 return NS_OK;
224 const char *name = JS_GetStringBytes(JSVAL_TO_STRING(aId));
225 if (strcmp(name, "step") == 0) {
226 *_retval = JS_DefineFunction(aCtx, aScopeObj, "step", (JSNative)stepFunc, 0,
227 JSFUN_FAST_NATIVE) != nsnull;
228 *_objp = aScopeObj;
229 return NS_OK;
231 return NS_OK;