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