|
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/. */ |
|
6 |
|
7 #include "base/basictypes.h" |
|
8 #include "BluetoothReplyRunnable.h" |
|
9 #include "DOMRequest.h" |
|
10 #include "mozilla/dom/bluetooth/BluetoothTypes.h" |
|
11 #include "nsServiceManagerUtils.h" |
|
12 |
|
13 USING_BLUETOOTH_NAMESPACE |
|
14 |
|
15 BluetoothReplyRunnable::BluetoothReplyRunnable(nsIDOMDOMRequest* aReq) |
|
16 : mDOMRequest(aReq) |
|
17 {} |
|
18 |
|
19 void |
|
20 BluetoothReplyRunnable::SetReply(BluetoothReply* aReply) |
|
21 { |
|
22 mReply = aReply; |
|
23 } |
|
24 |
|
25 void |
|
26 BluetoothReplyRunnable::ReleaseMembers() |
|
27 { |
|
28 mDOMRequest = nullptr; |
|
29 } |
|
30 |
|
31 BluetoothReplyRunnable::~BluetoothReplyRunnable() |
|
32 {} |
|
33 |
|
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); |
|
40 |
|
41 return mReply->type() == BluetoothReply::TBluetoothReplySuccess ? |
|
42 rs->FireSuccessAsync(mDOMRequest, aVal) : |
|
43 rs->FireErrorAsync(mDOMRequest, mReply->get_BluetoothReplyError().error()); |
|
44 } |
|
45 |
|
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); |
|
52 |
|
53 return rs->FireErrorAsync(mDOMRequest, mErrorString); |
|
54 } |
|
55 |
|
56 NS_IMETHODIMP |
|
57 BluetoothReplyRunnable::Run() |
|
58 { |
|
59 MOZ_ASSERT(NS_IsMainThread()); |
|
60 MOZ_ASSERT(mDOMRequest); |
|
61 MOZ_ASSERT(mReply); |
|
62 |
|
63 nsresult rv; |
|
64 |
|
65 AutoSafeJSContext cx; |
|
66 JS::Rooted<JS::Value> v(cx, JSVAL_VOID); |
|
67 |
|
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 } |
|
77 |
|
78 if (NS_FAILED(rv)) { |
|
79 BT_WARNING("Could not fire DOMRequest!"); |
|
80 } |
|
81 |
|
82 ReleaseMembers(); |
|
83 MOZ_ASSERT(!mDOMRequest, |
|
84 "mDOMRequest still alive! Deriving class should call " |
|
85 "BluetoothReplyRunnable::ReleaseMembers()!"); |
|
86 |
|
87 return rv; |
|
88 } |
|
89 |
|
90 BluetoothVoidReplyRunnable::BluetoothVoidReplyRunnable(nsIDOMDOMRequest* aReq) |
|
91 : BluetoothReplyRunnable(aReq) |
|
92 {} |
|
93 |
|
94 BluetoothVoidReplyRunnable::~BluetoothVoidReplyRunnable() |
|
95 {} |
|
96 |