Bumping manifests a=b2g-bump
[gecko.git] / dom / bluetooth2 / BluetoothReplyRunnable.cpp
blob78d36451c596804ec6156e4502909cd3d8bc75ff
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,
20 Promise* aPromise,
21 const nsAString& aName)
22 : mDOMRequest(aReq)
23 , mPromise(aPromise)
24 , mErrorStatus(STATUS_FAIL)
25 , mName(aName)
27 if (aPromise) {
28 BT_API2_LOGR("<%s>", NS_ConvertUTF16toUTF8(mName).get());
32 void
33 BluetoothReplyRunnable::SetReply(BluetoothReply* aReply)
35 mReply = aReply;
38 void
39 BluetoothReplyRunnable::ReleaseMembers()
41 mDOMRequest = nullptr;
42 mPromise = nullptr;
45 BluetoothReplyRunnable::~BluetoothReplyRunnable()
48 nsresult
49 BluetoothReplyRunnable::FireReplySuccess(JS::Handle<JS::Value> aVal)
51 MOZ_ASSERT(mReply->type() == BluetoothReply::TBluetoothReplySuccess);
53 // DOMRequest
54 if (mDOMRequest) {
55 nsCOMPtr<nsIDOMRequestService> rs =
56 do_GetService(DOMREQUEST_SERVICE_CONTRACTID);
57 NS_ENSURE_TRUE(rs, NS_ERROR_FAILURE);
59 return rs->FireSuccessAsync(mDOMRequest, aVal);
62 // Promise
63 if (mPromise) {
64 BT_API2_LOGR("<%s>", NS_ConvertUTF16toUTF8(mName).get());
65 mPromise->MaybeResolve(aVal);
68 return NS_OK;
71 nsresult
72 BluetoothReplyRunnable::FireErrorString()
74 // DOMRequest
75 if (mDOMRequest) {
76 nsCOMPtr<nsIDOMRequestService> rs =
77 do_GetService(DOMREQUEST_SERVICE_CONTRACTID);
78 NS_ENSURE_TRUE(rs, NS_ERROR_FAILURE);
80 return rs->FireErrorAsync(mDOMRequest, mErrorString);
83 // Promise
84 if (mPromise) {
85 BT_API2_LOGR("<%s>", NS_ConvertUTF16toUTF8(mName).get());
87 nsresult rv =
88 NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_DOM_BLUETOOTH, mErrorStatus);
89 mPromise->MaybeReject(rv);
92 return NS_OK;
95 NS_IMETHODIMP
96 BluetoothReplyRunnable::Run()
98 MOZ_ASSERT(NS_IsMainThread());
99 MOZ_ASSERT(mReply);
101 AutoSafeJSContext cx;
102 JS::Rooted<JS::Value> v(cx, JSVAL_VOID);
104 nsresult rv;
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();
111 } else {
112 rv = FireReplySuccess(v);
115 if (NS_FAILED(rv)) {
116 BT_WARNING("Could not fire DOMRequest/Promise!");
119 ReleaseMembers();
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()!");
127 return rv;
130 BluetoothVoidReplyRunnable::BluetoothVoidReplyRunnable(nsIDOMDOMRequest* aReq,
131 Promise* aPromise,
132 const nsAString& aName)
133 : BluetoothReplyRunnable(aReq, aPromise, aName)
136 BluetoothVoidReplyRunnable::~BluetoothVoidReplyRunnable()