Bug 1382092 - Support default payment UI service in DOM code part 2. r=baku
authorEden Chuang <echuang@mozilla.com>
Fri, 04 Aug 2017 17:24:59 +0800
changeset 425182 39c022e64d9859bd866064742539191758423dd0
parent 425181 84aac02c78a9a366560a8bc965adea3963c422c4
child 425183 1eaa794d25333caf9dfcb2240c764a49f4afa9cd
push id1567
push userjlorenzo@mozilla.com
push dateThu, 02 Nov 2017 12:36:05 +0000
treeherdermozilla-release@e512c14a0406 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbaku
bugs1382092
milestone57.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1382092 - Support default payment UI service in DOM code part 2. r=baku
dom/interfaces/payments/nsIPaymentUIService.idl
dom/payments/BasicCardPayment.cpp
dom/payments/PaymentRequestService.cpp
dom/payments/PaymentRequestService.h
--- a/dom/interfaces/payments/nsIPaymentUIService.idl
+++ b/dom/interfaces/payments/nsIPaymentUIService.idl
@@ -1,17 +1,23 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "nsISupports.idl"
 #include "nsIPaymentActionResponse.idl"
 
-[scriptable, uuid(ea008d0c-9e9f-411f-a6c5-a62106ba7ab9)]
+[scriptable, uuid(01f8bd55-9017-438b-85ec-7c15d2b35cdc)]
 interface nsIPaymentUIService : nsISupports
 {
-  nsIPaymentActionResponse canMakePayment(in AString requestId);
-  nsIPaymentActionResponse showPayment(in AString requestId);
-  nsIPaymentActionResponse abortPayment(in AString requestId);
-  nsIPaymentActionResponse completePayment(in AString requestId);
-  nsIPaymentActionResponse updatePayment(in AString requestId);
+  void showPayment(in AString requestId);
+  void abortPayment(in AString requestId);
+  void completePayment(in AString requestId);
+  void updatePayment(in AString requestId);
 };
+
+%{C++
+#define NS_PAYMENT_UI_SERVICE_CID \
+  { 0x01f8bd55, 0x9017, 0x438b, { 0x85, 0xec, 0x7c, 0x15, 0xd2, 0xb3, 0x5c, 0xdc } }
+#define NS_PAYMENT_UI_SERVICE_CONTRACT_ID \
+  "@mozilla.org/dom/payments/payment-ui-service;1"
+%}
--- a/dom/payments/BasicCardPayment.cpp
+++ b/dom/payments/BasicCardPayment.cpp
@@ -348,19 +348,17 @@ BasicCardService::DecodeBasicCardData(co
   return NS_OK;
 }
 
 #ifdef PaymentBasicCardMacros
 #undef PaymentBasicCardMacros
 #undef EncodeBasicCardProperty
 #undef EncodeAddressProperty
 #undef DecodeBasicCardProperty
-#undef DecodeBasicCardCardNumber
 #undef DecodeAddressProperty
-#undef DecodeAddressLine
 #undef AMEX
 #undef CARTEBANCAIRE
 #undef DINERS
 #undef DISCOVER
 #undef JCB
 #undef MASTERCARD
 #undef MIR
 #undef UNIONPAY
--- a/dom/payments/PaymentRequestService.cpp
+++ b/dom/payments/PaymentRequestService.cpp
@@ -143,94 +143,51 @@ NS_IMETHODIMP
 PaymentRequestService::SetTestingUIService(nsIPaymentUIService* aUIService)
 {
   // aUIService can be nullptr
   mTestingUIService = aUIService;
   return NS_OK;
 }
 
 nsresult
-PaymentRequestService::CallTestingUIAction(const nsAString& aRequestId, uint32_t aActionType)
+PaymentRequestService::LaunchUIAction(const nsAString& aRequestId, uint32_t aActionType)
 {
-  nsCOMPtr<nsIPaymentActionResponse> response;
+  nsCOMPtr<nsIPaymentUIService> uiService;
   nsresult rv;
   if (mTestingUIService) {
-    switch (aActionType) {
-      case nsIPaymentActionRequest::CANMAKE_ACTION: {
-        rv = mTestingUIService->CanMakePayment(aRequestId, getter_AddRefs(response));
-        break;
-      }
-      case nsIPaymentActionRequest::SHOW_ACTION: {
-        rv = mTestingUIService->ShowPayment(aRequestId, getter_AddRefs(response));
-        break;
-      }
-      case nsIPaymentActionRequest::ABORT_ACTION: {
-        rv = mTestingUIService->AbortPayment(aRequestId, getter_AddRefs(response));
-        break;
-      }
-      case nsIPaymentActionRequest::COMPLETE_ACTION: {
-        rv = mTestingUIService->CompletePayment(aRequestId, getter_AddRefs(response));
-        break;
-      }
-      case nsIPaymentActionRequest::UPDATE_ACTION: {
-        rv = mTestingUIService->UpdatePayment(aRequestId, getter_AddRefs(response));
-        break;
-      }
-      default : {
-        return NS_ERROR_FAILURE;
-      }
-    }
+    uiService = mTestingUIService;
+  } else {
+    uiService = do_GetService(NS_PAYMENT_UI_SERVICE_CONTRACT_ID, &rv);
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
-  } else {
-    // Since there is no UI implementation and no testing UI Service is registered,
-    // set false response for canMakePayment(), ABORT_SUCCEEDED for abort() and
-    // COMPLETE_SUCCEEDED for complete().
-    switch (aActionType) {
-      case nsIPaymentActionRequest::CANMAKE_ACTION: {
-        nsCOMPtr<nsIPaymentCanMakeActionResponse> canMakeResponse =
-          do_CreateInstance(NS_PAYMENT_CANMAKE_ACTION_RESPONSE_CONTRACT_ID);
-        MOZ_ASSERT(canMakeResponse);
-        rv = canMakeResponse->Init(aRequestId, false);
-        NS_ENSURE_SUCCESS(rv, rv);
-        response = do_QueryInterface(canMakeResponse);
-        MOZ_ASSERT(response);
-        break;
-      }
-      case nsIPaymentActionRequest::ABORT_ACTION: {
-        nsCOMPtr<nsIPaymentAbortActionResponse> abortResponse =
-          do_CreateInstance(NS_PAYMENT_ABORT_ACTION_RESPONSE_CONTRACT_ID);
-        MOZ_ASSERT(abortResponse);
-        rv = abortResponse->Init(aRequestId, nsIPaymentActionResponse::ABORT_SUCCEEDED);
-        NS_ENSURE_SUCCESS(rv, rv);
-        response = do_QueryInterface(abortResponse);
-        MOZ_ASSERT(response);
-        break;
-      }
-      case nsIPaymentActionRequest::COMPLETE_ACTION: {
-        nsCOMPtr<nsIPaymentCompleteActionResponse> completeResponse =
-          do_CreateInstance(NS_PAYMENT_COMPLETE_ACTION_RESPONSE_CONTRACT_ID);
-        MOZ_ASSERT(completeResponse);
-        rv = completeResponse->Init(aRequestId, nsIPaymentActionResponse::COMPLETE_SUCCEEDED);
-        NS_ENSURE_SUCCESS(rv, rv);
-        response = do_QueryInterface(completeResponse);
-        MOZ_ASSERT(response);
-        break;
-      }
-      default : {
-        break;
-      }
+  }
+  switch (aActionType) {
+    case nsIPaymentActionRequest::SHOW_ACTION: {
+      rv = uiService->ShowPayment(aRequestId);
+      break;
+    }
+    case nsIPaymentActionRequest::ABORT_ACTION: {
+      rv = uiService->AbortPayment(aRequestId);
+      break;
+    }
+    case nsIPaymentActionRequest::COMPLETE_ACTION: {
+      rv = uiService->CompletePayment(aRequestId);
+      break;
+    }
+    case nsIPaymentActionRequest::UPDATE_ACTION: {
+      rv = uiService->UpdatePayment(aRequestId);
+      break;
+    }
+    default : {
+      return NS_ERROR_FAILURE;
     }
   }
-  if (response) {
-    rv = RespondPayment(response);
-    if (NS_WARN_IF(NS_FAILED(rv))) {
-      return rv;
-    }
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    return rv;
   }
   return NS_OK;
 }
 
 NS_IMETHODIMP
 PaymentRequestService::RemoveActionCallback(nsIPaymentActionCallback* aCallback)
 {
   NS_ENSURE_ARG_POINTER(aCallback);
@@ -300,41 +257,35 @@ PaymentRequestService::RequestPayment(ns
       }
       break;
     }
     /*
      *  TODO: 1. Check third party payment app support by traversing all
      *           registered third party payment apps.
      */
     case nsIPaymentActionRequest::CANMAKE_ACTION: {
+      nsCOMPtr<nsIPaymentCanMakeActionResponse> canMakeResponse =
+        do_CreateInstance(NS_PAYMENT_CANMAKE_ACTION_RESPONSE_CONTRACT_ID);
+      MOZ_ASSERT(canMakeResponse);
       if (IsBasicCardPayment(requestId)) {
-        nsCOMPtr<nsIPaymentCanMakeActionResponse> canMakeResponse =
-          do_CreateInstance(NS_PAYMENT_CANMAKE_ACTION_RESPONSE_CONTRACT_ID);
-        MOZ_ASSERT(canMakeResponse);
         rv = canMakeResponse->Init(requestId, true);
-        if (NS_WARN_IF(NS_FAILED(rv))) {
-          return rv;
-        }
-        nsCOMPtr<nsIPaymentActionResponse> response = do_QueryInterface(canMakeResponse);
-        MOZ_ASSERT(response);
-        rv = RespondPayment(response);
-        if (NS_WARN_IF(NS_FAILED(rv))) {
-          return rv;
-        }
       } else {
-        rv = CallTestingUIAction(requestId, type);
-        if (NS_WARN_IF(NS_FAILED(rv))) {
-          return NS_ERROR_FAILURE;
-        }
+        rv = canMakeResponse->Init(requestId, false);
+      }
+      if (NS_WARN_IF(NS_FAILED(rv))) {
+        return rv;
+      }
+      nsCOMPtr<nsIPaymentActionResponse> response = do_QueryInterface(canMakeResponse);
+      MOZ_ASSERT(response);
+      rv = RespondPayment(response);
+      if (NS_WARN_IF(NS_FAILED(rv))) {
+        return rv;
       }
       break;
     }
-    /*
-     *  TODO: Launch/inform payment UI here once the UI module is implemented.
-     */
     case nsIPaymentActionRequest::SHOW_ACTION: {
       if (mShowingRequest) {
         nsCOMPtr<nsIPaymentResponseData> responseData =
           do_CreateInstance(NS_GENERAL_RESPONSE_DATA_CONTRACT_ID);
         MOZ_ASSERT(responseData);
         nsCOMPtr<nsIPaymentShowActionResponse> showResponse =
           do_CreateInstance(NS_PAYMENT_SHOW_ACTION_RESPONSE_CONTRACT_ID);
         MOZ_ASSERT(showResponse);
@@ -351,26 +302,26 @@ PaymentRequestService::RequestPayment(ns
         if (NS_WARN_IF(NS_FAILED(rv))) {
           return rv;
         }
       } else {
         rv = GetPaymentRequestById(requestId, getter_AddRefs(mShowingRequest));
         if (NS_WARN_IF(NS_FAILED(rv))) {
           return NS_ERROR_FAILURE;
         }
-        rv = CallTestingUIAction(requestId, type);
+        rv = LaunchUIAction(requestId, type);
         if (NS_WARN_IF(NS_FAILED(rv))) {
           return NS_ERROR_FAILURE;
         }
       }
       break;
     }
     case nsIPaymentActionRequest::ABORT_ACTION:
     case nsIPaymentActionRequest::COMPLETE_ACTION: {
-      rv = CallTestingUIAction(requestId, type);
+      rv = LaunchUIAction(requestId, type);
       if (NS_WARN_IF(NS_FAILED(rv))) {
         return NS_ERROR_FAILURE;
       }
       break;
     }
     case nsIPaymentActionRequest::UPDATE_ACTION: {
       nsCOMPtr<nsIPaymentUpdateActionRequest> request = do_QueryInterface(aRequest);
       MOZ_ASSERT(request);
@@ -385,17 +336,17 @@ PaymentRequestService::RequestPayment(ns
       rv = GetPaymentRequestById(requestId, getter_AddRefs(payment));
       if (NS_WARN_IF(NS_FAILED(rv))) {
         return rv;
       }
       rv = payment->UpdatePaymentDetails(details);
       if (NS_WARN_IF(NS_FAILED(rv))) {
         return rv;
       }
-      rv = CallTestingUIAction(requestId, type);
+      rv = LaunchUIAction(requestId, type);
       if (NS_WARN_IF(NS_FAILED(rv))) {
         return NS_ERROR_FAILURE;
       }
       break;
     }
     default: {
       return NS_ERROR_FAILURE;
     }
--- a/dom/payments/PaymentRequestService.h
+++ b/dom/payments/PaymentRequestService.h
@@ -39,17 +39,17 @@ private:
   nsresult
   SetActionCallback(const nsAString& aRequestId,
                     nsIPaymentActionCallback* aCallback);
   nsresult
   RemoveActionCallback(const nsAString& aRequestId);
 
   // this method is only used for testing
   nsresult
-  CallTestingUIAction(const nsAString& aRequestId, uint32_t aActionType);
+  LaunchUIAction(const nsAString& aRequestId, uint32_t aActionType);
 
   bool
   IsBasicCardPayment(const nsAString& aRequestId);
 
   FallibleTArray<nsCOMPtr<nsIPaymentRequest>> mRequestQueue;
 
   nsInterfaceHashtable<nsStringHashKey, nsIPaymentActionCallback> mCallbackHashtable;