Bug 1115758 - Part 2: Add USSDSession.cancel (dom). r=hsinyi
authorSzu-Yu Chen [:aknow] <szchen@mozilla.com>
Wed, 07 Jan 2015 15:28:44 +0800
changeset 235516 19941167e2f344cd52d6ed7b9e5d18c049bb92be
parent 235515 a0068ce0aa5541fd5ce91e6e4331e430177bbe81
child 235517 d385fe32e60a1e27d0aabc4410dea51f6901cfe1
push id366
push usercmanchester@mozilla.com
push dateThu, 08 Jan 2015 16:40:24 +0000
reviewershsinyi
bugs1115758
milestone37.0a1
Bug 1115758 - Part 2: Add USSDSession.cancel (dom). r=hsinyi
dom/telephony/USSDSession.cpp
dom/telephony/USSDSession.h
--- a/dom/telephony/USSDSession.cpp
+++ b/dom/telephony/USSDSession.cpp
@@ -40,16 +40,38 @@ USSDSession::GetParentObject() const
 }
 
 JSObject*
 USSDSession::WrapObject(JSContext* aCx)
 {
   return USSDSessionBinding::Wrap(aCx, this);
 }
 
+already_AddRefed<Promise>
+USSDSession::CreatePromise(ErrorResult& aRv)
+{
+  if (!mService) {
+    aRv.Throw(NS_ERROR_FAILURE);
+    return nullptr;
+  }
+
+  nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(mWindow);
+  if (!global) {
+    aRv.Throw(NS_ERROR_FAILURE);
+    return nullptr;
+  }
+
+  nsRefPtr<Promise> promise = Promise::Create(global, aRv);
+  if (aRv.Failed()) {
+    return nullptr;
+  }
+
+  return promise.forget();
+}
+
 // WebIDL
 
 already_AddRefed<USSDSession>
 USSDSession::Constructor(const GlobalObject& aGlobal, uint32_t aServiceId,
                          ErrorResult& aRv)
 {
   nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal.GetAsSupports());
   if (!window) {
@@ -66,32 +88,40 @@ USSDSession::Constructor(const GlobalObj
 
   nsRefPtr<USSDSession> session = new USSDSession(window, ril, aServiceId);
   return session.forget();
 }
 
 already_AddRefed<Promise>
 USSDSession::Send(const nsAString& aUssd, ErrorResult& aRv)
 {
-  if (!mService) {
-    aRv.Throw(NS_ERROR_FAILURE);
-    return nullptr;
-  }
-
-  nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(mWindow);
-  if (!global) {
-    return nullptr;
-  }
-
-  nsRefPtr<Promise> promise = Promise::Create(global, aRv);
-  if (aRv.Failed()) {
+  nsRefPtr<Promise> promise = CreatePromise(aRv);
+  if (!promise) {
     return nullptr;
   }
 
   nsCOMPtr<nsITelephonyCallback> callback = new TelephonyCallback(promise);
 
   nsresult rv = mService->SendUSSD(mServiceId, aUssd, callback);
   if (NS_FAILED(rv)) {
     promise->MaybeReject(NS_ERROR_DOM_INVALID_STATE_ERR);
   }
 
   return promise.forget();
 }
+
+already_AddRefed<Promise>
+USSDSession::Cancel(ErrorResult& aRv)
+{
+  nsRefPtr<Promise> promise = CreatePromise(aRv);
+  if (!promise) {
+    return nullptr;
+  }
+
+  nsCOMPtr<nsITelephonyCallback> callback = new TelephonyCallback(promise);
+
+  nsresult rv = mService->CancelUSSD(mServiceId, callback);
+  if (NS_FAILED(rv)) {
+    promise->MaybeReject(NS_ERROR_DOM_INVALID_STATE_ERR);
+  }
+
+  return promise.forget();
+}
--- a/dom/telephony/USSDSession.h
+++ b/dom/telephony/USSDSession.h
@@ -42,19 +42,25 @@ public:
   // WebIDL
   static already_AddRefed<USSDSession>
   Constructor(const GlobalObject& aGlobal, uint32_t aServiceId,
               ErrorResult& aRv);
 
   already_AddRefed<Promise>
   Send(const nsAString& aUssd, ErrorResult& aRv);
 
+  already_AddRefed<Promise>
+  Cancel(ErrorResult& aRv);
+
 private:
   ~USSDSession();
 
+  already_AddRefed<Promise>
+  CreatePromise(ErrorResult& aRv);
+
   nsCOMPtr<nsPIDOMWindow> mWindow;
   nsCOMPtr<nsITelephonyService> mService;
   uint32_t mServiceId;
 };
 
 } // namespace dom
 } // namespace mozilla