Dmitriy Marinov
Dmitriy Marinov

Reputation: 45

How to set OffTheRecord profile for QWebEngineView?

How to setup OffTheRecord profile for QWebEngineView?

I use QT5.10 for Linux.

I am going to use it in embedded environment with read-only filesystem and I need to prevent WebEngine writing files and creating folders in filesystem.

#include <QApplication>
#include <QWebEngineView>
#include <QWebEngineSettings>
#include <QWebEngineProfile>

int main(int argc, char *argv[]) {

   QApplication a(argc, argv);
   QWebEngineView view;

   auto profile = view.page()->profile();

   profile->setHttpCacheType(QWebEngineProfile::MemoryHttpCache);
   profile->setPersistentCookiesPolicy(QWebEngineProfile::NoPersistentCookies);
   //profile->setPersistentStoragePath(nullptr);

   std::cout << "StoragePath: " << profile->persistentStoragePath().toStdString() << std::endl;
   std::cout << "isOffTheRecord: " << profile->isOffTheRecord() << std::endl;

   profile->settings()->setAttribute(QWebEngineSettings::AllowRunningInsecureContent, true); // Since Qt5.7
   profile->settings()->setAttribute(QWebEngineSettings::XSSAuditingEnabled, false);

   view.setUrl(QUrl(QStringLiteral("http://localhost/index.html")));

   view.resize(1920, 1080);
   view.show();

   return a.exec();
}

Upvotes: 2

Views: 2702

Answers (2)

MrEricSir
MrEricSir

Reputation: 8242

The documentation for QWebEngineProfile's default constructor states:

Constructs a new off-the-record profile with the parent parent.

An off-the-record profile leaves no record on the local machine, and has no persistent data or cache. Thus, the HTTP cache can only be in memory and the cookies can only be non-persistent. Trying to change these settings will have no effect.

Once you've created a default QWebEngineProfile, pass it to a QWebEnginePage and set that as the page in your QWebEngineView.

Here's a simple example that compiles and runs (tested on Mac OS):

#include <QApplication>
#include <QWebEngineView>
#include <QWebEngineSettings>
#include <QWebEnginePage>
#include <QWebEngineProfile>
#include <QDebug>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QWebEngineView view;
    QWebEngineProfile profile;
    QWebEnginePage page(&profile);

    qDebug() << "StoragePath:" << profile.persistentStoragePath();
    qDebug() << "isOffTheRecord:" << profile.isOffTheRecord();

    view.setPage(&page);
    view.setUrl(QUrl(QStringLiteral("http://www.stackoverflow.com/")));
    view.show();

    return a.exec();
}

When running the above you should see this appear in standard out:

StoragePath: ""
isOffTheRecord: true

Upvotes: 1

mohabouje
mohabouje

Reputation: 4050

Try this configuration:

First of all, disable any possible cookie. Use setPersistentCookiesPolicy and set it to NoPersistentCookies

If you can write in to a given folder, try to save all temporal files in a secure storage:

auto *profile = QWebEngineProfile::defaultProfile();
profile->setCachePath("yourfolder");
profile->setPersistentStoragePath("yourfolder");

This should give you the control of all the temporal files that are generated by the Web Engine.

If not, taking a look in to Qt repo, you can see that the variable that manage this state is controlled in BrowserContextAdapter, this variable is set up to false, if the storage path is empty while creating the browser context.

So if you create your own QWebEngineProfile with an empty QString as path and use it as default profile:

QWebEngineProfile* profile = new QWebEngineProfile(QString(), parent)
std::cout << "isOffTheRecord: " << profile->isOffTheRecord() << std::endl; // Should return true

This can be done easily if you use it to create any single QWebEnginePage manually using this profile and set it in your QWebEngineView using setPage:

engineview->setPage(new QWebEnginePage(profile, parent));

Upvotes: 2

Related Questions