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
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/bluetooth/BluetoothTypes.h"
11 #include "nsServiceManagerUtils.h"
13 USING_BLUETOOTH_NAMESPACE
15 BluetoothReplyRunnable::BluetoothReplyRunnable(nsIDOMDOMRequest* aReq)
16 : mDOMRequest(aReq)
17 {}
19 void
20 BluetoothReplyRunnable::SetReply(BluetoothReply* aReply)
21 {
22 mReply = aReply;
23 }
25 void
26 BluetoothReplyRunnable::ReleaseMembers()
27 {
28 mDOMRequest = nullptr;
29 }
31 BluetoothReplyRunnable::~BluetoothReplyRunnable()
32 {}
34 nsresult
35 BluetoothReplyRunnable::FireReply(JS::Handle<JS::Value> aVal)
36 {
37 nsCOMPtr<nsIDOMRequestService> rs =
38 do_GetService(DOMREQUEST_SERVICE_CONTRACTID);
39 NS_ENSURE_TRUE(rs, NS_ERROR_FAILURE);
41 return mReply->type() == BluetoothReply::TBluetoothReplySuccess ?
42 rs->FireSuccessAsync(mDOMRequest, aVal) :
43 rs->FireErrorAsync(mDOMRequest, mReply->get_BluetoothReplyError().error());
44 }
46 nsresult
47 BluetoothReplyRunnable::FireErrorString()
48 {
49 nsCOMPtr<nsIDOMRequestService> rs =
50 do_GetService("@mozilla.org/dom/dom-request-service;1");
51 NS_ENSURE_TRUE(rs, NS_ERROR_FAILURE);
53 return rs->FireErrorAsync(mDOMRequest, mErrorString);
54 }
56 NS_IMETHODIMP
57 BluetoothReplyRunnable::Run()
58 {
59 MOZ_ASSERT(NS_IsMainThread());
60 MOZ_ASSERT(mDOMRequest);
61 MOZ_ASSERT(mReply);
63 nsresult rv;
65 AutoSafeJSContext cx;
66 JS::Rooted<JS::Value> v(cx, JSVAL_VOID);
68 if (mReply->type() != BluetoothReply::TBluetoothReplySuccess) {
69 rv = FireReply(v);
70 } else {
71 if (!ParseSuccessfulReply(&v)) {
72 rv = FireErrorString();
73 } else {
74 rv = FireReply(v);
75 }
76 }
78 if (NS_FAILED(rv)) {
79 BT_WARNING("Could not fire DOMRequest!");
80 }
82 ReleaseMembers();
83 MOZ_ASSERT(!mDOMRequest,
84 "mDOMRequest still alive! Deriving class should call "
85 "BluetoothReplyRunnable::ReleaseMembers()!");
87 return rv;
88 }
90 BluetoothVoidReplyRunnable::BluetoothVoidReplyRunnable(nsIDOMDOMRequest* aReq)
91 : BluetoothReplyRunnable(aReq)
92 {}
94 BluetoothVoidReplyRunnable::~BluetoothVoidReplyRunnable()
95 {}