| Index: third_party/WebKit/Source/modules/app_banner/BeforeInstallPromptEvent.cpp
|
| diff --git a/third_party/WebKit/Source/modules/app_banner/BeforeInstallPromptEvent.cpp b/third_party/WebKit/Source/modules/app_banner/BeforeInstallPromptEvent.cpp
|
| index 8c7d0e3a56a753581674b4ce98ef755d8ac84552..1d78fb2383aaa40a9aa9c30acaecacf0d0cdabed 100644
|
| --- a/third_party/WebKit/Source/modules/app_banner/BeforeInstallPromptEvent.cpp
|
| +++ b/third_party/WebKit/Source/modules/app_banner/BeforeInstallPromptEvent.cpp
|
| @@ -4,43 +4,48 @@
|
|
|
| #include "modules/app_banner/BeforeInstallPromptEvent.h"
|
|
|
| -#include "bindings/core/v8/ScriptPromise.h"
|
| #include "core/dom/DOMException.h"
|
| +#include "core/dom/Document.h"
|
| #include "core/dom/ExceptionCode.h"
|
| #include "core/frame/UseCounter.h"
|
| -#include "modules/app_banner/AppBannerCallbacks.h"
|
| #include "modules/app_banner/BeforeInstallPromptEventInit.h"
|
| -#include "public/platform/modules/app_banner/WebAppBannerClient.h"
|
|
|
| namespace blink {
|
|
|
| BeforeInstallPromptEvent::BeforeInstallPromptEvent(
|
| const AtomicString& name,
|
| - ExecutionContext* executionContext,
|
| - const Vector<String>& platforms,
|
| - int requestId,
|
| - WebAppBannerClient* client)
|
| + LocalFrame& frame,
|
| + mojom::blink::AppBannerServicePtr servicePtr,
|
| + mojom::blink::AppBannerEventRequest eventRequest,
|
| + const Vector<String>& platforms)
|
| : Event(name, false, true),
|
| + m_bannerService(std::move(servicePtr)),
|
| + m_binding(this, std::move(eventRequest)),
|
| m_platforms(platforms),
|
| - m_requestId(requestId),
|
| - m_client(client),
|
| - m_userChoice(new UserChoiceProperty(executionContext,
|
| + m_userChoice(new UserChoiceProperty(frame.document(),
|
| this,
|
| UserChoiceProperty::UserChoice)),
|
| - m_registered(false) {
|
| - UseCounter::count(executionContext, UseCounter::BeforeInstallPromptEvent);
|
| + m_promptCalled(false) {
|
| + DCHECK(m_bannerService);
|
| + DCHECK(m_binding.is_bound());
|
| + UseCounter::count(&frame, UseCounter::BeforeInstallPromptEvent);
|
| }
|
|
|
| BeforeInstallPromptEvent::BeforeInstallPromptEvent(
|
| const AtomicString& name,
|
| const BeforeInstallPromptEventInit& init)
|
| - : Event(name, false, true), m_requestId(-1), m_client(nullptr) {
|
| + : Event(name, false, true), m_binding(this), m_promptCalled(false) {
|
| if (init.hasPlatforms())
|
| m_platforms = init.platforms();
|
| }
|
|
|
| BeforeInstallPromptEvent::~BeforeInstallPromptEvent() {}
|
|
|
| +void BeforeInstallPromptEvent::dispose() {
|
| + m_bannerService.reset();
|
| + m_binding.Close();
|
| +}
|
| +
|
| Vector<String> BeforeInstallPromptEvent::platforms() const {
|
| return m_platforms;
|
| }
|
| @@ -48,14 +53,10 @@ Vector<String> BeforeInstallPromptEvent::platforms() const {
|
| ScriptPromise BeforeInstallPromptEvent::userChoice(ScriptState* scriptState) {
|
| UseCounter::count(scriptState->getExecutionContext(),
|
| UseCounter::BeforeInstallPromptEventUserChoice);
|
| - if (m_userChoice && m_client && m_requestId != -1) {
|
| - if (!m_registered) {
|
| - m_registered = true;
|
| - m_client->registerBannerCallbacks(
|
| - m_requestId, new AppBannerCallbacks(m_userChoice.get()));
|
| - }
|
| + // |m_binding| must be bound to allow the AppBannerService to resolve the
|
| + // userChoice promise.
|
| + if (m_userChoice && m_binding.is_bound())
|
| return m_userChoice->promise(scriptState->world());
|
| - }
|
| return ScriptPromise::rejectWithDOMException(
|
| scriptState,
|
| DOMException::create(InvalidStateError,
|
| @@ -63,23 +64,21 @@ ScriptPromise BeforeInstallPromptEvent::userChoice(ScriptState* scriptState) {
|
| }
|
|
|
| ScriptPromise BeforeInstallPromptEvent::prompt(ScriptState* scriptState) {
|
| - UseCounter::count(scriptState->getExecutionContext(),
|
| - UseCounter::BeforeInstallPromptEventPrompt);
|
| -
|
| - // |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)
|
| + // |m_bannerService| must be bound to allow us to inform the AppBannerService
|
| + // to display the banner now.
|
| + if (!defaultPrevented() || m_promptCalled || !m_bannerService.is_bound()) {
|
| 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);
|
| + UseCounter::count(scriptState->getExecutionContext(),
|
| + UseCounter::BeforeInstallPromptEventPrompt);
|
| +
|
| + m_promptCalled = true;
|
| + m_bannerService->DisplayAppBanner();
|
| return ScriptPromise::castUndefined(scriptState);
|
| }
|
|
|
| @@ -89,8 +88,20 @@ const AtomicString& BeforeInstallPromptEvent::interfaceName() const {
|
|
|
| void BeforeInstallPromptEvent::preventDefault() {
|
| Event::preventDefault();
|
| - UseCounter::count(target()->getExecutionContext(),
|
| - UseCounter::BeforeInstallPromptEventPreventDefault);
|
| + if (target()) {
|
| + UseCounter::count(target()->getExecutionContext(),
|
| + UseCounter::BeforeInstallPromptEventPreventDefault);
|
| + }
|
| +}
|
| +
|
| +void BeforeInstallPromptEvent::BannerAccepted(const String& platform) {
|
| + m_userChoice->resolve(AppBannerPromptResult::create(
|
| + platform, AppBannerPromptResult::Outcome::Accepted));
|
| +}
|
| +
|
| +void BeforeInstallPromptEvent::BannerDismissed() {
|
| + m_userChoice->resolve(AppBannerPromptResult::create(
|
| + emptyAtom, AppBannerPromptResult::Outcome::Dismissed));
|
| }
|
|
|
| DEFINE_TRACE(BeforeInstallPromptEvent) {
|
|
|