|
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/. */ |
|
6 |
|
7 #include "nsIXPConnect.h" |
|
8 #include "mozStorageStatement.h" |
|
9 #include "mozStorageService.h" |
|
10 |
|
11 #include "nsMemory.h" |
|
12 #include "nsString.h" |
|
13 #include "nsServiceManagerUtils.h" |
|
14 |
|
15 #include "mozStorageStatementJSHelper.h" |
|
16 |
|
17 #include "mozStorageStatementRow.h" |
|
18 #include "mozStorageStatementParams.h" |
|
19 |
|
20 #include "jsapi.h" |
|
21 |
|
22 namespace mozilla { |
|
23 namespace storage { |
|
24 |
|
25 //////////////////////////////////////////////////////////////////////////////// |
|
26 //// Global Functions |
|
27 |
|
28 static |
|
29 bool |
|
30 stepFunc(JSContext *aCtx, |
|
31 uint32_t, |
|
32 jsval *_vp) |
|
33 { |
|
34 nsCOMPtr<nsIXPConnect> xpc(Service::getXPConnect()); |
|
35 nsCOMPtr<nsIXPConnectWrappedNative> wrapper; |
|
36 JSObject *obj = JS_THIS_OBJECT(aCtx, _vp); |
|
37 if (!obj) { |
|
38 return false; |
|
39 } |
|
40 |
|
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; |
|
46 } |
|
47 |
|
48 #ifdef DEBUG |
|
49 { |
|
50 nsCOMPtr<mozIStorageStatement> isStatement( |
|
51 do_QueryInterface(wrapper->Native()) |
|
52 ); |
|
53 NS_ASSERTION(isStatement, "How is this not a statement?!"); |
|
54 } |
|
55 #endif |
|
56 |
|
57 Statement *stmt = static_cast<Statement *>( |
|
58 static_cast<mozIStorageStatement *>(wrapper->Native()) |
|
59 ); |
|
60 |
|
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; |
|
67 } |
|
68 |
|
69 if (NS_FAILED(rv)) { |
|
70 ::JS_ReportError(aCtx, "mozIStorageStatement::step() returned an error"); |
|
71 return false; |
|
72 } |
|
73 |
|
74 *_vp = BOOLEAN_TO_JSVAL(hasMore); |
|
75 return true; |
|
76 } |
|
77 |
|
78 //////////////////////////////////////////////////////////////////////////////// |
|
79 //// StatementJSHelper |
|
80 |
|
81 nsresult |
|
82 StatementJSHelper::getRow(Statement *aStatement, |
|
83 JSContext *aCtx, |
|
84 JSObject *aScopeObj, |
|
85 jsval *_row) |
|
86 { |
|
87 nsresult rv; |
|
88 |
|
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 |
|
95 |
|
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); |
|
100 |
|
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) |
|
108 ); |
|
109 NS_ENSURE_SUCCESS(rv, rv); |
|
110 } |
|
111 |
|
112 JS::Rooted<JSObject*> obj(aCtx); |
|
113 obj = aStatement->mStatementRowHolder->GetJSObject(); |
|
114 NS_ENSURE_STATE(obj); |
|
115 |
|
116 *_row = OBJECT_TO_JSVAL(obj); |
|
117 return NS_OK; |
|
118 } |
|
119 |
|
120 nsresult |
|
121 StatementJSHelper::getParams(Statement *aStatement, |
|
122 JSContext *aCtx, |
|
123 JSObject *aScopeObj, |
|
124 jsval *_params) |
|
125 { |
|
126 nsresult rv; |
|
127 |
|
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 |
|
134 |
|
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); |
|
140 |
|
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) |
|
148 ); |
|
149 NS_ENSURE_SUCCESS(rv, rv); |
|
150 } |
|
151 |
|
152 JS::Rooted<JSObject*> obj(aCtx); |
|
153 obj = aStatement->mStatementParamsHolder->GetJSObject(); |
|
154 NS_ENSURE_STATE(obj); |
|
155 |
|
156 *_params = OBJECT_TO_JSVAL(obj); |
|
157 return NS_OK; |
|
158 } |
|
159 |
|
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 |
|
166 |
|
167 //////////////////////////////////////////////////////////////////////////////// |
|
168 //// nsIXPCScriptable |
|
169 |
|
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" |
|
176 |
|
177 NS_IMETHODIMP |
|
178 StatementJSHelper::GetProperty(nsIXPConnectWrappedNative *aWrapper, |
|
179 JSContext *aCtx, |
|
180 JSObject *aScopeObj, |
|
181 jsid aId, |
|
182 jsval *_result, |
|
183 bool *_retval) |
|
184 { |
|
185 if (!JSID_IS_STRING(aId)) |
|
186 return NS_OK; |
|
187 |
|
188 JS::Rooted<JSObject*> scope(aCtx, aScopeObj); |
|
189 JS::Rooted<jsid> id(aCtx, aId); |
|
190 |
|
191 #ifdef DEBUG |
|
192 { |
|
193 nsCOMPtr<mozIStorageStatement> isStatement( |
|
194 do_QueryInterface(aWrapper->Native())); |
|
195 NS_ASSERTION(isStatement, "How is this not a statement?!"); |
|
196 } |
|
197 #endif |
|
198 |
|
199 Statement *stmt = static_cast<Statement *>( |
|
200 static_cast<mozIStorageStatement *>(aWrapper->Native()) |
|
201 ); |
|
202 |
|
203 JSFlatString *str = JSID_TO_FLAT_STRING(id); |
|
204 if (::JS_FlatStringEqualsAscii(str, "row")) |
|
205 return getRow(stmt, aCtx, scope, _result); |
|
206 |
|
207 if (::JS_FlatStringEqualsAscii(str, "params")) |
|
208 return getParams(stmt, aCtx, scope, _result); |
|
209 |
|
210 return NS_OK; |
|
211 } |
|
212 |
|
213 |
|
214 NS_IMETHODIMP |
|
215 StatementJSHelper::NewResolve(nsIXPConnectWrappedNative *aWrapper, |
|
216 JSContext *aCtx, |
|
217 JSObject *aScopeObj, |
|
218 jsid aId, |
|
219 JSObject **_objp, |
|
220 bool *_retval) |
|
221 { |
|
222 if (!JSID_IS_STRING(aId)) |
|
223 return NS_OK; |
|
224 |
|
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; |
|
231 } |
|
232 return NS_OK; |
|
233 } |
|
234 |
|
235 } // namespace storage |
|
236 } // namespace mozilla |