4#include <private/qpermissions_p.h>
5#include <private/qstdweb_p.h>
16#include <emscripten.h>
17#include <emscripten/bind.h>
18#include <emscripten/val.h>
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,
45 val permissions = val::global(
"navigator")[
"permissions"];
46 if (permissions.isUndefined() || permissions.isNull())
50 callbacks.thenFunc = [permissionName](
val permissionState)
52 updatePermission(permissionName, permissionState[
"state"].as<std::string>(), {});
56 updatePermission(permissionName, wapiDenied, {});
60 query.set(
"name",
val(permissionName));
65 void checkPermissions()
72 void bootstrapCheckPermissions()
79 int permissionTypeIdFromString(
const std::string &permission)
81 if (permission == wapiCamera || permission == wapiVideoCapture)
82 return qMetaTypeId<QCameraPermission>();
83 if (permission == wapiMicrophone || permission == wapiAudioCapture)
84 return qMetaTypeId<QMicrophonePermission>();
85 if (permission == wapiGeolocation)
86 return qMetaTypeId<QLocationPermission>();
88 qCWarning(lcPermissions,
"Unknown permission type '%s'", permission.c_str());
95 if (
state == wapiGranted)
97 if (
state == wapiDenied)
99 if (
state == wapiPrompt)
102 qCWarning(lcPermissions,
"Unknown permission state '%s'",
state.c_str());
114 const int type = permissionTypeIdFromString(
name);
118 const auto status = permissionStatusFromString(
state);
119 (*permissionStatuses)[
type] = status;
129 val mediaDevices = val::global(
"navigator")[
"mediaDevices"];
130 if (mediaDevices.isUndefined())
136 updatePermission(
device, wapiGranted,
cb);
140 if (
error[
"name"].as<std::string>() ==
"NotAllowedError")
141 return updatePermission(
device, wapiDenied,
cb);
142 updatePermission(
device, wapiPrompt,
cb);
145 val constraint = val::object();
147 constraint.set(
"video",
true);
149 constraint.set(
"audio",
true);
154 using GeoRequest = std::pair<QPermission, PermissionCallback>;
157 bool processingLocationRequest =
false;
159 void processNextGeolocationRequest();
164 Q_ASSERT(geolocationRequestQueue->size());
166 processingLocationRequest =
false;
168 auto cb = geolocationRequestQueue->front().second;
169 geolocationRequestQueue->pop_front();
170 updatePermission(wapiGeolocation, wapiGranted,
cb);
171 processNextGeolocationRequest();
176 Q_ASSERT(geolocationRequestQueue->size());
178 static int deniedError = []
180 val posErr = val::global(
"GeolocationPositionError");
181 if (posErr.isUndefined() || posErr.isNull())
183 return posErr[
"PERMISSION_DENIED"].as<
int>();
186 processingLocationRequest =
false;
188 auto cb = geolocationRequestQueue->front().second;
189 geolocationRequestQueue->pop_front();
191 const int errorCode =
error[
"code"].as<
int>();
192 updatePermission(wapiGeolocation, errorCode == deniedError ? wapiDenied : wapiPrompt,
cb);
193 processNextGeolocationRequest();
197 function(
"qtLocationSuccess", &geolocationSuccess);
198 function(
"qtLocationError", &geolocationError);
201 void processNextGeolocationRequest()
203 if (processingLocationRequest)
206 if (geolocationRequestQueue->empty())
209 processingLocationRequest =
true;
211 val geolocation = val::global(
"navigator")[
"geolocation"];
212 Q_ASSERT(!geolocation.isUndefined());
215 const auto &permission = geolocationRequestQueue->front().first;
219 val options = val::object();
220 options.set(
"enableHighAccuracy", highAccuracy ?
true :
false);
221 geolocation.call<
void>(
"getCurrentPosition", val::module_property(
"qtLocationSuccess"),
222 val::module_property(
"qtLocationError"), options);
231 val geolocation = val::global(
"navigator")[
"geolocation"];
232 if (geolocation.isUndefined() || geolocation.isNull())
235 if (processingLocationRequest)
236 qCWarning(lcPermissions,
"Permission to access location requested, while another request is in progress");
238 geolocationRequestQueue->push_back(std::make_pair(permission,
cb));
239 processNextGeolocationRequest();
247 const auto it = permissionStatuses->
find(permission.
type().
id());
258 return callback(status);
260 const int requestedTypeId = permission.
type().
id();
261 if (requestedTypeId == qMetaTypeId<QCameraPermission>())
262 return requestMediaDevicePermission(wapiCamera, callback);
264 if (requestedTypeId == qMetaTypeId<QMicrophonePermission>())
265 return requestMediaDevicePermission(wapiMicrophone, callback);
267 if (requestedTypeId == qMetaTypeId<QLocationPermission>())
268 return requestGeolocationPermission(permission, callback);
IOBluetoothDevice * device
Access the user's location.
Q_CORE_EXPORT Accuracy accuracy() const
Returns the accuracy of the request.
\inmodule QtCore \inheaderfile QPermissions
QMetaType type() const
Returns the type of the permission.
iterator find(const T &value)
bool singleShot
whether the timer is a single-shot timer
QSet< QString >::iterator it
void requestPermission(const QPermission &permission, const PermissionCallback &callback)
std::function< void(Qt::PermissionStatus)> PermissionCallback
Qt::PermissionStatus checkPermission(const QPermission &permission)
Combined button and popup list for selecting options.
void make(emscripten::val target, QString methodName, PromiseCallbacks callbacks, Args... args)
Q_CONSTRUCTOR_FUNCTION(initializeStandardUserDefaults)
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction function
DBusConnection const char DBusError * error
#define Q_GLOBAL_STATIC(TYPE, NAME,...)
#define qCWarning(category,...)
#define qCDebug(category,...)
static qreal position(const QQuickItem *item, QQuickAnchors::Anchor anchorLine)
SSL_CTX int(* cb)(SSL *ssl, unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg)
PromiseCallbacks callbacks
#define QStringLiteral(str)
EMSCRIPTEN_BINDINGS(qtClipboardModule)
std::function< void(emscripten::val)> thenFunc
std::function< void(emscripten::val)> catchFunc