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

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 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" 12 #include "public/platform/WebString.h"
esprehn 2016/10/07 00:39:51 it's very strange to use WebString inside modules
dominickn 2016/10/13 00:18:15 Done.
14 13
15 namespace blink { 14 namespace blink {
16 15
17 BeforeInstallPromptEvent::BeforeInstallPromptEvent( 16 BeforeInstallPromptEvent::BeforeInstallPromptEvent(
18 const AtomicString& name, 17 const AtomicString& name,
19 ExecutionContext* executionContext, 18 LocalFrame* frame,
20 const Vector<String>& platforms, 19 mojo::ScopedMessagePipeHandle serviceHandle,
21 int requestId, 20 mojo::ScopedMessagePipeHandle eventHandle,
22 WebAppBannerClient* client) 21 const Vector<String>& platforms)
23 : Event(name, false, true), 22 : Event(name, false, true),
23 m_bannerService(mojo::MakeProxy(mojom::blink::AppBannerServicePtrInfo(
esprehn 2016/10/07 00:39:51 Nothing uses mojo::MakeProxy inside blink today, w
24 std::move(serviceHandle),
25 mojom::blink::AppBannerService::Version_))),
esprehn 2016/10/07 00:39:51 We never pass a Version_ anywhere else in blink, w
dcheng 2016/10/07 07:03:16 Before we get here, is it possible to turn it back
26 m_binding(this, std::move(eventHandle)),
24 m_platforms(platforms), 27 m_platforms(platforms),
25 m_requestId(requestId), 28 m_userChoice(new UserChoiceProperty(frame->document(),
26 m_client(client),
27 m_userChoice(new UserChoiceProperty(executionContext,
28 this, 29 this,
29 UserChoiceProperty::UserChoice)), 30 UserChoiceProperty::UserChoice)),
30 m_registered(false) { 31 m_promptCalled(false) {
31 UseCounter::count(executionContext, UseCounter::BeforeInstallPromptEvent); 32 DCHECK(m_bannerService);
33 DCHECK(m_binding.is_bound());
34 UseCounter::count(frame, UseCounter::BeforeInstallPromptEvent);
32 } 35 }
33 36
34 BeforeInstallPromptEvent::BeforeInstallPromptEvent( 37 BeforeInstallPromptEvent::BeforeInstallPromptEvent(
35 const AtomicString& name, 38 const AtomicString& name,
36 const BeforeInstallPromptEventInit& init) 39 const BeforeInstallPromptEventInit& init)
37 : Event(name, false, true), m_requestId(-1), m_client(nullptr) { 40 : Event(name, false, true), m_binding(this), m_promptCalled(false) {
38 if (init.hasPlatforms()) 41 if (init.hasPlatforms())
39 m_platforms = init.platforms(); 42 m_platforms = init.platforms();
40 } 43 }
41 44
42 BeforeInstallPromptEvent::~BeforeInstallPromptEvent() {} 45 BeforeInstallPromptEvent::~BeforeInstallPromptEvent() {}
43 46
47 void BeforeInstallPromptEvent::dispose() {
48 m_bannerService.reset();
49 m_binding.Close();
50 }
51
44 Vector<String> BeforeInstallPromptEvent::platforms() const { 52 Vector<String> BeforeInstallPromptEvent::platforms() const {
45 return m_platforms; 53 return m_platforms;
46 } 54 }
47 55
48 ScriptPromise BeforeInstallPromptEvent::userChoice(ScriptState* scriptState) { 56 ScriptPromise BeforeInstallPromptEvent::userChoice(ScriptState* scriptState) {
49 UseCounter::count(scriptState->getExecutionContext(), 57 UseCounter::count(scriptState->getExecutionContext(),
50 UseCounter::BeforeInstallPromptEventUserChoice); 58 UseCounter::BeforeInstallPromptEventUserChoice);
51 if (m_userChoice && m_client && m_requestId != -1) { 59 // |m_binding| must be bound to allow the AppBannerService to resolve the
52 if (!m_registered) { 60 // userChoice promise.
53 m_registered = true; 61 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()); 62 return m_userChoice->promise(scriptState->world());
58 }
59 return ScriptPromise::rejectWithDOMException( 63 return ScriptPromise::rejectWithDOMException(
60 scriptState, 64 scriptState,
61 DOMException::create(InvalidStateError, 65 DOMException::create(InvalidStateError,
62 "userChoice cannot be accessed on this event.")); 66 "userChoice cannot be accessed on this event."));
63 } 67 }
64 68
65 ScriptPromise BeforeInstallPromptEvent::prompt(ScriptState* scriptState) { 69 ScriptPromise BeforeInstallPromptEvent::prompt(ScriptState* scriptState) {
66 UseCounter::count(scriptState->getExecutionContext(), 70 UseCounter::count(scriptState->getExecutionContext(),
67 UseCounter::BeforeInstallPromptEventPrompt); 71 UseCounter::BeforeInstallPromptEventPrompt);
68 72
69 // |m_registered| will be true if userChoice has already been accessed 73 // |m_bannerService| must be bound to allow us to inform the AppBannerService
70 // or prompt() has already been called. Return a rejected promise in both 74 // to display the banner now.
71 // these cases, as well as if we have a null client or invalid requestId. 75 if (!defaultPrevented() || m_promptCalled || !m_bannerService.is_bound())
72 if (m_registered || !defaultPrevented() || !m_client || m_requestId == -1)
73 return ScriptPromise::rejectWithDOMException( 76 return ScriptPromise::rejectWithDOMException(
74 scriptState, 77 scriptState,
75 DOMException::create(InvalidStateError, 78 DOMException::create(InvalidStateError,
76 "The prompt() method may only be called once, " 79 "The prompt() method may only be called once, "
77 "following preventDefault().")); 80 "following preventDefault()."));
78 81
79 m_registered = true; 82 m_promptCalled = true;
80 m_client->registerBannerCallbacks(m_requestId, 83 m_bannerService->DisplayAppBanner();
81 new AppBannerCallbacks(m_userChoice.get()));
82 m_client->showAppBanner(m_requestId);
83 return ScriptPromise::castUndefined(scriptState); 84 return ScriptPromise::castUndefined(scriptState);
84 } 85 }
85 86
86 const AtomicString& BeforeInstallPromptEvent::interfaceName() const { 87 const AtomicString& BeforeInstallPromptEvent::interfaceName() const {
87 return EventNames::BeforeInstallPromptEvent; 88 return EventNames::BeforeInstallPromptEvent;
88 } 89 }
89 90
90 void BeforeInstallPromptEvent::preventDefault() { 91 void BeforeInstallPromptEvent::preventDefault() {
91 Event::preventDefault(); 92 Event::preventDefault();
92 UseCounter::count(target()->getExecutionContext(), 93 UseCounter::count(target()->getExecutionContext(),
93 UseCounter::BeforeInstallPromptEventPreventDefault); 94 UseCounter::BeforeInstallPromptEventPreventDefault);
94 } 95 }
95 96
97 void BeforeInstallPromptEvent::BannerAccepted(const WTF::String& platform) {
esprehn 2016/10/07 00:39:51 Don't need the WTF:: on wtf things
dominickn 2016/10/13 00:18:15 Done.
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));
105 }
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