michael@0: /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */ michael@0: /* vim: set ts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "base/basictypes.h" michael@0: #include "BluetoothReplyRunnable.h" michael@0: #include "DOMRequest.h" michael@0: #include "mozilla/dom/bluetooth/BluetoothTypes.h" michael@0: #include "nsServiceManagerUtils.h" michael@0: michael@0: USING_BLUETOOTH_NAMESPACE michael@0: michael@0: BluetoothReplyRunnable::BluetoothReplyRunnable(nsIDOMDOMRequest* aReq) michael@0: : mDOMRequest(aReq) michael@0: {} michael@0: michael@0: void michael@0: BluetoothReplyRunnable::SetReply(BluetoothReply* aReply) michael@0: { michael@0: mReply = aReply; michael@0: } michael@0: michael@0: void michael@0: BluetoothReplyRunnable::ReleaseMembers() michael@0: { michael@0: mDOMRequest = nullptr; michael@0: } michael@0: michael@0: BluetoothReplyRunnable::~BluetoothReplyRunnable() michael@0: {} michael@0: michael@0: nsresult michael@0: BluetoothReplyRunnable::FireReply(JS::Handle aVal) michael@0: { michael@0: nsCOMPtr rs = michael@0: do_GetService(DOMREQUEST_SERVICE_CONTRACTID); michael@0: NS_ENSURE_TRUE(rs, NS_ERROR_FAILURE); michael@0: michael@0: return mReply->type() == BluetoothReply::TBluetoothReplySuccess ? michael@0: rs->FireSuccessAsync(mDOMRequest, aVal) : michael@0: rs->FireErrorAsync(mDOMRequest, mReply->get_BluetoothReplyError().error()); michael@0: } michael@0: michael@0: nsresult michael@0: BluetoothReplyRunnable::FireErrorString() michael@0: { michael@0: nsCOMPtr rs = michael@0: do_GetService("@mozilla.org/dom/dom-request-service;1"); michael@0: NS_ENSURE_TRUE(rs, NS_ERROR_FAILURE); michael@0: michael@0: return rs->FireErrorAsync(mDOMRequest, mErrorString); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: BluetoothReplyRunnable::Run() michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread()); michael@0: MOZ_ASSERT(mDOMRequest); michael@0: MOZ_ASSERT(mReply); michael@0: michael@0: nsresult rv; michael@0: michael@0: AutoSafeJSContext cx; michael@0: JS::Rooted v(cx, JSVAL_VOID); michael@0: michael@0: if (mReply->type() != BluetoothReply::TBluetoothReplySuccess) { michael@0: rv = FireReply(v); michael@0: } else { michael@0: if (!ParseSuccessfulReply(&v)) { michael@0: rv = FireErrorString(); michael@0: } else { michael@0: rv = FireReply(v); michael@0: } michael@0: } michael@0: michael@0: if (NS_FAILED(rv)) { michael@0: BT_WARNING("Could not fire DOMRequest!"); michael@0: } michael@0: michael@0: ReleaseMembers(); michael@0: MOZ_ASSERT(!mDOMRequest, michael@0: "mDOMRequest still alive! Deriving class should call " michael@0: "BluetoothReplyRunnable::ReleaseMembers()!"); michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: BluetoothVoidReplyRunnable::BluetoothVoidReplyRunnable(nsIDOMDOMRequest* aReq) michael@0: : BluetoothReplyRunnable(aReq) michael@0: {} michael@0: michael@0: BluetoothVoidReplyRunnable::~BluetoothVoidReplyRunnable() michael@0: {} michael@0: