1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=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
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "BluetoothCommon.h"
8 #include "BluetoothMapRequestHandle.h"
9 #include "BluetoothReplyRunnable.h"
10 #include "BluetoothService.h"
12 #include "mozilla/dom/BluetoothMapRequestHandleBinding.h"
13 #include "mozilla/dom/ContentChild.h"
14 #include "mozilla/dom/Promise.h"
16 using namespace mozilla
;
19 USING_BLUETOOTH_NAMESPACE
21 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(BluetoothMapRequestHandle
, mOwner
)
22 NS_IMPL_CYCLE_COLLECTING_ADDREF(BluetoothMapRequestHandle
)
23 NS_IMPL_CYCLE_COLLECTING_RELEASE(BluetoothMapRequestHandle
)
24 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(BluetoothMapRequestHandle
)
25 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
26 NS_INTERFACE_MAP_ENTRY(nsISupports
)
29 BluetoothMapRequestHandle::BluetoothMapRequestHandle(nsPIDOMWindow
* aOwner
)
35 BluetoothMapRequestHandle::~BluetoothMapRequestHandle()
39 already_AddRefed
<BluetoothMapRequestHandle
>
40 BluetoothMapRequestHandle::Create(nsPIDOMWindow
* aOwner
)
44 nsRefPtr
<BluetoothMapRequestHandle
> handle
=
45 new BluetoothMapRequestHandle(aOwner
);
47 return handle
.forget();
50 already_AddRefed
<Promise
>
51 BluetoothMapRequestHandle::ReplyToFolderListing(long aMasId
,
52 const nsAString
& aFolderlists
, ErrorResult
& aRv
)
54 nsCOMPtr
<nsIGlobalObject
> global
= do_QueryInterface(GetParentObject());
56 aRv
.Throw(NS_ERROR_FAILURE
);
60 nsRefPtr
<Promise
> promise
= Promise::Create(global
, aRv
);
61 NS_ENSURE_TRUE(!aRv
.Failed(), nullptr);
63 BluetoothService
* bs
= BluetoothService::Get();
65 aRv
.Throw(NS_ERROR_FAILURE
);
69 if (XRE_GetProcessType() == GeckoProcessType_Default
) {
71 bs
->ReplyToMapFolderListing(aMasId
, aFolderlists
,
72 new BluetoothVoidReplyRunnable(nullptr, promise
));
74 ContentChild
*cc
= ContentChild::GetSingleton();
76 aRv
.Throw(NS_ERROR_FAILURE
);
80 bs
->ReplyToMapFolderListing(aMasId
, aFolderlists
,
81 new BluetoothVoidReplyRunnable(nullptr, promise
));
84 return promise
.forget();
87 already_AddRefed
<Promise
>
88 BluetoothMapRequestHandle::ReplyToMessagesListing(long aMasId
,
91 const nsAString
& aTimestamp
,
95 nsCOMPtr
<nsIGlobalObject
> global
= do_QueryInterface(GetParentObject());
97 aRv
.Throw(NS_ERROR_FAILURE
);
101 nsRefPtr
<Promise
> promise
= Promise::Create(global
, aRv
);
102 NS_ENSURE_TRUE(!aRv
.Failed(), nullptr);
104 BluetoothService
* bs
= BluetoothService::Get();
106 aRv
.Throw(NS_ERROR_FAILURE
);
110 if (XRE_GetProcessType() == GeckoProcessType_Default
) {
112 bs
->ReplyToMapMessagesListing(aMasId
, &aBlob
, aNewMessage
, aTimestamp
,
113 aSize
, new BluetoothVoidReplyRunnable(nullptr, promise
));
115 ContentChild
*cc
= ContentChild::GetSingleton();
117 aRv
.Throw(NS_ERROR_FAILURE
);
121 BlobChild
* actor
= cc
->GetOrCreateActorForBlob(&aBlob
);
123 aRv
.Throw(NS_ERROR_FAILURE
);
127 bs
->ReplyToMapMessagesListing(nullptr, actor
, aMasId
, aNewMessage
,
128 aTimestamp
, aSize
, new BluetoothVoidReplyRunnable(nullptr, promise
));
131 return promise
.forget();
134 already_AddRefed
<Promise
>
135 BluetoothMapRequestHandle::ReplyToGetMessage(long aMasId
, File
& aBlob
,
138 nsCOMPtr
<nsIGlobalObject
> global
= do_QueryInterface(GetParentObject());
140 aRv
.Throw(NS_ERROR_FAILURE
);
144 nsRefPtr
<Promise
> promise
= Promise::Create(global
, aRv
);
145 NS_ENSURE_TRUE(!aRv
.Failed(), nullptr);
147 BluetoothService
* bs
= BluetoothService::Get();
149 aRv
.Throw(NS_ERROR_FAILURE
);
153 if (XRE_GetProcessType() == GeckoProcessType_Default
) {
155 bs
->ReplyToMapGetMessage(&aBlob
, aMasId
,
156 new BluetoothVoidReplyRunnable(nullptr, promise
));
158 ContentChild
*cc
= ContentChild::GetSingleton();
160 aRv
.Throw(NS_ERROR_FAILURE
);
164 BlobChild
* actor
= cc
->GetOrCreateActorForBlob(&aBlob
);
166 aRv
.Throw(NS_ERROR_FAILURE
);
170 bs
->ReplyToMapGetMessage(nullptr, actor
, aMasId
,
171 new BluetoothVoidReplyRunnable(nullptr, promise
));
174 return promise
.forget();
177 already_AddRefed
<Promise
>
178 BluetoothMapRequestHandle::ReplyToSetMessageStatus(long aMasId
,
182 nsCOMPtr
<nsIGlobalObject
> global
= do_QueryInterface(GetParentObject());
184 aRv
.Throw(NS_ERROR_FAILURE
);
188 nsRefPtr
<Promise
> promise
= Promise::Create(global
, aRv
);
189 NS_ENSURE_TRUE(!aRv
.Failed(), nullptr);
191 BluetoothService
* bs
= BluetoothService::Get();
193 aRv
.Throw(NS_ERROR_FAILURE
);
197 bs
->ReplyToMapSetMessageStatus(aMasId
, aStatus
,
198 new BluetoothVoidReplyRunnable(nullptr, promise
));
200 return promise
.forget();
203 already_AddRefed
<Promise
>
204 BluetoothMapRequestHandle::ReplyToSendMessage(long aMasId
,
208 nsCOMPtr
<nsIGlobalObject
> global
= do_QueryInterface(GetParentObject());
210 aRv
.Throw(NS_ERROR_FAILURE
);
214 nsRefPtr
<Promise
> promise
= Promise::Create(global
, aRv
);
215 NS_ENSURE_TRUE(!aRv
.Failed(), nullptr);
217 BluetoothService
* bs
= BluetoothService::Get();
219 aRv
.Throw(NS_ERROR_FAILURE
);
223 bs
->ReplyToMapSendMessage(aMasId
, aStatus
,
224 new BluetoothVoidReplyRunnable(nullptr, promise
));
226 return promise
.forget();
229 already_AddRefed
<Promise
>
230 BluetoothMapRequestHandle::ReplyToMessageUpdate(long aMasId
,
234 nsCOMPtr
<nsIGlobalObject
> global
= do_QueryInterface(GetParentObject());
236 aRv
.Throw(NS_ERROR_FAILURE
);
240 nsRefPtr
<Promise
> promise
= Promise::Create(global
, aRv
);
241 NS_ENSURE_TRUE(!aRv
.Failed(), nullptr);
243 BluetoothService
* bs
= BluetoothService::Get();
245 aRv
.Throw(NS_ERROR_FAILURE
);
249 bs
->ReplyToMapMessageUpdate(aMasId
, aStatus
,
250 new BluetoothVoidReplyRunnable(nullptr, promise
));
252 return promise
.forget();
256 BluetoothMapRequestHandle::WrapObject(JSContext
* aCx
)
258 return BluetoothMapRequestHandleBinding::Wrap(aCx
, this);