| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "modules/app_banner/BeforeInstallPromptEvent.h" | 5 #include "modules/app_banner/BeforeInstallPromptEvent.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/ScriptPromise.h" | |
| 8 #include "core/dom/DOMException.h" | 7 #include "core/dom/DOMException.h" |
| 8 #include "core/dom/Document.h" |
| 9 #include "core/dom/ExceptionCode.h" | 9 #include "core/dom/ExceptionCode.h" |
| 10 #include "core/frame/UseCounter.h" | 10 #include "core/frame/UseCounter.h" |
| 11 #include "modules/app_banner/AppBannerCallbacks.h" | |
| 12 #include "modules/app_banner/BeforeInstallPromptEventInit.h" | 11 #include "modules/app_banner/BeforeInstallPromptEventInit.h" |
| 13 #include "public/platform/modules/app_banner/WebAppBannerClient.h" | 12 #include "public/platform/InterfaceProvider.h" |
| 13 #include "public/platform/WebString.h" |
| 14 #include "public/platform/modules/app_banner/WebAppBannerPromptResult.h" |
| 14 | 15 |
| 15 namespace blink { | 16 namespace blink { |
| 16 | 17 |
| 17 BeforeInstallPromptEvent::BeforeInstallPromptEvent( | 18 BeforeInstallPromptEvent::BeforeInstallPromptEvent( |
| 18 const AtomicString& name, | 19 const AtomicString& name, |
| 19 ExecutionContext* executionContext, | 20 LocalFrame* frame, |
| 20 const Vector<String>& platforms, | |
| 21 int requestId, | 21 int requestId, |
| 22 WebAppBannerClient* client) | 22 const Vector<String>& platforms) |
| 23 : Event(name, false, true), | 23 : Event(name, false, true), |
| 24 m_binding(this), |
| 25 m_requestId(requestId), |
| 24 m_platforms(platforms), | 26 m_platforms(platforms), |
| 25 m_requestId(requestId), | 27 m_userChoice(new UserChoiceProperty(frame->document(), |
| 26 m_client(client), | |
| 27 m_userChoice(new UserChoiceProperty(executionContext, | |
| 28 this, | 28 this, |
| 29 UserChoiceProperty::UserChoice)), | 29 UserChoiceProperty::UserChoice)), |
| 30 m_registered(false) { | 30 m_promptCalled(false) { |
| 31 UseCounter::count(executionContext, UseCounter::BeforeInstallPromptEvent); | 31 UseCounter::count(frame, UseCounter::BeforeInstallPromptEvent); |
| 32 frame->interfaceProvider()->getInterface(mojo::GetProxy(&m_bannerService)); |
| 33 m_bannerService->SetEvent(m_binding.CreateInterfacePtrAndBind()); |
| 32 } | 34 } |
| 33 | 35 |
| 34 BeforeInstallPromptEvent::BeforeInstallPromptEvent( | 36 BeforeInstallPromptEvent::BeforeInstallPromptEvent( |
| 35 const AtomicString& name, | 37 const AtomicString& name, |
| 36 const BeforeInstallPromptEventInit& init) | 38 const BeforeInstallPromptEventInit& init) |
| 37 : Event(name, false, true), m_requestId(-1), m_client(nullptr) { | 39 : Event(name, false, true), |
| 40 m_binding(this), |
| 41 m_requestId(-1), |
| 42 m_promptCalled(false) { |
| 38 if (init.hasPlatforms()) | 43 if (init.hasPlatforms()) |
| 39 m_platforms = init.platforms(); | 44 m_platforms = init.platforms(); |
| 40 } | 45 } |
| 41 | 46 |
| 42 BeforeInstallPromptEvent::~BeforeInstallPromptEvent() {} | 47 BeforeInstallPromptEvent::~BeforeInstallPromptEvent() {} |
| 43 | 48 |
| 44 Vector<String> BeforeInstallPromptEvent::platforms() const { | 49 Vector<String> BeforeInstallPromptEvent::platforms() const { |
| 45 return m_platforms; | 50 return m_platforms; |
| 46 } | 51 } |
| 47 | 52 |
| 48 ScriptPromise BeforeInstallPromptEvent::userChoice(ScriptState* scriptState) { | 53 ScriptPromise BeforeInstallPromptEvent::userChoice(ScriptState* scriptState) { |
| 49 UseCounter::count(scriptState->getExecutionContext(), | 54 UseCounter::count(scriptState->getExecutionContext(), |
| 50 UseCounter::BeforeInstallPromptEventUserChoice); | 55 UseCounter::BeforeInstallPromptEventUserChoice); |
| 51 if (m_userChoice && m_client && m_requestId != -1) { | 56 if (m_userChoice && m_requestId != -1) |
| 52 if (!m_registered) { | |
| 53 m_registered = true; | |
| 54 m_client->registerBannerCallbacks( | |
| 55 m_requestId, new AppBannerCallbacks(m_userChoice.get())); | |
| 56 } | |
| 57 return m_userChoice->promise(scriptState->world()); | 57 return m_userChoice->promise(scriptState->world()); |
| 58 } | |
| 59 return ScriptPromise::rejectWithDOMException( | 58 return ScriptPromise::rejectWithDOMException( |
| 60 scriptState, | 59 scriptState, |
| 61 DOMException::create(InvalidStateError, | 60 DOMException::create(InvalidStateError, |
| 62 "userChoice cannot be accessed on this event.")); | 61 "userChoice cannot be accessed on this event.")); |
| 63 } | 62 } |
| 64 | 63 |
| 65 ScriptPromise BeforeInstallPromptEvent::prompt(ScriptState* scriptState) { | 64 ScriptPromise BeforeInstallPromptEvent::prompt(ScriptState* scriptState) { |
| 66 UseCounter::count(scriptState->getExecutionContext(), | 65 UseCounter::count(scriptState->getExecutionContext(), |
| 67 UseCounter::BeforeInstallPromptEventPrompt); | 66 UseCounter::BeforeInstallPromptEventPrompt); |
| 68 | 67 |
| 69 // |m_registered| will be true if userChoice has already been accessed | 68 if (!defaultPrevented() || m_promptCalled || m_requestId == -1) |
| 70 // or prompt() has already been called. Return a rejected promise in both | |
| 71 // these cases, as well as if we have a null client or invalid requestId. | |
| 72 if (m_registered || !defaultPrevented() || !m_client || m_requestId == -1) | |
| 73 return ScriptPromise::rejectWithDOMException( | 69 return ScriptPromise::rejectWithDOMException( |
| 74 scriptState, | 70 scriptState, |
| 75 DOMException::create(InvalidStateError, | 71 DOMException::create(InvalidStateError, |
| 76 "The prompt() method may only be called once, " | 72 "The prompt() method may only be called once, " |
| 77 "following preventDefault().")); | 73 "following preventDefault().")); |
| 78 | 74 |
| 79 m_registered = true; | 75 m_promptCalled = true; |
| 80 m_client->registerBannerCallbacks(m_requestId, | 76 m_bannerService->DisplayAppBanner(m_requestId); |
| 81 new AppBannerCallbacks(m_userChoice.get())); | |
| 82 m_client->showAppBanner(m_requestId); | |
| 83 return ScriptPromise::castUndefined(scriptState); | 77 return ScriptPromise::castUndefined(scriptState); |
| 84 } | 78 } |
| 85 | 79 |
| 86 const AtomicString& BeforeInstallPromptEvent::interfaceName() const { | 80 const AtomicString& BeforeInstallPromptEvent::interfaceName() const { |
| 87 return EventNames::BeforeInstallPromptEvent; | 81 return EventNames::BeforeInstallPromptEvent; |
| 88 } | 82 } |
| 89 | 83 |
| 90 void BeforeInstallPromptEvent::preventDefault() { | 84 void BeforeInstallPromptEvent::preventDefault() { |
| 91 Event::preventDefault(); | 85 Event::preventDefault(); |
| 92 UseCounter::count(target()->getExecutionContext(), | 86 UseCounter::count(target()->getExecutionContext(), |
| 93 UseCounter::BeforeInstallPromptEventPreventDefault); | 87 UseCounter::BeforeInstallPromptEventPreventDefault); |
| 94 } | 88 } |
| 95 | 89 |
| 90 void BeforeInstallPromptEvent::BannerAccepted(int requestId, |
| 91 const WTF::String& platform) { |
| 92 if (requestId != m_requestId) |
| 93 return; |
| 94 m_userChoice->resolve(AppBannerPromptResult::create( |
| 95 platform, WebAppBannerPromptResult::Outcome::Accepted)); |
| 96 } |
| 97 |
| 98 void BeforeInstallPromptEvent::BannerDismissed(int requestId) { |
| 99 if (requestId != m_requestId) |
| 100 return; |
| 101 m_userChoice->resolve(AppBannerPromptResult::create( |
| 102 "", WebAppBannerPromptResult::Outcome::Dismissed)); |
| 103 } |
| 104 |
| 96 DEFINE_TRACE(BeforeInstallPromptEvent) { | 105 DEFINE_TRACE(BeforeInstallPromptEvent) { |
| 97 visitor->trace(m_userChoice); | 106 visitor->trace(m_userChoice); |
| 98 Event::trace(visitor); | 107 Event::trace(visitor); |
| 99 } | 108 } |
| 100 | 109 |
| 101 } // namespace blink | 110 } // namespace blink |
| OLD | NEW |