4#include <private/qpermissions_p.h>
5#include <private/qstdweb_p.h>
7#include <qglobalstatic.h>
8#include <qpermissions.h>
9#include <qmetaobject.h>
10#include <qnamespace.h>
16#include <emscripten.h>
17#include <emscripten/bind.h>
18#include <emscripten/val.h>
26using namespace QPermissions::Private;
27using namespace emscripten;
31 constexpr const char *wapiGranted =
"granted";
32 constexpr const char *wapiDenied =
"denied";
33 constexpr const char *wapiPrompt =
"prompt";
34 constexpr const char *wapiCamera =
"camera";
35 constexpr const char *wapiVideoCapture =
"video_capture";
36 constexpr const char *wapiMicrophone =
"microphone";
37 constexpr const char *wapiAudioCapture =
"audio_capture";
38 constexpr const char *wapiGeolocation =
"geolocation";
40 void updatePermission(
const std::string &name,
const std::string &state,
41 PermissionCallback callback);
43 void checkPermission(
const std::string &permissionName)
45 val permissions = val::global(
"navigator")[
"permissions"];
46 if (permissions.isUndefined() || permissions.isNull())
49 qstdweb::PromiseCallbacks callbacks;
50 callbacks.thenFunc = [permissionName](val permissionState)
52 updatePermission(permissionName, permissionState[
"state"].as<
std::string>(), {});
54 callbacks.catchFunc = [permissionName](val err)
56 if (err[
"name"].as<std::string>() ==
"NotAllowedError")
57 return updatePermission(permissionName, wapiDenied, {});
59 qCInfo(lcPermissions,
"'%s' '%s'", err[
"name"].as<std::string>().c_str(),
60 err[
"message"].as<std::string>().c_str());
63 val query = val::object();
64 query.set(
"name", val(permissionName));
66 qstdweb::Promise::make(permissions,
QStringLiteral(
"query"), callbacks, query);
69 void checkPermissions()
71 checkPermission(wapiCamera);
72 checkPermission(wapiMicrophone);
73 checkPermission(wapiGeolocation);
76 void bootstrapCheckPermissions()
78 QTimer::singleShot(0, []{checkPermissions();});
81 Q_CONSTRUCTOR_FUNCTION(bootstrapCheckPermissions);
83 int permissionTypeIdFromString(
const std::string &permission)
85 if (permission == wapiCamera || permission == wapiVideoCapture)
86 return qMetaTypeId<QCameraPermission>();
87 if (permission == wapiMicrophone || permission == wapiAudioCapture)
88 return qMetaTypeId<QMicrophonePermission>();
89 if (permission == wapiGeolocation)
90 return qMetaTypeId<QLocationPermission>();
92 qCWarning(lcPermissions,
"Unknown permission type '%s'", permission.c_str());
99 if (state == wapiGranted)
101 if (state == wapiDenied)
103 if (state == wapiPrompt)
106 qCWarning(lcPermissions,
"Unknown permission state '%s'", state.c_str());
114 void updatePermission(
const std::string &name,
const std::string &state, PermissionCallback callback)
116 qCDebug(lcPermissions) <<
"Updating" << name <<
"permission to" << state;
118 const int type = permissionTypeIdFromString(name);
122 const auto status = permissionStatusFromString(state);
123 (*permissionStatuses)[type] = status;
129 void requestMediaDevicePermission(
const std::string &device,
const PermissionCallback &cb)
133 val mediaDevices = val::global(
"navigator")[
"mediaDevices"];
134 if (mediaDevices.isUndefined())
137 qstdweb::PromiseCallbacks queryCallbacks;
138 queryCallbacks.thenFunc = [device, cb](val)
140 updatePermission(device, wapiGranted, cb);
142 queryCallbacks.catchFunc = [device, cb](val error)
144 if (error[
"name"].as<std::string>() ==
"NotAllowedError")
145 return updatePermission(device, wapiDenied, cb);
146 updatePermission(device, wapiPrompt, cb);
149 val constraint = val::object();
150 if (device == wapiCamera)
151 constraint.set(
"video",
true);
153 constraint.set(
"audio",
true);
155 qstdweb::Promise::make(mediaDevices,
QStringLiteral(
"getUserMedia"), queryCallbacks, constraint);
158 using GeoRequest = std::pair<QPermission, PermissionCallback>;
161 bool processingLocationRequest =
false;
163 void processNextGeolocationRequest();
165 void geolocationSuccess(val position)
168 Q_ASSERT(geolocationRequestQueue->size());
170 processingLocationRequest =
false;
172 auto cb = geolocationRequestQueue->front().second;
173 geolocationRequestQueue->pop_front();
174 updatePermission(wapiGeolocation, wapiGranted, cb);
175 processNextGeolocationRequest();
178 void geolocationError(val error)
180 Q_ASSERT(geolocationRequestQueue->size());
182 static int deniedError = []
184 val posErr = val::global(
"GeolocationPositionError");
185 if (posErr.isUndefined() || posErr.isNull())
187 return posErr[
"PERMISSION_DENIED"].as<
int>();
190 processingLocationRequest =
false;
192 auto cb = geolocationRequestQueue->front().second;
193 geolocationRequestQueue->pop_front();
195 const int errorCode = error[
"code"].as<
int>();
196 updatePermission(wapiGeolocation, errorCode == deniedError ? wapiDenied : wapiPrompt, cb);
197 processNextGeolocationRequest();
200 EMSCRIPTEN_BINDINGS(qt_permissions) {
201 function(
"qtLocationSuccess", &geolocationSuccess);
202 function(
"qtLocationError", &geolocationError);
205 void processNextGeolocationRequest()
207 if (processingLocationRequest)
210 if (geolocationRequestQueue->empty())
213 processingLocationRequest =
true;
215 val geolocation = val::global(
"navigator")[
"geolocation"];
216 Q_ASSERT(!geolocation.isUndefined());
217 Q_ASSERT(!geolocation.isNull());
219 const auto &permission = geolocationRequestQueue->front().first;
220 const auto locationPermission = *permission.value<QLocationPermission>();
221 const bool highAccuracy = locationPermission.accuracy() == QLocationPermission::Precise;
223 val options = val::object();
224 options.set(
"enableHighAccuracy", highAccuracy ?
true :
false);
225 geolocation.call<
void>(
"getCurrentPosition", val::module_property(
"qtLocationSuccess"),
226 val::module_property(
"qtLocationError"), options);
229 void requestGeolocationPermission(
const QPermission &permission,
const PermissionCallback &cb)
232 Q_UNUSED(permission);
235 val geolocation = val::global(
"navigator")[
"geolocation"];
236 if (geolocation.isUndefined() || geolocation.isNull())
239 if (processingLocationRequest)
240 qCWarning(lcPermissions,
"Permission to access location requested, while another request is in progress");
242 geolocationRequestQueue->push_back(std::make_pair(permission, cb));
243 processNextGeolocationRequest();
251 const auto it = permissionStatuses->find(permission.type().id());
252 return it != permissionStatuses->end() ? it.value() : Qt::PermissionStatus::Undetermined;
257 Q_ASSERT(permission.type().isValid());
262 return callback(status);
264 const int requestedTypeId = permission.type().id();
265 if (requestedTypeId == qMetaTypeId<QCameraPermission>())
266 return requestMediaDevicePermission(wapiCamera, callback);
268 if (requestedTypeId == qMetaTypeId<QMicrophonePermission>())
269 return requestMediaDevicePermission(wapiMicrophone, callback);
271 if (requestedTypeId == qMetaTypeId<QLocationPermission>())
272 return requestGeolocationPermission(permission, callback);
274 (*permissionStatuses)[requestedTypeId] = Qt::PermissionStatus::Denied;
\inmodule QtCore \inheaderfile QPermissions
void requestPermission(const QPermission &permission, const PermissionCallback &callback)
Qt::PermissionStatus checkPermission(const QPermission &permission)
#define Q_GLOBAL_STATIC(TYPE, NAME,...)
#define QStringLiteral(str)