|
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 "mozStorageAsyncStatement.h" |
|
9 #include "mozStorageService.h" |
|
10 |
|
11 #include "nsMemory.h" |
|
12 #include "nsString.h" |
|
13 #include "nsServiceManagerUtils.h" |
|
14 |
|
15 #include "mozStorageAsyncStatementJSHelper.h" |
|
16 |
|
17 #include "mozStorageAsyncStatementParams.h" |
|
18 |
|
19 #include "jsapi.h" |
|
20 |
|
21 namespace mozilla { |
|
22 namespace storage { |
|
23 |
|
24 //////////////////////////////////////////////////////////////////////////////// |
|
25 //// AsyncStatementJSHelper |
|
26 |
|
27 nsresult |
|
28 AsyncStatementJSHelper::getParams(AsyncStatement *aStatement, |
|
29 JSContext *aCtx, |
|
30 JSObject *aScopeObj, |
|
31 jsval *_params) |
|
32 { |
|
33 nsresult rv; |
|
34 |
|
35 #ifdef DEBUG |
|
36 int32_t state; |
|
37 (void)aStatement->GetState(&state); |
|
38 NS_ASSERTION(state == mozIStorageAsyncStatement::MOZ_STORAGE_STATEMENT_READY, |
|
39 "Invalid state to get the params object - all calls will fail!"); |
|
40 #endif |
|
41 |
|
42 if (!aStatement->mStatementParamsHolder) { |
|
43 nsCOMPtr<mozIStorageStatementParams> params = |
|
44 new AsyncStatementParams(aStatement); |
|
45 NS_ENSURE_TRUE(params, NS_ERROR_OUT_OF_MEMORY); |
|
46 |
|
47 JS::RootedObject scope(aCtx, aScopeObj); |
|
48 nsCOMPtr<nsIXPConnect> xpc(Service::getXPConnect()); |
|
49 rv = xpc->WrapNative( |
|
50 aCtx, |
|
51 ::JS_GetGlobalForObject(aCtx, scope), |
|
52 params, |
|
53 NS_GET_IID(mozIStorageStatementParams), |
|
54 getter_AddRefs(aStatement->mStatementParamsHolder) |
|
55 ); |
|
56 NS_ENSURE_SUCCESS(rv, rv); |
|
57 } |
|
58 |
|
59 JS::Rooted<JSObject*> obj(aCtx); |
|
60 obj = aStatement->mStatementParamsHolder->GetJSObject(); |
|
61 NS_ENSURE_STATE(obj); |
|
62 |
|
63 *_params = OBJECT_TO_JSVAL(obj); |
|
64 return NS_OK; |
|
65 } |
|
66 |
|
67 NS_IMETHODIMP_(MozExternalRefCountType) AsyncStatementJSHelper::AddRef() { return 2; } |
|
68 NS_IMETHODIMP_(MozExternalRefCountType) AsyncStatementJSHelper::Release() { return 1; } |
|
69 NS_INTERFACE_MAP_BEGIN(AsyncStatementJSHelper) |
|
70 NS_INTERFACE_MAP_ENTRY(nsIXPCScriptable) |
|
71 NS_INTERFACE_MAP_ENTRY(nsISupports) |
|
72 NS_INTERFACE_MAP_END |
|
73 |
|
74 //////////////////////////////////////////////////////////////////////////////// |
|
75 //// nsIXPCScriptable |
|
76 |
|
77 #define XPC_MAP_CLASSNAME AsyncStatementJSHelper |
|
78 #define XPC_MAP_QUOTED_CLASSNAME "AsyncStatementJSHelper" |
|
79 #define XPC_MAP_WANT_GETPROPERTY |
|
80 #define XPC_MAP_FLAGS nsIXPCScriptable::ALLOW_PROP_MODS_DURING_RESOLVE |
|
81 #include "xpc_map_end.h" |
|
82 |
|
83 NS_IMETHODIMP |
|
84 AsyncStatementJSHelper::GetProperty(nsIXPConnectWrappedNative *aWrapper, |
|
85 JSContext *aCtx, |
|
86 JSObject *aScopeObj, |
|
87 jsid aId, |
|
88 jsval *_result, |
|
89 bool *_retval) |
|
90 { |
|
91 if (!JSID_IS_STRING(aId)) |
|
92 return NS_OK; |
|
93 |
|
94 // Cast to async via mozI* since direct from nsISupports is ambiguous. |
|
95 JS::RootedObject scope(aCtx, aScopeObj); |
|
96 JS::RootedId id(aCtx, aId); |
|
97 mozIStorageAsyncStatement *iAsyncStmt = |
|
98 static_cast<mozIStorageAsyncStatement *>(aWrapper->Native()); |
|
99 AsyncStatement *stmt = static_cast<AsyncStatement *>(iAsyncStmt); |
|
100 |
|
101 #ifdef DEBUG |
|
102 { |
|
103 nsISupports *supp = aWrapper->Native(); |
|
104 nsCOMPtr<mozIStorageAsyncStatement> isStatement(do_QueryInterface(supp)); |
|
105 NS_ASSERTION(isStatement, "How is this not an async statement?!"); |
|
106 } |
|
107 #endif |
|
108 |
|
109 if (::JS_FlatStringEqualsAscii(JSID_TO_FLAT_STRING(id), "params")) |
|
110 return getParams(stmt, aCtx, scope, _result); |
|
111 |
|
112 return NS_OK; |
|
113 } |
|
114 |
|
115 } // namespace storage |
|
116 } // namespace mozilla |