c++ - 如何为 QWebEngineView 设置 OffTheRecord 配置文件?

标签 c++ linux qt qt5.10

如何为 QWebEngineView 设置 OffTheRecord 配置文件?

我在 Linux 上使用 QT5.10。

我将在具有只读文件系统的嵌入式环境中使用它,并且我需要防止 WebEngine 写入文件并在文件系统中创建文件夹。

#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();
}

最佳答案

尝试这个配置:

首先,禁用任何可能的 cookie。使用setPersistentCookiesPolicy并将其设置为 NoPersistentCookies

如果您可以写入给定文件夹,请尝试将所有临时文件保存在安全存储中:

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

这将使您能够控制 Web 引擎生成的所有临时文件。

如果没有,查看 Qt 存储库,您可以看到管理此状态的变量被控制在 BrowserContextAdapter 中。 ,如果在创建浏览器上下文时存储路径为空,则该变量设置为 false。

因此,如果您使用空 QString 作为路径创建自己的 QWebEngineProfile 并将其用作默认配置文件:

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

如果您使用它创建任何单个QWebEnginePage,这可以轻松完成。手动使用此配置文件并使用 setPage 在 QWebEngineView 中设置它:

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

关于c++ - 如何为 QWebEngineView 设置 OffTheRecord 配置文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47741388/

相关文章:

linux - 带有 setuid 二进制文件的 LD_PRELOAD

c++ - Qt 工作线程——我必须为每个特定任务继承还是可以使用回调?

python - 如何在 PyQT 的辅助显示器上显示一个窗口?

c++ - 使用 LLVM pass 添加内在函数

linux - 在端口 10334 连接到定位器的第二个 geode 容器的问题

c++ - 使用 C++ boost 库如何创建两个圆并将它们添加到 boost R 树中?

linux - 我可以将 O_DIRECT 用于写入请求以避免在电源故障期间丢失数据吗?

android - Qt for Android 错误 : Target id 'android--1' is not valid

c++ - friend 和模板类

c++ - 将返回指针的 C API 函数包装到宏调用中