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_redispatched(false) |
| 26 , m_userChoice(new UserChoiceProperty(executionContext, this, UserChoiceProp erty::UserChoice)) | |
| 27 , m_prompt(new PromptProperty(executionContext, this, PromptProperty::Prompt )) | |
| 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) | 35 , m_redispatched(false) |
| 37 { | 36 { |
| 38 if (init.hasPlatforms()) | 37 if (init.hasPlatforms()) |
| 39 m_platforms = init.platforms(); | 38 m_platforms = init.platforms(); |
| 40 } | 39 } |
| 41 | 40 |
| 42 BeforeInstallPromptEvent::~BeforeInstallPromptEvent() | 41 BeforeInstallPromptEvent::~BeforeInstallPromptEvent() |
| 43 { | 42 { |
| 44 } | 43 } |
| 45 | 44 |
| 46 Vector<String> BeforeInstallPromptEvent::platforms() const | 45 Vector<String> BeforeInstallPromptEvent::platforms() const |
| 47 { | 46 { |
| 48 return m_platforms; | 47 return m_platforms; |
| 49 } | 48 } |
| 50 | 49 |
| 51 ScriptPromise BeforeInstallPromptEvent::userChoice(ScriptState* scriptState) | 50 ScriptPromise BeforeInstallPromptEvent::userChoice(ScriptState* scriptState) |
| 52 { | 51 { |
| 53 if (m_userChoice.isEmpty() && m_client) { | 52 if (m_userChoice) |
| 54 ASSERT(m_requestId != -1); | 53 return m_userChoice->promise(scriptState->world()); |
| 55 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolv er::create(scriptState); | 54 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 } | 55 } |
| 62 | 56 |
| 63 const AtomicString& BeforeInstallPromptEvent::interfaceName() const | 57 const AtomicString& BeforeInstallPromptEvent::interfaceName() const |
| 64 { | 58 { |
| 65 return EventNames::BeforeInstallPromptEvent; | 59 return EventNames::BeforeInstallPromptEvent; |
| 66 } | 60 } |
| 67 | 61 |
| 68 ScriptPromise BeforeInstallPromptEvent::prompt(ScriptState* scriptState) | 62 ScriptPromise BeforeInstallPromptEvent::prompt(ScriptState* scriptState) |
| 69 { | 63 { |
| 70 if (m_client && defaultPrevented() && !m_redispatched) { | 64 if (m_prompt) { |
| 71 ASSERT(m_requestId != -1); | 65 if (m_prompt->state() == PromptProperty::Pending) { |
| 72 m_redispatched = true; | 66 if (m_client && defaultPrevented() && !m_redispatched) { |
| 73 m_client->showAppBanner(m_requestId); | 67 ASSERT(m_requestId != -1); |
| 74 return ScriptPromise::cast(scriptState, v8::Undefined(scriptState->isola te())); | 68 m_redispatched = true; |
| 69 m_prompt->resolve(ToV8UndefinedGenerator()); | |
| 70 m_client->showAppBanner(m_requestId); | |
| 71 } else { | |
| 72 m_prompt->reject(DOMException::create(InvalidStateError, "The pr ompt() method may only be called once, following preventDefault().")); | |
| 73 } | |
| 74 } | |
| 75 return m_prompt->promise(scriptState->world()); | |
| 75 } | 76 } |
| 77 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::crea te(InvalidStateError, "The prompt() method cannot be called on this event.")); | |
|
mlamouri (slow - plz ping)
2015/07/24 15:08:25
Linus Torvalds once said "If you need more than 3
dominickn
2015/07/24 23:53:36
Done, and I even removed m_redispatched because th
| |
| 78 } | |
| 76 | 79 |
| 77 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::crea te(InvalidStateError, "The prompt() method may only be called once, following pr eventDefault().")); | 80 DEFINE_TRACE(BeforeInstallPromptEvent) |
| 81 { | |
| 82 visitor->trace(m_userChoice); | |
| 83 visitor->trace(m_prompt); | |
| 84 Event::trace(visitor); | |
| 78 } | 85 } |
| 79 | 86 |
| 80 } // namespace blink | 87 } // namespace blink |
| OLD | NEW |