Bug 1477117 - Part 3 - dispatch the PaymentMethodChangeEvent. r=baku
authorEden Chuang <echuang@mozilla.com>
Tue, 27 Nov 2018 11:17:33 +0100
changeset 505059 ae31de05b602600e90d8e312d279ed5e58779311
parent 505058 b3aea4b20b5360b16336a66863927a169f5fd025
child 505060 ab5b1091c88e42785e247ace0c7cc2d0026661e7
push id10290
push userffxbld-merge
push dateMon, 03 Dec 2018 16:23:23 +0000
treeherdermozilla-beta@700bed2445e6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbaku
bugs1477117
milestone65.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 1477117 - Part 3 - dispatch the PaymentMethodChangeEvent. r=baku Save the changing method information in PaymentMethodChangeEvent and dispatch it.
dom/payments/PaymentMethodChangeEvent.cpp
dom/payments/PaymentMethodChangeEvent.h
dom/payments/PaymentRequest.cpp
dom/payments/PaymentRequestUpdateEvent.h
dom/payments/PaymentResponse.h
--- a/dom/payments/PaymentMethodChangeEvent.cpp
+++ b/dom/payments/PaymentMethodChangeEvent.cpp
@@ -1,17 +1,18 @@
 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* 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 "BasicCardPayment.h"
 #include "mozilla/dom/PaymentMethodChangeEvent.h"
 #include "mozilla/dom/PaymentRequestUpdateEvent.h"
-#include "mozilla/HoldDropJSObjects.h"
+#include "PaymentRequestUtils.h"
 
 namespace mozilla {
 namespace dom {
 
 NS_IMPL_CYCLE_COLLECTION_CLASS(PaymentMethodChangeEvent)
 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(PaymentMethodChangeEvent,
                                                 PaymentRequestUpdateEvent)
   tmp->mMethodDetails = nullptr;
@@ -32,65 +33,146 @@ NS_IMPL_RELEASE_INHERITED(PaymentMethodC
 
 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PaymentMethodChangeEvent)
 NS_INTERFACE_MAP_END_INHERITING(PaymentRequestUpdateEvent)
 
 already_AddRefed<PaymentMethodChangeEvent>
 PaymentMethodChangeEvent::Constructor(
   mozilla::dom::EventTarget* aOwner,
   const nsAString& aType,
-  const PaymentMethodChangeEventInit& aEventInitDict)
+  const PaymentRequestUpdateEventInit& aEventInitDict,
+  const nsAString& aMethodName,
+  const ChangeDetails& aMethodDetails)
 {
   RefPtr<PaymentMethodChangeEvent> e = new PaymentMethodChangeEvent(aOwner);
   bool trusted = e->Init(aOwner);
   e->InitEvent(aType, aEventInitDict.mBubbles, aEventInitDict.mCancelable);
   e->SetTrusted(trusted);
   e->SetComposed(aEventInitDict.mComposed);
-  e->init(aEventInitDict);
+  e->SetMethodName(aMethodName);
+  e->SetMethodDetails(aMethodDetails);
   return e.forget();
 }
 
 already_AddRefed<PaymentMethodChangeEvent>
 PaymentMethodChangeEvent::Constructor(
   const GlobalObject& aGlobal,
   const nsAString& aType,
   const PaymentMethodChangeEventInit& aEventInitDict,
   ErrorResult& aRv)
 {
   nsCOMPtr<mozilla::dom::EventTarget> owner =
     do_QueryInterface(aGlobal.GetAsSupports());
-  return Constructor(owner, aType, aEventInitDict);
+  RefPtr<PaymentMethodChangeEvent> e = new PaymentMethodChangeEvent(owner);
+  bool trusted = e->Init(owner);
+  e->InitEvent(aType, aEventInitDict.mBubbles, aEventInitDict.mCancelable);
+  e->SetTrusted(trusted);
+  e->SetComposed(aEventInitDict.mComposed);
+  e->init(aEventInitDict);
+  return e.forget();
 }
 
 PaymentMethodChangeEvent::PaymentMethodChangeEvent(EventTarget* aOwner)
   : PaymentRequestUpdateEvent(aOwner)
 {
   MOZ_ASSERT(aOwner);
   mozilla::HoldJSObjects(this);
 }
 
 void
-PaymentMethodChangeEvent::init(
-  const PaymentMethodChangeEventInit& aEventInitDict)
+PaymentMethodChangeEvent::init(const PaymentMethodChangeEventInit& aEventInitDict)
 {
   mMethodName.Assign(aEventInitDict.mMethodName);
   mMethodDetails = aEventInitDict.mMethodDetails;
 }
 
 void
 PaymentMethodChangeEvent::GetMethodName(nsAString& aMethodName)
 {
   aMethodName.Assign(mMethodName);
 }
 
 void
-PaymentMethodChangeEvent::GetMethodDetails(JSContext* cx,
-                                           JS::MutableHandle<JSObject*> retval)
+PaymentMethodChangeEvent::SetMethodName(const nsAString& aMethodName)
+{
+  mMethodName = aMethodName;
+}
+
+void
+PaymentMethodChangeEvent::GetMethodDetails(JSContext* aCx,
+                                           JS::MutableHandle<JSObject*> aRetVal)
 {
-  retval.set(mMethodDetails.get());
+  MOZ_ASSERT(aCx);
+
+  if (mMethodDetails) {
+    aRetVal.set(mMethodDetails.get());
+    return;
+  }
+
+  RefPtr<BasicCardService> service = BasicCardService::GetService();
+  MOZ_ASSERT(service);
+  aRetVal.set(nullptr);
+  switch(mInternalDetails.type()) {
+    case ChangeDetails::GeneralMethodDetails: {
+      const GeneralDetails& rawDetails = mInternalDetails.generalDetails();
+      DeserializeToJSObject(rawDetails.details, aCx, aRetVal);
+      break;
+    }
+    case ChangeDetails::BasicCardMethodDetails: {
+      const BasicCardDetails& rawDetails = mInternalDetails.basicCardDetails();
+      BasicCardChangeDetails basicCardDetails;
+      PaymentOptions options;
+      mRequest->GetOptions(options);
+      if (options.mRequestBillingAddress) {
+        if (!rawDetails.billingAddress.country.IsEmpty() ||
+            !rawDetails.billingAddress.addressLine.IsEmpty() ||
+            !rawDetails.billingAddress.region.IsEmpty() ||
+            !rawDetails.billingAddress.regionCode.IsEmpty() ||
+            !rawDetails.billingAddress.city.IsEmpty() ||
+            !rawDetails.billingAddress.dependentLocality.IsEmpty() ||
+            !rawDetails.billingAddress.postalCode.IsEmpty() ||
+            !rawDetails.billingAddress.sortingCode.IsEmpty() ||
+            !rawDetails.billingAddress.organization.IsEmpty() ||
+            !rawDetails.billingAddress.recipient.IsEmpty() ||
+            !rawDetails.billingAddress.phone.IsEmpty()) {
+          nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(GetParentObject());
+          basicCardDetails.mBillingAddress.Construct();
+          basicCardDetails.mBillingAddress.Value() =
+            new PaymentAddress(window,
+                               rawDetails.billingAddress.country,
+                               rawDetails.billingAddress.addressLine,
+                               rawDetails.billingAddress.region,
+                               rawDetails.billingAddress.regionCode,
+                               rawDetails.billingAddress.city,
+                               rawDetails.billingAddress.dependentLocality,
+                               rawDetails.billingAddress.postalCode,
+                               rawDetails.billingAddress.sortingCode,
+                               rawDetails.billingAddress.organization,
+                               rawDetails.billingAddress.recipient,
+                               rawDetails.billingAddress.phone);
+        }
+      }
+      MOZ_ASSERT(aCx);
+      JS::RootedValue value(aCx);
+      if (NS_WARN_IF(!basicCardDetails.ToObjectInternal(aCx, &value))) {
+        return;
+      }
+      aRetVal.set(&value.toObject());
+      break;
+    }
+    default: {
+      break;
+    }
+  }
+}
+
+void
+PaymentMethodChangeEvent::SetMethodDetails(const ChangeDetails& aMethodDetails)
+{
+  mInternalDetails = aMethodDetails;
 }
 
 PaymentMethodChangeEvent::~PaymentMethodChangeEvent()
 {
   mozilla::DropJSObjects(this);
 }
 
 JSObject*
--- a/dom/payments/PaymentMethodChangeEvent.h
+++ b/dom/payments/PaymentMethodChangeEvent.h
@@ -27,34 +27,39 @@ public:
 
   virtual JSObject* WrapObjectInternal(
     JSContext* aCx,
     JS::Handle<JSObject*> aGivenProto) override;
 
   static already_AddRefed<PaymentMethodChangeEvent> Constructor(
     EventTarget* aOwner,
     const nsAString& aType,
-    const PaymentMethodChangeEventInit& aEventInitDict);
+    const PaymentRequestUpdateEventInit& aEventInitDict,
+    const nsAString& aMethodName,
+    const ChangeDetails& aMethodDetails);
 
   // Called by WebIDL constructor
   static already_AddRefed<PaymentMethodChangeEvent> Constructor(
     const GlobalObject& aGlobal,
     const nsAString& aType,
     const PaymentMethodChangeEventInit& aEventInitDict,
     ErrorResult& aRv);
 
   void GetMethodName(nsAString& aMethodName);
+  void SetMethodName(const nsAString& aMethodName);
 
   void GetMethodDetails(JSContext* cx, JS::MutableHandle<JSObject*> retval);
+  void SetMethodDetails(const ChangeDetails& aMethodDetails);
 
 protected:
   void init(const PaymentMethodChangeEventInit& aEventInitDict);
   ~PaymentMethodChangeEvent();
 
 private:
   JS::Heap<JSObject*> mMethodDetails;
+  ChangeDetails mInternalDetails;
   nsString mMethodName;
 };
 
 } // namespace dom
 } // namespace mozilla
 
 #endif // mozilla_dom_PaymentMethodChangeEvent_h
--- a/dom/payments/PaymentRequest.cpp
+++ b/dom/payments/PaymentRequest.cpp
@@ -1042,18 +1042,33 @@ PaymentRequest::DispatchMerchantValidati
   return rv.StealNSResult();
 }
 
 nsresult
 PaymentRequest::DispatchPaymentMethodChangeEvent(const nsAString& aMethodName,
                                                  const ChangeDetails& aMethodDetails)
 {
   MOZ_ASSERT(ReadyForUpdate());
-  // TODO: create and dispatch a PaymentMethodChangeEvent
-  return NS_OK;
+
+  PaymentRequestUpdateEventInit init;
+  init.mBubbles = false;
+  init.mCancelable = false;
+
+  RefPtr<PaymentMethodChangeEvent> event =
+    PaymentMethodChangeEvent::Constructor(this,
+                                          NS_LITERAL_STRING("paymentmethodchange"),
+                                          init,
+                                          aMethodName,
+                                          aMethodDetails);
+  event->SetTrusted(true);
+  event->SetRequest(this);
+
+  ErrorResult rv;
+  DispatchEvent(*event, rv);
+  return rv.StealNSResult();
 }
 
 already_AddRefed<PaymentAddress>
 PaymentRequest::GetShippingAddress() const
 {
   RefPtr<PaymentAddress> address = mShippingAddress;
   return address.forget();
 }
--- a/dom/payments/PaymentRequestUpdateEvent.h
+++ b/dom/payments/PaymentRequestUpdateEvent.h
@@ -49,18 +49,16 @@ public:
               ErrorResult& aRv);
 
   void UpdateWith(Promise& aPromise, ErrorResult& aRv);
 
   void SetRequest(PaymentRequest* aRequest);
 
 protected:
   ~PaymentRequestUpdateEvent();
-
-private:
   // Indicating whether an updateWith()-initiated update is currently in progress.
   bool mWaitForUpdate;
   RefPtr<PaymentRequest> mRequest;
 };
 
 } // namespace dom
 } // namespace mozilla
 
--- a/dom/payments/PaymentResponse.h
+++ b/dom/payments/PaymentResponse.h
@@ -80,17 +80,17 @@ public:
     return *this;
   }
   ResponseData& operator = (const BasicCardData& aBasicCardData) {
     mType = BasicCardResponse;
     mGeneralData = GeneralData();
     mBasicCardData = aBasicCardData;
     return *this;
   }
-  virtual ~ResponseData() = default;
+  ~ResponseData() = default;
 
   const Type& type() const { return mType; }
   const GeneralData& generalData() const { return mGeneralData; }
   const BasicCardData& basicCardData() const { return mBasicCardData;}
 private:
   Type mType;
   GeneralData mGeneralData;
   BasicCardData mBasicCardData;