Title: [237132] trunk/Source/WebCore
Revision
237132
Author
aes...@apple.com
Date
2018-10-15 12:34:18 -0700 (Mon, 15 Oct 2018)

Log Message

[Payment Request] Use a PendingActivity token rather than calling (un)setPendingActivity
https://bugs.webkit.org/show_bug.cgi?id=190557

Reviewed by Alex Christensen.

PaymentRequest has pending activity whenever there is an active payment handler, since
user-initiated events can occur whenever the payment UI is displayed.

Rather than manually track the pending activity with setPendingActivity() and
unsetPendingActivity(), use a PendingActivity token tied to the lifetime of the active
payment handler. Also, rewrite canSuspendForDocumentSuspension() in terms of
hasPendingActivity().

* Modules/paymentrequest/PaymentRequest.cpp:
(WebCore::PaymentRequest::show):
(WebCore::PaymentRequest::abortWithException):
(WebCore::PaymentRequest::canSuspendForDocumentSuspension const):
(WebCore::PaymentRequest::paymentMethodChanged):
(WebCore::PaymentRequest::completeMerchantValidation):
(WebCore::PaymentRequest::settleDetailsPromise):
(WebCore::PaymentRequest::complete):
(WebCore::PaymentRequest::cancel):
* Modules/paymentrequest/PaymentRequest.h:

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (237131 => 237132)


--- trunk/Source/WebCore/ChangeLog	2018-10-15 19:28:09 UTC (rev 237131)
+++ trunk/Source/WebCore/ChangeLog	2018-10-15 19:34:18 UTC (rev 237132)
@@ -1,3 +1,29 @@
+2018-10-15  Andy Estes  <aes...@apple.com>
+
+        [Payment Request] Use a PendingActivity token rather than calling (un)setPendingActivity
+        https://bugs.webkit.org/show_bug.cgi?id=190557
+
+        Reviewed by Alex Christensen.
+
+        PaymentRequest has pending activity whenever there is an active payment handler, since
+        user-initiated events can occur whenever the payment UI is displayed.
+
+        Rather than manually track the pending activity with setPendingActivity() and
+        unsetPendingActivity(), use a PendingActivity token tied to the lifetime of the active
+        payment handler. Also, rewrite canSuspendForDocumentSuspension() in terms of
+        hasPendingActivity().
+
+        * Modules/paymentrequest/PaymentRequest.cpp:
+        (WebCore::PaymentRequest::show):
+        (WebCore::PaymentRequest::abortWithException):
+        (WebCore::PaymentRequest::canSuspendForDocumentSuspension const):
+        (WebCore::PaymentRequest::paymentMethodChanged):
+        (WebCore::PaymentRequest::completeMerchantValidation):
+        (WebCore::PaymentRequest::settleDetailsPromise):
+        (WebCore::PaymentRequest::complete):
+        (WebCore::PaymentRequest::cancel):
+        * Modules/paymentrequest/PaymentRequest.h:
+
 2018-10-15  Wenson Hsieh  <wenson_hs...@apple.com>
 
         Changing view scale should zoom to initial scale if the page is already at initial scale

Modified: trunk/Source/WebCore/Modules/paymentrequest/PaymentRequest.cpp (237131 => 237132)


--- trunk/Source/WebCore/Modules/paymentrequest/PaymentRequest.cpp	2018-10-15 19:28:09 UTC (rev 237131)
+++ trunk/Source/WebCore/Modules/paymentrequest/PaymentRequest.cpp	2018-10-15 19:34:18 UTC (rev 237132)
@@ -434,8 +434,7 @@
     }
 
     ASSERT(!m_activePaymentHandler);
-    m_activePaymentHandler = WTFMove(selectedPaymentHandler);
-    setPendingActivity(this); // unsetPendingActivity() is called below in stop()
+    m_activePaymentHandler = PaymentHandlerWithPendingActivity { selectedPaymentHandler.releaseNonNull(), makePendingActivity(*this) };
 
     if (!detailsPromise)
         return;
@@ -449,10 +448,9 @@
     if (m_state != State::Interactive)
         return;
 
-    if (auto paymentHandler = std::exchange(m_activePaymentHandler, nullptr)) {
-        unsetPendingActivity(this);
+    if (auto paymentHandler = activePaymentHandler())
         paymentHandler->hide();
-    }
+    m_activePaymentHandler = std::nullopt;
 
     ASSERT(m_state == State::Interactive);
     m_state = State::Closed;
@@ -526,14 +524,7 @@
 
 bool PaymentRequest::canSuspendForDocumentSuspension() const
 {
-    switch (m_state) {
-    case State::Created:
-        ASSERT(!m_activePaymentHandler);
-        return true;
-    case State::Interactive:
-    case State::Closed:
-        return !m_activePaymentHandler;
-    }
+    return !hasPendingActivity();
 }
 
 void PaymentRequest::shippingAddressChanged(Ref<PaymentAddress>&& shippingAddress)
@@ -559,7 +550,7 @@
         if (hasEventListeners(eventName))
             dispatchEvent(PaymentMethodChangeEvent::create(eventName, methodName, WTFMove(methodDetailsFunction)));
         else
-            m_activePaymentHandler->detailsUpdated(UpdateReason::PaymentMethodChanged, { }, { }, { }, { });
+            activePaymentHandler()->detailsUpdated(UpdateReason::PaymentMethodChanged, { }, { }, { }, { });
     });
 }
 
@@ -600,7 +591,7 @@
             return;
         }
 
-        auto exception = m_activePaymentHandler->merchantValidationCompleted(m_merchantSessionPromise->result());
+        auto exception = activePaymentHandler()->merchantValidationCompleted(m_merchantSessionPromise->result());
         if (exception.hasException()) {
             abortWithException(exception.releaseException());
             return;
@@ -658,7 +649,7 @@
     m_details.modifiers = WTFMove(detailsUpdate.modifiers);
     m_serializedModifierData = WTFMove(std::get<1>(shippingOptionAndModifierData));
 
-    auto result = m_activePaymentHandler->detailsUpdated(reason, WTFMove(detailsUpdate.error), WTFMove(detailsUpdate.shippingAddressErrors), WTFMove(detailsUpdate.payerErrors), detailsUpdate.paymentMethodErrors.get());
+    auto result = activePaymentHandler()->detailsUpdated(reason, WTFMove(detailsUpdate.error), WTFMove(detailsUpdate.shippingAddressErrors), WTFMove(detailsUpdate.payerErrors), detailsUpdate.paymentMethodErrors.get());
     if (result.hasException()) {
         abortWithException(result.releaseException());
         return;
@@ -715,7 +706,8 @@
 void PaymentRequest::complete(std::optional<PaymentComplete>&& result)
 {
     ASSERT(m_state == State::Closed);
-    std::exchange(m_activePaymentHandler, nullptr)->complete(WTFMove(result));
+    activePaymentHandler()->complete(WTFMove(result));
+    m_activePaymentHandler = std::nullopt;
 }
 
 void PaymentRequest::cancel()
@@ -723,7 +715,7 @@
     if (m_state != State::Interactive)
         return;
 
-    m_activePaymentHandler = nullptr;
+    m_activePaymentHandler = std::nullopt;
 
     if (m_isUpdating) {
         m_isCancelPending = true;

Modified: trunk/Source/WebCore/Modules/paymentrequest/PaymentRequest.h (237131 => 237132)


--- trunk/Source/WebCore/Modules/paymentrequest/PaymentRequest.h	2018-10-15 19:28:09 UTC (rev 237131)
+++ trunk/Source/WebCore/Modules/paymentrequest/PaymentRequest.h	2018-10-15 19:34:18 UTC (rev 237132)
@@ -106,11 +106,17 @@
         String serializedData;
     };
 
+    struct PaymentHandlerWithPendingActivity {
+        Ref<PaymentHandler> paymentHandler;
+        Ref<PendingActivity<PaymentRequest>> pendingActivity;
+    };
+
     PaymentRequest(Document&, PaymentOptions&&, PaymentDetailsInit&&, Vector<String>&& serializedModifierData, Vector<Method>&& serializedMethodData, String&& selectedShippingOption);
 
     void settleDetailsPromise(UpdateReason);
     void whenDetailsSettled(std::function<void()>&&);
     void abortWithException(Exception&&);
+    PaymentHandler* activePaymentHandler() { return m_activePaymentHandler ? m_activePaymentHandler->paymentHandler.ptr() : nullptr; }
 
     // ActiveDOMObject
     const char* activeDOMObjectName() const final { return "PaymentRequest"; }
@@ -132,7 +138,7 @@
     RefPtr<PaymentAddress> m_shippingAddress;
     State m_state { State::Created };
     std::optional<ShowPromise> m_showPromise;
-    RefPtr<PaymentHandler> m_activePaymentHandler;
+    std::optional<PaymentHandlerWithPendingActivity> m_activePaymentHandler;
     RefPtr<DOMPromise> m_detailsPromise;
     RefPtr<DOMPromise> m_merchantSessionPromise;
     bool m_isUpdating { false };
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to