【问题标题】:How to set OffTheRecord profile for QWebEngineView?如何为 QWebEngineView 设置 OffTheRecord 配置文件?
【发布时间】:2017-12-10 17:18:43
【问题描述】:

如何为 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();
}

【问题讨论】:

    标签: c++ linux qt qt5.10


    【解决方案1】:

    试试这个配置:

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

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

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

    这应该让您可以控制 Web 引擎生成的所有临时文件。

    如果没有,查看Qt repo,可以看到管理这个状态的变量在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));
    

    【讨论】:

      【解决方案2】:

      QWebEngineProfile 的 default constructor 的文档说明:

      与父级父级构建一个新的非记录配置文件。

      非记录配置文件不会在本地计算机上留下任何记录,并且 没有持久性数据或缓存。因此,HTTP 缓存只能在 内存和cookies只能是非持久的。试图改变 这些设置将不起作用。

      创建默认QWebEngineProfile 后,将其传递给QWebEnginePage 并将其设置为QWebEngineView 中的页面。

      这是一个编译和运行的简单示例(在 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();
      }
      

      当运行上述程序时,您应该会看到它出现在标准输出中:

      StoragePath: ""
      isOffTheRecord: true
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-10-05
        • 1970-01-01
        • 2023-01-30
        • 1970-01-01
        • 1970-01-01
        • 2012-09-17
        • 1970-01-01
        • 2019-10-17
        相关资源
        最近更新 更多