Enforce user gesture for getUserMedia in case a previous getUserMedia call was denied
[WebKit-https.git] / Source / WebCore / Modules / mediastream / MediaDevices.cpp
1 /*
2  * Copyright (C) 2015 Ericsson AB. All rights reserved.
3  * Copyright (C) 2015-2019 Apple Inc. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer
13  *    in the documentation and/or other materials provided with the
14  *    distribution.
15  * 3. Neither the name of Ericsson nor the names of its contributors
16  *    may be used to endorse or promote products derived from this
17  *    software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #include "config.h"
33 #include "MediaDevices.h"
34
35 #if ENABLE(MEDIA_STREAM)
36
37 #include "Document.h"
38 #include "Event.h"
39 #include "EventNames.h"
40 #include "JSDOMPromiseDeferred.h"
41 #include "JSMediaDeviceInfo.h"
42 #include "MediaTrackSupportedConstraints.h"
43 #include "RealtimeMediaSourceSettings.h"
44 #include "UserGestureIndicator.h"
45 #include "UserMediaController.h"
46 #include "UserMediaRequest.h"
47 #include <wtf/IsoMallocInlines.h>
48 #include <wtf/RandomNumber.h>
49
50 namespace WebCore {
51
52 WTF_MAKE_ISO_ALLOCATED_IMPL(MediaDevices);
53
54 inline MediaDevices::MediaDevices(Document& document)
55     : ActiveDOMObject(document)
56     , m_scheduledEventTimer(*this, &MediaDevices::scheduledEventTimerFired)
57     , m_eventNames(eventNames())
58 {
59     suspendIfNeeded();
60
61     static_assert(static_cast<size_t>(MediaDevices::DisplayCaptureSurfaceType::Monitor) == static_cast<size_t>(RealtimeMediaSourceSettings::DisplaySurfaceType::Monitor), "MediaDevices::DisplayCaptureSurfaceType::Monitor is not equal to RealtimeMediaSourceSettings::DisplaySurfaceType::Monitor as expected");
62     static_assert(static_cast<size_t>(MediaDevices::DisplayCaptureSurfaceType::Window) == static_cast<size_t>(RealtimeMediaSourceSettings::DisplaySurfaceType::Window), "MediaDevices::DisplayCaptureSurfaceType::Window is not RealtimeMediaSourceSettings::DisplaySurfaceType::Window as expected");
63     static_assert(static_cast<size_t>(MediaDevices::DisplayCaptureSurfaceType::Application) == static_cast<size_t>(RealtimeMediaSourceSettings::DisplaySurfaceType::Application), "MediaDevices::DisplayCaptureSurfaceType::Application is not RealtimeMediaSourceSettings::DisplaySurfaceType::Application as expected");
64     static_assert(static_cast<size_t>(MediaDevices::DisplayCaptureSurfaceType::Browser) == static_cast<size_t>(RealtimeMediaSourceSettings::DisplaySurfaceType::Browser), "MediaDevices::DisplayCaptureSurfaceType::Browser is not RealtimeMediaSourceSettings::DisplaySurfaceType::Browser as expected");
65
66     if (auto* controller = UserMediaController::from(document.page())) {
67         m_canAccessCamera = controller->canCallGetUserMedia(document, { UserMediaController::CaptureType::Camera }) == UserMediaController::GetUserMediaAccess::CanCall;
68         m_canAccessMicrophone = controller->canCallGetUserMedia(document, { UserMediaController::CaptureType::Microphone }) == UserMediaController::GetUserMediaAccess::CanCall;
69     }
70 }
71
72 MediaDevices::~MediaDevices() = default;
73
74 void MediaDevices::stop()
75 {
76     if (m_deviceChangeToken) {
77         auto* controller = UserMediaController::from(document()->page());
78         controller->removeDeviceChangeObserver(m_deviceChangeToken);
79     }
80     m_devices.clear();
81     m_scheduledEventTimer.stop();
82 }
83
84 Ref<MediaDevices> MediaDevices::create(Document& document)
85 {
86     return adoptRef(*new MediaDevices(document));
87 }
88
89 Document* MediaDevices::document() const
90 {
91     return downcast<Document>(scriptExecutionContext());
92 }
93
94 static MediaConstraints createMediaConstraints(const Variant<bool, MediaTrackConstraints>& constraints)
95 {
96     return WTF::switchOn(constraints,
97         [&] (bool isValid) {
98             MediaConstraints constraints;
99             constraints.isValid = isValid;
100             return constraints;
101         },
102         [&] (const MediaTrackConstraints& trackConstraints) {
103             return createMediaConstraints(trackConstraints);
104         }
105     );
106 }
107
108 bool MediaDevices::computeUserGesturePriviledge(GestureAllowedRequest requestType)
109 {
110     auto* currentGestureToken = UserGestureIndicator::currentUserGesture().get();
111     if (m_currentGestureToken != currentGestureToken) {
112         m_currentGestureToken = currentGestureToken;
113         m_requestTypesForCurrentGesture = { };
114     }
115
116     bool isUserGesturePriviledged = m_currentGestureToken && !m_requestTypesForCurrentGesture.contains(requestType);
117     m_requestTypesForCurrentGesture.add(requestType);
118     return isUserGesturePriviledged;
119 }
120
121 void MediaDevices::getUserMedia(const StreamConstraints& constraints, Promise&& promise)
122 {
123     auto* document = this->document();
124     if (!document || !document->isFullyActive()) {
125         promise.reject(Exception { InvalidStateError, "Document is not fully active"_s });
126         return;
127     }
128
129     auto audioConstraints = createMediaConstraints(constraints.audio);
130     auto videoConstraints = createMediaConstraints(constraints.video);
131
132     bool isUserGesturePriviledged = false;
133
134     if (audioConstraints.isValid)
135         isUserGesturePriviledged |= computeUserGesturePriviledge(GestureAllowedRequest::Microphone);
136
137     if (videoConstraints.isValid) {
138         isUserGesturePriviledged |= computeUserGesturePriviledge(GestureAllowedRequest::Camera);
139         videoConstraints.setDefaultVideoConstraints();
140     }
141
142     auto request = UserMediaRequest::create(*document, { MediaStreamRequest::Type::UserMedia, WTFMove(audioConstraints), WTFMove(videoConstraints), isUserGesturePriviledged }, WTFMove(promise));
143     request->start();
144 }
145
146 void MediaDevices::getDisplayMedia(const StreamConstraints& constraints, Promise&& promise)
147 {
148     auto* document = this->document();
149     if (!document)
150         return;
151
152     bool isUserGesturePriviledged = computeUserGesturePriviledge(GestureAllowedRequest::Display);
153     if (!isUserGesturePriviledged) {
154         promise.reject(Exception { InvalidAccessError, "getDisplayMedia must be called from a user gesture handler."_s });
155         return;
156     }
157
158     auto request = UserMediaRequest::create(*document, { MediaStreamRequest::Type::DisplayMedia, { }, createMediaConstraints(constraints.video), isUserGesturePriviledged }, WTFMove(promise));
159     request->start();
160 }
161
162 void MediaDevices::refreshDevices(const Vector<CaptureDevice>& newDevices)
163 {
164     Vector<Ref<MediaDeviceInfo>> devices;
165     for (auto& newDevice : newDevices) {
166         if (!m_canAccessMicrophone && newDevice.type() == CaptureDevice::DeviceType::Microphone)
167             continue;
168         if (!m_canAccessCamera && newDevice.type() == CaptureDevice::DeviceType::Camera)
169             continue;
170
171         auto index = m_devices.findMatching([&newDevice](auto& oldDevice) {
172             return oldDevice->deviceId() == newDevice.persistentId();
173         });
174         if (index != notFound) {
175             devices.append(m_devices[index].copyRef());
176             continue;
177         }
178
179         auto deviceType = newDevice.type() == CaptureDevice::DeviceType::Microphone ? MediaDeviceInfo::Kind::Audioinput : MediaDeviceInfo::Kind::Videoinput;
180         devices.append(MediaDeviceInfo::create(newDevice.label(), newDevice.persistentId(), newDevice.groupId(), deviceType));
181     }
182     m_devices = WTFMove(devices);
183 }
184
185 void MediaDevices::enumerateDevices(EnumerateDevicesPromise&& promise)
186 {
187     auto* document = this->document();
188     if (!document)
189         return;
190
191     auto* controller = UserMediaController::from(document->page());
192     if (!controller) {
193         promise.resolve({ });
194         return;
195     }
196     if (!m_canAccessCamera && !m_canAccessMicrophone) {
197         controller->logGetUserMediaDenial(*document, UserMediaController::GetUserMediaAccess::BlockedByFeaturePolicy, UserMediaController::BlockedCaller::EnumerateDevices);
198         promise.resolve({ });
199         return;
200     }
201
202     controller->enumerateMediaDevices(*document, [this, weakThis = makeWeakPtr(this), promise = WTFMove(promise)](const auto& newDevices, const auto& deviceIDHashSalt) mutable {
203         if (!weakThis || isContextStopped())
204             return;
205
206         this->document()->setDeviceIDHashSalt(deviceIDHashSalt);
207         refreshDevices(newDevices);
208         promise.resolve(m_devices);
209     });
210 }
211
212 MediaTrackSupportedConstraints MediaDevices::getSupportedConstraints()
213 {
214     auto& supported = RealtimeMediaSourceCenter::singleton().supportedConstraints();
215     MediaTrackSupportedConstraints result;
216     result.width = supported.supportsWidth();
217     result.height = supported.supportsHeight();
218     result.aspectRatio = supported.supportsAspectRatio();
219     result.frameRate = supported.supportsFrameRate();
220     result.facingMode = supported.supportsFacingMode();
221     result.volume = supported.supportsVolume();
222     result.sampleRate = supported.supportsSampleRate();
223     result.sampleSize = supported.supportsSampleSize();
224     result.echoCancellation = supported.supportsEchoCancellation();
225     result.deviceId = supported.supportsDeviceId();
226     result.groupId = supported.supportsGroupId();
227
228     return result;
229 }
230
231 void MediaDevices::scheduledEventTimerFired()
232 {
233     ASSERT(!isContextStopped());
234     dispatchEvent(Event::create(eventNames().devicechangeEvent, Event::CanBubble::No, Event::IsCancelable::No));
235 }
236
237 bool MediaDevices::hasPendingActivity() const
238 {
239     return !isContextStopped() && hasEventListeners(m_eventNames.devicechangeEvent);
240 }
241
242 const char* MediaDevices::activeDOMObjectName() const
243 {
244     return "MediaDevices";
245 }
246
247 void MediaDevices::listenForDeviceChanges()
248 {
249     if (m_listeningForDeviceChanges || (!m_canAccessCamera && !m_canAccessMicrophone))
250         return;
251
252     m_listeningForDeviceChanges = true;
253
254     auto* document = this->document();
255     auto* controller = document ? UserMediaController::from(document->page()) : nullptr;
256     if (!controller)
257         return;
258
259     m_deviceChangeToken = controller->addDeviceChangeObserver([weakThis = makeWeakPtr(*this), this]() {
260         if (!weakThis || isContextStopped() || m_scheduledEventTimer.isActive())
261             return;
262
263         m_scheduledEventTimer.startOneShot(Seconds(randomNumber() / 2));
264     });
265 }
266
267 bool MediaDevices::addEventListener(const AtomString& eventType, Ref<EventListener>&& listener, const AddEventListenerOptions& options)
268 {
269     if (eventType == eventNames().devicechangeEvent)
270         listenForDeviceChanges();
271
272     return EventTargetWithInlineData::addEventListener(eventType, WTFMove(listener), options);
273 }
274
275 } // namespace WebCore
276
277 #endif // ENABLE(MEDIA_STREAM)