Commit 92c5d3d1 authored by sanjoy.pal's avatar sanjoy.pal Committed by Commit bot

Add a timeout to the update event in case page doesn't resolve promise from updateWith

Specification:
https://www.w3.org/TR/payment-request/#updatewith

BUG=629462

Review-Url: https://codereview.chromium.org/2170783002
Cr-Commit-Position: refs/heads/master@{#407082}
parent b6b8de68
......@@ -9,10 +9,13 @@
#include "core/dom/DOMException.h"
#include "core/dom/ExceptionCode.h"
#include "modules/payments/PaymentUpdater.h"
#include "public/platform/WebTraceLocation.h"
namespace blink {
namespace {
static const int abortTimeout = 60; // Reject the payment request if the page does not resolve the promise from updateWith within 60 seconds.
class UpdatePaymentDetailsFunction : public ScriptFunction {
public:
static v8::Local<v8::Function> createFunction(ScriptState* scriptState, PaymentUpdater* updater)
......@@ -93,6 +96,8 @@ PaymentRequestUpdateEvent* PaymentRequestUpdateEvent::create(const AtomicString&
void PaymentRequestUpdateEvent::setPaymentDetailsUpdater(PaymentUpdater* updater)
{
DCHECK(!m_abortTimer.isActive());
m_abortTimer.startOneShot(abortTimeout, BLINK_FROM_HERE);
m_updater = updater;
}
......@@ -113,11 +118,20 @@ void PaymentRequestUpdateEvent::updateWith(ScriptState* scriptState, ScriptPromi
stopImmediatePropagation();
m_waitForUpdate = true;
m_abortTimer.stop();
promise.then(UpdatePaymentDetailsFunction::createFunction(scriptState, m_updater),
UpdatePaymentDetailsErrorFunction::createFunction(scriptState, m_updater));
}
void PaymentRequestUpdateEvent::onTimerFired(Timer<PaymentRequestUpdateEvent>*)
{
if (!m_updater)
return;
m_updater->onUpdatePaymentDetailsFailure(ScriptValue());
}
DEFINE_TRACE(PaymentRequestUpdateEvent)
{
visitor->trace(m_updater);
......@@ -126,12 +140,14 @@ DEFINE_TRACE(PaymentRequestUpdateEvent)
PaymentRequestUpdateEvent::PaymentRequestUpdateEvent()
: m_waitForUpdate(false)
, m_abortTimer(this, &PaymentRequestUpdateEvent::onTimerFired)
{
}
PaymentRequestUpdateEvent::PaymentRequestUpdateEvent(const AtomicString& type, const PaymentRequestUpdateEventInit& init)
: Event(type, init)
, m_waitForUpdate(false)
, m_abortTimer(this, &PaymentRequestUpdateEvent::onTimerFired)
{
}
......
......@@ -10,6 +10,7 @@
#include "core/events/Event.h"
#include "modules/ModulesExport.h"
#include "modules/payments/PaymentRequestUpdateEventInit.h"
#include "platform/Timer.h"
#include "platform/heap/Handle.h"
namespace blink {
......@@ -31,6 +32,8 @@ public:
void updateWith(ScriptState*, ScriptPromise, ExceptionState&);
void onTimerFired(Timer<PaymentRequestUpdateEvent>*);
DECLARE_VIRTUAL_TRACE();
private:
......@@ -39,6 +42,7 @@ private:
Member<PaymentUpdater> m_updater;
bool m_waitForUpdate;
Timer<PaymentRequestUpdateEvent> m_abortTimer;
};
} // namespace blink
......
......@@ -101,5 +101,18 @@ TEST(PaymentRequestUpdateEventTest, UpdaterNotRequired)
EXPECT_FALSE(scope.getExceptionState().hadException());
}
TEST(PaymentRequestUpdateEventTest, OnUpdatePaymentDetailsTimeout)
{
V8TestingScope scope;
PaymentRequestUpdateEvent* event = PaymentRequestUpdateEvent::create();
MockPaymentUpdater* updater = new MockPaymentUpdater;
event->setPaymentDetailsUpdater(updater);
EXPECT_CALL(*updater, onUpdatePaymentDetails(testing::_)).Times(0);
EXPECT_CALL(*updater, onUpdatePaymentDetailsFailure(testing::_));
event->onTimerFired(0);
}
} // namespace
} // namespace blink
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment