Skip to content

Commit 167e653

Browse files
committed
Plugins::WebBrowser: create incognito window
1 parent d1ec85a commit 167e653

File tree

5 files changed

+52
-27
lines changed

5 files changed

+52
-27
lines changed

Plugins/WebBrowser/FrmWebBrowser.cpp

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ CFrmWebBrowser::CFrmWebBrowser(CParameterWebBrowser *pPara, QWidget *parent)
3333
, m_pFavAction(nullptr)
3434
, m_pGo(nullptr)
3535
, m_pAddPage(nullptr)
36+
, m_pAddPageIncognito(nullptr)
3637
, m_pAddWindow(nullptr)
38+
, m_pAddWindowIncognito(nullptr)
3739
, m_pDownload(nullptr)
3840
, m_pInspector(nullptr)
3941
, m_pUrl(nullptr)
@@ -118,7 +120,7 @@ CFrmWebBrowser::CFrmWebBrowser(CParameterWebBrowser *pPara, QWidget *parent)
118120

119121
m_pAddPage = m_pToolBar->addAction(QIcon::fromTheme("add"), tr("Add tab page"),
120122
this, [&](){
121-
auto pView = createWindow(QWebEnginePage::WebBrowserTab);
123+
CreateWindow(QWebEnginePage::WebBrowserTab);
122124
if(!m_pPara->GetTabUrl().isEmpty()) {
123125
m_pUrlLineEdit->setText(m_pPara->GetTabUrl());
124126
slotReturnPressed();
@@ -185,34 +187,37 @@ CFrmWebBrowser::~CFrmWebBrowser()
185187
}
186188
}
187189

188-
QWebEngineView* CFrmWebBrowser::createWindow(QWebEnginePage::WebWindowType type)
190+
QWebEngineView* CFrmWebBrowser::CreateWindow(
191+
QWebEnginePage::WebWindowType type, bool offTheRecord)
189192
{
190193
qDebug(log) << "Create window:" << type;
191194

192195
CFrmWebView* pView = nullptr;
193196
switch (type) {
194197
case QWebEnginePage::WebBrowserTab: {
195-
auto pTab = CreateTab(&pView);
198+
auto pTab = CreateTab(&pView, offTheRecord);
196199
int index = m_pTab->addTab(pTab, pView->favIcon(), tr("New page"));
197200
if(-1 < index)
198201
m_pTab->setCurrentIndex(index);
199202
break;
200203
}
201204
case QWebEnginePage::WebBrowserBackgroundTab: {
202-
auto pTab = CreateTab(&pView);
205+
auto pTab = CreateTab(&pView, offTheRecord);
203206
int index = m_pTab->currentIndex();
204207
m_pTab->addTab(pTab, pView->favIcon(), tr("New page"));
205208
if(-1 < index)
206209
m_pTab->setCurrentIndex(index);
207210
break;
208211
}
209212
case QWebEnginePage::WebBrowserWindow: {
210-
auto pWin = CreateTab(&pView);
213+
auto pWin = CreateTab(&pView, offTheRecord);
211214
pWin->show();
212215
break;
213216
}
214217
case QWebEnginePage::WebDialog: {
215218
qDebug(log) << "Don't support type:" << type;
219+
auto pWin = CreateTab(&pView, offTheRecord);
220+
pWin->show();
216221
}
217222
}
218223

@@ -298,9 +303,9 @@ void CFrmWebBrowser::SetConnect(CFrmWebView* pWeb)
298303
m_pProgressBar->setValue(pWeb->progress());
299304
}
300305

301-
QWebEngineProfile* CFrmWebBrowser::GetProfile()
306+
QWebEngineProfile* CFrmWebBrowser::GetProfile(bool offTheRecord)
302307
{
303-
if (!g_profile) {
308+
if (!offTheRecord && !g_profile) {
304309

305310
#if QT_VERSION >= QT_VERSION_CHECK(6, 9, 0)
306311
const QString name = "io.github.KangLin.RabbitRemoteControl."
@@ -330,14 +335,14 @@ QWebEngineProfile* CFrmWebBrowser::GetProfile()
330335
<< "Storage name:" << g_profile->storageName()
331336
<< "Is off the Record:" << g_profile->isOffTheRecord();
332337
}
333-
return g_profile? g_profile.get() : QWebEngineProfile::defaultProfile();
338+
return offTheRecord ? QWebEngineProfile::defaultProfile() : g_profile.get();
334339
}
335340

336-
CFrmWebView *CFrmWebBrowser::CreateWebView()
341+
CFrmWebView *CFrmWebBrowser::CreateWebView(bool offTheRecord)
337342
{
338343
auto pView = new CFrmWebView(this);
339344
if(pView) {
340-
auto profile = GetProfile();
345+
auto profile = GetProfile(offTheRecord);
341346
#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
342347
profile->setPersistentPermissionsPolicy(QWebEngineProfile::PersistentPermissionsPolicy::AskEveryTime);
343348
#endif
@@ -347,7 +352,7 @@ CFrmWebView *CFrmWebBrowser::CreateWebView()
347352
return pView;
348353
}
349354

350-
QWidget* CFrmWebBrowser::CreateTab(CFrmWebView **view)
355+
QWidget* CFrmWebBrowser::CreateTab(CFrmWebView **view, bool offTheRecord)
351356
{
352357
QSplitter *pSplitter = new QSplitter(Qt::Vertical, this);
353358
if(pSplitter) {
@@ -357,16 +362,16 @@ QWidget* CFrmWebBrowser::CreateTab(CFrmWebView **view)
357362
if(*view) {
358363
pWeb = *view;
359364
} else {
360-
pWeb = CreateWebView();
365+
pWeb = CreateWebView(offTheRecord);
361366
*view = pWeb;
362367
}
363368
} else {
364-
pWeb = CreateWebView();
369+
pWeb = CreateWebView(offTheRecord);
365370
}
366371
if(pWeb) {
367372
pSplitter->addWidget(pWeb);
368373
SetConnect(pWeb);
369-
auto pDevTools = CreateWebView();
374+
auto pDevTools = CreateWebView(offTheRecord);
370375
if(pDevTools) {
371376
pDevTools->hide();
372377
pSplitter->addWidget(pDevTools);
@@ -440,13 +445,30 @@ int CFrmWebBrowser::InitMenu(QMenu *pMenu)
440445

441446
pMenu->addSeparator();
442447
pMenu->addAction(m_pAddPage);
448+
m_pAddPageIncognito = pMenu->addAction(
449+
QIcon::fromTheme("add"), tr("Add incognito tab"),
450+
this, [&](){
451+
CreateWindow(QWebEnginePage::WebBrowserTab, true);
452+
if(!m_pPara->GetTabUrl().isEmpty()) {
453+
m_pUrlLineEdit->setText(m_pPara->GetTabUrl());
454+
slotReturnPressed();
455+
}
456+
});
457+
m_pAddPageIncognito->setStatusTip(m_pAddPageIncognito->text());
443458
m_pAddWindow = pMenu->addAction(
444459
QIcon::fromTheme("add"), tr("Add window"),
445460
this, [&](){
446-
createWindow(QWebEnginePage::WebBrowserWindow);
461+
CreateWindow(QWebEnginePage::WebBrowserWindow);
447462
});
448463
m_pAddWindow->setVisible(false);
449464
m_pAddWindow->setStatusTip(m_pAddWindow->text());
465+
m_pAddWindowIncognito = pMenu->addAction(
466+
QIcon::fromTheme("add"), tr("Add Incognito Window"),
467+
this, [&](){
468+
CreateWindow(QWebEnginePage::WebBrowserWindow, true);
469+
});
470+
m_pAddWindowIncognito->setVisible(false);
471+
m_pAddWindowIncognito->setStatusTip(m_pAddWindowIncognito->text());
450472

451473
pMenu->addSeparator();
452474
m_pFind = pMenu->addAction(
@@ -660,7 +682,7 @@ void CFrmWebBrowser::slotReturnPressed()
660682
emit sigInformation(u.toString());
661683
CFrmWebView* pWeb = CurrentView();
662684
if(!pWeb)
663-
pWeb = qobject_cast<CFrmWebView*>(createWindow(QWebEnginePage::WebBrowserTab));
685+
pWeb = qobject_cast<CFrmWebView*>(CreateWindow(QWebEnginePage::WebBrowserTab));
664686
pWeb->load(u);
665687
if(m_pGo->isVisible())
666688
m_pGo->setVisible(false);
@@ -691,7 +713,7 @@ int CFrmWebBrowser::Load(QSettings &set)
691713
for(int i = 0; i < nCount; i++)
692714
{
693715
QString u = set.value(QString::number(i)).toString();
694-
auto pView = createWindow(QWebEnginePage::WebBrowserTab);
716+
auto pView = CreateWindow(QWebEnginePage::WebBrowserTab);
695717
pView->load(QUrl(u));
696718
}
697719
if(-1 < nCurrent && m_pTab->count() > nCurrent)

Plugins/WebBrowser/FrmWebBrowser.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ class CFrmWebBrowser : public QWidget
2121
explicit CFrmWebBrowser(CParameterWebBrowser* pPara, QWidget *parent = nullptr);
2222
virtual ~CFrmWebBrowser();
2323

24-
QWebEngineView *createWindow(QWebEnginePage::WebWindowType type);
2524
int InitMenu(QMenu* pMenu);
26-
int Start();
27-
int Stop();
2825
int Load(QSettings &set);
2926
int Save(QSettings &set);
27+
int Start();
28+
int Stop();
29+
QWebEngineView *CreateWindow(QWebEnginePage::WebWindowType type, bool offTheRecord = false);
3030

3131
Q_SIGNALS:
3232
void sigInformation(const QString& szInfo);
@@ -43,9 +43,9 @@ private Q_SLOTS:
4343
private:
4444
void EnableAction(bool enable);
4545
void SetConnect(CFrmWebView* pWeb);
46-
[[nodiscard]] QWebEngineProfile* GetProfile();
47-
[[nodiscard]] CFrmWebView *CreateWebView();
48-
[[nodiscard]] QWidget* CreateTab(/*[in/out]*/CFrmWebView **view = nullptr);
46+
[[nodiscard]] QWebEngineProfile* GetProfile(bool offTheRecord = false);
47+
[[nodiscard]] CFrmWebView *CreateWebView(bool offTheRecord = false);
48+
[[nodiscard]] QWidget* CreateTab(/*[in/out]*/CFrmWebView **view = nullptr, bool offTheRecord = false);
4949
enum class ViewType{
5050
Web = 0,
5151
DevTools = 1
@@ -71,7 +71,9 @@ private Q_SLOTS:
7171
QAction* m_pFavAction;
7272
QAction* m_pGo;
7373
QAction* m_pAddPage;
74+
QAction* m_pAddPageIncognito;
7475
QAction* m_pAddWindow;
76+
QAction* m_pAddWindowIncognito;
7577
QAction* m_pDownload;
7678
QAction* m_pInspector;
7779
QAction* m_pUrl;

Plugins/WebBrowser/FrmWebView.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ QIcon CFrmWebView::favIcon() const
163163
QWebEngineView *CFrmWebView::createWindow(QWebEnginePage::WebWindowType type)
164164
{
165165
if(m_pBrowser)
166-
return m_pBrowser->createWindow(type);
166+
return m_pBrowser->CreateWindow(type);
167167
return this;
168168
}
169169

Plugins/WebBrowser/OperateWebBrowser.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ int COperateWebBrowser::Clean()
8888
qDebug(log) << Q_FUNC_INFO;
8989
int nRet = 0;
9090
if(m_pWeb) {
91-
delete m_pWeb;
91+
m_pWeb->deleteLater();
9292
m_pWeb = nullptr;
9393
}
9494
return nRet;
@@ -100,7 +100,8 @@ int COperateWebBrowser::Start()
100100
int nRet = 0;
101101
if(m_pWeb)
102102
nRet = m_pWeb->Start();
103-
emit sigRunning();
103+
if(!nRet)
104+
emit sigRunning();
104105
return nRet;
105106
}
106107

docs/Compile/Linux_zh_CN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@
184184

185185
- 在 QtWebEngine 程序中,访问:
186186
- https://www.webmfiles.org/demo-files/ 上传或播放 H264/H265 视频文件,测试支持性
187-
- https://html5test.com/
187+
- https://html5test.com/
188188
- https://webrtc.github.io/test-pages/
189189
- https://browserleaks.com/webrtc
190190

0 commit comments

Comments
 (0)