Bumping manifests a=b2g-bump
[gecko.git] / dom / bluetooth / BluetoothMapRequestHandle.cpp
blob2b8f3f6421c30dc35b31623448a10daf90e0f5ea
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;
17 using namespace dom;
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)
27 NS_INTERFACE_MAP_END
29 BluetoothMapRequestHandle::BluetoothMapRequestHandle(nsPIDOMWindow* aOwner)
30 : mOwner(aOwner)
32 MOZ_ASSERT(aOwner);
35 BluetoothMapRequestHandle::~BluetoothMapRequestHandle()
39 already_AddRefed<BluetoothMapRequestHandle>
40 BluetoothMapRequestHandle::Create(nsPIDOMWindow* aOwner)
42 MOZ_ASSERT(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());
55 if (!global) {
56 aRv.Throw(NS_ERROR_FAILURE);
57 return nullptr;
60 nsRefPtr<Promise> promise = Promise::Create(global, aRv);
61 NS_ENSURE_TRUE(!aRv.Failed(), nullptr);
63 BluetoothService* bs = BluetoothService::Get();
64 if (!bs) {
65 aRv.Throw(NS_ERROR_FAILURE);
66 return nullptr;
69 if (XRE_GetProcessType() == GeckoProcessType_Default) {
70 // In-process reply
71 bs->ReplyToMapFolderListing(aMasId, aFolderlists,
72 new BluetoothVoidReplyRunnable(nullptr, promise));
73 } else {
74 ContentChild *cc = ContentChild::GetSingleton();
75 if (!cc) {
76 aRv.Throw(NS_ERROR_FAILURE);
77 return nullptr;
80 bs->ReplyToMapFolderListing(aMasId, aFolderlists,
81 new BluetoothVoidReplyRunnable(nullptr, promise));
84 return promise.forget();
87 already_AddRefed<Promise>
88 BluetoothMapRequestHandle::ReplyToMessagesListing(long aMasId,
89 File& aBlob,
90 bool aNewMessage,
91 const nsAString& aTimestamp,
92 int aSize,
93 ErrorResult& aRv)
95 nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(GetParentObject());
96 if (!global) {
97 aRv.Throw(NS_ERROR_FAILURE);
98 return nullptr;
101 nsRefPtr<Promise> promise = Promise::Create(global, aRv);
102 NS_ENSURE_TRUE(!aRv.Failed(), nullptr);
104 BluetoothService* bs = BluetoothService::Get();
105 if (!bs) {
106 aRv.Throw(NS_ERROR_FAILURE);
107 return nullptr;
110 if (XRE_GetProcessType() == GeckoProcessType_Default) {
111 // In-process reply
112 bs->ReplyToMapMessagesListing(aMasId, &aBlob, aNewMessage, aTimestamp,
113 aSize, new BluetoothVoidReplyRunnable(nullptr, promise));
114 } else {
115 ContentChild *cc = ContentChild::GetSingleton();
116 if (!cc) {
117 aRv.Throw(NS_ERROR_FAILURE);
118 return nullptr;
121 BlobChild* actor = cc->GetOrCreateActorForBlob(&aBlob);
122 if (!actor) {
123 aRv.Throw(NS_ERROR_FAILURE);
124 return nullptr;
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,
136 ErrorResult& aRv)
138 nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(GetParentObject());
139 if (!global) {
140 aRv.Throw(NS_ERROR_FAILURE);
141 return nullptr;
144 nsRefPtr<Promise> promise = Promise::Create(global, aRv);
145 NS_ENSURE_TRUE(!aRv.Failed(), nullptr);
147 BluetoothService* bs = BluetoothService::Get();
148 if (!bs) {
149 aRv.Throw(NS_ERROR_FAILURE);
150 return nullptr;
153 if (XRE_GetProcessType() == GeckoProcessType_Default) {
154 // In-process reply
155 bs->ReplyToMapGetMessage(&aBlob, aMasId,
156 new BluetoothVoidReplyRunnable(nullptr, promise));
157 } else {
158 ContentChild *cc = ContentChild::GetSingleton();
159 if (!cc) {
160 aRv.Throw(NS_ERROR_FAILURE);
161 return nullptr;
164 BlobChild* actor = cc->GetOrCreateActorForBlob(&aBlob);
165 if (!actor) {
166 aRv.Throw(NS_ERROR_FAILURE);
167 return nullptr;
170 bs->ReplyToMapGetMessage(nullptr, actor, aMasId,
171 new BluetoothVoidReplyRunnable(nullptr, promise));
174 return promise.forget();
177 already_AddRefed<Promise>
178 BluetoothMapRequestHandle::ReplyToSetMessageStatus(long aMasId,
179 bool aStatus,
180 ErrorResult& aRv)
182 nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(GetParentObject());
183 if (!global) {
184 aRv.Throw(NS_ERROR_FAILURE);
185 return nullptr;
188 nsRefPtr<Promise> promise = Promise::Create(global, aRv);
189 NS_ENSURE_TRUE(!aRv.Failed(), nullptr);
191 BluetoothService* bs = BluetoothService::Get();
192 if (!bs) {
193 aRv.Throw(NS_ERROR_FAILURE);
194 return nullptr;
197 bs->ReplyToMapSetMessageStatus(aMasId, aStatus,
198 new BluetoothVoidReplyRunnable(nullptr, promise));
200 return promise.forget();
203 already_AddRefed<Promise>
204 BluetoothMapRequestHandle::ReplyToSendMessage(long aMasId,
205 bool aStatus,
206 ErrorResult& aRv)
208 nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(GetParentObject());
209 if (!global) {
210 aRv.Throw(NS_ERROR_FAILURE);
211 return nullptr;
214 nsRefPtr<Promise> promise = Promise::Create(global, aRv);
215 NS_ENSURE_TRUE(!aRv.Failed(), nullptr);
217 BluetoothService* bs = BluetoothService::Get();
218 if (!bs) {
219 aRv.Throw(NS_ERROR_FAILURE);
220 return nullptr;
223 bs->ReplyToMapSendMessage(aMasId, aStatus,
224 new BluetoothVoidReplyRunnable(nullptr, promise));
226 return promise.forget();
229 already_AddRefed<Promise>
230 BluetoothMapRequestHandle::ReplyToMessageUpdate(long aMasId,
231 bool aStatus,
232 ErrorResult& aRv)
234 nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(GetParentObject());
235 if (!global) {
236 aRv.Throw(NS_ERROR_FAILURE);
237 return nullptr;
240 nsRefPtr<Promise> promise = Promise::Create(global, aRv);
241 NS_ENSURE_TRUE(!aRv.Failed(), nullptr);
243 BluetoothService* bs = BluetoothService::Get();
244 if (!bs) {
245 aRv.Throw(NS_ERROR_FAILURE);
246 return nullptr;
249 bs->ReplyToMapMessageUpdate(aMasId, aStatus,
250 new BluetoothVoidReplyRunnable(nullptr, promise));
252 return promise.forget();
255 JSObject*
256 BluetoothMapRequestHandle::WrapObject(JSContext* aCx)
258 return BluetoothMapRequestHandleBinding::Wrap(aCx, this);