Chromium Code Reviews| 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 | 14 |
| 15 namespace blink { | 15 namespace blink { |
| 16 | 16 |
| 17 BeforeInstallPromptEvent::BeforeInstallPromptEvent( | 17 BeforeInstallPromptEvent::BeforeInstallPromptEvent( |
| 18 const AtomicString& name, | 18 const AtomicString& name, |
| 19 ExecutionContext* executionContext, | 19 LocalFrame* frame, |
| 20 const Vector<String>& platforms, | 20 mojo::ScopedMessagePipeHandle serviceHandle, |
| 21 int requestId, | 21 mojo::ScopedMessagePipeHandle eventHandle, |
| 22 WebAppBannerClient* client) | 22 const Vector<String>& platforms) |
| 23 : Event(name, false, true), | 23 : Event(name, false, true), |
| 24 m_bannerService(mojo::MakeProxy(mojom::blink::AppBannerServicePtrInfo( | |
| 25 std::move(serviceHandle), | |
| 26 mojom::blink::AppBannerService::Version_))), | |
| 27 m_binding(this, std::move(eventHandle)), | |
| 24 m_platforms(platforms), | 28 m_platforms(platforms), |
| 25 m_requestId(requestId), | 29 m_userChoice(new UserChoiceProperty(frame->document(), |
| 26 m_client(client), | |
| 27 m_userChoice(new UserChoiceProperty(executionContext, | |
| 28 this, | 30 this, |
| 29 UserChoiceProperty::UserChoice)), | 31 UserChoiceProperty::UserChoice)), |
| 30 m_registered(false) { | 32 m_promptCalled(false) { |
| 31 UseCounter::count(executionContext, UseCounter::BeforeInstallPromptEvent); | 33 DCHECK(m_bannerService); |
| 34 DCHECK(m_binding.is_bound()); | |
| 35 UseCounter::count(frame, UseCounter::BeforeInstallPromptEvent); | |
| 32 } | 36 } |
| 33 | 37 |
| 34 BeforeInstallPromptEvent::BeforeInstallPromptEvent( | 38 BeforeInstallPromptEvent::BeforeInstallPromptEvent( |
| 35 const AtomicString& name, | 39 const AtomicString& name, |
| 36 const BeforeInstallPromptEventInit& init) | 40 const BeforeInstallPromptEventInit& init) |
| 37 : Event(name, false, true), m_requestId(-1), m_client(nullptr) { | 41 : Event(name, false, true), m_binding(this), m_promptCalled(false) { |
| 38 if (init.hasPlatforms()) | 42 if (init.hasPlatforms()) |
| 39 m_platforms = init.platforms(); | 43 m_platforms = init.platforms(); |
| 40 } | 44 } |
| 41 | 45 |
| 42 BeforeInstallPromptEvent::~BeforeInstallPromptEvent() {} | 46 BeforeInstallPromptEvent::~BeforeInstallPromptEvent() {} |
| 43 | 47 |
| 48 void BeforeInstallPromptEvent::dispose() { | |
| 49 m_binding.Close(); | |
|
Sam McNally
2016/10/06 22:48:48
Reset m_bannerService too.
dominickn
2016/10/06 23:38:10
Done.
| |
| 50 } | |
| 51 | |
| 44 Vector<String> BeforeInstallPromptEvent::platforms() const { | 52 Vector<String> BeforeInstallPromptEvent::platforms() const { |
| 45 return m_platforms; | 53 return m_platforms; |
| 46 } | 54 } |
| 47 | 55 |
| 48 ScriptPromise BeforeInstallPromptEvent::userChoice(ScriptState* scriptState) { | 56 ScriptPromise BeforeInstallPromptEvent::userChoice(ScriptState* scriptState) { |
| 49 UseCounter::count(scriptState->getExecutionContext(), | 57 UseCounter::count(scriptState->getExecutionContext(), |
| 50 UseCounter::BeforeInstallPromptEventUserChoice); | 58 UseCounter::BeforeInstallPromptEventUserChoice); |
| 51 if (m_userChoice && m_client && m_requestId != -1) { | 59 if (m_userChoice && m_binding.is_bound()) |
| 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()); | 60 return m_userChoice->promise(scriptState->world()); |
| 58 } | |
| 59 return ScriptPromise::rejectWithDOMException( | 61 return ScriptPromise::rejectWithDOMException( |
| 60 scriptState, | 62 scriptState, |
| 61 DOMException::create(InvalidStateError, | 63 DOMException::create(InvalidStateError, |
| 62 "userChoice cannot be accessed on this event.")); | 64 "userChoice cannot be accessed on this event.")); |
| 63 } | 65 } |
| 64 | 66 |
| 65 ScriptPromise BeforeInstallPromptEvent::prompt(ScriptState* scriptState) { | 67 ScriptPromise BeforeInstallPromptEvent::prompt(ScriptState* scriptState) { |
| 66 UseCounter::count(scriptState->getExecutionContext(), | 68 UseCounter::count(scriptState->getExecutionContext(), |
| 67 UseCounter::BeforeInstallPromptEventPrompt); | 69 UseCounter::BeforeInstallPromptEventPrompt); |
| 68 | 70 |
| 69 // |m_registered| will be true if userChoice has already been accessed | 71 if (!defaultPrevented() || m_promptCalled || !m_bannerService.is_bound()) |
| 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( | 72 return ScriptPromise::rejectWithDOMException( |
| 74 scriptState, | 73 scriptState, |
| 75 DOMException::create(InvalidStateError, | 74 DOMException::create(InvalidStateError, |
| 76 "The prompt() method may only be called once, " | 75 "The prompt() method may only be called once, " |
| 77 "following preventDefault().")); | 76 "following preventDefault().")); |
| 78 | 77 |
| 79 m_registered = true; | 78 m_promptCalled = true; |
| 80 m_client->registerBannerCallbacks(m_requestId, | 79 m_bannerService->DisplayAppBanner(); |
| 81 new AppBannerCallbacks(m_userChoice.get())); | |
| 82 m_client->showAppBanner(m_requestId); | |
| 83 return ScriptPromise::castUndefined(scriptState); | 80 return ScriptPromise::castUndefined(scriptState); |
| 84 } | 81 } |
| 85 | 82 |
| 86 const AtomicString& BeforeInstallPromptEvent::interfaceName() const { | 83 const AtomicString& BeforeInstallPromptEvent::interfaceName() const { |
| 87 return EventNames::BeforeInstallPromptEvent; | 84 return EventNames::BeforeInstallPromptEvent; |
| 88 } | 85 } |
| 89 | 86 |
| 90 void BeforeInstallPromptEvent::preventDefault() { | 87 void BeforeInstallPromptEvent::preventDefault() { |
| 91 Event::preventDefault(); | 88 Event::preventDefault(); |
| 92 UseCounter::count(target()->getExecutionContext(), | 89 UseCounter::count(target()->getExecutionContext(), |
| 93 UseCounter::BeforeInstallPromptEventPreventDefault); | 90 UseCounter::BeforeInstallPromptEventPreventDefault); |
| 94 } | 91 } |
| 95 | 92 |
| 93 void BeforeInstallPromptEvent::BannerAccepted(const WTF::String& platform) { | |
| 94 m_userChoice->resolve(AppBannerPromptResult::create( | |
| 95 platform, AppBannerPromptResult::Outcome::Accepted)); | |
| 96 } | |
| 97 | |
| 98 void BeforeInstallPromptEvent::BannerDismissed() { | |
| 99 m_userChoice->resolve(AppBannerPromptResult::create( | |
| 100 "", AppBannerPromptResult::Outcome::Dismissed)); | |
| 101 } | |
| 102 | |
| 96 DEFINE_TRACE(BeforeInstallPromptEvent) { | 103 DEFINE_TRACE(BeforeInstallPromptEvent) { |
| 97 visitor->trace(m_userChoice); | 104 visitor->trace(m_userChoice); |
| 98 Event::trace(visitor); | 105 Event::trace(visitor); |
| 99 } | 106 } |
| 100 | 107 |
| 101 } // namespace blink | 108 } // namespace blink |
| OLD | NEW |