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/. */
7 #include "nsIXPConnect.h"
8 #include "mozStorageAsyncStatement.h"
9 #include "mozStorageService.h"
13 #include "nsServiceManagerUtils.h"
15 #include "mozStorageAsyncStatementJSHelper.h"
17 #include "mozStorageAsyncStatementParams.h"
21 #include "xpc_make_class.h"
26 ////////////////////////////////////////////////////////////////////////////////
27 //// AsyncStatementJSHelper
30 AsyncStatementJSHelper::getParams(AsyncStatement
*aStatement
,
35 MOZ_ASSERT(NS_IsMainThread());
39 (void)aStatement
->GetState(&state
);
40 NS_ASSERTION(state
== mozIStorageAsyncStatement::MOZ_STORAGE_STATEMENT_READY
,
41 "Invalid state to get the params object - all calls will fail!");
44 JS::RootedObject
scope(aCtx
, aScopeObj
);
46 if (!aStatement
->mStatementParamsHolder
) {
47 dom::GlobalObject
global(aCtx
, scope
);
48 if (global
.Failed()) {
49 return NS_ERROR_UNEXPECTED
;
52 nsCOMPtr
<nsPIDOMWindowInner
> window
= do_QueryInterface(global
.GetAsSupports());
54 RefPtr
<AsyncStatementParams
> params(new AsyncStatementParams(window
, aStatement
));
55 NS_ENSURE_TRUE(params
, NS_ERROR_OUT_OF_MEMORY
);
57 RefPtr
<AsyncStatementParamsHolder
> paramsHolder
= new AsyncStatementParamsHolder(params
);
58 NS_ENSURE_TRUE(paramsHolder
, NS_ERROR_OUT_OF_MEMORY
);
60 aStatement
->mStatementParamsHolder
=
61 new nsMainThreadPtrHolder
<AsyncStatementParamsHolder
>(
62 "Statement::mStatementParamsHolder", paramsHolder
);
65 RefPtr
<AsyncStatementParams
> params(aStatement
->mStatementParamsHolder
->Get());
66 JSObject
* obj
= params
->WrapObject(aCtx
, nullptr);
68 return NS_ERROR_UNEXPECTED
;
71 _params
->setObject(*obj
);
75 NS_IMETHODIMP_(MozExternalRefCountType
) AsyncStatementJSHelper::AddRef() { return 2; }
76 NS_IMETHODIMP_(MozExternalRefCountType
) AsyncStatementJSHelper::Release() { return 1; }
77 NS_INTERFACE_MAP_BEGIN(AsyncStatementJSHelper
)
78 NS_INTERFACE_MAP_ENTRY(nsIXPCScriptable
)
79 NS_INTERFACE_MAP_ENTRY(nsISupports
)
82 ////////////////////////////////////////////////////////////////////////////////
85 #define XPC_MAP_CLASSNAME AsyncStatementJSHelper
86 #define XPC_MAP_QUOTED_CLASSNAME "AsyncStatementJSHelper"
87 #define XPC_MAP_FLAGS (XPC_SCRIPTABLE_WANT_RESOLVE | \
88 XPC_SCRIPTABLE_ALLOW_PROP_MODS_DURING_RESOLVE)
89 #include "xpc_map_end.h"
92 AsyncStatementJSHelper::Resolve(nsIXPConnectWrappedNative
*aWrapper
,
99 if (!JSID_IS_STRING(aId
))
102 // Cast to async via mozI* since direct from nsISupports is ambiguous.
103 JS::RootedObject
scope(aCtx
, aScopeObj
);
104 JS::RootedId
id(aCtx
, aId
);
105 mozIStorageAsyncStatement
*iAsyncStmt
=
106 static_cast<mozIStorageAsyncStatement
*>(aWrapper
->Native());
107 AsyncStatement
*stmt
= static_cast<AsyncStatement
*>(iAsyncStmt
);
111 nsISupports
*supp
= aWrapper
->Native();
112 nsCOMPtr
<mozIStorageAsyncStatement
> isStatement(do_QueryInterface(supp
));
113 NS_ASSERTION(isStatement
, "How is this not an async statement?!");
117 if (::JS_FlatStringEqualsAscii(JSID_TO_FLAT_STRING(id
), "params")) {
118 JS::RootedValue
val(aCtx
);
119 nsresult rv
= getParams(stmt
, aCtx
, scope
, val
.address());
120 NS_ENSURE_SUCCESS(rv
, rv
);
121 *_retval
= ::JS_DefinePropertyById(aCtx
, scope
, id
, val
, JSPROP_RESOLVING
);
129 ////////////////////////////////////////////////////////////////////////////////
130 //// AsyncStatementParamsHolder
132 NS_IMPL_ISUPPORTS0(AsyncStatementParamsHolder
);
134 AsyncStatementParamsHolder::~AsyncStatementParamsHolder()
136 MOZ_ASSERT(NS_IsMainThread());
137 // We are considered dead at this point, so any wrappers for row or params
138 // need to lose their reference to the statement.
139 mParams
->mStatement
= nullptr;
142 } // namespace storage
143 } // namespace mozilla