5#include <private/qpermissions_p.h>
6#include <private/qstdweb_p.h>
8#include <qglobalstatic.h>
9#include <qpermissions.h>
10#include <qmetaobject.h>
11#include <qnamespace.h>
17#include <emscripten.h>
18#include <emscripten/bind.h>
19#include <emscripten/val.h>
27using namespace QPermissions::Private;
28using namespace emscripten;
32 constexpr const char *wapiGranted =
"granted";
33 constexpr const char *wapiDenied =
"denied";
34 constexpr const char *wapiPrompt =
"prompt";
35 constexpr const char *wapiCamera =
"camera";
36 constexpr const char *wapiVideoCapture =
"video_capture";
37 constexpr const char *wapiMicrophone =
"microphone";
38 constexpr const char *wapiAudioCapture =
"audio_capture";
39 constexpr const char *wapiGeolocation =
"geolocation";
41 void updatePermission(
const std::string &name,
const std::string &state,
42 PermissionCallback callback);
44 void checkPermission(
const std::string &permissionName)
46 val permissions = val::global(
"navigator")[
"permissions"];
47 if (permissions.isUndefined() || permissions.isNull())
50 qstdweb::PromiseCallbacks callbacks;
51 callbacks.thenFunc = [permissionName](val permissionState)
53 updatePermission(permissionName, permissionState[
"state"].as<
std::string>(), {});
55 callbacks.catchFunc = [permissionName](val err)
57 if (err[
"name"].as<std::string>() ==
"NotAllowedError")
58 return updatePermission(permissionName, wapiDenied, {});
60 qCInfo(lcPermissions,
"'%s' '%s'", err[
"name"].as<
std::string>().c_str(),
61 err[
"message"].as<
std::string>().c_str());
64 val query = val::object();
65 query.set(
"name", val(permissionName));
67 qstdweb::Promise::make(permissions,
QStringLiteral(
"query"), callbacks, query);
70 void checkPermissions()
72 checkPermission(wapiCamera);
73 checkPermission(wapiMicrophone);
74 checkPermission(wapiGeolocation);
77 void bootstrapCheckPermissions()
79 QTimer::singleShot(0, []{checkPermissions();});
82 Q_CONSTRUCTOR_FUNCTION(bootstrapCheckPermissions);
84 int permissionTypeIdFromString(
const std::string &permission)
86 if (permission == wapiCamera || permission == wapiVideoCapture)
87 return qMetaTypeId<QCameraPermission>();
88 if (permission == wapiMicrophone || permission == wapiAudioCapture)
89 return qMetaTypeId<QMicrophonePermission>();
90 if (permission == wapiGeolocation)
91 return qMetaTypeId<QLocationPermission>();
93 qCWarning(lcPermissions,
"Unknown permission type '%s'", permission.c_str());
100 if (state == wapiGranted)
102 if (state == wapiDenied)
104 if (state == wapiPrompt)
107 qCWarning(lcPermissions,
"Unknown permission state '%s'", state.c_str());
115 void updatePermission(
const std::string &name,
const std::string &state, PermissionCallback callback)
117 qCDebug(lcPermissions) <<
"Updating" << name <<
"permission to" << state;
119 const int type = permissionTypeIdFromString(name);
123 const auto status = permissionStatusFromString(state);
124 (*permissionStatuses)[type] = status;
130 void requestMediaDevicePermission(
const std::string &device,
const PermissionCallback &cb)
134 val mediaDevices = val::global(
"navigator")[
"mediaDevices"];
135 if (mediaDevices.isUndefined())
138 qstdweb::PromiseCallbacks queryCallbacks;
139 queryCallbacks.thenFunc = [device, cb](val stream)
142 if (!stream.isNull() && !stream.isUndefined() && !stream[
"getTracks"].isUndefined()) {
143 emscripten::val tracks = stream.call<emscripten::val>(
"getTracks");
144 if (!tracks.isUndefined() && tracks[
"length"].as<
int>() > 0) {
145 for (
int i = 0; i < tracks[
"length"].as<
int>(); i++) {
146 tracks[i].call<
void>(
"stop");
150 stream = emscripten::val::null();
152 updatePermission(device, wapiGranted, cb);
154 queryCallbacks.catchFunc = [device, cb](val error)
156 if (error[
"name"].as<std::string>() ==
"NotAllowedError")
157 return updatePermission(device, wapiDenied, cb);
158 updatePermission(device, wapiPrompt, cb);
161 val constraint = val::object();
162 if (device == wapiCamera)
163 constraint.set(
"video",
true);
165 constraint.set(
"audio",
true);
167 qstdweb::Promise::make(mediaDevices,
QStringLiteral(
"getUserMedia"), queryCallbacks, constraint);
170 using GeoRequest = std::pair<QPermission, PermissionCallback>;
173 bool processingLocationRequest =
false;
175 void processNextGeolocationRequest();
177 void geolocationSuccess(val position)
180 Q_ASSERT(geolocationRequestQueue->size());
182 processingLocationRequest =
false;
184 auto cb = geolocationRequestQueue->front().second;
185 geolocationRequestQueue->pop_front();
186 updatePermission(wapiGeolocation, wapiGranted, cb);
187 processNextGeolocationRequest();
190 void geolocationError(val error)
192 Q_ASSERT(geolocationRequestQueue->size());
194 static int deniedError = []
196 val posErr = val::global(
"GeolocationPositionError");
197 if (posErr.isUndefined() || posErr.isNull())
199 return posErr[
"PERMISSION_DENIED"].as<
int>();
202 processingLocationRequest =
false;
204 auto cb = geolocationRequestQueue->front().second;
205 geolocationRequestQueue->pop_front();
207 const int errorCode = error[
"code"].as<
int>();
208 updatePermission(wapiGeolocation, errorCode == deniedError ? wapiDenied : wapiPrompt, cb);
209 processNextGeolocationRequest();
212 EMSCRIPTEN_BINDINGS(qt_permissions) {
213 function(
"qtLocationSuccess", &geolocationSuccess);
214 function(
"qtLocationError", &geolocationError);
217 void processNextGeolocationRequest()
219 if (processingLocationRequest)
222 if (geolocationRequestQueue->empty())
225 processingLocationRequest =
true;
227 val geolocation = val::global(
"navigator")[
"geolocation"];
228 Q_ASSERT(!geolocation.isUndefined());
229 Q_ASSERT(!geolocation.isNull());
231 const auto &permission = geolocationRequestQueue->front().first;
232 const auto locationPermission = *permission.value<QLocationPermission>();
233 const bool highAccuracy = locationPermission.accuracy() == QLocationPermission::Precise;
235 val options = val::object();
236 options.set(
"enableHighAccuracy", highAccuracy ?
true :
false);
237 geolocation.call<
void>(
"getCurrentPosition", val::module_property(
"qtLocationSuccess"),
238 val::module_property(
"qtLocationError"), options);
241 void requestGeolocationPermission(
const QPermission &permission,
const PermissionCallback &cb)
244 Q_UNUSED(permission);
247 val geolocation = val::global(
"navigator")[
"geolocation"];
248 if (geolocation.isUndefined() || geolocation.isNull())
251 if (processingLocationRequest)
252 qCWarning(lcPermissions,
"Permission to access location requested, while another request is in progress");
254 geolocationRequestQueue->push_back(std::make_pair(permission, cb));
255 processNextGeolocationRequest();
263 const auto it = permissionStatuses->find(permission.type().id());
264 return it != permissionStatuses->end() ? it.value() : Qt::PermissionStatus::Undetermined;
269 Q_ASSERT(permission.type().isValid());
274 return callback(status);
276 const int requestedTypeId = permission.type().id();
277 if (requestedTypeId == qMetaTypeId<QCameraPermission>())
278 return requestMediaDevicePermission(wapiCamera, callback);
280 if (requestedTypeId == qMetaTypeId<QMicrophonePermission>())
281 return requestMediaDevicePermission(wapiMicrophone, callback);
283 if (requestedTypeId == qMetaTypeId<QLocationPermission>())
284 return requestGeolocationPermission(permission, callback);
286 (*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)