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/mediastream/MediaDevices.h" | 6 #include "modules/mediastream/MediaDevices.h" |
7 | 7 |
8 #include "bindings/core/v8/ScriptPromise.h" | 8 #include "bindings/core/v8/ScriptPromise.h" |
9 #include "bindings/core/v8/ScriptPromiseResolver.h" | 9 #include "bindings/core/v8/ScriptPromiseResolver.h" |
10 #include "bindings/core/v8/ScriptState.h" | 10 #include "bindings/core/v8/ScriptState.h" |
11 #include "core/dom/DOMException.h" | 11 #include "core/dom/DOMException.h" |
12 #include "core/dom/Document.h" | 12 #include "core/dom/Document.h" |
13 #include "core/dom/ExceptionCode.h" | 13 #include "core/dom/ExceptionCode.h" |
| 14 #include "modules/mediastream/MediaStream.h" |
| 15 #include "modules/mediastream/NavigatorMediaStream.h" |
| 16 #include "modules/mediastream/NavigatorUserMediaErrorCallback.h" |
| 17 #include "modules/mediastream/NavigatorUserMediaSuccessCallback.h" |
14 #include "modules/mediastream/UserMediaController.h" | 18 #include "modules/mediastream/UserMediaController.h" |
15 | 19 |
16 namespace blink { | 20 namespace blink { |
17 | 21 |
18 ScriptPromise MediaDevices::enumerateDevices(ScriptState* scriptState) | 22 ScriptPromise MediaDevices::enumerateDevices(ScriptState* scriptState) |
19 { | 23 { |
20 Document* document = toDocument(scriptState->executionContext()); | 24 Document* document = toDocument(scriptState->executionContext()); |
21 UserMediaController* userMedia = UserMediaController::from(document->frame()
); | 25 UserMediaController* userMedia = UserMediaController::from(document->frame()
); |
22 if (!userMedia) | 26 if (!userMedia) |
23 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(NotSupportedError, "No media device controller available; is this a detac
hed window?")); | 27 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(NotSupportedError, "No media device controller available; is this a detac
hed window?")); |
24 | 28 |
25 MediaDevicesRequest* request = MediaDevicesRequest::create(scriptState, user
Media); | 29 MediaDevicesRequest* request = MediaDevicesRequest::create(scriptState, user
Media); |
26 return request->start(); | 30 return request->start(); |
27 } | 31 } |
28 | 32 |
| 33 namespace { |
| 34 |
| 35 class PromiseSuccessCallback : public NavigatorUserMediaSuccessCallback { |
| 36 public: |
| 37 PromiseSuccessCallback(PassRefPtrWillBeRawPtr<ScriptPromiseResolver> resolve
r) |
| 38 : m_resolver(resolver) |
| 39 { |
| 40 } |
| 41 |
| 42 ~PromiseSuccessCallback() |
| 43 { |
| 44 } |
| 45 |
| 46 void handleEvent(MediaStream* stream) |
| 47 { |
| 48 m_resolver->resolve(stream); |
| 49 } |
| 50 private: |
| 51 RefPtrWillBeRawPtr<ScriptPromiseResolver> m_resolver; |
| 52 }; |
| 53 |
| 54 class PromiseErrorCallback : public NavigatorUserMediaErrorCallback { |
| 55 public: |
| 56 PromiseErrorCallback(PassRefPtrWillBeRawPtr<ScriptPromiseResolver> resolver) |
| 57 : m_resolver(resolver) |
| 58 { |
| 59 } |
| 60 |
| 61 ~PromiseErrorCallback() |
| 62 { |
| 63 } |
| 64 |
| 65 void handleEvent(NavigatorUserMediaError* error) |
| 66 { |
| 67 m_resolver->reject(error); |
| 68 } |
| 69 private: |
| 70 RefPtrWillBeRawPtr<ScriptPromiseResolver> m_resolver; |
| 71 }; |
| 72 |
| 73 } // namespace |
| 74 |
| 75 ScriptPromise MediaDevices::getUserMedia(ScriptState* scriptState, const Diction
ary& options, ExceptionState& exceptionState) |
| 76 { |
| 77 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); |
| 78 |
| 79 NavigatorUserMediaSuccessCallback* successCallback = new PromiseSuccessCallb
ack(resolver); |
| 80 NavigatorUserMediaErrorCallback* errorCallback = new PromiseErrorCallback(re
solver); |
| 81 |
| 82 Document* document = toDocument(scriptState->executionContext()); |
| 83 UserMediaController* userMedia = UserMediaController::from(document->frame()
); |
| 84 if (!userMedia) |
| 85 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(NotSupportedError, "No media device controller available; is this a detac
hed window?")); |
| 86 |
| 87 UserMediaRequest* request = UserMediaRequest::create(document, userMedia, op
tions, successCallback, errorCallback, exceptionState); |
| 88 if (!request) { |
| 89 ASSERT(exceptionState.hadException()); |
| 90 return exceptionState.reject(scriptState); |
| 91 } |
| 92 |
| 93 request->start(); |
| 94 return resolver->promise(); |
| 95 } |
| 96 |
29 } // namespace blink | 97 } // namespace blink |
OLD | NEW |