pr.nizar
pr.nizar

Reputation: 651

How does work setFeaturePermission member function in QWebEnginePage?

I'm using Qt5.5.0 and wanted to know how does QWebEnginePage::setFeaturePermission work?

In this scenario I wanted to grant the loaded page media audio video capture permission but it does not work:

#include <QtWebEngineWidgets>

int main(int argc, char **argv)
{
    QApplication app(argc, argv);
    QWebEngineView *view = new QWebEngineView();
    view->page()->setUrl(QUrl("http://127.0.0.1:3333/index.html"));
    view->page()->setFeaturePermission(view->page()->url(), QWebEnginePage::MediaAudioVideoCapture, QWebEnginePage::PermissionGrantedByUser);
    view->page()->settings()->setAttribute(QWebEngineSettings::LocalContentCanAccessRemoteUrls, true);
    view->page()->settings()->setAttribute(QWebEngineSettings::LocalStorageEnabled, true);
    view->show();
    return app.exec();
}

What's wrong with my code?

Upvotes: 2

Views: 1589

Answers (1)

Orest Hera
Orest Hera

Reputation: 6776

According to the official Qt documentation for QWebEnginePage::setFeaturePermission:

Note: Call this method on featurePermissionRequested() signal, as it is meant to serve pending feature requests only. Setting feature permissions ahead of a request has no effect.

So, it has effect only when a feature is requested, for example here is a part of basic Qt Widget Application where QWebEngineView is created in the main window constructor and the signal featurePermissionRequested of the page (QWebEnginePage) is connected to the appropriate slot:

// slot to handle permission request
void MainWindow::featurePermissionRequested(const QUrl & securityOrigin,
    QWebEnginePage::Feature feature)
{
    // print origin and feature
    qDebug() << securityOrigin << feature;

    // grant permission
    view->page()->setFeaturePermission(view->page()->url(),
        QWebEnginePage::MediaAudioCapture, QWebEnginePage::PermissionGrantedByUser);
}

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // store web view pointer as class the member QWebEngineView *view;
    view = new QWebEngineView();

    // add view widget to 'verticalLayout' added to UI in UI Design
    ui->verticalLayout->addWidget(view);

    // set view page
    view->page()->setUrl(QUrl("https://some_audio_capturing_site"));

    // connect page signal with 'this' object slot
    connect(view->page(),
        SIGNAL(featurePermissionRequested(const QUrl&, QWebEnginePage::Feature)),
        SLOT(featurePermissionRequested(const QUrl&, QWebEnginePage::Feature)));
}

I tested this example on one of audio capturing Web sites. When that site requests permission for microphone access it can be seen by debug print that the slot is triggered. It prints the site URL and 2 corresponding to QWebEnginePage::MediaAudioCapture. Nothing happens if the permission is not granted. However, after calling setFeaturePermission in that slot everything works as expected. The Web site is able to capture audio.

Upvotes: 4

Related Questions