2014-05-20 23:11:31 -07:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "base/basictypes.h"
|
|
|
|
#include "BluetoothReplyRunnable.h"
|
|
|
|
#include "DOMRequest.h"
|
2014-08-14 18:47:15 -07:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2014-05-20 23:11:31 -07:00
|
|
|
#include "mozilla/dom/bluetooth/BluetoothTypes.h"
|
2014-05-28 03:11:25 -07:00
|
|
|
#include "mozilla/dom/Promise.h"
|
2014-05-20 23:11:31 -07:00
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
|
2014-05-28 03:11:25 -07:00
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
2014-05-20 23:11:31 -07:00
|
|
|
USING_BLUETOOTH_NAMESPACE
|
|
|
|
|
2014-05-28 03:11:25 -07:00
|
|
|
BluetoothReplyRunnable::BluetoothReplyRunnable(nsIDOMDOMRequest* aReq,
|
2014-06-16 00:50:45 -07:00
|
|
|
Promise* aPromise,
|
|
|
|
const nsAString& aName)
|
2014-05-20 23:11:31 -07:00
|
|
|
: mDOMRequest(aReq)
|
2014-05-28 03:11:25 -07:00
|
|
|
, mPromise(aPromise)
|
2014-08-24 23:51:29 -07:00
|
|
|
, mErrorStatus(STATUS_FAIL)
|
2014-06-16 00:50:45 -07:00
|
|
|
, mName(aName)
|
|
|
|
{
|
|
|
|
if (aPromise) {
|
|
|
|
BT_API2_LOGR("<%s>", NS_ConvertUTF16toUTF8(mName).get());
|
|
|
|
}
|
|
|
|
}
|
2014-05-20 23:11:31 -07:00
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothReplyRunnable::SetReply(BluetoothReply* aReply)
|
|
|
|
{
|
|
|
|
mReply = aReply;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothReplyRunnable::ReleaseMembers()
|
|
|
|
{
|
|
|
|
mDOMRequest = nullptr;
|
2014-05-28 03:11:25 -07:00
|
|
|
mPromise = nullptr;
|
2014-05-20 23:11:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
BluetoothReplyRunnable::~BluetoothReplyRunnable()
|
|
|
|
{}
|
|
|
|
|
|
|
|
nsresult
|
2014-05-28 03:11:25 -07:00
|
|
|
BluetoothReplyRunnable::FireReplySuccess(JS::Handle<JS::Value> aVal)
|
2014-05-20 23:11:31 -07:00
|
|
|
{
|
2014-05-28 03:11:25 -07:00
|
|
|
MOZ_ASSERT(mReply->type() == BluetoothReply::TBluetoothReplySuccess);
|
|
|
|
|
|
|
|
// DOMRequest
|
|
|
|
if (mDOMRequest) {
|
|
|
|
nsCOMPtr<nsIDOMRequestService> rs =
|
|
|
|
do_GetService(DOMREQUEST_SERVICE_CONTRACTID);
|
|
|
|
NS_ENSURE_TRUE(rs, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
return rs->FireSuccessAsync(mDOMRequest, aVal);
|
|
|
|
}
|
2014-05-29 02:16:20 -07:00
|
|
|
|
2014-05-28 03:11:25 -07:00
|
|
|
// Promise
|
|
|
|
if (mPromise) {
|
2014-06-16 00:50:45 -07:00
|
|
|
BT_API2_LOGR("<%s>", NS_ConvertUTF16toUTF8(mName).get());
|
2014-05-28 03:11:25 -07:00
|
|
|
mPromise->MaybeResolve(aVal);
|
|
|
|
}
|
2014-05-20 23:11:31 -07:00
|
|
|
|
2014-05-28 03:11:25 -07:00
|
|
|
return NS_OK;
|
2014-05-20 23:11:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
BluetoothReplyRunnable::FireErrorString()
|
|
|
|
{
|
2014-05-28 03:11:25 -07:00
|
|
|
// DOMRequest
|
|
|
|
if (mDOMRequest) {
|
|
|
|
nsCOMPtr<nsIDOMRequestService> rs =
|
|
|
|
do_GetService(DOMREQUEST_SERVICE_CONTRACTID);
|
|
|
|
NS_ENSURE_TRUE(rs, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
return rs->FireErrorAsync(mDOMRequest, mErrorString);
|
|
|
|
}
|
2014-05-29 02:16:20 -07:00
|
|
|
|
2014-05-28 03:11:25 -07:00
|
|
|
// Promise
|
|
|
|
if (mPromise) {
|
2014-06-16 00:50:45 -07:00
|
|
|
BT_API2_LOGR("<%s>", NS_ConvertUTF16toUTF8(mName).get());
|
|
|
|
|
2014-08-24 23:51:29 -07:00
|
|
|
nsresult rv =
|
|
|
|
NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_DOM_BLUETOOTH, mErrorStatus);
|
|
|
|
mPromise->MaybeReject(rv);
|
2014-05-28 03:11:25 -07:00
|
|
|
}
|
2014-05-20 23:11:31 -07:00
|
|
|
|
2014-05-28 03:11:25 -07:00
|
|
|
return NS_OK;
|
2014-05-20 23:11:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BluetoothReplyRunnable::Run()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mReply);
|
|
|
|
|
|
|
|
AutoSafeJSContext cx;
|
|
|
|
JS::Rooted<JS::Value> v(cx, JSVAL_VOID);
|
|
|
|
|
2014-05-28 03:11:25 -07:00
|
|
|
nsresult rv;
|
2014-05-20 23:11:31 -07:00
|
|
|
if (mReply->type() != BluetoothReply::TBluetoothReplySuccess) {
|
2014-08-24 23:51:29 -07:00
|
|
|
SetError(mReply->get_BluetoothReplyError().errorString(),
|
|
|
|
mReply->get_BluetoothReplyError().errorStatus());
|
2014-05-28 03:11:25 -07:00
|
|
|
rv = FireErrorString();
|
|
|
|
} else if (!ParseSuccessfulReply(&v)) {
|
|
|
|
rv = FireErrorString();
|
2014-05-20 23:11:31 -07:00
|
|
|
} else {
|
2014-05-28 03:11:25 -07:00
|
|
|
rv = FireReplySuccess(v);
|
2014-05-20 23:11:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-28 03:11:25 -07:00
|
|
|
BT_WARNING("Could not fire DOMRequest/Promise!");
|
2014-05-20 23:11:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ReleaseMembers();
|
|
|
|
MOZ_ASSERT(!mDOMRequest,
|
2014-05-28 03:11:25 -07:00
|
|
|
"mDOMRequest is still alive! Deriving class should call "
|
|
|
|
"BluetoothReplyRunnable::ReleaseMembers()!");
|
|
|
|
MOZ_ASSERT(!mPromise,
|
|
|
|
"mPromise is still alive! Deriving class should call "
|
2014-05-20 23:11:31 -07:00
|
|
|
"BluetoothReplyRunnable::ReleaseMembers()!");
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-05-28 03:11:25 -07:00
|
|
|
BluetoothVoidReplyRunnable::BluetoothVoidReplyRunnable(nsIDOMDOMRequest* aReq,
|
2014-06-16 00:50:45 -07:00
|
|
|
Promise* aPromise,
|
|
|
|
const nsAString& aName)
|
|
|
|
: BluetoothReplyRunnable(aReq, aPromise, aName)
|
2014-05-20 23:11:31 -07:00
|
|
|
{}
|
|
|
|
|
|
|
|
BluetoothVoidReplyRunnable::~BluetoothVoidReplyRunnable()
|
|
|
|
{}
|
|
|
|
|