Bug 481045. <svg:use> shouldn't paint its kids. r+sr=roc
[mozilla-central.git] / storage / src / mozStorageStatementJSHelper.cpp
blobbbe75a14c9d49c9942ea9739260aaf44ce869c0c
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<nsIXPConnect> xpc(mozStorageService::XPConnect());
60 nsCOMPtr<nsIXPConnectWrappedNative> wrapper;
61 nsresult rv = xpc->GetWrappedNativeOfJSObject(
62 aCtx, JS_THIS_OBJECT(aCtx, _vp), getter_AddRefs(wrapper)
64 if (NS_FAILED(rv)) {
65 JS_ReportError(aCtx, "mozIStorageStatement::step() could not obtain native statement");
66 return JS_FALSE;
69 mozStorageStatement *stmt =
70 static_cast<mozStorageStatement *>(wrapper->Native());
72 #ifdef DEBUG
74 nsCOMPtr<mozIStorageStatement> isStatement(do_QueryInterface(stmt));
75 NS_ASSERTION(isStatement, "How is this not a statement?!");
77 #endif
79 PRBool hasMore = PR_FALSE;
80 rv = stmt->ExecuteStep(&hasMore);
81 if (NS_SUCCEEDED(rv) && !hasMore) {
82 *_vp = JSVAL_FALSE;
83 (void)stmt->Reset();
84 return JS_TRUE;
87 if (NS_FAILED(rv)) {
88 JS_ReportError(aCtx, "mozIStorageStatement::step() returned an error");
89 return JS_FALSE;
92 *_vp = BOOLEAN_TO_JSVAL(hasMore);
93 return JS_TRUE;
96 nsresult
97 mozStorageStatementJSHelper::getRow(mozStorageStatement *aStatement,
98 JSContext *aCtx, JSObject *aScopeObj,
99 jsval *_row)
101 nsresult rv;
103 PRInt32 state;
104 (void)aStatement->GetState(&state);
105 if (state != mozIStorageStatement::MOZ_STORAGE_STATEMENT_EXECUTING)
106 return NS_ERROR_UNEXPECTED;
108 if (!aStatement->mStatementRowHolder) {
109 nsCOMPtr<mozIStorageStatementRow> row =
110 new mozStorageStatementRow(aStatement);
111 NS_ENSURE_TRUE(row, NS_ERROR_OUT_OF_MEMORY);
113 nsCOMPtr<nsIXPConnect> xpc(mozStorageService::XPConnect());
114 rv = xpc->WrapNative(
115 aCtx,
116 ::JS_GetGlobalForObject(aCtx, aScopeObj),
117 row,
118 NS_GET_IID(mozIStorageStatementRow),
119 getter_AddRefs(aStatement->mStatementRowHolder)
121 NS_ENSURE_SUCCESS(rv, rv);
124 JSObject *obj = nsnull;
125 rv = aStatement->mStatementRowHolder->GetJSObject(&obj);
126 NS_ENSURE_SUCCESS(rv, rv);
128 *_row = OBJECT_TO_JSVAL(obj);
129 return NS_OK;
132 nsresult
133 mozStorageStatementJSHelper::getParams(mozStorageStatement *aStatement,
134 JSContext *aCtx, JSObject *aScopeObj,
135 jsval *_params)
137 nsresult rv;
139 PRInt32 state;
140 (void)aStatement->GetState(&state);
141 if (state != mozIStorageStatement::MOZ_STORAGE_STATEMENT_READY)
142 return NS_ERROR_UNEXPECTED;
144 if (!aStatement->mStatementParamsHolder) {
145 nsCOMPtr<mozIStorageStatementParams> params =
146 new mozStorageStatementParams(aStatement);
147 NS_ENSURE_TRUE(params, NS_ERROR_OUT_OF_MEMORY);
149 nsCOMPtr<nsIXPConnect> xpc(mozStorageService::XPConnect());
150 rv = xpc->WrapNative(
151 aCtx,
152 ::JS_GetGlobalForObject(aCtx, aScopeObj),
153 params,
154 NS_GET_IID(mozIStorageStatementParams),
155 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;