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

Side by Side Diff: third_party/WebKit/Source/modules/mediasession/MediaSession.cpp

Issue 2545893003: Remove RefPtr<ScriptState> from MediaSession (Closed)
Patch Set: temp Created 4 years 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/mediasession/MediaSession.h" 5 #include "modules/mediasession/MediaSession.h"
6 6
7 #include "bindings/core/v8/ScriptState.h"
8 #include "core/dom/Document.h" 7 #include "core/dom/Document.h"
9 #include "core/dom/DocumentUserGestureToken.h" 8 #include "core/dom/DocumentUserGestureToken.h"
10 #include "core/dom/ExecutionContext.h" 9 #include "core/dom/ExecutionContext.h"
11 #include "core/events/Event.h" 10 #include "core/events/Event.h"
12 #include "core/frame/LocalFrame.h" 11 #include "core/frame/LocalFrame.h"
13 #include "modules/EventTargetModules.h" 12 #include "modules/EventTargetModules.h"
14 #include "modules/mediasession/MediaMetadata.h" 13 #include "modules/mediasession/MediaMetadata.h"
15 #include "modules/mediasession/MediaMetadataSanitizer.h" 14 #include "modules/mediasession/MediaMetadataSanitizer.h"
16 #include "platform/UserGestureIndicator.h" 15 #include "platform/UserGestureIndicator.h"
17 #include "public/platform/InterfaceProvider.h" 16 #include "public/platform/InterfaceProvider.h"
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 return MediaSessionAction::SEEK_BACKWARD; 61 return MediaSessionAction::SEEK_BACKWARD;
63 if (EventTypeNames::seekforward == eventName) 62 if (EventTypeNames::seekforward == eventName)
64 return MediaSessionAction::SEEK_FORWARD; 63 return MediaSessionAction::SEEK_FORWARD;
65 64
66 NOTREACHED(); 65 NOTREACHED();
67 return WTF::nullopt; 66 return WTF::nullopt;
68 } 67 }
69 68
70 } // anonymous namespace 69 } // anonymous namespace
71 70
72 MediaSession::MediaSession(ScriptState* scriptState) 71 MediaSession::MediaSession(ExecutionContext* executionContext)
73 : m_scriptState(scriptState), m_clientBinding(this) {} 72 : ContextLifecycleObserver(executionContext), m_clientBinding(this) {}
74 73
75 MediaSession* MediaSession::create(ScriptState* scriptState) { 74 MediaSession* MediaSession::create(ExecutionContext* executionContext) {
76 return new MediaSession(scriptState); 75 return new MediaSession(executionContext);
77 } 76 }
78 77
79 void MediaSession::dispose() { 78 void MediaSession::dispose() {
80 m_clientBinding.Close(); 79 m_clientBinding.Close();
81 } 80 }
82 81
83 void MediaSession::setMetadata(MediaMetadata* metadata) { 82 void MediaSession::setMetadata(MediaMetadata* metadata) {
84 if (mojom::blink::MediaSessionService* service = 83 if (mojom::blink::MediaSessionService* service = getService()) {
85 getService(m_scriptState.get())) {
86 service->SetMetadata(MediaMetadataSanitizer::sanitizeAndConvertToMojo( 84 service->SetMetadata(MediaMetadataSanitizer::sanitizeAndConvertToMojo(
87 metadata, getExecutionContext())); 85 metadata, getExecutionContext()));
88 } 86 }
89 } 87 }
90 88
91 MediaMetadata* MediaSession::metadata() const { 89 MediaMetadata* MediaSession::metadata() const {
92 return m_metadata; 90 return m_metadata;
93 } 91 }
94 92
95 const WTF::AtomicString& MediaSession::interfaceName() const { 93 const WTF::AtomicString& MediaSession::interfaceName() const {
96 return EventTargetNames::MediaSession; 94 return EventTargetNames::MediaSession;
97 } 95 }
98 96
99 ExecutionContext* MediaSession::getExecutionContext() const { 97 ExecutionContext* MediaSession::getExecutionContext() const {
100 return m_scriptState->getExecutionContext(); 98 return ContextLifecycleObserver::getExecutionContext();
101 } 99 }
102 100
103 mojom::blink::MediaSessionService* MediaSession::getService( 101 mojom::blink::MediaSessionService* MediaSession::getService() {
104 ScriptState* scriptState) {
105 if (m_service) 102 if (m_service)
106 return m_service.get(); 103 return m_service.get();
104 if (!getExecutionContext())
105 return nullptr;
107 106
108 DCHECK(scriptState->getExecutionContext()->isDocument()) 107 DCHECK(getExecutionContext()->isDocument())
109 << "MediaSession::getService() is only available from a frame"; 108 << "MediaSession::getService() is only available from a frame";
110 Document* document = toDocument(scriptState->getExecutionContext()); 109 Document* document = toDocument(getExecutionContext());
111 if (!document->frame()) 110 if (!document->frame())
112 return nullptr; 111 return nullptr;
113 112
114 InterfaceProvider* interfaceProvider = document->frame()->interfaceProvider(); 113 InterfaceProvider* interfaceProvider = document->frame()->interfaceProvider();
115 if (!interfaceProvider) 114 if (!interfaceProvider)
116 return nullptr; 115 return nullptr;
117 116
118 interfaceProvider->getInterface(mojo::GetProxy(&m_service)); 117 interfaceProvider->getInterface(mojo::GetProxy(&m_service));
119 if (m_service.get()) 118 if (m_service.get())
120 m_service->SetClient(m_clientBinding.CreateInterfacePtrAndBind()); 119 m_service->SetClient(m_clientBinding.CreateInterfacePtrAndBind());
121 120
122 return m_service.get(); 121 return m_service.get();
123 } 122 }
124 123
125 bool MediaSession::addEventListenerInternal( 124 bool MediaSession::addEventListenerInternal(
126 const AtomicString& eventType, 125 const AtomicString& eventType,
127 EventListener* listener, 126 EventListener* listener,
128 const AddEventListenerOptionsResolved& options) { 127 const AddEventListenerOptionsResolved& options) {
129 if (mojom::blink::MediaSessionService* service = 128 if (mojom::blink::MediaSessionService* service = getService()) {
130 getService(m_scriptState.get())) {
131 auto mojomAction = eventNameToMojomAction(eventType); 129 auto mojomAction = eventNameToMojomAction(eventType);
132 DCHECK(mojomAction.has_value()); 130 DCHECK(mojomAction.has_value());
133 service->EnableAction(mojomAction.value()); 131 service->EnableAction(mojomAction.value());
134 } 132 }
135 return EventTarget::addEventListenerInternal(eventType, listener, options); 133 return EventTarget::addEventListenerInternal(eventType, listener, options);
136 } 134 }
137 135
138 bool MediaSession::removeEventListenerInternal( 136 bool MediaSession::removeEventListenerInternal(
139 const AtomicString& eventType, 137 const AtomicString& eventType,
140 const EventListener* listener, 138 const EventListener* listener,
141 const EventListenerOptions& options) { 139 const EventListenerOptions& options) {
142 if (mojom::blink::MediaSessionService* service = 140 if (mojom::blink::MediaSessionService* service = getService()) {
143 getService(m_scriptState.get())) {
144 auto mojomAction = eventNameToMojomAction(eventType); 141 auto mojomAction = eventNameToMojomAction(eventType);
145 DCHECK(mojomAction.has_value()); 142 DCHECK(mojomAction.has_value());
146 service->DisableAction(mojomAction.value()); 143 service->DisableAction(mojomAction.value());
147 } 144 }
148 return EventTarget::removeEventListenerInternal(eventType, listener, options); 145 return EventTarget::removeEventListenerInternal(eventType, listener, options);
149 } 146 }
150 147
151 void MediaSession::DidReceiveAction( 148 void MediaSession::DidReceiveAction(
152 blink::mojom::blink::MediaSessionAction action) { 149 blink::mojom::blink::MediaSessionAction action) {
153 DCHECK(getExecutionContext()->isDocument()); 150 DCHECK(getExecutionContext()->isDocument());
154 Document* document = toDocument(getExecutionContext()); 151 Document* document = toDocument(getExecutionContext());
155 UserGestureIndicator gestureIndicator( 152 UserGestureIndicator gestureIndicator(
156 DocumentUserGestureToken::create(document)); 153 DocumentUserGestureToken::create(document));
157 dispatchEvent(Event::create(mojomActionToEventName(action))); 154 dispatchEvent(Event::create(mojomActionToEventName(action)));
158 } 155 }
159 156
160 DEFINE_TRACE(MediaSession) { 157 DEFINE_TRACE(MediaSession) {
161 visitor->trace(m_metadata); 158 visitor->trace(m_metadata);
162 EventTargetWithInlineData::trace(visitor); 159 EventTargetWithInlineData::trace(visitor);
160 ContextLifecycleObserver::trace(visitor);
163 } 161 }
164 162
165 } // namespace blink 163 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698