2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2014-05-21 10:11:31 +04:00
|
|
|
/* 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"
|
2015-08-24 11:58:13 +03:00
|
|
|
#include "BluetoothUtils.h"
|
2014-05-21 10:11:31 +04:00
|
|
|
#include "DOMRequest.h"
|
2016-03-18 17:48:38 +03:00
|
|
|
#include "nsContentUtils.h"
|
2014-08-15 05:47:15 +04:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2014-05-21 10:11:31 +04:00
|
|
|
#include "mozilla/dom/bluetooth/BluetoothTypes.h"
|
2014-05-28 14:11:25 +04:00
|
|
|
#include "mozilla/dom/Promise.h"
|
2014-05-21 10:11:31 +04:00
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
|
2014-05-28 14:11:25 +04:00
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
2014-05-21 10:11:31 +04:00
|
|
|
USING_BLUETOOTH_NAMESPACE
|
|
|
|
|
2014-05-28 14:11:25 +04:00
|
|
|
BluetoothReplyRunnable::BluetoothReplyRunnable(nsIDOMDOMRequest* aReq,
|
2015-07-02 12:55:54 +03:00
|
|
|
Promise* aPromise)
|
2016-02-24 12:56:22 +03:00
|
|
|
: mPromise(aPromise)
|
|
|
|
, mDOMRequest(aReq)
|
2014-08-25 10:51:29 +04:00
|
|
|
, mErrorStatus(STATUS_FAIL)
|
2015-07-02 10:13:41 +03:00
|
|
|
{}
|
2014-05-21 10:11:31 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothReplyRunnable::SetReply(BluetoothReply* aReply)
|
|
|
|
{
|
2016-03-22 13:26:45 +03:00
|
|
|
mReply.reset(aReply);
|
2014-05-21 10:11:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothReplyRunnable::ReleaseMembers()
|
|
|
|
{
|
|
|
|
mDOMRequest = nullptr;
|
2014-05-28 14:11:25 +04:00
|
|
|
mPromise = nullptr;
|
2014-05-21 10:11:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
BluetoothReplyRunnable::~BluetoothReplyRunnable()
|
|
|
|
{}
|
|
|
|
|
|
|
|
nsresult
|
2014-05-28 14:11:25 +04:00
|
|
|
BluetoothReplyRunnable::FireReplySuccess(JS::Handle<JS::Value> aVal)
|
2014-05-21 10:11:31 +04:00
|
|
|
{
|
2014-05-28 14:11:25 +04: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 13:16:20 +04:00
|
|
|
|
2014-05-28 14:11:25 +04:00
|
|
|
// Promise
|
|
|
|
if (mPromise) {
|
|
|
|
mPromise->MaybeResolve(aVal);
|
|
|
|
}
|
2014-05-21 10:11:31 +04:00
|
|
|
|
2015-08-24 11:58:13 +03:00
|
|
|
OnSuccessFired();
|
2014-05-28 14:11:25 +04:00
|
|
|
return NS_OK;
|
2014-05-21 10:11:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
BluetoothReplyRunnable::FireErrorString()
|
|
|
|
{
|
2014-05-28 14:11:25 +04: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 13:16:20 +04:00
|
|
|
|
2014-05-28 14:11:25 +04:00
|
|
|
// Promise
|
|
|
|
if (mPromise) {
|
2014-08-25 10:51:29 +04:00
|
|
|
nsresult rv =
|
|
|
|
NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_DOM_BLUETOOTH, mErrorStatus);
|
|
|
|
mPromise->MaybeReject(rv);
|
2014-05-28 14:11:25 +04:00
|
|
|
}
|
2014-05-21 10:11:31 +04:00
|
|
|
|
2015-08-24 11:58:13 +03:00
|
|
|
OnErrorFired();
|
2014-05-28 14:11:25 +04:00
|
|
|
return NS_OK;
|
2014-05-21 10:11:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BluetoothReplyRunnable::Run()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mReply);
|
|
|
|
|
2016-03-18 17:48:38 +03:00
|
|
|
JS::Rooted<JS::Value> v(nsContentUtils::RootingCx(), JS::UndefinedValue());
|
2014-05-21 10:11:31 +04:00
|
|
|
|
2014-05-28 14:11:25 +04:00
|
|
|
nsresult rv;
|
2014-05-21 10:11:31 +04:00
|
|
|
if (mReply->type() != BluetoothReply::TBluetoothReplySuccess) {
|
2016-02-24 12:56:22 +03:00
|
|
|
ParseErrorStatus();
|
2014-05-28 14:11:25 +04:00
|
|
|
rv = FireErrorString();
|
|
|
|
} else if (!ParseSuccessfulReply(&v)) {
|
|
|
|
rv = FireErrorString();
|
2014-05-21 10:11:31 +04:00
|
|
|
} else {
|
2014-05-28 14:11:25 +04:00
|
|
|
rv = FireReplySuccess(v);
|
2014-05-21 10:11:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-28 14:11:25 +04:00
|
|
|
BT_WARNING("Could not fire DOMRequest/Promise!");
|
2014-05-21 10:11:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
ReleaseMembers();
|
|
|
|
MOZ_ASSERT(!mDOMRequest,
|
2014-05-28 14:11:25 +04:00
|
|
|
"mDOMRequest is still alive! Deriving class should call "
|
|
|
|
"BluetoothReplyRunnable::ReleaseMembers()!");
|
|
|
|
MOZ_ASSERT(!mPromise,
|
|
|
|
"mPromise is still alive! Deriving class should call "
|
2014-05-21 10:11:31 +04:00
|
|
|
"BluetoothReplyRunnable::ReleaseMembers()!");
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-08-24 11:58:13 +03:00
|
|
|
void
|
|
|
|
BluetoothReplyRunnable::OnSuccessFired()
|
|
|
|
{}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothReplyRunnable::OnErrorFired()
|
|
|
|
{}
|
|
|
|
|
2016-02-24 12:56:22 +03:00
|
|
|
void
|
|
|
|
BluetoothReplyRunnable::ParseErrorStatus()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mReply);
|
|
|
|
MOZ_ASSERT(mReply->type() == BluetoothReply::TBluetoothReplyError);
|
|
|
|
|
|
|
|
if (mReply->get_BluetoothReplyError().errorStatus().type() ==
|
|
|
|
BluetoothErrorStatus::TBluetoothStatus) {
|
|
|
|
SetError(
|
|
|
|
mReply->get_BluetoothReplyError().errorString(),
|
|
|
|
mReply->get_BluetoothReplyError().errorStatus().get_BluetoothStatus());
|
|
|
|
} else {
|
|
|
|
SetError(mReply->get_BluetoothReplyError().errorString(), STATUS_FAIL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-28 14:11:25 +04:00
|
|
|
BluetoothVoidReplyRunnable::BluetoothVoidReplyRunnable(nsIDOMDOMRequest* aReq,
|
2015-07-02 12:55:54 +03:00
|
|
|
Promise* aPromise)
|
|
|
|
: BluetoothReplyRunnable(aReq, aPromise)
|
2014-05-21 10:11:31 +04:00
|
|
|
{}
|
|
|
|
|
|
|
|
BluetoothVoidReplyRunnable::~BluetoothVoidReplyRunnable()
|
|
|
|
{}
|
|
|
|
|
2015-08-24 11:58:13 +03:00
|
|
|
BluetoothReplyTaskQueue::SubReplyRunnable::SubReplyRunnable(
|
|
|
|
nsIDOMDOMRequest* aReq,
|
|
|
|
Promise* aPromise,
|
|
|
|
BluetoothReplyTaskQueue* aRootQueue)
|
|
|
|
: BluetoothReplyRunnable(aReq, aPromise)
|
|
|
|
, mRootQueue(aRootQueue)
|
|
|
|
{}
|
|
|
|
|
|
|
|
BluetoothReplyTaskQueue::SubReplyRunnable::~SubReplyRunnable()
|
|
|
|
{}
|
|
|
|
|
|
|
|
BluetoothReplyTaskQueue*
|
|
|
|
BluetoothReplyTaskQueue::SubReplyRunnable::GetRootQueue() const
|
|
|
|
{
|
|
|
|
return mRootQueue;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothReplyTaskQueue::SubReplyRunnable::OnSuccessFired()
|
|
|
|
{
|
|
|
|
mRootQueue->OnSubReplySuccessFired(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothReplyTaskQueue::SubReplyRunnable::OnErrorFired()
|
|
|
|
{
|
|
|
|
mRootQueue->OnSubReplyErrorFired(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
BluetoothReplyTaskQueue::VoidSubReplyRunnable::VoidSubReplyRunnable(
|
|
|
|
nsIDOMDOMRequest* aReq,
|
|
|
|
Promise* aPromise,
|
|
|
|
BluetoothReplyTaskQueue* aRootQueue)
|
|
|
|
: BluetoothReplyTaskQueue::SubReplyRunnable(aReq, aPromise, aRootQueue)
|
|
|
|
{}
|
|
|
|
|
|
|
|
BluetoothReplyTaskQueue::VoidSubReplyRunnable::~VoidSubReplyRunnable()
|
|
|
|
{}
|
|
|
|
|
|
|
|
bool
|
|
|
|
BluetoothReplyTaskQueue::VoidSubReplyRunnable::ParseSuccessfulReply(
|
|
|
|
JS::MutableHandle<JS::Value> aValue)
|
|
|
|
{
|
|
|
|
aValue.setUndefined();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
BluetoothReplyTaskQueue::SubTask::SubTask(
|
|
|
|
BluetoothReplyTaskQueue* aRootQueue,
|
|
|
|
SubReplyRunnable* aReply)
|
|
|
|
: mRootQueue(aRootQueue)
|
|
|
|
, mReply(aReply)
|
|
|
|
{
|
|
|
|
if (!mReply) {
|
|
|
|
mReply = new VoidSubReplyRunnable(nullptr, nullptr, mRootQueue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BluetoothReplyTaskQueue::SubTask::~SubTask()
|
|
|
|
{}
|
|
|
|
|
|
|
|
BluetoothReplyTaskQueue*
|
|
|
|
BluetoothReplyTaskQueue::SubTask::GetRootQueue() const
|
|
|
|
{
|
|
|
|
return mRootQueue;
|
|
|
|
}
|
|
|
|
|
|
|
|
BluetoothReplyTaskQueue::SubReplyRunnable*
|
|
|
|
BluetoothReplyTaskQueue::SubTask::GetReply() const
|
|
|
|
{
|
|
|
|
return mReply;
|
|
|
|
}
|
|
|
|
|
|
|
|
BluetoothReplyTaskQueue::BluetoothReplyTaskQueue(
|
|
|
|
BluetoothReplyRunnable* aReply)
|
|
|
|
: mReply(aReply)
|
|
|
|
{}
|
|
|
|
|
|
|
|
BluetoothReplyTaskQueue::~BluetoothReplyTaskQueue()
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothReplyTaskQueue::AppendTask(already_AddRefed<SubTask> aTask)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<SubTask> task(aTask);
|
2015-08-24 11:58:13 +03:00
|
|
|
|
|
|
|
if (task) {
|
|
|
|
mTasks.AppendElement(task.forget());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BluetoothReplyTaskQueue::Run()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (!mTasks.IsEmpty()) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<SubTask> task = mTasks[0];
|
2015-08-24 11:58:13 +03:00
|
|
|
mTasks.RemoveElementAt(0);
|
|
|
|
|
|
|
|
MOZ_ASSERT(task);
|
|
|
|
|
|
|
|
if (!task->Execute()) {
|
|
|
|
FireErrorReply();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothReplyTaskQueue::OnSubReplySuccessFired(SubReplyRunnable* aSubReply)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aSubReply);
|
|
|
|
|
|
|
|
if (mTasks.IsEmpty()) {
|
|
|
|
FireSuccessReply();
|
|
|
|
} else {
|
|
|
|
if (NS_WARN_IF(NS_FAILED(NS_DispatchToMainThread(this)))) {
|
|
|
|
FireErrorReply();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothReplyTaskQueue::OnSubReplyErrorFired(SubReplyRunnable* aSubReply)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aSubReply);
|
|
|
|
|
|
|
|
FireErrorReply();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothReplyTaskQueue::FireSuccessReply()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (mReply) {
|
|
|
|
DispatchReplySuccess(mReply);
|
|
|
|
}
|
|
|
|
OnSuccessFired();
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothReplyTaskQueue::FireErrorReply()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (mReply) {
|
|
|
|
DispatchReplyError(mReply, STATUS_FAIL);
|
|
|
|
}
|
|
|
|
OnErrorFired();
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothReplyTaskQueue::OnSuccessFired()
|
|
|
|
{}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothReplyTaskQueue::OnErrorFired()
|
|
|
|
{}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothReplyTaskQueue::Clear()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
mReply = nullptr;
|
|
|
|
mTasks.Clear();
|
|
|
|
}
|