| 1 | /* |
| 2 | * Copyright (C) 2014 Igalia S.L |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Lesser General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public |
| 15 | * License along with this library; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 | |
| 19 | #include "config.h" |
| 20 | #include "WebKitUserMediaPermissionRequest.h" |
| 21 | |
| 22 | #include "UserMediaPermissionRequestProxy.h" |
| 23 | #include "WebKitPermissionRequest.h" |
| 24 | #include "WebKitUserMediaPermissionRequestPrivate.h" |
| 25 | #include <glib/gi18n-lib.h> |
| 26 | #include <wtf/glib/WTFGType.h> |
| 27 | |
| 28 | using namespace WebKit; |
| 29 | |
| 30 | /** |
| 31 | * SECTION: WebKitUserMediaPermissionRequest |
| 32 | * @Short_description: A permission request for accessing user's audio/video devices. |
| 33 | * @Title: WebKitUserMediaPermissionRequest |
| 34 | * @See_also: #WebKitPermissionRequest, #WebKitWebView |
| 35 | * |
| 36 | * WebKitUserMediaPermissionRequest represents a request for |
| 37 | * permission to decide whether WebKit should be allowed to access the user's |
| 38 | * audio and video source devices when requested through the getUserMedia API. |
| 39 | * |
| 40 | * When a WebKitUserMediaPermissionRequest is not handled by the user, |
| 41 | * it is denied by default. |
| 42 | * |
| 43 | * Since: 2.8 |
| 44 | */ |
| 45 | |
| 46 | enum { |
| 47 | PROP_0, |
| 48 | PROP_IS_FOR_AUDIO_DEVICE, |
| 49 | PROP_IS_FOR_VIDEO_DEVICE |
| 50 | }; |
| 51 | |
| 52 | static void webkit_permission_request_interface_init(WebKitPermissionRequestIface*); |
| 53 | |
| 54 | struct _WebKitUserMediaPermissionRequestPrivate { |
| 55 | RefPtr<UserMediaPermissionRequestProxy> request; |
| 56 | bool madeDecision; |
| 57 | }; |
| 58 | |
| 59 | WEBKIT_DEFINE_TYPE_WITH_CODE( |
| 60 | WebKitUserMediaPermissionRequest, webkit_user_media_permission_request, G_TYPE_OBJECT, |
| 61 | G_IMPLEMENT_INTERFACE(WEBKIT_TYPE_PERMISSION_REQUEST, webkit_permission_request_interface_init)) |
| 62 | |
| 63 | static void webkitUserMediaPermissionRequestAllow(WebKitPermissionRequest* request) |
| 64 | { |
| 65 | ASSERT(WEBKIT_IS_USER_MEDIA_PERMISSION_REQUEST(request)); |
| 66 | |
| 67 | WebKitUserMediaPermissionRequestPrivate* priv = WEBKIT_USER_MEDIA_PERMISSION_REQUEST(request)->priv; |
| 68 | |
| 69 | // Only one decision at a time. |
| 70 | if (priv->madeDecision) |
| 71 | return; |
| 72 | |
| 73 | priv->madeDecision = true; |
| 74 | |
| 75 | auto videoDeviceUIDs = priv->request->videoDeviceUIDs(); |
| 76 | auto audioDeviceUIDs = priv->request->audioDeviceUIDs(); |
| 77 | |
| 78 | auto videoDevice = !videoDeviceUIDs.isEmpty() ? videoDeviceUIDs[0] : emptyString(); |
| 79 | auto audioDevice = !audioDeviceUIDs.isEmpty() ? audioDeviceUIDs[0] : emptyString(); |
| 80 | |
| 81 | priv->request->allow(audioDevice, videoDevice); |
| 82 | } |
| 83 | |
| 84 | static void webkitUserMediaPermissionRequestDeny(WebKitPermissionRequest* request) |
| 85 | { |
| 86 | ASSERT(WEBKIT_IS_USER_MEDIA_PERMISSION_REQUEST(request)); |
| 87 | |
| 88 | WebKitUserMediaPermissionRequestPrivate* priv = WEBKIT_USER_MEDIA_PERMISSION_REQUEST(request)->priv; |
| 89 | |
| 90 | // Only one decision at a time. |
| 91 | if (priv->madeDecision) |
| 92 | return; |
| 93 | |
| 94 | priv->madeDecision = true; |
| 95 | priv->request->deny(UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::PermissionDenied); |
| 96 | } |
| 97 | |
| 98 | static void webkit_permission_request_interface_init(WebKitPermissionRequestIface* iface) |
| 99 | { |
| 100 | iface->allow = webkitUserMediaPermissionRequestAllow; |
| 101 | iface->deny = webkitUserMediaPermissionRequestDeny; |
| 102 | } |
| 103 | |
| 104 | static void webkitUserMediaPermissionRequestDispose(GObject* object) |
| 105 | { |
| 106 | // Default behaviour when no decision has been made is denying the request. |
| 107 | webkitUserMediaPermissionRequestDeny(WEBKIT_PERMISSION_REQUEST(object)); |
| 108 | G_OBJECT_CLASS(webkit_user_media_permission_request_parent_class)->dispose(object); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * webkit_user_media_permission_is_for_audio_device: |
| 113 | * @request: a #WebKitUserMediaPermissionRequest |
| 114 | * |
| 115 | * Returns: %TRUE if access to an audio device was requested. |
| 116 | * |
| 117 | * Since: 2.8 |
| 118 | */ |
| 119 | gboolean webkit_user_media_permission_is_for_audio_device(WebKitUserMediaPermissionRequest* request) |
| 120 | { |
| 121 | g_return_val_if_fail(request->priv->request, FALSE); |
| 122 | return request->priv->request->requiresAudioCapture(); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * webkit_user_media_permission_is_for_video_device: |
| 127 | * @request: a #WebKitUserMediaPermissionRequest |
| 128 | * |
| 129 | * Returns: %TRUE if access to a video device was requested. |
| 130 | * |
| 131 | * Since: 2.8 |
| 132 | */ |
| 133 | gboolean webkit_user_media_permission_is_for_video_device(WebKitUserMediaPermissionRequest* request) |
| 134 | { |
| 135 | g_return_val_if_fail(request->priv->request, FALSE); |
| 136 | return request->priv->request->requiresVideoCapture(); |
| 137 | } |
| 138 | |
| 139 | static void webkitUserMediaPermissionRequestGetProperty(GObject* object, guint propId, GValue* value, GParamSpec* paramSpec) |
| 140 | { |
| 141 | WebKitUserMediaPermissionRequest* request = WEBKIT_USER_MEDIA_PERMISSION_REQUEST(object); |
| 142 | |
| 143 | switch (propId) { |
| 144 | case PROP_IS_FOR_AUDIO_DEVICE: |
| 145 | g_value_set_boolean(value, webkit_user_media_permission_is_for_audio_device(request)); |
| 146 | break; |
| 147 | case PROP_IS_FOR_VIDEO_DEVICE: |
| 148 | g_value_set_boolean(value, webkit_user_media_permission_is_for_video_device(request)); |
| 149 | break; |
| 150 | default: |
| 151 | G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, paramSpec); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | static void webkit_user_media_permission_request_class_init(WebKitUserMediaPermissionRequestClass* klass) |
| 156 | { |
| 157 | GObjectClass* objectClass = G_OBJECT_CLASS(klass); |
| 158 | objectClass->dispose = webkitUserMediaPermissionRequestDispose; |
| 159 | objectClass->get_property = webkitUserMediaPermissionRequestGetProperty; |
| 160 | |
| 161 | /** |
| 162 | * WebKitUserPermissionRequest:is-for-audio-device: |
| 163 | * |
| 164 | * Whether the media device to which the permission was requested has a microphone or not. |
| 165 | * |
| 166 | * Since: 2.8 |
| 167 | */ |
| 168 | g_object_class_install_property(objectClass, PROP_IS_FOR_AUDIO_DEVICE, |
| 169 | g_param_spec_boolean("is-for-audio-device" , _("Is for audio device" ), |
| 170 | _("Whether the media device to which the permission was requested has a microphone or not." ), |
| 171 | FALSE, |
| 172 | WEBKIT_PARAM_READABLE)); |
| 173 | |
| 174 | /** |
| 175 | * WebKitUserPermissionRequest:is-for-video-device: |
| 176 | * |
| 177 | * Whether the media device to which the permission was requested has a video capture capability or not. |
| 178 | * |
| 179 | * Since: 2.8 |
| 180 | */ |
| 181 | g_object_class_install_property(objectClass, PROP_IS_FOR_VIDEO_DEVICE, |
| 182 | g_param_spec_boolean("is-for-video-device" , _("Is for video device" ), |
| 183 | _("Whether the media device to which the permission was requested has a video capture capability or not." ), |
| 184 | FALSE, |
| 185 | WEBKIT_PARAM_READABLE)); |
| 186 | } |
| 187 | |
| 188 | WebKitUserMediaPermissionRequest* webkitUserMediaPermissionRequestCreate(UserMediaPermissionRequestProxy& request, API::SecurityOrigin& userMediaDocumentOrigin, API::SecurityOrigin& topLevelDocumentOrigin) |
| 189 | { |
| 190 | WebKitUserMediaPermissionRequest* usermediaPermissionRequest = WEBKIT_USER_MEDIA_PERMISSION_REQUEST(g_object_new(WEBKIT_TYPE_USER_MEDIA_PERMISSION_REQUEST, nullptr)); |
| 191 | |
| 192 | // FIXME: store SecurityOrigins |
| 193 | UNUSED_PARAM(userMediaDocumentOrigin); |
| 194 | UNUSED_PARAM(topLevelDocumentOrigin); |
| 195 | |
| 196 | usermediaPermissionRequest->priv->request = &request; |
| 197 | return usermediaPermissionRequest; |
| 198 | } |
| 199 | |