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