RADLib
RADical C++ application framework
RADWeb.h
Go to the documentation of this file.
1 #ifndef RADWEB_H
2 #define RADWEB_H
3 
10 #pragma once
11 
12 #include <RADCore/RADCore.h>
13 
14 #include <cstdint>
15 #include <memory>
16 #include <string>
17 #include <vector>
18 
19 namespace RADWeb {
20 
22  enum class RADWebCookiePolicy {
29  };
30 
34  Ask,
36  Allow,
38  Deny
39  };
40 
44  int id = 0;
46  std::string uri;
48  std::string destination;
50  std::string suggestedFileName;
52  double progress = 0.0;
54  uint64_t receivedBytes = 0;
56  bool finished = false;
57  };
58 
62  std::string url;
64  std::string title;
66  int64_t lastVisitedUnixMs = 0;
68  int64_t visitCount = 0;
69  };
70 
72  struct RADWebBookmark {
74  int64_t id = 0;
76  std::string url;
78  std::string title;
80  int64_t createdUnixMs = 0;
81  };
82 
84  struct RADWebTabInfo {
86  int id = 0;
88  int index = -1;
90  std::string url;
92  std::string title;
94  bool active = false;
95  };
96 
98  struct RADWebSettings {
100  bool javaScriptEnabled = true;
102  bool imagesEnabled = true;
106  bool webGlEnabled = true;
118  std::string downloadsDirectory;
119  };
120 
124  int width = 1200;
126  int height = 800;
128  std::string title = "RADWeb Browser";
130  std::string profileDirectory;
132  bool ephemeralProfile = false;
134  bool navigationBarVisible = true;
137  };
138 
141  public:
142  RADEvents:
144  RAD_EVENT(loadStarted, (std::string));
146  RAD_EVENT(loadFinished, (std::string));
148  RAD_EVENT(loadFailed, (std::string, std::string));
150  RAD_EVENT(titleChanged, (std::string));
152  RAD_EVENT(uriChanged, (std::string));
154  RAD_EVENT(javaScriptFinished, (std::string));
156  RAD_EVENT(javaScriptFailed, (std::string));
158  RAD_EVENT(errorOccurred, (std::string));
172  RAD_EVENT(downloadFailed, (int, std::string));
174  RAD_EVENT(permissionRequested, (std::string, std::string));
181 
186 
188  void setProfileDirectory(const std::string& directory);
190  const std::string& profileDirectory() const;
192  void setEphemeralProfile(bool enabled);
194  bool ephemeralProfile() const;
195 
197  bool create(int width = 1200, int height = 800, const std::string& title = "RADWeb Browser");
199  bool create(const RADWebWindowOptions& options);
201  bool isCreated() const;
202 
204  void setNavigationBarVisible(bool visible);
206  bool navigationBarVisible() const;
207 
209  int newTab(const std::string& url = "about:blank", bool makeActive = true);
211  bool closeTab(int index);
213  bool duplicateTab(int index, bool makeActive = true);
215  bool moveTab(int fromIndex, int toIndex);
217  int tabCount() const;
219  int activeTabIndex() const;
223  std::vector<RADWebTabInfo> tabs() const;
225  bool setActiveTab(int index);
226 
228  void loadUrl(const std::string& url);
230  void loadUrl(int tabIndex, const std::string& url);
232  void loadHtml(const std::string& html, const std::string& baseUri = "about:blank");
234  void evaluateJavaScript(const std::string& script);
235 
237  void show();
239  void close();
241  void run();
243  void quit();
244 
246  bool canGoBack() const;
248  bool canGoForward() const;
250  void goBack();
252  void goForward();
254  void reload();
256  void stop();
257 
263  void showHistory();
269  void showSettings();
271  void showDevTools();
272 
274  void setZoomLevel(double zoomLevel);
276  double zoomLevel() const;
277 
279  void searchText(const std::string& text, bool caseSensitive = false, bool wrap = true, unsigned maxMatchCount = 1000);
281  void searchNext();
285  void clearSearch();
286 
288  void setJavaScriptEnabled(bool enabled);
290  void setImagesEnabled(bool enabled);
292  void setDeveloperExtrasEnabled(bool enabled);
294  void setWebGlEnabled(bool enabled);
298  void setHardwareAccelerationEnabled(bool enabled);
303 
305  void setDownloadsDirectory(const std::string& directory);
307  const std::string& downloadsDirectory() const;
308 
314  void rememberPermissionDecision(const std::string& origin, const std::string& permission, bool allowed);
317 
323  void clearCookies();
325  void clearCache();
326 
328  void setSessionRestoreEnabled(bool enabled);
330  bool sessionRestoreEnabled() const;
332  bool saveSession();
335 
337  std::vector<RADWebHistoryItem> history(size_t limit = 100) const;
339  void clearHistory();
341  bool addBookmark(const std::string& url, const std::string& title = {});
343  bool removeBookmark(int64_t id);
345  std::vector<RADWebBookmark> bookmarks() const;
347  std::vector<RADWebDownloadInfo> downloads(size_t limit = 100) const;
349  bool cancelDownload(int id);
351  bool openDownload(int id);
356 
358  std::string uri() const;
360  std::string title() const;
361 
363  void* nativeWindow() const;
365  void* nativeWebView() const;
366 
367  private:
368  struct Impl;
369  std::shared_ptr<Impl> pImpl_;
370  };
371 
372 } // namespace RADWeb
373 
374 #endif
#define RAD_EVENT(Name, Signature)
Declares a typed RADCore event; example: RAD_EVENT(done, (int)).
Definition: RADCore.h:65
#define RADEvents
Marks a public event declaration block in RADObject-derived classes.
Definition: RADCore.h:43
Base class for event receivers, senders, and thread-affinity aware objects.
Definition: RADCore.h:931
WebKitGTK-backed browser window with tabs, history, downloads, and settings.
Definition: RADWeb.h:140
void setPermissionPolicy(RADWebPermissionPolicy policy)
Sets default permission request policy.
void clearSearch()
Clears active search highlighting.
RADWebBrowserWindow()
Creates an uninitialized browser window wrapper.
void goBack()
Navigates the active tab backward.
std::string title() const
Returns current active tab title.
void setDeveloperExtrasEnabled(bool enabled)
Enables or disables developer extras.
double zoomLevel() const
Returns active tab zoom level.
RADCore::Event< void(std::string) > errorOccurred
Raised for browser-level setup/runtime errors.
Definition: RADWeb.h:158
bool navigationBarVisible() const
Returns true when the navigation bar is visible.
bool canGoForward() const
Returns true when the active tab can go forward.
RADCore::Event< void(int) > tabAdded
Raised with tab id when a tab is added.
Definition: RADWeb.h:160
void setWebGlEnabled(bool enabled)
Enables or disables WebGL.
void * nativeWebView() const
Returns the native WebKit web view pointer for advanced integration.
RADCore::Event< void(RADWeb::RADWebDownloadInfo) > downloadFinished
Raised when a download finishes.
Definition: RADWeb.h:170
RADWebPermissionPolicy permissionPolicy() const
Returns default permission request policy.
void setNavigationBarVisible(bool visible)
Shows or hides the built-in navigation bar.
void setJavaScriptEnabled(bool enabled)
Enables or disables JavaScript.
bool sessionRestoreEnabled() const
Returns true when session restore is enabled.
bool saveSession()
Saves current window tabs/session state.
std::string uri() const
Returns current active tab URI.
bool cancelDownload(int id)
Cancels an active download by id.
RADWebCookiePolicy cookiePolicy() const
Returns WebKit cookie policy.
RADCore::Event< void(int) > tabClosed
Raised with tab id when a tab is closed.
Definition: RADWeb.h:162
void setMediaPlaybackRequiresUserGesture(bool required)
Sets whether media playback requires user gesture.
void clearHistory()
Deletes all stored history.
RADCore::Event< void(RADWeb::RADWebDownloadInfo) > downloadStarted
Raised when a download starts.
Definition: RADWeb.h:166
void goForward()
Navigates the active tab forward.
void setDownloadsDirectory(const std::string &directory)
Sets directory for future downloads.
void reload()
Reloads the active tab.
void showHistory()
Opens the history pane.
RADCore::Event< void(std::string) > loadStarted
Raised when the active tab starts loading url.
Definition: RADWeb.h:144
void stop()
Stops loading the active tab.
std::vector< RADWebTabInfo > tabs() const
Returns snapshots of all tabs.
RADCore::Event< void() > bookmarksChanged
Raised when bookmarks change.
Definition: RADWeb.h:178
void focusUrlEntry()
Focuses the built-in URL entry.
bool create(int width=1200, int height=800, const std::string &title="RADWeb Browser")
Creates the browser window with basic dimensions and title.
void run()
Runs the GTK/WebKit event loop.
bool setActiveTab(int index)
Activates the tab at index.
const std::string & profileDirectory() const
Returns the configured profile directory.
~RADWebBrowserWindow() override
Destroys the browser window and native resources.
std::vector< RADWebDownloadInfo > downloads(size_t limit=100) const
Returns recent downloads up to limit.
void setEphemeralProfile(bool enabled)
Enables or disables ephemeral profile mode before create().
bool ephemeralProfile() const
Returns true when using an ephemeral profile.
RADCore::Event< void(RADWeb::RADWebDownloadInfo) > downloadUpdated
Raised as download progress changes.
Definition: RADWeb.h:168
void close()
Closes the browser window.
void showBookmarks()
Opens the bookmarks pane.
RADCore::Event< void(std::string) > javaScriptFailed
Raised with message when JavaScript evaluation fails.
Definition: RADWeb.h:156
bool closeTab(int index)
Closes the tab at index.
bool removeBookmark(int64_t id)
Removes a bookmark by id.
int tabCount() const
Returns the number of open tabs.
void setImagesEnabled(bool enabled)
Enables or disables image loading.
RADCore::Event< void(std::string) > javaScriptFinished
Raised with result text when JavaScript evaluation succeeds.
Definition: RADWeb.h:154
void show()
Shows the native browser window.
void clearCompletedDownloads()
Removes completed downloads from the visible/download history list.
void clearPermissionDecisions()
Clears remembered permission decisions.
void setProfileDirectory(const std::string &directory)
Sets the persistent profile directory before create().
void showDownloads()
Opens the downloads pane.
void clearCookies()
Deletes cookies for the active profile.
RADCore::Event< void(std::string) > loadFinished
Raised when the active tab finishes loading url.
Definition: RADWeb.h:146
void focusFindEntry()
Focuses the built-in find entry.
void applySettings(const RADWebSettings &settings)
Applies settings to the browser/profile.
RADCore::Event< void(std::string) > uriChanged
Raised when active tab URI changes.
Definition: RADWeb.h:152
RADCore::Event< void() > historyChanged
Raised when history entries change.
Definition: RADWeb.h:176
RADWebTabInfo activeTab() const
Returns a snapshot of the active tab.
bool openDownload(int id)
Opens a downloaded file by id.
void clearCache()
Clears WebKit cache data.
void quit()
Quits the GTK/WebKit event loop.
void loadHtml(const std::string &html, const std::string &baseUri="about:blank")
Loads HTML into the active tab using baseUri for relative resources.
bool addBookmark(const std::string &url, const std::string &title={})
Adds a bookmark for url and optional title.
bool openDownloadsDirectory()
Opens the downloads directory in the system file manager.
void setHardwareAccelerationEnabled(bool enabled)
Enables or disables hardware acceleration requests.
const std::string & downloadsDirectory() const
Returns downloads directory.
void setSessionRestoreEnabled(bool enabled)
Enables or disables session restore.
bool restoreSession()
Restores previously saved session state.
bool canGoBack() const
Returns true when the active tab can go back.
void searchPrevious()
Moves to the previous search result.
bool create(const RADWebWindowOptions &options)
Creates the browser window from a full options struct.
void rememberPermissionDecision(const std::string &origin, const std::string &permission, bool allowed)
Remembers an origin-specific permission decision.
void showSettings()
Opens the settings pane.
void setCookiePolicy(RADWebCookiePolicy policy)
Sets WebKit cookie policy.
void setZoomLevel(double zoomLevel)
Sets active tab zoom level.
RADCore::Event< void(std::string, std::string) > permissionRequested
Raised with origin and permission name when user policy is needed.
Definition: RADWeb.h:174
void loadUrl(int tabIndex, const std::string &url)
Loads url in a specific tab.
void searchText(const std::string &text, bool caseSensitive=false, bool wrap=true, unsigned maxMatchCount=1000)
Starts text search in the active page.
int activeTabIndex() const
Returns the active tab index.
RADWebSettings settings() const
Returns current browser settings.
std::vector< RADWebHistoryItem > history(size_t limit=100) const
Returns most recent history items up to limit.
RADCore::Event< void(int) > activeTabChanged
Raised with tab index when the active tab changes.
Definition: RADWeb.h:164
void * nativeWindow() const
Returns the native GTK window pointer for advanced integration.
void searchNext()
Moves to the next search result.
RADCore::Event< void() > closed
Raised when the window closes.
Definition: RADWeb.h:180
void loadUrl(const std::string &url)
Loads url in the active tab.
RADCore::Event< void(int, std::string) > downloadFailed
Raised with id and message when a download fails.
Definition: RADWeb.h:172
bool isCreated() const
Returns true once native WebKit/GTK objects are created.
RADCore::Event< void(std::string) > titleChanged
Raised when active tab title changes.
Definition: RADWeb.h:150
void showDevTools()
Opens WebKit developer tools for the active tab.
int newTab(const std::string &url="about:blank", bool makeActive=true)
Opens url in a new tab and returns the tab id.
void evaluateJavaScript(const std::string &script)
Evaluates JavaScript in the active tab.
bool duplicateTab(int index, bool makeActive=true)
Duplicates the tab at index.
std::vector< RADWebBookmark > bookmarks() const
Returns all bookmarks.
RADCore::Event< void(std::string, std::string) > loadFailed
Raised with url and message when a load fails.
Definition: RADWeb.h:148
bool moveTab(int fromIndex, int toIndex)
Moves a tab from one index to another.
Definition: RADWeb.h:19
RADWebCookiePolicy
Cookie acceptance policy for the WebKit profile.
Definition: RADWeb.h:22
@ AcceptNoThirdParty
Accept first-party cookies and reject third-party cookies.
@ AcceptNever
Reject all cookies.
@ AcceptAlways
Accept all cookies.
RADWebPermissionPolicy
Default handling policy for WebKit permission requests.
Definition: RADWeb.h:32
@ Deny
Deny requests by default.
@ Allow
Allow requests by default.
@ Ask
Ask or emit permissionRequested before deciding.
One persisted bookmark entry.
Definition: RADWeb.h:72
int64_t createdUnixMs
Creation time in Unix milliseconds.
Definition: RADWeb.h:80
std::string url
Bookmarked URL.
Definition: RADWeb.h:76
std::string title
Bookmark display title.
Definition: RADWeb.h:78
Download state used by download events and the downloads pane.
Definition: RADWeb.h:42
uint64_t receivedBytes
Number of bytes received so far.
Definition: RADWeb.h:54
std::string destination
Local destination path.
Definition: RADWeb.h:48
bool finished
True when the download completed successfully.
Definition: RADWeb.h:56
std::string suggestedFileName
Filename suggested by WebKit/server metadata.
Definition: RADWeb.h:50
std::string uri
Source URI.
Definition: RADWeb.h:46
double progress
Progress in the 0.0 to 1.0 range when known.
Definition: RADWeb.h:52
One persisted browser history entry.
Definition: RADWeb.h:60
std::string title
Most recent page title.
Definition: RADWeb.h:64
std::string url
Visited URL.
Definition: RADWeb.h:62
int64_t visitCount
Number of recorded visits.
Definition: RADWeb.h:68
int64_t lastVisitedUnixMs
Last visit time in Unix milliseconds.
Definition: RADWeb.h:66
Browser settings that can be applied at runtime.
Definition: RADWeb.h:98
bool hardwareAccelerationEnabled
Request hardware acceleration where supported.
Definition: RADWeb.h:108
bool sessionRestoreEnabled
Restore prior tabs on startup when available.
Definition: RADWeb.h:112
bool javaScriptEnabled
Enable JavaScript execution.
Definition: RADWeb.h:100
bool developerExtrasEnabled
Enable WebKit developer extras.
Definition: RADWeb.h:104
bool mediaPlaybackRequiresUserGesture
Require a user gesture before media playback.
Definition: RADWeb.h:110
RADWebPermissionPolicy permissionPolicy
Default permission policy.
Definition: RADWeb.h:116
RADWebCookiePolicy cookiePolicy
Cookie policy for the WebKit cookie manager.
Definition: RADWeb.h:114
std::string downloadsDirectory
Directory used for downloads.
Definition: RADWeb.h:118
bool webGlEnabled
Enable WebGL where supported.
Definition: RADWeb.h:106
bool imagesEnabled
Enable image loading.
Definition: RADWeb.h:102
Snapshot of one browser tab.
Definition: RADWeb.h:84
std::string url
Current tab URL.
Definition: RADWeb.h:90
std::string title
Current tab title.
Definition: RADWeb.h:92
int index
Current zero-based tab index.
Definition: RADWeb.h:88
bool active
True when this is the active tab.
Definition: RADWeb.h:94
Creation options for RADWebBrowserWindow.
Definition: RADWeb.h:122
std::string title
Initial window title.
Definition: RADWeb.h:128
bool sessionRestoreEnabled
Enable session restore for this window.
Definition: RADWeb.h:136
std::string profileDirectory
Persistent profile directory; empty selects a default.
Definition: RADWeb.h:130
int height
Initial window height.
Definition: RADWeb.h:126
bool navigationBarVisible
Show the built-in navigation bar.
Definition: RADWeb.h:134
int width
Initial window width.
Definition: RADWeb.h:124
bool ephemeralProfile
Use a non-persistent WebKit profile.
Definition: RADWeb.h:132