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)
141 updatePermission(device, wapiGranted, cb);
143 queryCallbacks.catchFunc = [device, cb](val error)
145 if (error[
"name"].as<std::string>() ==
"NotAllowedError")
146 return updatePermission(device, wapiDenied, cb);
147 updatePermission(device, wapiPrompt, cb);
150 val constraint = val::object();
151 if (device == wapiCamera)
152 constraint.set(
"video",
true);
154 constraint.set(
"audio",
true);
156 qstdweb::Promise::make(mediaDevices,
QStringLiteral(
"getUserMedia"), queryCallbacks, constraint);
159 using GeoRequest = std::pair<QPermission, PermissionCallback>;
162 bool processingLocationRequest =
false;
164 void processNextGeolocationRequest();
166 void geolocationSuccess(val position)
169 Q_ASSERT(geolocationRequestQueue->size());
171 processingLocationRequest =
false;
173 auto cb = geolocationRequestQueue->front().second;
174 geolocationRequestQueue->pop_front();
175 updatePermission(wapiGeolocation, wapiGranted, cb);
176 processNextGeolocationRequest();
179 void geolocationError(val error)
181 Q_ASSERT(geolocationRequestQueue->size());
183 static int deniedError = []
185 val posErr = val::global(
"GeolocationPositionError");
186 if (posErr.isUndefined() || posErr.isNull())
188 return posErr[
"PERMISSION_DENIED"].as<
int>();
191 processingLocationRequest =
false;
193 auto cb = geolocationRequestQueue->front().second;
194 geolocationRequestQueue->pop_front();
196 const int errorCode = error[
"code"].as<
int>();
197 updatePermission(wapiGeolocation, errorCode == deniedError ? wapiDenied : wapiPrompt, cb);
198 processNextGeolocationRequest();
201 EMSCRIPTEN_BINDINGS(qt_permissions) {
202 function(
"qtLocationSuccess", &geolocationSuccess);
203 function(
"qtLocationError", &geolocationError);
206 void processNextGeolocationRequest()
208 if (processingLocationRequest)
211 if (geolocationRequestQueue->empty())
214 processingLocationRequest =
true;
216 val geolocation = val::global(
"navigator")[
"geolocation"];
217 Q_ASSERT(!geolocation.isUndefined());
218 Q_ASSERT(!geolocation.isNull());
220 const auto &permission = geolocationRequestQueue->front().first;
221 const auto locationPermission = *permission.value<QLocationPermission>();
222 const bool highAccuracy = locationPermission.accuracy() == QLocationPermission::Precise;
224 val options = val::object();
225 options.set(
"enableHighAccuracy", highAccuracy ?
true :
false);
226 geolocation.call<
void>(
"getCurrentPosition", val::module_property(
"qtLocationSuccess"),
227 val::module_property(
"qtLocationError"), options);
230 void requestGeolocationPermission(
const QPermission &permission,
const PermissionCallback &cb)
233 Q_UNUSED(permission);
236 val geolocation = val::global(
"navigator")[
"geolocation"];
237 if (geolocation.isUndefined() || geolocation.isNull())
240 if (processingLocationRequest)
241 qCWarning(lcPermissions,
"Permission to access location requested, while another request is in progress");
243 geolocationRequestQueue->push_back(std::make_pair(permission, cb));
244 processNextGeolocationRequest();
252 const auto it = permissionStatuses->find(permission.type().id());
253 return it != permissionStatuses->end() ? it.value() : Qt::PermissionStatus::Undetermined;
258 Q_ASSERT(permission.type().isValid());
263 return callback(status);
265 const int requestedTypeId = permission.type().id();
266 if (requestedTypeId == qMetaTypeId<QCameraPermission>())
267 return requestMediaDevicePermission(wapiCamera, callback);
269 if (requestedTypeId == qMetaTypeId<QMicrophonePermission>())
270 return requestMediaDevicePermission(wapiMicrophone, callback);
272 if (requestedTypeId == qMetaTypeId<QLocationPermission>())
273 return requestGeolocationPermission(permission, callback);
275 (*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)