1 /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
2 /* vim: set ts=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 file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "base/basictypes.h"
8 #include "BluetoothReplyRunnable.h"
9 #include "DOMRequest.h"
10 #include "mozilla/dom/ScriptSettings.h"
11 #include "mozilla/dom/bluetooth/BluetoothTypes.h"
12 #include "mozilla/dom/Promise.h"
13 #include "nsServiceManagerUtils.h"
15 using namespace mozilla::dom
;
17 USING_BLUETOOTH_NAMESPACE
19 BluetoothReplyRunnable::BluetoothReplyRunnable(nsIDOMDOMRequest
* aReq
,
21 const nsAString
& aName
)
24 , mErrorStatus(STATUS_FAIL
)
28 BT_API2_LOGR("<%s>", NS_ConvertUTF16toUTF8(mName
).get());
33 BluetoothReplyRunnable::SetReply(BluetoothReply
* aReply
)
39 BluetoothReplyRunnable::ReleaseMembers()
41 mDOMRequest
= nullptr;
45 BluetoothReplyRunnable::~BluetoothReplyRunnable()
49 BluetoothReplyRunnable::FireReplySuccess(JS::Handle
<JS::Value
> aVal
)
51 MOZ_ASSERT(mReply
->type() == BluetoothReply::TBluetoothReplySuccess
);
55 nsCOMPtr
<nsIDOMRequestService
> rs
=
56 do_GetService(DOMREQUEST_SERVICE_CONTRACTID
);
57 NS_ENSURE_TRUE(rs
, NS_ERROR_FAILURE
);
59 return rs
->FireSuccessAsync(mDOMRequest
, aVal
);
64 BT_API2_LOGR("<%s>", NS_ConvertUTF16toUTF8(mName
).get());
65 mPromise
->MaybeResolve(aVal
);
72 BluetoothReplyRunnable::FireErrorString()
76 nsCOMPtr
<nsIDOMRequestService
> rs
=
77 do_GetService(DOMREQUEST_SERVICE_CONTRACTID
);
78 NS_ENSURE_TRUE(rs
, NS_ERROR_FAILURE
);
80 return rs
->FireErrorAsync(mDOMRequest
, mErrorString
);
85 BT_API2_LOGR("<%s>", NS_ConvertUTF16toUTF8(mName
).get());
88 NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_DOM_BLUETOOTH
, mErrorStatus
);
89 mPromise
->MaybeReject(rv
);
96 BluetoothReplyRunnable::Run()
98 MOZ_ASSERT(NS_IsMainThread());
101 AutoSafeJSContext cx
;
102 JS::Rooted
<JS::Value
> v(cx
, JSVAL_VOID
);
105 if (mReply
->type() != BluetoothReply::TBluetoothReplySuccess
) {
106 SetError(mReply
->get_BluetoothReplyError().errorString(),
107 mReply
->get_BluetoothReplyError().errorStatus());
108 rv
= FireErrorString();
109 } else if (!ParseSuccessfulReply(&v
)) {
110 rv
= FireErrorString();
112 rv
= FireReplySuccess(v
);
116 BT_WARNING("Could not fire DOMRequest/Promise!");
120 MOZ_ASSERT(!mDOMRequest
,
121 "mDOMRequest is still alive! Deriving class should call "
122 "BluetoothReplyRunnable::ReleaseMembers()!");
123 MOZ_ASSERT(!mPromise
,
124 "mPromise is still alive! Deriving class should call "
125 "BluetoothReplyRunnable::ReleaseMembers()!");
130 BluetoothVoidReplyRunnable::BluetoothVoidReplyRunnable(nsIDOMDOMRequest
* aReq
,
132 const nsAString
& aName
)
133 : BluetoothReplyRunnable(aReq
, aPromise
, aName
)
136 BluetoothVoidReplyRunnable::~BluetoothVoidReplyRunnable()