Add support for clearing browsing data in wxWebView

This commit is contained in:
Tobias Taschner
2024-12-23 00:28:46 +01:00
committed by Vadim Zeitlin
parent 3cce10714e
commit f982139ffc
4 changed files with 134 additions and 2 deletions
+13
View File
@@ -94,6 +94,15 @@ enum wxWebViewUserScriptInjectionTime
wxWEBVIEW_INJECT_AT_DOCUMENT_END
};
enum wxWebViewBrowsingDataTypes
{
wxWEBVIEW_BROWSING_DATA_COOKIES = 1,
wxWEBVIEW_BROWSING_DATA_CACHE = 2,
wxWEBVIEW_BROWSING_DATA_DOM_STORAGE = 4,
wxWEBVIEW_BROWSING_DATA_OTHER = 8,
wxWEBVIEW_BROWSING_DATA_ALL = 16
};
class WXDLLIMPEXP_WEBVIEW wxWebViewHandlerRequest
{
public:
@@ -261,6 +270,9 @@ public:
virtual bool SetUserAgent(const wxString& userAgent) { wxUnusedVar(userAgent); return false; }
virtual wxString GetUserAgent() const;
virtual bool SetProxy(const wxString& proxy) { wxUnusedVar(proxy); return false; }
virtual bool ClearBrowsingData(int types = wxWEBVIEW_BROWSING_DATA_ALL,
wxDateTime since = wxDateTime((time_t)0) )
{ wxUnusedVar(types); wxUnusedVar(since); return false; }
// Script
virtual bool RunScript(const wxString& javascript, wxString* output = nullptr) const;
@@ -436,6 +448,7 @@ wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEBVIEW, wxEVT_WEBVIEW_TITLE_CHANGED, wxWe
wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEBVIEW, wxEVT_WEBVIEW_FULLSCREEN_CHANGED, wxWebViewEvent);
wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEBVIEW, wxEVT_WEBVIEW_SCRIPT_MESSAGE_RECEIVED, wxWebViewEvent);
wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEBVIEW, wxEVT_WEBVIEW_SCRIPT_RESULT, wxWebViewEvent);
wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_WEBVIEW, wxEVT_WEBVIEW_BROWSING_DATA_CLEARED, wxWebViewEvent);
typedef void (wxEvtHandler::*wxWebViewEventFunction)
(wxWebViewEvent&);
+54 -2
View File
@@ -161,6 +161,25 @@ enum wxWebViewIE_EmulationLevel
wxWEBVIEWIE_EMU_IE11_FORCE = 11001
};
/**
Types of browsing data that can be cleared.
@since 3.3.0
*/
enum wxWebViewBrowsingDataTypes
{
/** All stored and session cookies */
wxWEBVIEW_BROWSING_DATA_COOKIES = 1,
/** Cached data from disk and memory */
wxWEBVIEW_BROWSING_DATA_CACHE = 2,
/** All DOM Storage: File Systems, Indexed DB, Local Storage, Web SQL, Cache Storage */
wxWEBVIEW_BROWSING_DATA_DOM_STORAGE = 4,
/** Other browsing data like history, settings, auto fill, passwords, etc. */
wxWEBVIEW_BROWSING_DATA_OTHER = 8,
/** All browsing data */
wxWEBVIEW_BROWSING_DATA_ALL = 16
};
/**
A class describing the window information for a new child window.
@@ -989,6 +1008,10 @@ public:
Process a @c wxEVT_WEBVIEW_SCRIPT_RESULT event.
Available only in wxWidgets 3.1.6 or later. For usage details see
RunScriptAsync().
@event{wxEVT_WEBVIEW_BROWSING_DATA_CLEARED(id, func)}
Process a @c wxEVT_WEBVIEW_BROWSING_DATA_CLEARED event
only available in wxWidgets 3.3.0 or later. For usage details see
ClearBrowsingData().
@endEventTable
@since 2.9.3
@@ -1328,6 +1351,30 @@ public:
*/
virtual bool SetProxy(const wxString& proxy);
/**
Clears the browsing data of the web view.
This function clears the browsing data of the web view, such as cookies,
cache, history, etc. The exact data that is cleared depends on the
backend used.
This operation is asynchronous and may take some time to complete. When finished
@c wxEVT_WEBVIEW_BROWSING_DATA_CLEARED event is generated.
@param types The types of browsing data to clear. By default, it clears all types of browsing data.
@param since The time since when the browsing data should be cleared. By default, it clears all browsing data.
@return @true if the specified browsing data can be cleared by the backend. Completion will be notified by a
@c wxEVT_WEBVIEW_BROWSING_DATA_CLEARED event, @false otherwise.
@since 3.3.0
@note This is only implemented on the Edge, WebKit2GTK+ and macOS backends.
@see wxWebViewBrowsingDataTypes
*/
virtual bool ClearBrowsingData(int types = wxWEBVIEW_BROWSING_DATA_ALL,
wxDateTime since = wxDateTime((time_t)0) );
/**
@name Scripting
*/
@@ -1981,6 +2028,10 @@ public:
Process a @c wxEVT_WEBVIEW_SCRIPT_RESULT event
only available in wxWidgets 3.1.6 or later. For usage details see
wxWebView::RunScriptAsync().
@event{wxEVT_WEBVIEW_BROWSING_DATA_CLEARED(id, func)}
Process a @c wxEVT_WEBVIEW_BROWSING_DATA_CLEARED event
only available in wxWidgets 3.3.0 or later. For usage details see
wxWebView::ClearBrowsingData().
@endEventTable
@since 2.9.3
@@ -2038,8 +2089,9 @@ public:
wxWebViewWindowFeatures* GetTargetWindowFeatures() const;
/**
Returns @true the script execution failed. Only valid for events of type
@c wxEVT_WEBVIEW_SCRIPT_RESULT
Returns @true if the operation failed.
Only valid for events of type
@c wxEVT_WEBVIEW_SCRIPT_RESULT and @c wxEVT_WEBVIEW_BROWSING_DATA_CLEARED
@since 3.1.6
*/
+66
View File
@@ -119,6 +119,16 @@ public:
Private = 2
};
enum
{
ID_CLEAR_BROWSING_DATA_ALL = wxID_HIGHEST + 1,
ID_CLEAR_BROWSING_DATA_CACHE,
ID_CLEAR_BROWSING_DATA_COOKIES,
ID_CLEAR_BROWSING_DATA_DOM_STORAGE,
ID_CLEAR_BROWSING_DATA_OTHER,
ID_CLEAR_BROWSING_DATA_LAST_HOUR
};
WebFrame(const wxString& url, int flags = 0, wxWebViewWindowFeatures* windowFeatures = nullptr);
virtual ~WebFrame();
@@ -184,6 +194,7 @@ public:
void OnAddUserScript(wxCommandEvent& evt);
void OnSetCustomUserAgent(wxCommandEvent& evt);
void OnSetProxy(wxCommandEvent& evt);
void OnClearBrowsingData(wxCommandEvent& evt);
void OnClearSelection(wxCommandEvent& evt);
void OnDeleteSelection(wxCommandEvent& evt);
void OnSelectAll(wxCommandEvent& evt);
@@ -592,6 +603,15 @@ WebFrame::WebFrame(const wxString& url, int flags, wxWebViewWindowFeatures* wind
initShow();
}
m_browser->Bind(wxEVT_WEBVIEW_BROWSING_DATA_CLEARED, [](wxWebViewEvent& event) {
if (event.IsError())
wxLogError("Failed to clear browsing data");
else
wxLogMessage("Browsing data cleared");
event.Skip();
});
#ifndef __WXMAC__
//We register the wxfs:// protocol for testing purposes
m_browser->RegisterHandler(wxSharedPtr<wxWebViewHandler>(new wxWebViewArchiveHandler("wxfs")));
@@ -652,6 +672,17 @@ WebFrame::WebFrame(const wxString& url, int flags, wxWebViewWindowFeatures* wind
m_tools_menu->AppendSubMenu(m_tools_history_menu, "History");
// Browsing data menu
wxMenu* browsingDataMenu = new wxMenu();
browsingDataMenu->Append(ID_CLEAR_BROWSING_DATA_ALL, _("All"));
browsingDataMenu->Append(ID_CLEAR_BROWSING_DATA_CACHE, _("Cache"));
browsingDataMenu->Append(ID_CLEAR_BROWSING_DATA_COOKIES, _("Cookies"));
browsingDataMenu->Append(ID_CLEAR_BROWSING_DATA_DOM_STORAGE, _("DOM Storage"));
browsingDataMenu->Append(ID_CLEAR_BROWSING_DATA_OTHER, _("Other"));
browsingDataMenu->AppendSeparator();
browsingDataMenu->Append(ID_CLEAR_BROWSING_DATA_LAST_HOUR, _("All in last hour"));
m_tools_menu->AppendSubMenu(browsingDataMenu, _("Clear Browsing Data"));
//Create an editing menu
wxMenu* editmenu = new wxMenu();
m_edit_cut = editmenu->Append(wxID_ANY, _("Cut"));
@@ -821,6 +852,7 @@ WebFrame::WebFrame(const wxString& url, int flags, wxWebViewWindowFeatures* wind
Bind(wxEVT_MENU, &WebFrame::OnAddUserScript, this, addUserScript->GetId());
Bind(wxEVT_MENU, &WebFrame::OnSetCustomUserAgent, this, setCustomUserAgent->GetId());
Bind(wxEVT_MENU, &WebFrame::OnSetProxy, this, setProxy->GetId());
Bind(wxEVT_MENU, &WebFrame::OnClearBrowsingData, this, ID_CLEAR_BROWSING_DATA_ALL, ID_CLEAR_BROWSING_DATA_LAST_HOUR);
Bind(wxEVT_MENU, &WebFrame::OnClearSelection, this, m_selection_clear->GetId());
Bind(wxEVT_MENU, &WebFrame::OnDeleteSelection, this, m_selection_delete->GetId());
Bind(wxEVT_MENU, &WebFrame::OnSelectAll, this, selectall->GetId());
@@ -1569,6 +1601,40 @@ void WebFrame::OnSetProxy(wxCommandEvent& WXUNUSED(evt))
wxLogError("Could not set proxy");
}
void WebFrame::OnClearBrowsingData(wxCommandEvent &evt)
{
int dataTypes;
wxDateTime since((time_t) 0);
switch (evt.GetId())
{
case ID_CLEAR_BROWSING_DATA_ALL:
dataTypes = wxWEBVIEW_BROWSING_DATA_ALL;
break;
case ID_CLEAR_BROWSING_DATA_CACHE:
dataTypes = wxWEBVIEW_BROWSING_DATA_CACHE;
break;
case ID_CLEAR_BROWSING_DATA_COOKIES:
dataTypes = wxWEBVIEW_BROWSING_DATA_COOKIES;
break;
case ID_CLEAR_BROWSING_DATA_DOM_STORAGE:
dataTypes = wxWEBVIEW_BROWSING_DATA_DOM_STORAGE;
break;
case ID_CLEAR_BROWSING_DATA_OTHER:
dataTypes = wxWEBVIEW_BROWSING_DATA_OTHER;
break;
case ID_CLEAR_BROWSING_DATA_LAST_HOUR:
dataTypes = wxWEBVIEW_BROWSING_DATA_ALL;
since = wxDateTime::Now() - wxTimeSpan::Hour();
break;
default:
wxFAIL_MSG("Unexpected event ID");
return;
}
if (!m_browser->ClearBrowsingData(dataTypes, since))
wxLogError("Clearing this browsing data type is not supported by this backend");
}
void WebFrame::OnClearSelection(wxCommandEvent& WXUNUSED(evt))
{
m_browser->ClearSelection();
+1
View File
@@ -57,6 +57,7 @@ wxDEFINE_EVENT( wxEVT_WEBVIEW_TITLE_CHANGED, wxWebViewEvent );
wxDEFINE_EVENT( wxEVT_WEBVIEW_FULLSCREEN_CHANGED, wxWebViewEvent);
wxDEFINE_EVENT( wxEVT_WEBVIEW_SCRIPT_MESSAGE_RECEIVED, wxWebViewEvent);
wxDEFINE_EVENT( wxEVT_WEBVIEW_SCRIPT_RESULT, wxWebViewEvent);
wxDEFINE_EVENT( wxEVT_WEBVIEW_BROWSING_DATA_CLEARED, wxWebViewEvent);
// wxWebViewConfigurationDefault
class wxWebViewConfigurationImplDefault : public wxWebViewConfigurationImpl