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
29 nsresult
AsyncStatementJSHelper::getParams(AsyncStatement
* aStatement
,
30 JSContext
* aCtx
, JSObject
* aScopeObj
,
32 MOZ_ASSERT(NS_IsMainThread());
36 (void)aStatement
->GetState(&state
);
37 NS_ASSERTION(state
== mozIStorageAsyncStatement::MOZ_STORAGE_STATEMENT_READY
,
38 "Invalid state to get the params object - all calls will fail!");
41 JS::RootedObject
scope(aCtx
, aScopeObj
);
43 if (!aStatement
->mStatementParamsHolder
) {
44 dom::GlobalObject
global(aCtx
, scope
);
45 if (global
.Failed()) {
46 return NS_ERROR_UNEXPECTED
;
49 nsCOMPtr
<nsPIDOMWindowInner
> window
=
50 do_QueryInterface(global
.GetAsSupports());
52 RefPtr
<AsyncStatementParams
> params(
53 new AsyncStatementParams(window
, aStatement
));
54 NS_ENSURE_TRUE(params
, NS_ERROR_OUT_OF_MEMORY
);
56 RefPtr
<AsyncStatementParamsHolder
> paramsHolder
=
57 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(
66 aStatement
->mStatementParamsHolder
->Get());
67 JSObject
* obj
= params
->WrapObject(aCtx
, nullptr);
69 return NS_ERROR_UNEXPECTED
;
72 _params
->setObject(*obj
);
76 NS_IMETHODIMP_(MozExternalRefCountType
) AsyncStatementJSHelper::AddRef() {
79 NS_IMETHODIMP_(MozExternalRefCountType
) AsyncStatementJSHelper::Release() {
82 NS_INTERFACE_MAP_BEGIN(AsyncStatementJSHelper
)
83 NS_INTERFACE_MAP_ENTRY(nsIXPCScriptable
)
84 NS_INTERFACE_MAP_ENTRY(nsISupports
)
87 ////////////////////////////////////////////////////////////////////////////////
90 #define XPC_MAP_CLASSNAME AsyncStatementJSHelper
91 #define XPC_MAP_QUOTED_CLASSNAME "AsyncStatementJSHelper"
92 #define XPC_MAP_FLAGS \
93 (XPC_SCRIPTABLE_WANT_RESOLVE | XPC_SCRIPTABLE_ALLOW_PROP_MODS_DURING_RESOLVE)
94 #include "xpc_map_end.h"
97 AsyncStatementJSHelper::Resolve(nsIXPConnectWrappedNative
* aWrapper
,
98 JSContext
* aCtx
, JSObject
* aScopeObj
, jsid aId
,
99 bool* resolvedp
, bool* _retval
) {
100 if (!JSID_IS_STRING(aId
)) return NS_OK
;
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_LinearStringEqualsLiteral(JSID_TO_LINEAR_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() {
135 MOZ_ASSERT(NS_IsMainThread());
136 // We are considered dead at this point, so any wrappers for row or params
137 // need to lose their reference to the statement.
138 mParams
->mStatement
= nullptr;
141 } // namespace storage
142 } // namespace mozilla