Index: Source/modules/app_banner/BeforeInstallPromptEvent.cpp |
diff --git a/Source/modules/app_banner/BeforeInstallPromptEvent.cpp b/Source/modules/app_banner/BeforeInstallPromptEvent.cpp |
index c4d117dd853329d8168d27b16327fbb5f745a663..2f5c1eeaddd7b1eaa886fee485c3c1879bc7574d 100644 |
--- a/Source/modules/app_banner/BeforeInstallPromptEvent.cpp |
+++ b/Source/modules/app_banner/BeforeInstallPromptEvent.cpp |
@@ -5,12 +5,10 @@ |
#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/AppBannerCallbacks.h" |
#include "modules/app_banner/BeforeInstallPromptEventInit.h" |
#include "public/platform/modules/app_banner/WebAppBannerClient.h" |
@@ -20,12 +18,13 @@ BeforeInstallPromptEvent::BeforeInstallPromptEvent() |
{ |
} |
-BeforeInstallPromptEvent::BeforeInstallPromptEvent(const AtomicString& name, const Vector<String>& platforms, int requestId, WebAppBannerClient* client) |
+BeforeInstallPromptEvent::BeforeInstallPromptEvent(const AtomicString& name, ExecutionContext* executionContext, const Vector<String>& platforms, int requestId, WebAppBannerClient* client) |
: Event(name, false, true) |
, m_platforms(platforms) |
, m_requestId(requestId) |
, m_client(client) |
- , m_redispatched(false) |
+ , m_userChoice(new UserChoiceProperty(executionContext, this, UserChoiceProperty::UserChoice)) |
+ , m_registered(false) |
{ |
} |
@@ -33,7 +32,6 @@ BeforeInstallPromptEvent::BeforeInstallPromptEvent(const AtomicString& name, con |
: Event(name, false, true) |
, m_requestId(-1) |
, m_client(nullptr) |
- , m_redispatched(false) |
{ |
if (init.hasPlatforms()) |
m_platforms = init.platforms(); |
@@ -50,14 +48,14 @@ Vector<String> BeforeInstallPromptEvent::platforms() const |
ScriptPromise BeforeInstallPromptEvent::userChoice(ScriptState* scriptState) |
{ |
- 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)); |
+ if (m_userChoice && m_client && m_requestId != -1) { |
+ if (!m_registered) { |
+ m_registered = true; |
+ m_client->registerBannerCallbacks(m_requestId, new AppBannerCallbacks(m_userChoice.get())); |
+ } |
+ return m_userChoice->promise(scriptState->world()); |
} |
- |
- return m_userChoice; |
+ return ScriptPromise::rejectWithDOMException(scriptState, DOMException::create(InvalidStateError, "userChoice cannot be accessed on this event.")); |
} |
const AtomicString& BeforeInstallPromptEvent::interfaceName() const |
@@ -67,14 +65,22 @@ const AtomicString& BeforeInstallPromptEvent::interfaceName() const |
ScriptPromise BeforeInstallPromptEvent::prompt(ScriptState* scriptState) |
{ |
- if (m_client && defaultPrevented() && !m_redispatched) { |
- ASSERT(m_requestId != -1); |
- m_redispatched = true; |
- m_client->showAppBanner(m_requestId); |
- return ScriptPromise::cast(scriptState, v8::Undefined(scriptState->isolate())); |
- } |
+ // |m_registered| will be true if userChoice has already been accessed |
+ // or prompt() has already been called. Return a rejected promise in both |
+ // these cases, as well as if we have a null client or invalid requestId. |
+ if (m_registered || !defaultPrevented() || !m_client || m_requestId == -1) |
+ return ScriptPromise::rejectWithDOMException(scriptState, DOMException::create(InvalidStateError, "The prompt() method may only be called once, following preventDefault().")); |
- return ScriptPromise::rejectWithDOMException(scriptState, DOMException::create(InvalidStateError, "The prompt() method may only be called once, following preventDefault().")); |
+ m_registered = true; |
+ m_client->registerBannerCallbacks(m_requestId, new AppBannerCallbacks(m_userChoice.get())); |
+ m_client->showAppBanner(m_requestId); |
+ return ScriptPromise::cast(scriptState, v8::Undefined(scriptState->isolate())); |
+} |
+ |
+DEFINE_TRACE(BeforeInstallPromptEvent) |
+{ |
+ visitor->trace(m_userChoice); |
+ Event::trace(visitor); |
} |
} // namespace blink |