Bumping manifests a=b2g-bump
[gecko.git] / dom / telephony / USSDSession.cpp
blobd6471bd605359568c3f680c2bd49e9d64bd80211
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)
24 NS_INTERFACE_MAP_END
26 USSDSession::USSDSession(nsPIDOMWindow* aWindow, nsITelephonyService* aService,
27 uint32_t aServiceId)
28 : mWindow(aWindow), mService(aService), mServiceId(aServiceId)
32 USSDSession::~USSDSession()
36 nsPIDOMWindow*
37 USSDSession::GetParentObject() const
39 return mWindow;
42 JSObject*
43 USSDSession::WrapObject(JSContext* aCx)
45 return USSDSessionBinding::Wrap(aCx, this);
48 already_AddRefed<Promise>
49 USSDSession::CreatePromise(ErrorResult& aRv)
51 if (!mService) {
52 aRv.Throw(NS_ERROR_FAILURE);
53 return nullptr;
56 nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(mWindow);
57 if (!global) {
58 aRv.Throw(NS_ERROR_FAILURE);
59 return nullptr;
62 nsRefPtr<Promise> promise = Promise::Create(global, aRv);
63 if (aRv.Failed()) {
64 return nullptr;
67 return promise.forget();
70 // WebIDL
72 already_AddRefed<USSDSession>
73 USSDSession::Constructor(const GlobalObject& aGlobal, uint32_t aServiceId,
74 ErrorResult& aRv)
76 nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal.GetAsSupports());
77 if (!window) {
78 aRv.Throw(NS_ERROR_UNEXPECTED);
79 return nullptr;
82 nsCOMPtr<nsITelephonyService> ril =
83 do_GetService(TELEPHONY_SERVICE_CONTRACTID);
84 if (!ril) {
85 aRv.Throw(NS_ERROR_UNEXPECTED);
86 return nullptr;
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);
97 if (!promise) {
98 return nullptr;
101 nsCOMPtr<nsITelephonyCallback> callback = new TelephonyCallback(promise);
103 nsresult rv = mService->SendUSSD(mServiceId, aUssd, callback);
104 if (NS_FAILED(rv)) {
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);
115 if (!promise) {
116 return nullptr;
119 nsCOMPtr<nsITelephonyCallback> callback = new TelephonyCallback(promise);
121 nsresult rv = mService->CancelUSSD(mServiceId, callback);
122 if (NS_FAILED(rv)) {
123 promise->MaybeReject(NS_ERROR_DOM_INVALID_STATE_ERR);
126 return promise.forget();