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 : * ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
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/
10 : *
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.
15 : *
16 : * The Original Code is Oracle Corporation code.
17 : *
18 : * The Initial Developer of the Original Code is
19 : * Oracle Corporation
20 : * Portions created by the Initial Developer are Copyright (C) 2004
21 : * the Initial Developer. All Rights Reserved.
22 : *
23 : * Contributor(s):
24 : * Vladimir Vukicevic <vladimir.vukicevic@oracle.com>
25 : * Shawn Wilsher <me@shawnwilsher.com>
26 : *
27 : * Alternatively, the contents of this file may be used under the terms of
28 : * either the GNU General Public License Version 2 or later (the "GPL"), or
29 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 : * in which case the provisions of the GPL or the LGPL are applicable instead
31 : * of those above. If you wish to allow use of your version of this file only
32 : * under the terms of either the GPL or the LGPL, and not to allow others to
33 : * use your version of this file under the terms of the MPL, indicate your
34 : * decision by deleting the provisions above and replace them with the notice
35 : * and other provisions required by the GPL or the LGPL. If you do not delete
36 : * the provisions above, a recipient may use your version of this file under
37 : * the terms of any one of the MPL, the GPL or the LGPL.
38 : *
39 : * ***** END LICENSE BLOCK ***** */
40 :
41 : #include "nsMemory.h"
42 : #include "nsString.h"
43 : #include "nsCOMPtr.h"
44 :
45 : #include "mozStoragePrivateHelpers.h"
46 : #include "mozStorageAsyncStatement.h"
47 : #include "mozStorageAsyncStatementParams.h"
48 : #include "mozIStorageStatement.h"
49 :
50 : namespace mozilla {
51 : namespace storage {
52 :
53 : ////////////////////////////////////////////////////////////////////////////////
54 : //// AsyncStatementParams
55 :
56 1276 : AsyncStatementParams::AsyncStatementParams(AsyncStatement *aStatement)
57 1276 : : mStatement(aStatement)
58 : {
59 1276 : NS_ASSERTION(mStatement != nsnull, "mStatement is null");
60 1276 : }
61 :
62 39578 : NS_IMPL_ISUPPORTS2(
63 : AsyncStatementParams
64 : , mozIStorageStatementParams
65 : , nsIXPCScriptable
66 : )
67 :
68 : ////////////////////////////////////////////////////////////////////////////////
69 : //// nsIXPCScriptable
70 :
71 : #define XPC_MAP_CLASSNAME AsyncStatementParams
72 : #define XPC_MAP_QUOTED_CLASSNAME "AsyncStatementParams"
73 : #define XPC_MAP_WANT_SETPROPERTY
74 : #define XPC_MAP_WANT_NEWRESOLVE
75 : #define XPC_MAP_FLAGS nsIXPCScriptable::ALLOW_PROP_MODS_DURING_RESOLVE
76 : #include "xpc_map_end.h"
77 :
78 : NS_IMETHODIMP
79 15645 : AsyncStatementParams::SetProperty(
80 : nsIXPConnectWrappedNative *aWrapper,
81 : JSContext *aCtx,
82 : JSObject *aScopeObj,
83 : jsid aId,
84 : jsval *_vp,
85 : bool *_retval
86 : )
87 : {
88 15645 : NS_ENSURE_TRUE(mStatement, NS_ERROR_NOT_INITIALIZED);
89 :
90 15645 : if (JSID_IS_INT(aId)) {
91 4 : int idx = JSID_TO_INT(aId);
92 :
93 8 : nsCOMPtr<nsIVariant> variant(convertJSValToVariant(aCtx, *_vp));
94 4 : NS_ENSURE_TRUE(variant, NS_ERROR_UNEXPECTED);
95 4 : nsresult rv = mStatement->BindByIndex(idx, variant);
96 4 : NS_ENSURE_SUCCESS(rv, rv);
97 : }
98 15641 : else if (JSID_IS_STRING(aId)) {
99 15641 : JSString *str = JSID_TO_STRING(aId);
100 : size_t length;
101 15641 : const jschar *chars = JS_GetInternedStringCharsAndLength(str, &length);
102 31282 : NS_ConvertUTF16toUTF8 name(chars, length);
103 :
104 31282 : nsCOMPtr<nsIVariant> variant(convertJSValToVariant(aCtx, *_vp));
105 15641 : NS_ENSURE_TRUE(variant, NS_ERROR_UNEXPECTED);
106 15641 : nsresult rv = mStatement->BindByName(name, variant);
107 15641 : NS_ENSURE_SUCCESS(rv, rv);
108 : }
109 : else {
110 0 : return NS_ERROR_INVALID_ARG;
111 : }
112 :
113 15645 : *_retval = true;
114 15645 : return NS_OK;
115 : }
116 :
117 : NS_IMETHODIMP
118 2876 : AsyncStatementParams::NewResolve(
119 : nsIXPConnectWrappedNative *aWrapper,
120 : JSContext *aCtx,
121 : JSObject *aScopeObj,
122 : jsid aId,
123 : PRUint32 aFlags,
124 : JSObject **_objp,
125 : bool *_retval
126 : )
127 : {
128 2876 : NS_ENSURE_TRUE(mStatement, NS_ERROR_NOT_INITIALIZED);
129 : // We do not throw at any point after this because we want to allow the
130 : // prototype chain to be checked for the property.
131 :
132 2876 : bool resolved = false;
133 2876 : bool ok = true;
134 2876 : if (JSID_IS_INT(aId)) {
135 4 : PRUint32 idx = JSID_TO_INT(aId);
136 : // All indexes are good because we don't know how many parameters there
137 : // really are.
138 : ok = ::JS_DefineElement(aCtx, aScopeObj, idx, JSVAL_VOID, nsnull,
139 4 : nsnull, 0);
140 4 : resolved = true;
141 : }
142 2872 : else if (JSID_IS_STRING(aId)) {
143 : // We are unable to tell if there's a parameter with this name and so
144 : // we must assume that there is. This screws the rest of the prototype
145 : // chain, but people really shouldn't be depending on this anyways.
146 : ok = ::JS_DefinePropertyById(aCtx, aScopeObj, aId, JSVAL_VOID, nsnull,
147 2872 : nsnull, 0);
148 2872 : resolved = true;
149 : }
150 :
151 2876 : *_retval = ok;
152 2876 : *_objp = resolved && ok ? aScopeObj : nsnull;
153 2876 : return NS_OK;
154 : }
155 :
156 : } // namespace storage
157 : } // namespace mozilla
|