1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
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 "SDBRequest.h"
10 #include "SDBConnection.h"
14 #include "mozilla/MacroForEach.h"
16 #include "nsISDBCallbacks.h"
17 #include "nsISupportsUtils.h"
18 #include "nsIVariant.h"
19 #include "nsThreadUtils.h"
22 namespace mozilla::dom
{
24 SDBRequest::SDBRequest(SDBConnection
* aConnection
)
25 : mConnection(aConnection
),
27 mHaveResultOrErrorCode(false) {
28 AssertIsOnOwningThread();
29 MOZ_ASSERT(aConnection
);
32 SDBRequest::~SDBRequest() { AssertIsOnOwningThread(); }
34 void SDBRequest::SetResult(nsIVariant
* aResult
) {
35 AssertIsOnOwningThread();
37 MOZ_ASSERT(mConnection
);
38 MOZ_ASSERT(!mHaveResultOrErrorCode
);
41 mHaveResultOrErrorCode
= true;
46 void SDBRequest::SetError(nsresult aRv
) {
47 AssertIsOnOwningThread();
48 MOZ_ASSERT(mConnection
);
49 MOZ_ASSERT(mResultCode
== NS_OK
);
50 MOZ_ASSERT(!mHaveResultOrErrorCode
);
53 mHaveResultOrErrorCode
= true;
58 void SDBRequest::FireCallback() {
59 AssertIsOnOwningThread();
62 nsCOMPtr
<nsISDBCallback
> callback
;
63 callback
.swap(mCallback
);
66 NS_DispatchToCurrentThread(NewRunnableMethod
<RefPtr
<SDBRequest
>>(
67 "nsISDBCallback::OnComplete", callback
, &nsISDBCallback::OnComplete
,
72 NS_IMPL_CYCLE_COLLECTING_ADDREF(SDBRequest
)
73 NS_IMPL_CYCLE_COLLECTING_RELEASE(SDBRequest
)
75 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(SDBRequest
)
76 NS_INTERFACE_MAP_ENTRY(nsISupports
)
77 NS_INTERFACE_MAP_ENTRY(nsISDBRequest
)
80 NS_IMPL_CYCLE_COLLECTION(SDBRequest
, mCallback
, mResult
)
83 SDBRequest::GetResult(nsIVariant
** aResult
) {
84 AssertIsOnOwningThread();
87 if (!mHaveResultOrErrorCode
) {
88 return NS_ERROR_NOT_AVAILABLE
;
91 NS_IF_ADDREF(*aResult
= mResult
);
96 SDBRequest::GetResultCode(nsresult
* aResultCode
) {
97 AssertIsOnOwningThread();
98 MOZ_ASSERT(aResultCode
);
100 if (!mHaveResultOrErrorCode
) {
101 return NS_ERROR_NOT_AVAILABLE
;
104 *aResultCode
= mResultCode
;
109 SDBRequest::GetCallback(nsISDBCallback
** aCallback
) {
110 AssertIsOnOwningThread();
111 MOZ_ASSERT(aCallback
);
113 NS_IF_ADDREF(*aCallback
= mCallback
);
118 SDBRequest::SetCallback(nsISDBCallback
* aCallback
) {
119 AssertIsOnOwningThread();
121 mCallback
= aCallback
;
125 } // namespace mozilla::dom