1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
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 "mozilla/dom/USSDSession.h"
9 #include "mozilla/dom/USSDSessionBinding.h"
10 #include "mozilla/dom/telephony/TelephonyCallback.h"
11 #include "nsIGlobalObject.h"
12 #include "nsServiceManagerUtils.h"
14 using namespace mozilla::dom
;
15 using namespace mozilla::dom::telephony
;
16 using mozilla::ErrorResult
;
18 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(USSDSession
, mWindow
)
19 NS_IMPL_CYCLE_COLLECTING_ADDREF(USSDSession
)
20 NS_IMPL_CYCLE_COLLECTING_RELEASE(USSDSession
)
21 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(USSDSession
)
22 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
23 NS_INTERFACE_MAP_ENTRY(nsISupports
)
26 USSDSession::USSDSession(nsPIDOMWindow
* aWindow
, nsITelephonyService
* aService
,
28 : mWindow(aWindow
), mService(aService
), mServiceId(aServiceId
)
32 USSDSession::~USSDSession()
37 USSDSession::GetParentObject() const
43 USSDSession::WrapObject(JSContext
* aCx
)
45 return USSDSessionBinding::Wrap(aCx
, this);
48 already_AddRefed
<Promise
>
49 USSDSession::CreatePromise(ErrorResult
& aRv
)
52 aRv
.Throw(NS_ERROR_FAILURE
);
56 nsCOMPtr
<nsIGlobalObject
> global
= do_QueryInterface(mWindow
);
58 aRv
.Throw(NS_ERROR_FAILURE
);
62 nsRefPtr
<Promise
> promise
= Promise::Create(global
, aRv
);
67 return promise
.forget();
72 already_AddRefed
<USSDSession
>
73 USSDSession::Constructor(const GlobalObject
& aGlobal
, uint32_t aServiceId
,
76 nsCOMPtr
<nsPIDOMWindow
> window
= do_QueryInterface(aGlobal
.GetAsSupports());
78 aRv
.Throw(NS_ERROR_UNEXPECTED
);
82 nsCOMPtr
<nsITelephonyService
> ril
=
83 do_GetService(TELEPHONY_SERVICE_CONTRACTID
);
85 aRv
.Throw(NS_ERROR_UNEXPECTED
);
89 nsRefPtr
<USSDSession
> session
= new USSDSession(window
, ril
, aServiceId
);
90 return session
.forget();
93 already_AddRefed
<Promise
>
94 USSDSession::Send(const nsAString
& aUssd
, ErrorResult
& aRv
)
96 nsRefPtr
<Promise
> promise
= CreatePromise(aRv
);
101 nsCOMPtr
<nsITelephonyCallback
> callback
= new TelephonyCallback(promise
);
103 nsresult rv
= mService
->SendUSSD(mServiceId
, aUssd
, callback
);
105 promise
->MaybeReject(NS_ERROR_DOM_INVALID_STATE_ERR
);
108 return promise
.forget();
111 already_AddRefed
<Promise
>
112 USSDSession::Cancel(ErrorResult
& aRv
)
114 nsRefPtr
<Promise
> promise
= CreatePromise(aRv
);
119 nsCOMPtr
<nsITelephonyCallback
> callback
= new TelephonyCallback(promise
);
121 nsresult rv
= mService
->CancelUSSD(mServiceId
, callback
);
123 promise
->MaybeReject(NS_ERROR_DOM_INVALID_STATE_ERR
);
126 return promise
.forget();