Truly independent web browser
LibWeb: Add navigator.webkitGetUserMedia()
sideshowbarkera548f7e0bc37
diff --git a/Libraries/LibWeb/HTML/Navigator.cpp b/Libraries/LibWeb/HTML/Navigator.cppindex a7874b0e2c6..dec6df5db4a 100644--- a/Libraries/LibWeb/HTML/Navigator.cpp+++ b/Libraries/LibWeb/HTML/Navigator.cpp@@ -27,6 +27,9 @@#include <LibWeb/PermissionsAPI/Permissions.h>#include <LibWeb/Platform/EventLoopPlugin.h>#include <LibWeb/ServiceWorker/ServiceWorkerContainer.h>+#include <LibWeb/WebIDL/AbstractOperations.h>+#include <LibWeb/WebIDL/CallbackType.h>+#include <LibWeb/WebIDL/Promise.h>namespace Web::HTML {@@ -155,6 +158,35 @@ GC::Ref<MediaCapture::MediaDevices> Navigator::media_devices()return *m_media_devices;}+// https://w3c.github.io/mediacapture-main/#dom-navigator-getusermedia+void Navigator::webkit_get_user_media(MediaCapture::MediaStreamConstraints const& constraints, GC::Ref<WebIDL::CallbackType> success_callback, GC::Ref<WebIDL::CallbackType> error_callback)+{+ // 1. Let constraints be the method's first argument.+ // 2. Let successCallback be the callback indicated by the method's second argument.+ // 3. Let errorCallback be the callback indicated by the method's third argument.++ // 4. Run the steps specified by the getUserMedia() algorithm with constraints as the argument, and let p be the+ // resulting promise.+ auto promise = WebIDL::create_promise(relevant_realm(*m_window));+ media_devices()->get_user_media(constraints, promise);++ // 5. Upon fulfillment of p with value stream, run the following step:+ auto on_fulfilled = GC::create_function<WebIDL::ExceptionOr<JS::Value>(JS::Value)>(GC::Heap::the(), GC::captures(success_callback), [](WebIDL::CallbackType& success_callback, JS::Value stream) -> WebIDL::ExceptionOr<JS::Value> {+ // 1. Invoke successCallback with stream as the argument.+ (void)WebIDL::invoke_callback(success_callback, {}, WebIDL::ExceptionBehavior::Report, { { stream } });+ return JS::js_undefined();+ });++ // 6. Upon rejection of p with reason r, run the following step:+ auto on_rejected = GC::create_function<WebIDL::ExceptionOr<JS::Value>(JS::Value)>(GC::Heap::the(), GC::captures(error_callback), [](WebIDL::CallbackType& error_callback, JS::Value reason) -> WebIDL::ExceptionOr<JS::Value> {+ // 1. Invoke errorCallback with r as the argument.+ (void)WebIDL::invoke_callback(error_callback, {}, WebIDL::ExceptionBehavior::Report, { { reason } });+ return JS::js_undefined();+ });++ WebIDL::react_to_promise(promise, on_fulfilled, on_rejected);+}+// https://w3c.github.io/battery/#the-getbattery-methodGC::Ref<WebIDL::Promise> Navigator::get_battery(){diff --git a/Libraries/LibWeb/HTML/Navigator.h b/Libraries/LibWeb/HTML/Navigator.hindex 8c196028796..dca427766e1 100644--- a/Libraries/LibWeb/HTML/Navigator.h+++ b/Libraries/LibWeb/HTML/Navigator.h@@ -76,6 +76,7 @@ public:GC::Ref<MediaCapabilitiesAPI::MediaCapabilities> media_capabilities();GC::Ref<MediaCapture::MediaDevices> media_devices();+ void webkit_get_user_media(MediaCapture::MediaStreamConstraints const&, GC::Ref<WebIDL::CallbackType> success_callback, GC::Ref<WebIDL::CallbackType> error_callback);GC::Ptr<MediaCapture::MediaDevices> media_devices_if_created() const { return m_media_devices; }static WebIDL::Long max_touch_points();diff --git a/Libraries/LibWeb/HTML/Navigator.idl b/Libraries/LibWeb/HTML/Navigator.idlindex 11a0a50c0ea..64d649abafa 100644--- a/Libraries/LibWeb/HTML/Navigator.idl+++ b/Libraries/LibWeb/HTML/Navigator.idl@@ -24,6 +24,11 @@ interface Navigator {// https://w3c.github.io/mediacapture-main/#dom-navigator-mediadevices[SecureContext, SameObject] readonly attribute MediaDevices mediaDevices;+ // https://w3c.github.io/mediacapture-main/#dom-navigator-getusermedia+ // AD-HOC: The spec names this legacy method getUserMedia. Pages that tell a Chrome-like browser by its prefixed names+ // look for webkitGetUserMedia, and otherwise take us for an old Safari (WebRTC adapter libraries, e.g.).+ [SecureContext] undefined webkitGetUserMedia(MediaStreamConstraints constraints, NavigatorUserMediaSuccessCallback successCallback, NavigatorUserMediaErrorCallback errorCallback);+// https://w3c.github.io/webappsec-credential-management/#framework-credential-management[SecureContext, SameObject] readonly attribute CredentialsContainer credentials;@@ -73,3 +78,9 @@ Navigator includes NavigatorAutomationInformation;Navigator includes NavigatorStorage;Navigator includes NavigatorLocks;Navigator includes NavigatorDeviceMemory;++// https://w3c.github.io/mediacapture-main/#dom-navigatorusermediasuccesscallback+callback NavigatorUserMediaSuccessCallback = undefined (MediaStream stream);++// https://w3c.github.io/mediacapture-main/#dom-navigatorusermediaerrorcallback+callback NavigatorUserMediaErrorCallback = undefined (DOMException error);diff --git a/Tests/LibWeb/Text/expected/MediaCapture/webkitGetUserMedia.txt b/Tests/LibWeb/Text/expected/MediaCapture/webkitGetUserMedia.txtnew file mode 100644index 00000000000..4d332af356f--- /dev/null+++ b/Tests/LibWeb/Text/expected/MediaCapture/webkitGetUserMedia.txt@@ -0,0 +1,2 @@+webkitGetUserMedia is a function: true+errorCallback got: TypeErrordiff --git a/Tests/LibWeb/Text/input/MediaCapture/webkitGetUserMedia.html b/Tests/LibWeb/Text/input/MediaCapture/webkitGetUserMedia.htmlnew file mode 100644index 00000000000..7e5b21d3341--- /dev/null+++ b/Tests/LibWeb/Text/input/MediaCapture/webkitGetUserMedia.html@@ -0,0 +1,17 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ asyncTest(done => {+ println(`webkitGetUserMedia is a function: ${typeof navigator.webkitGetUserMedia === "function"}`);+ navigator.webkitGetUserMedia(+ { audio: false, video: false },+ () => {+ println("FAIL: successCallback was called");+ done();+ },+ error => {+ println(`errorCallback got: ${error.name}`);+ done();+ });+ });+</script>