Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(23)

Side by Side Diff: third_party/WebKit/Source/modules/app_banner/BeforeInstallPromptEvent.cpp

Issue 2393513004: Convert app banners to use Mojo. (Closed)
Patch Set: Rebase. init() can cause frames to detach Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
8 #include "core/dom/DOMException.h" 7 #include "core/dom/DOMException.h"
8 #include "core/dom/Document.h"
9 #include "core/dom/ExceptionCode.h" 9 #include "core/dom/ExceptionCode.h"
10 #include "core/frame/UseCounter.h" 10 #include "core/frame/UseCounter.h"
11 #include "modules/app_banner/AppBannerCallbacks.h"
12 #include "modules/app_banner/BeforeInstallPromptEventInit.h" 11 #include "modules/app_banner/BeforeInstallPromptEventInit.h"
13 #include "public/platform/modules/app_banner/WebAppBannerClient.h"
14 12
15 namespace blink { 13 namespace blink {
16 14
17 BeforeInstallPromptEvent::BeforeInstallPromptEvent( 15 BeforeInstallPromptEvent::BeforeInstallPromptEvent(
18 const AtomicString& name, 16 const AtomicString& name,
19 ExecutionContext* executionContext, 17 LocalFrame* frame,
20 const Vector<String>& platforms, 18 mojom::blink::AppBannerServicePtr servicePtr,
21 int requestId, 19 mojom::blink::AppBannerEventRequest eventRequest,
22 WebAppBannerClient* client) 20 const Vector<String>& platforms)
23 : Event(name, false, true), 21 : Event(name, false, true),
22 m_bannerService(std::move(servicePtr)),
23 m_binding(this, std::move(eventRequest)),
24 m_platforms(platforms), 24 m_platforms(platforms),
25 m_requestId(requestId), 25 m_userChoice(new UserChoiceProperty(frame->document(),
26 m_client(client),
27 m_userChoice(new UserChoiceProperty(executionContext,
28 this, 26 this,
29 UserChoiceProperty::UserChoice)), 27 UserChoiceProperty::UserChoice)),
30 m_registered(false) { 28 m_promptCalled(false) {
31 UseCounter::count(executionContext, UseCounter::BeforeInstallPromptEvent); 29 DCHECK(m_bannerService);
30 DCHECK(m_binding.is_bound());
31 UseCounter::count(frame, UseCounter::BeforeInstallPromptEvent);
32 } 32 }
33 33
34 BeforeInstallPromptEvent::BeforeInstallPromptEvent( 34 BeforeInstallPromptEvent::BeforeInstallPromptEvent(
35 const AtomicString& name, 35 const AtomicString& name,
36 const BeforeInstallPromptEventInit& init) 36 const BeforeInstallPromptEventInit& init)
37 : Event(name, false, true), m_requestId(-1), m_client(nullptr) { 37 : Event(name, false, true), m_binding(this), m_promptCalled(false) {
38 if (init.hasPlatforms()) 38 if (init.hasPlatforms())
39 m_platforms = init.platforms(); 39 m_platforms = init.platforms();
40 } 40 }
41 41
42 BeforeInstallPromptEvent::~BeforeInstallPromptEvent() {} 42 BeforeInstallPromptEvent::~BeforeInstallPromptEvent() {}
43 43
44 void BeforeInstallPromptEvent::dispose() {
45 m_bannerService.reset();
46 m_binding.Close();
47 }
48
44 Vector<String> BeforeInstallPromptEvent::platforms() const { 49 Vector<String> BeforeInstallPromptEvent::platforms() const {
45 return m_platforms; 50 return m_platforms;
46 } 51 }
47 52
48 ScriptPromise BeforeInstallPromptEvent::userChoice(ScriptState* scriptState) { 53 ScriptPromise BeforeInstallPromptEvent::userChoice(ScriptState* scriptState) {
49 UseCounter::count(scriptState->getExecutionContext(), 54 UseCounter::count(scriptState->getExecutionContext(),
50 UseCounter::BeforeInstallPromptEventUserChoice); 55 UseCounter::BeforeInstallPromptEventUserChoice);
51 if (m_userChoice && m_client && m_requestId != -1) { 56 // |m_binding| must be bound to allow the AppBannerService to resolve the
52 if (!m_registered) { 57 // userChoice promise.
53 m_registered = true; 58 if (m_userChoice && m_binding.is_bound())
54 m_client->registerBannerCallbacks(
55 m_requestId, new AppBannerCallbacks(m_userChoice.get()));
56 }
57 return m_userChoice->promise(scriptState->world()); 59 return m_userChoice->promise(scriptState->world());
58 }
59 return ScriptPromise::rejectWithDOMException( 60 return ScriptPromise::rejectWithDOMException(
60 scriptState, 61 scriptState,
61 DOMException::create(InvalidStateError, 62 DOMException::create(InvalidStateError,
62 "userChoice cannot be accessed on this event.")); 63 "userChoice cannot be accessed on this event."));
63 } 64 }
64 65
65 ScriptPromise BeforeInstallPromptEvent::prompt(ScriptState* scriptState) { 66 ScriptPromise BeforeInstallPromptEvent::prompt(ScriptState* scriptState) {
66 UseCounter::count(scriptState->getExecutionContext(), 67 // |m_bannerService| must be bound to allow us to inform the AppBannerService
67 UseCounter::BeforeInstallPromptEventPrompt); 68 // to display the banner now.
68 69 if (!defaultPrevented() || m_promptCalled || !m_bannerService.is_bound()) {
69 // |m_registered| will be true if userChoice has already been accessed
70 // or prompt() has already been called. Return a rejected promise in both
71 // these cases, as well as if we have a null client or invalid requestId.
72 if (m_registered || !defaultPrevented() || !m_client || m_requestId == -1)
73 return ScriptPromise::rejectWithDOMException( 70 return ScriptPromise::rejectWithDOMException(
74 scriptState, 71 scriptState,
75 DOMException::create(InvalidStateError, 72 DOMException::create(InvalidStateError,
76 "The prompt() method may only be called once, " 73 "The prompt() method may only be called once, "
77 "following preventDefault().")); 74 "following preventDefault()."));
75 }
78 76
79 m_registered = true; 77 UseCounter::count(scriptState->getExecutionContext(),
80 m_client->registerBannerCallbacks(m_requestId, 78 UseCounter::BeforeInstallPromptEventPrompt);
81 new AppBannerCallbacks(m_userChoice.get())); 79
82 m_client->showAppBanner(m_requestId); 80 m_promptCalled = true;
81 m_bannerService->DisplayAppBanner();
83 return ScriptPromise::castUndefined(scriptState); 82 return ScriptPromise::castUndefined(scriptState);
84 } 83 }
85 84
86 const AtomicString& BeforeInstallPromptEvent::interfaceName() const { 85 const AtomicString& BeforeInstallPromptEvent::interfaceName() const {
87 return EventNames::BeforeInstallPromptEvent; 86 return EventNames::BeforeInstallPromptEvent;
88 } 87 }
89 88
90 void BeforeInstallPromptEvent::preventDefault() { 89 void BeforeInstallPromptEvent::preventDefault() {
91 Event::preventDefault(); 90 Event::preventDefault();
92 UseCounter::count(target()->getExecutionContext(), 91 if (target()) {
dcheng 2016/10/19 06:15:58 How come we didn't need this null check before?
dominickn 2016/10/19 08:17:00 We did need it before: crbug.com/655902 :) Callin
93 UseCounter::BeforeInstallPromptEventPreventDefault); 92 UseCounter::count(target()->getExecutionContext(),
93 UseCounter::BeforeInstallPromptEventPreventDefault);
94 }
95 }
96
97 void BeforeInstallPromptEvent::BannerAccepted(const String& platform) {
98 m_userChoice->resolve(AppBannerPromptResult::create(
99 platform, AppBannerPromptResult::Outcome::Accepted));
100 }
101
102 void BeforeInstallPromptEvent::BannerDismissed() {
103 m_userChoice->resolve(AppBannerPromptResult::create(
104 "", AppBannerPromptResult::Outcome::Dismissed));
dcheng 2016/10/19 06:15:58 Nit: emptyAtom instead of ""
dominickn 2016/10/19 08:17:00 Done.
94 } 105 }
95 106
96 DEFINE_TRACE(BeforeInstallPromptEvent) { 107 DEFINE_TRACE(BeforeInstallPromptEvent) {
97 visitor->trace(m_userChoice); 108 visitor->trace(m_userChoice);
98 Event::trace(visitor); 109 Event::trace(visitor);
99 } 110 }
100 111
101 } // namespace blink 112 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698