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