|
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 "nsMemory.h" |
|
8 #include "nsString.h" |
|
9 #include "nsCOMPtr.h" |
|
10 |
|
11 #include "jsapi.h" |
|
12 |
|
13 #include "mozStoragePrivateHelpers.h" |
|
14 #include "mozStorageAsyncStatement.h" |
|
15 #include "mozStorageAsyncStatementParams.h" |
|
16 #include "mozIStorageStatement.h" |
|
17 |
|
18 namespace mozilla { |
|
19 namespace storage { |
|
20 |
|
21 //////////////////////////////////////////////////////////////////////////////// |
|
22 //// AsyncStatementParams |
|
23 |
|
24 AsyncStatementParams::AsyncStatementParams(AsyncStatement *aStatement) |
|
25 : mStatement(aStatement) |
|
26 { |
|
27 NS_ASSERTION(mStatement != nullptr, "mStatement is null"); |
|
28 } |
|
29 |
|
30 NS_IMPL_ISUPPORTS( |
|
31 AsyncStatementParams |
|
32 , mozIStorageStatementParams |
|
33 , nsIXPCScriptable |
|
34 ) |
|
35 |
|
36 //////////////////////////////////////////////////////////////////////////////// |
|
37 //// nsIXPCScriptable |
|
38 |
|
39 #define XPC_MAP_CLASSNAME AsyncStatementParams |
|
40 #define XPC_MAP_QUOTED_CLASSNAME "AsyncStatementParams" |
|
41 #define XPC_MAP_WANT_SETPROPERTY |
|
42 #define XPC_MAP_WANT_NEWRESOLVE |
|
43 #define XPC_MAP_FLAGS nsIXPCScriptable::ALLOW_PROP_MODS_DURING_RESOLVE |
|
44 #include "xpc_map_end.h" |
|
45 |
|
46 NS_IMETHODIMP |
|
47 AsyncStatementParams::SetProperty( |
|
48 nsIXPConnectWrappedNative *aWrapper, |
|
49 JSContext *aCtx, |
|
50 JSObject *aScopeObj, |
|
51 jsid aId, |
|
52 JS::Value *_vp, |
|
53 bool *_retval |
|
54 ) |
|
55 { |
|
56 NS_ENSURE_TRUE(mStatement, NS_ERROR_NOT_INITIALIZED); |
|
57 |
|
58 if (JSID_IS_INT(aId)) { |
|
59 int idx = JSID_TO_INT(aId); |
|
60 |
|
61 nsCOMPtr<nsIVariant> variant(convertJSValToVariant(aCtx, *_vp)); |
|
62 NS_ENSURE_TRUE(variant, NS_ERROR_UNEXPECTED); |
|
63 nsresult rv = mStatement->BindByIndex(idx, variant); |
|
64 NS_ENSURE_SUCCESS(rv, rv); |
|
65 } |
|
66 else if (JSID_IS_STRING(aId)) { |
|
67 JSString *str = JSID_TO_STRING(aId); |
|
68 size_t length; |
|
69 const jschar *chars = JS_GetInternedStringCharsAndLength(str, &length); |
|
70 NS_ConvertUTF16toUTF8 name(chars, length); |
|
71 |
|
72 nsCOMPtr<nsIVariant> variant(convertJSValToVariant(aCtx, *_vp)); |
|
73 NS_ENSURE_TRUE(variant, NS_ERROR_UNEXPECTED); |
|
74 nsresult rv = mStatement->BindByName(name, variant); |
|
75 NS_ENSURE_SUCCESS(rv, rv); |
|
76 } |
|
77 else { |
|
78 return NS_ERROR_INVALID_ARG; |
|
79 } |
|
80 |
|
81 *_retval = true; |
|
82 return NS_OK; |
|
83 } |
|
84 |
|
85 NS_IMETHODIMP |
|
86 AsyncStatementParams::NewResolve( |
|
87 nsIXPConnectWrappedNative *aWrapper, |
|
88 JSContext *aCtx, |
|
89 JSObject *aScopeObj, |
|
90 jsid aId, |
|
91 JSObject **_objp, |
|
92 bool *_retval |
|
93 ) |
|
94 { |
|
95 JS::Rooted<JSObject*> scopeObj(aCtx, aScopeObj); |
|
96 |
|
97 NS_ENSURE_TRUE(mStatement, NS_ERROR_NOT_INITIALIZED); |
|
98 // We do not throw at any point after this because we want to allow the |
|
99 // prototype chain to be checked for the property. |
|
100 |
|
101 bool resolved = false; |
|
102 bool ok = true; |
|
103 if (JSID_IS_INT(aId)) { |
|
104 uint32_t idx = JSID_TO_INT(aId); |
|
105 // All indexes are good because we don't know how many parameters there |
|
106 // really are. |
|
107 ok = ::JS_DefineElement(aCtx, scopeObj, idx, JSVAL_VOID, nullptr, |
|
108 nullptr, 0); |
|
109 resolved = true; |
|
110 } |
|
111 else if (JSID_IS_STRING(aId)) { |
|
112 // We are unable to tell if there's a parameter with this name and so |
|
113 // we must assume that there is. This screws the rest of the prototype |
|
114 // chain, but people really shouldn't be depending on this anyways. |
|
115 ok = ::JS_DefinePropertyById(aCtx, scopeObj, aId, JSVAL_VOID, nullptr, |
|
116 nullptr, 0); |
|
117 resolved = true; |
|
118 } |
|
119 |
|
120 *_retval = ok; |
|
121 *_objp = resolved && ok ? scopeObj.get() : nullptr; |
|
122 return NS_OK; |
|
123 } |
|
124 |
|
125 } // namespace storage |
|
126 } // namespace mozilla |