| 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" | 7 #include "bindings/core/v8/ScriptPromise.h" |
| 8 #include "core/dom/DOMException.h" | 8 #include "core/dom/DOMException.h" |
| 9 #include "core/dom/ExceptionCode.h" | 9 #include "core/dom/ExceptionCode.h" |
| 10 #include "core/frame/UseCounter.h" |
| 10 #include "modules/app_banner/AppBannerCallbacks.h" | 11 #include "modules/app_banner/AppBannerCallbacks.h" |
| 11 #include "modules/app_banner/BeforeInstallPromptEventInit.h" | 12 #include "modules/app_banner/BeforeInstallPromptEventInit.h" |
| 12 #include "public/platform/modules/app_banner/WebAppBannerClient.h" | 13 #include "public/platform/modules/app_banner/WebAppBannerClient.h" |
| 13 | 14 |
| 14 namespace blink { | 15 namespace blink { |
| 15 | 16 |
| 16 BeforeInstallPromptEvent::BeforeInstallPromptEvent() | 17 BeforeInstallPromptEvent::BeforeInstallPromptEvent() |
| 17 { | 18 { |
| 18 } | 19 } |
| 19 | 20 |
| 20 BeforeInstallPromptEvent::BeforeInstallPromptEvent(const AtomicString& name, Exe
cutionContext* executionContext, const Vector<String>& platforms, int requestId,
WebAppBannerClient* client) | 21 BeforeInstallPromptEvent::BeforeInstallPromptEvent(const AtomicString& name, Exe
cutionContext* executionContext, const Vector<String>& platforms, int requestId,
WebAppBannerClient* client) |
| 21 : Event(name, false, true) | 22 : Event(name, false, true) |
| 22 , m_platforms(platforms) | 23 , m_platforms(platforms) |
| 23 , m_requestId(requestId) | 24 , m_requestId(requestId) |
| 24 , m_client(client) | 25 , m_client(client) |
| 25 , m_userChoice(new UserChoiceProperty(executionContext, this, UserChoiceProp
erty::UserChoice)) | 26 , m_userChoice(new UserChoiceProperty(executionContext, this, UserChoiceProp
erty::UserChoice)) |
| 26 , m_registered(false) | 27 , m_registered(false) |
| 27 { | 28 { |
| 29 UseCounter::count(executionContext, UseCounter::BeforeInstallPromptEvent); |
| 28 } | 30 } |
| 29 | 31 |
| 30 BeforeInstallPromptEvent::BeforeInstallPromptEvent(const AtomicString& name, con
st BeforeInstallPromptEventInit& init) | 32 BeforeInstallPromptEvent::BeforeInstallPromptEvent(const AtomicString& name, con
st BeforeInstallPromptEventInit& init) |
| 31 : Event(name, false, true) | 33 : Event(name, false, true) |
| 32 , m_requestId(-1) | 34 , m_requestId(-1) |
| 33 , m_client(nullptr) | 35 , m_client(nullptr) |
| 34 { | 36 { |
| 35 if (init.hasPlatforms()) | 37 if (init.hasPlatforms()) |
| 36 m_platforms = init.platforms(); | 38 m_platforms = init.platforms(); |
| 37 } | 39 } |
| 38 | 40 |
| 39 BeforeInstallPromptEvent::~BeforeInstallPromptEvent() | 41 BeforeInstallPromptEvent::~BeforeInstallPromptEvent() |
| 40 { | 42 { |
| 41 } | 43 } |
| 42 | 44 |
| 43 Vector<String> BeforeInstallPromptEvent::platforms() const | 45 Vector<String> BeforeInstallPromptEvent::platforms() const |
| 44 { | 46 { |
| 45 return m_platforms; | 47 return m_platforms; |
| 46 } | 48 } |
| 47 | 49 |
| 48 ScriptPromise BeforeInstallPromptEvent::userChoice(ScriptState* scriptState) | 50 ScriptPromise BeforeInstallPromptEvent::userChoice(ScriptState* scriptState) |
| 49 { | 51 { |
| 52 UseCounter::count(scriptState->getExecutionContext(), UseCounter::BeforeInst
allPromptEventUserChoice); |
| 50 if (m_userChoice && m_client && m_requestId != -1) { | 53 if (m_userChoice && m_client && m_requestId != -1) { |
| 51 if (!m_registered) { | 54 if (!m_registered) { |
| 52 m_registered = true; | 55 m_registered = true; |
| 53 m_client->registerBannerCallbacks(m_requestId, new AppBannerCallback
s(m_userChoice.get())); | 56 m_client->registerBannerCallbacks(m_requestId, new AppBannerCallback
s(m_userChoice.get())); |
| 54 } | 57 } |
| 55 return m_userChoice->promise(scriptState->world()); | 58 return m_userChoice->promise(scriptState->world()); |
| 56 } | 59 } |
| 57 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::crea
te(InvalidStateError, "userChoice cannot be accessed on this event.")); | 60 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::crea
te(InvalidStateError, "userChoice cannot be accessed on this event.")); |
| 58 } | 61 } |
| 59 | 62 |
| 60 const AtomicString& BeforeInstallPromptEvent::interfaceName() const | |
| 61 { | |
| 62 return EventNames::BeforeInstallPromptEvent; | |
| 63 } | |
| 64 | |
| 65 ScriptPromise BeforeInstallPromptEvent::prompt(ScriptState* scriptState) | 63 ScriptPromise BeforeInstallPromptEvent::prompt(ScriptState* scriptState) |
| 66 { | 64 { |
| 65 UseCounter::count(scriptState->getExecutionContext(), UseCounter::BeforeInst
allPromptEventPrompt); |
| 66 |
| 67 // |m_registered| will be true if userChoice has already been accessed | 67 // |m_registered| will be true if userChoice has already been accessed |
| 68 // or prompt() has already been called. Return a rejected promise in both | 68 // or prompt() has already been called. Return a rejected promise in both |
| 69 // these cases, as well as if we have a null client or invalid requestId. | 69 // these cases, as well as if we have a null client or invalid requestId. |
| 70 if (m_registered || !defaultPrevented() || !m_client || m_requestId == -1) | 70 if (m_registered || !defaultPrevented() || !m_client || m_requestId == -1) |
| 71 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(InvalidStateError, "The prompt() method may only be called once, followin
g preventDefault().")); | 71 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(InvalidStateError, "The prompt() method may only be called once, followin
g preventDefault().")); |
| 72 | 72 |
| 73 m_registered = true; | 73 m_registered = true; |
| 74 m_client->registerBannerCallbacks(m_requestId, new AppBannerCallbacks(m_user
Choice.get())); | 74 m_client->registerBannerCallbacks(m_requestId, new AppBannerCallbacks(m_user
Choice.get())); |
| 75 m_client->showAppBanner(m_requestId); | 75 m_client->showAppBanner(m_requestId); |
| 76 return ScriptPromise::castUndefined(scriptState); | 76 return ScriptPromise::castUndefined(scriptState); |
| 77 } | 77 } |
| 78 | 78 |
| 79 const AtomicString& BeforeInstallPromptEvent::interfaceName() const |
| 80 { |
| 81 return EventNames::BeforeInstallPromptEvent; |
| 82 } |
| 83 |
| 84 void BeforeInstallPromptEvent::preventDefault() |
| 85 { |
| 86 Event::preventDefault(); |
| 87 UseCounter::count(target()->getExecutionContext(), UseCounter::BeforeInstall
PromptEventPreventDefault); |
| 88 } |
| 89 |
| 79 DEFINE_TRACE(BeforeInstallPromptEvent) | 90 DEFINE_TRACE(BeforeInstallPromptEvent) |
| 80 { | 91 { |
| 81 visitor->trace(m_userChoice); | 92 visitor->trace(m_userChoice); |
| 82 Event::trace(visitor); | 93 Event::trace(visitor); |
| 83 } | 94 } |
| 84 | 95 |
| 85 } // namespace blink | 96 } // namespace blink |
| OLD | NEW |