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/audio_output_devices/HTMLMediaElementAudioOutputDevice.h" | 6 #include "modules/audio_output_devices/HTMLMediaElementAudioOutputDevice.h" |
7 | 7 |
8 #include "bindings/core/v8/ScriptPromiseResolver.h" | 8 #include "bindings/core/v8/ScriptPromiseResolver.h" |
9 #include "bindings/core/v8/ScriptState.h" | 9 #include "bindings/core/v8/ScriptState.h" |
10 #include "core/dom/ExecutionContext.h" | 10 #include "core/dom/ExecutionContext.h" |
11 #include "modules/audio_output_devices/AudioOutputDeviceClient.h" | |
11 #include "modules/audio_output_devices/SetSinkIdCallbacks.h" | 12 #include "modules/audio_output_devices/SetSinkIdCallbacks.h" |
12 #include "public/platform/WebSecurityOrigin.h" | 13 #include "public/platform/WebSecurityOrigin.h" |
13 | 14 |
14 namespace blink { | 15 namespace blink { |
15 | 16 |
17 class SetSinkIdResolver : public ScriptPromiseResolver { | |
Peter Beverloo
2015/11/12 16:45:42
note: you've split out the smaller SetSinkIdCallba
Guido Urdaneta
2015/11/12 18:45:44
Done.
| |
18 WTF_MAKE_NONCOPYABLE(SetSinkIdResolver); | |
19 public: | |
20 static SetSinkIdResolver* create(ScriptState*, HTMLMediaElement&, const Stri ng& sinkId); | |
21 ~SetSinkIdResolver() = default; | |
Peter Beverloo
2015/11/12 16:45:42
+override
Guido Urdaneta
2015/11/12 18:45:44
Done.
| |
22 | |
23 DECLARE_VIRTUAL_TRACE(); | |
24 | |
25 private: | |
26 SetSinkIdResolver(ScriptState*, HTMLMediaElement&, const String& sinkId); | |
27 void timerFired(Timer<SetSinkIdResolver>*); | |
28 | |
29 RefPtrWillBeMember<HTMLMediaElement> m_element; | |
30 String m_sinkId; | |
31 Timer<SetSinkIdResolver> m_timer; | |
32 }; | |
33 | |
34 SetSinkIdResolver* SetSinkIdResolver::create(ScriptState* scriptState, HTMLMedia Element& element, const String& sinkId) | |
35 { | |
36 SetSinkIdResolver* resolver = new SetSinkIdResolver(scriptState, element, si nkId); | |
37 resolver->suspendIfNeeded(); | |
38 resolver->keepAliveWhilePending(); | |
39 return resolver; | |
40 } | |
41 | |
42 SetSinkIdResolver::SetSinkIdResolver(ScriptState* scriptState, HTMLMediaElement& element, const String& sinkId) | |
43 : ScriptPromiseResolver(scriptState) | |
44 , m_element(element) | |
45 , m_sinkId(sinkId) | |
46 , m_timer(this, &SetSinkIdResolver::timerFired) | |
47 { | |
48 m_timer.startOneShot(0, BLINK_FROM_HERE); | |
49 } | |
50 | |
51 void SetSinkIdResolver::timerFired(Timer<SetSinkIdResolver>* timer) | |
52 { | |
53 if (m_sinkId == HTMLMediaElementAudioOutputDevice::sinkId(*m_element)) { | |
Peter Beverloo
2015/11/12 16:45:42
nit: since we can do this check synchronously, we
Guido Urdaneta
2015/11/12 18:45:44
Done.
| |
54 resolve(); | |
55 return; | |
56 } | |
57 | |
58 ExecutionContext* context = executionContext(); | |
59 ASSERT(context && context->isDocument()); | |
60 OwnPtr<SetSinkIdCallbacks> callbacks = adoptPtr(new SetSinkIdCallbacks(this, *m_element, m_sinkId)); | |
61 WebMediaPlayer* webMediaPlayer = m_element->webMediaPlayer(); | |
62 if (webMediaPlayer) { | |
63 // Using leakPtr() to transfer ownership because |webMediaPlayer| is a p latform object that takes raw pointers | |
64 webMediaPlayer->setSinkId(m_sinkId, WebSecurityOrigin(context->securityO rigin()), callbacks.leakPtr()); | |
65 } else { | |
66 if (AudioOutputDeviceClient* client = AudioOutputDeviceClient::from(cont ext)) { | |
67 client->checkIfAudioSinkExistsAndIsAuthorized(context, m_sinkId, cal lbacks.release()); | |
Peter Beverloo
2015/11/12 16:45:42
Note that there will still be a gap between updati
Guido Urdaneta
2015/11/12 18:45:44
Acknowledged.
| |
68 } else { | |
69 // The context has been detached. Impossible to get a security origi n to check. | |
70 ASSERT(context->activeDOMObjectsAreStopped()); | |
71 reject(DOMException::create(SecurityError, "Impossible to authorize device for detached context")); | |
72 } | |
73 } | |
74 } | |
75 | |
76 DEFINE_TRACE(SetSinkIdResolver) | |
77 { | |
78 visitor->trace(m_element); | |
79 ScriptPromiseResolver::trace(visitor); | |
80 } | |
81 | |
16 HTMLMediaElementAudioOutputDevice::HTMLMediaElementAudioOutputDevice() | 82 HTMLMediaElementAudioOutputDevice::HTMLMediaElementAudioOutputDevice() |
17 : m_sinkId("") | 83 : m_sinkId("") |
18 { | 84 { |
19 } | 85 } |
20 | 86 |
21 String HTMLMediaElementAudioOutputDevice::sinkId(HTMLMediaElement& element) | 87 String HTMLMediaElementAudioOutputDevice::sinkId(HTMLMediaElement& element) |
22 { | 88 { |
23 HTMLMediaElementAudioOutputDevice& aodElement = HTMLMediaElementAudioOutputD evice::from(element); | 89 HTMLMediaElementAudioOutputDevice& aodElement = HTMLMediaElementAudioOutputD evice::from(element); |
24 return aodElement.m_sinkId; | 90 return aodElement.m_sinkId; |
25 } | 91 } |
26 | 92 |
27 void HTMLMediaElementAudioOutputDevice::setSinkId(const String& sinkId) | 93 void HTMLMediaElementAudioOutputDevice::setSinkId(const String& sinkId) |
28 { | 94 { |
29 m_sinkId = sinkId; | 95 m_sinkId = sinkId; |
30 } | 96 } |
31 | 97 |
32 ScriptPromise HTMLMediaElementAudioOutputDevice::setSinkId(ScriptState* scriptSt ate, HTMLMediaElement& element, const String& sinkId) | 98 ScriptPromise HTMLMediaElementAudioOutputDevice::setSinkId(ScriptState* scriptSt ate, HTMLMediaElement& element, const String& sinkId) |
33 { | 99 { |
34 ASSERT(scriptState); | 100 SetSinkIdResolver* resolver = SetSinkIdResolver::create(scriptState, element , sinkId); |
35 | |
36 WebMediaPlayer* webMediaPlayer = element.webMediaPlayer(); | |
37 if (!webMediaPlayer) | |
38 return ScriptPromise::rejectWithDOMException(scriptState, DOMException:: create(AbortError, "No media player available")); | |
39 | |
40 ExecutionContext* context = scriptState->executionContext(); | |
41 ASSERT(context && context->isDocument()); | |
42 | |
43 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | |
44 webMediaPlayer->setSinkId(sinkId, WebSecurityOrigin(context->securityOrigin( )), new SetSinkIdCallbacks(resolver, element, sinkId)); | |
45 return resolver->promise(); | 101 return resolver->promise(); |
46 } | 102 } |
47 | 103 |
48 const char* HTMLMediaElementAudioOutputDevice::supplementName() | 104 const char* HTMLMediaElementAudioOutputDevice::supplementName() |
49 { | 105 { |
50 return "HTMLMediaElementAudioOutputDevice"; | 106 return "HTMLMediaElementAudioOutputDevice"; |
51 } | 107 } |
52 | 108 |
53 HTMLMediaElementAudioOutputDevice& HTMLMediaElementAudioOutputDevice::from(HTMLM ediaElement& element) | 109 HTMLMediaElementAudioOutputDevice& HTMLMediaElementAudioOutputDevice::from(HTMLM ediaElement& element) |
54 { | 110 { |
55 HTMLMediaElementAudioOutputDevice* supplement = static_cast<HTMLMediaElement AudioOutputDevice*>(WillBeHeapSupplement<HTMLMediaElement>::from(element, supple mentName())); | 111 HTMLMediaElementAudioOutputDevice* supplement = static_cast<HTMLMediaElement AudioOutputDevice*>(WillBeHeapSupplement<HTMLMediaElement>::from(element, supple mentName())); |
56 if (!supplement) { | 112 if (!supplement) { |
57 supplement = new HTMLMediaElementAudioOutputDevice(); | 113 supplement = new HTMLMediaElementAudioOutputDevice(); |
58 provideTo(element, supplementName(), adoptPtrWillBeNoop(supplement)); | 114 provideTo(element, supplementName(), adoptPtrWillBeNoop(supplement)); |
59 } | 115 } |
60 return *supplement; | 116 return *supplement; |
61 } | 117 } |
62 | 118 |
63 DEFINE_TRACE(HTMLMediaElementAudioOutputDevice) | 119 DEFINE_TRACE(HTMLMediaElementAudioOutputDevice) |
64 { | 120 { |
65 WillBeHeapSupplement<HTMLMediaElement>::trace(visitor); | 121 WillBeHeapSupplement<HTMLMediaElement>::trace(visitor); |
66 } | 122 } |
67 | 123 |
68 } // namespace blink | 124 } // namespace blink |
OLD | NEW |