Index: Source/modules/app_banner/BeforeInstallPromptEvent.cpp |
diff --git a/Source/modules/app_banner/BeforeInstallPromptEvent.cpp b/Source/modules/app_banner/BeforeInstallPromptEvent.cpp |
index 56646a3fbc38339bc3b07ed61e1065c113610b37..c4d117dd853329d8168d27b16327fbb5f745a663 100644 |
--- a/Source/modules/app_banner/BeforeInstallPromptEvent.cpp |
+++ b/Source/modules/app_banner/BeforeInstallPromptEvent.cpp |
@@ -5,9 +5,12 @@ |
#include "config.h" |
#include "modules/app_banner/BeforeInstallPromptEvent.h" |
+#include "bindings/core/v8/CallbackPromiseAdapter.h" |
#include "bindings/core/v8/ScriptPromise.h" |
+#include "bindings/core/v8/ScriptPromiseResolver.h" |
#include "core/dom/DOMException.h" |
#include "core/dom/ExceptionCode.h" |
+#include "modules/app_banner/AppBannerPromptResult.h" |
#include "modules/app_banner/BeforeInstallPromptEventInit.h" |
#include "public/platform/modules/app_banner/WebAppBannerClient.h" |
@@ -17,13 +20,12 @@ |
{ |
} |
-BeforeInstallPromptEvent::BeforeInstallPromptEvent(const AtomicString& name, ExecutionContext* executionContext, const Vector<String>& platforms, int requestId, WebAppBannerClient* client) |
+BeforeInstallPromptEvent::BeforeInstallPromptEvent(const AtomicString& name, const Vector<String>& platforms, int requestId, WebAppBannerClient* client) |
: Event(name, false, true) |
, m_platforms(platforms) |
, m_requestId(requestId) |
, m_client(client) |
- , m_userChoice(new UserChoiceProperty(executionContext, this, UserChoiceProperty::UserChoice)) |
- , m_prompt(new PromptProperty(executionContext, this, PromptProperty::Prompt)) |
+ , m_redispatched(false) |
{ |
} |
@@ -31,6 +33,7 @@ |
: Event(name, false, true) |
, m_requestId(-1) |
, m_client(nullptr) |
+ , m_redispatched(false) |
{ |
if (init.hasPlatforms()) |
m_platforms = init.platforms(); |
@@ -47,9 +50,14 @@ |
ScriptPromise BeforeInstallPromptEvent::userChoice(ScriptState* scriptState) |
{ |
- if (m_userChoice) |
- return m_userChoice->promise(scriptState->world()); |
- return ScriptPromise::rejectWithDOMException(scriptState, DOMException::create(InvalidStateError, "The prompt() method cannot be called on this event.")); |
+ if (m_userChoice.isEmpty() && m_client) { |
+ ASSERT(m_requestId != -1); |
+ RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState); |
+ m_userChoice = resolver->promise(); |
+ m_client->registerBannerCallbacks(m_requestId, new CallbackPromiseAdapter<AppBannerPromptResult, void>(resolver)); |
+ } |
+ |
+ return m_userChoice; |
} |
const AtomicString& BeforeInstallPromptEvent::interfaceName() const |
@@ -59,28 +67,14 @@ |
ScriptPromise BeforeInstallPromptEvent::prompt(ScriptState* scriptState) |
{ |
- if (!m_prompt) |
- return ScriptPromise::rejectWithDOMException(scriptState, DOMException::create(InvalidStateError, "The prompt() method cannot be called on this event.")); |
- |
- if (m_prompt->state() != PromptProperty::Pending) |
- return m_prompt->promise(scriptState->world()); |
- |
- if (!m_client || !defaultPrevented()) { |
- m_prompt->reject(DOMException::create(InvalidStateError, "The prompt() method may only be called once, following preventDefault().")); |
- } else { |
+ if (m_client && defaultPrevented() && !m_redispatched) { |
ASSERT(m_requestId != -1); |
- m_prompt->resolve(ToV8UndefinedGenerator()); |
+ m_redispatched = true; |
m_client->showAppBanner(m_requestId); |
+ return ScriptPromise::cast(scriptState, v8::Undefined(scriptState->isolate())); |
} |
- return m_prompt->promise(scriptState->world()); |
-} |
- |
-DEFINE_TRACE(BeforeInstallPromptEvent) |
-{ |
- visitor->trace(m_userChoice); |
- visitor->trace(m_prompt); |
- Event::trace(visitor); |
+ return ScriptPromise::rejectWithDOMException(scriptState, DOMException::create(InvalidStateError, "The prompt() method may only be called once, following preventDefault().")); |
} |
} // namespace blink |