Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */ |
michael@0 | 2 | /* vim: set ts=2 et sw=2 tw=80: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 5 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "base/basictypes.h" |
michael@0 | 8 | #include "BluetoothReplyRunnable.h" |
michael@0 | 9 | #include "DOMRequest.h" |
michael@0 | 10 | #include "mozilla/dom/bluetooth/BluetoothTypes.h" |
michael@0 | 11 | #include "nsServiceManagerUtils.h" |
michael@0 | 12 | |
michael@0 | 13 | USING_BLUETOOTH_NAMESPACE |
michael@0 | 14 | |
michael@0 | 15 | BluetoothReplyRunnable::BluetoothReplyRunnable(nsIDOMDOMRequest* aReq) |
michael@0 | 16 | : mDOMRequest(aReq) |
michael@0 | 17 | {} |
michael@0 | 18 | |
michael@0 | 19 | void |
michael@0 | 20 | BluetoothReplyRunnable::SetReply(BluetoothReply* aReply) |
michael@0 | 21 | { |
michael@0 | 22 | mReply = aReply; |
michael@0 | 23 | } |
michael@0 | 24 | |
michael@0 | 25 | void |
michael@0 | 26 | BluetoothReplyRunnable::ReleaseMembers() |
michael@0 | 27 | { |
michael@0 | 28 | mDOMRequest = nullptr; |
michael@0 | 29 | } |
michael@0 | 30 | |
michael@0 | 31 | BluetoothReplyRunnable::~BluetoothReplyRunnable() |
michael@0 | 32 | {} |
michael@0 | 33 | |
michael@0 | 34 | nsresult |
michael@0 | 35 | BluetoothReplyRunnable::FireReply(JS::Handle<JS::Value> aVal) |
michael@0 | 36 | { |
michael@0 | 37 | nsCOMPtr<nsIDOMRequestService> rs = |
michael@0 | 38 | do_GetService(DOMREQUEST_SERVICE_CONTRACTID); |
michael@0 | 39 | NS_ENSURE_TRUE(rs, NS_ERROR_FAILURE); |
michael@0 | 40 | |
michael@0 | 41 | return mReply->type() == BluetoothReply::TBluetoothReplySuccess ? |
michael@0 | 42 | rs->FireSuccessAsync(mDOMRequest, aVal) : |
michael@0 | 43 | rs->FireErrorAsync(mDOMRequest, mReply->get_BluetoothReplyError().error()); |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | nsresult |
michael@0 | 47 | BluetoothReplyRunnable::FireErrorString() |
michael@0 | 48 | { |
michael@0 | 49 | nsCOMPtr<nsIDOMRequestService> rs = |
michael@0 | 50 | do_GetService("@mozilla.org/dom/dom-request-service;1"); |
michael@0 | 51 | NS_ENSURE_TRUE(rs, NS_ERROR_FAILURE); |
michael@0 | 52 | |
michael@0 | 53 | return rs->FireErrorAsync(mDOMRequest, mErrorString); |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | NS_IMETHODIMP |
michael@0 | 57 | BluetoothReplyRunnable::Run() |
michael@0 | 58 | { |
michael@0 | 59 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 60 | MOZ_ASSERT(mDOMRequest); |
michael@0 | 61 | MOZ_ASSERT(mReply); |
michael@0 | 62 | |
michael@0 | 63 | nsresult rv; |
michael@0 | 64 | |
michael@0 | 65 | AutoSafeJSContext cx; |
michael@0 | 66 | JS::Rooted<JS::Value> v(cx, JSVAL_VOID); |
michael@0 | 67 | |
michael@0 | 68 | if (mReply->type() != BluetoothReply::TBluetoothReplySuccess) { |
michael@0 | 69 | rv = FireReply(v); |
michael@0 | 70 | } else { |
michael@0 | 71 | if (!ParseSuccessfulReply(&v)) { |
michael@0 | 72 | rv = FireErrorString(); |
michael@0 | 73 | } else { |
michael@0 | 74 | rv = FireReply(v); |
michael@0 | 75 | } |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | if (NS_FAILED(rv)) { |
michael@0 | 79 | BT_WARNING("Could not fire DOMRequest!"); |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | ReleaseMembers(); |
michael@0 | 83 | MOZ_ASSERT(!mDOMRequest, |
michael@0 | 84 | "mDOMRequest still alive! Deriving class should call " |
michael@0 | 85 | "BluetoothReplyRunnable::ReleaseMembers()!"); |
michael@0 | 86 | |
michael@0 | 87 | return rv; |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | BluetoothVoidReplyRunnable::BluetoothVoidReplyRunnable(nsIDOMDOMRequest* aReq) |
michael@0 | 91 | : BluetoothReplyRunnable(aReq) |
michael@0 | 92 | {} |
michael@0 | 93 | |
michael@0 | 94 | BluetoothVoidReplyRunnable::~BluetoothVoidReplyRunnable() |
michael@0 | 95 | {} |
michael@0 | 96 |