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