Loading...
Searching...
No Matches
Listener.h
Go to the documentation of this file.
1/**************************************************************************************************
2 * This file is a part of Ultralight, an ultra-portable web-browser engine. *
3 * *
4 * See <https://ultralig.ht> for licensing and more. *
5 * *
6 * (C) 2025 Ultralight, Inc. *
7 **************************************************************************************************/
8#pragma once
10#include <Ultralight/String.h>
11#include <Ultralight/RefPtr.h>
12#include <Ultralight/Geometry.h>
13#include <Ultralight/Buffer.h>
16
17namespace ultralight {
18
19class View;
20
21
22///
23/// Cursor types, @see ViewListener::OnChangeCursor
24///
71
72///
73/// User-defined interface to handle general events for a View.
74///
75/// @see View::set_view_listener
76///
78 public:
79 virtual ~ViewListener() { }
80
81 ///
82 /// Called when the page title changes
83 ///
84 virtual void OnChangeTitle(ultralight::View* caller, const String& title) { }
85
86 ///
87 /// Called when the page URL changes
88 ///
89 virtual void OnChangeURL(ultralight::View* caller, const String& url) { }
90
91 ///
92 /// Called when the tooltip changes (usually as result of a mouse hover)
93 ///
94 virtual void OnChangeTooltip(ultralight::View* caller, const String& tooltip) { }
95
96 ///
97 /// Called when the mouse cursor changes
98 ///
99 virtual void OnChangeCursor(ultralight::View* caller, Cursor cursor) { }
100
101 ///
102 /// Called when a message is added to the console (useful for errors / debug)
103 ///
105 const ultralight::ConsoleMessage& message) { }
106
107 ///
108 /// Called when the page wants to create a new child View.
109 ///
110 /// This is usually the result of a user clicking a link with target="_blank"
111 /// or by JavaScript calling window.open(url).
112 ///
113 /// To allow creation of these new Views, you should create a new View in this callback (eg,
114 /// Renderer::CreateView()), resize it to your container, and return it. You are responsible for
115 /// displaying the returned View.
116 ///
117 /// @param caller The View that called this event.
118 ///
119 /// @param opener_url The URL of the page that initiated this request.
120 ///
121 /// @param target_url The URL that the new View will navigate to.
122 ///
123 /// @param is_popup Whether or not this was triggered by window.open().
124 ///
125 /// @param popup_rect Popups can optionally request certain dimensions and coordinates via
126 /// window.open(). You can choose to respect these or not by resizing/moving
127 /// the View to this rect.
128 ///
129 /// @return Returns a RefPtr to a created View to use to satisfy the the request (or return
130 /// nullptr if you want to block the action).
131 ///
132 virtual RefPtr<View> OnCreateChildView(ultralight::View* caller, const String& opener_url,
133 const String& target_url, bool is_popup,
134 const IntRect& popup_rect);
135
136 ///
137 /// Called when the page wants to create a new View to display the local inspector in.
138 ///
139 /// You should create a new View in this callback (eg, Renderer::CreateView()), resize it to your
140 /// container, and return it. You are responsible for displaying the returned View.
141 ///
142 /// @return Returns a RefPtr to a created View to use to satisfy the the request (or return
143 /// nullptr if you want to block the action).
144 ///
146 const String& inspected_url);
147
148 ///
149 /// Called when the page requests to be closed.
150 ///
151 virtual void OnRequestClose(ultralight::View* caller) { }
152
153};
154
155///
156/// User-defined interface to handle load-related events for a View.
157///
158/// @see View::set_load_listener
159///
161 public:
162 virtual ~LoadListener() { }
163
164 ///
165 /// Called when the page begins loading a new URL into a frame.
166 ///
167 /// @param frame_id A unique ID for the frame.
168 ///
169 /// @param is_main_frame Whether or not this is the main frame.
170 ///
171 /// @param url The URL for the load.
172 ///
173 /// @note This will be called for each frame on the page. You can filter for the main frame load
174 /// by checking if `is_main_frame` is `true`.
175 ///
176 virtual void OnBeginLoading(ultralight::View* caller, uint64_t frame_id, bool is_main_frame,
177 const String& url) { }
178
179 ///
180 /// Called when the page finishes loading a URL into a frame.
181 ///
182 /// @param frame_id A unique ID for the frame.
183 ///
184 /// @param is_main_frame Whether or not this is the main frame.
185 ///
186 /// @param url The URL for the load.
187 ///
188 /// @note This will be called for each frame on the page. You can filter for the main frame load
189 /// by checking if `is_main_frame` is `true`.
190 ///
191 virtual void OnFinishLoading(ultralight::View* caller, uint64_t frame_id, bool is_main_frame,
192 const String& url) { }
193
194 ///
195 /// Called when an error occurs while loading a URL into a frame.
196 ///
197 /// @param frame_id A unique ID for the frame.
198 ///
199 /// @param is_main_frame Whether or not this is the main frame.
200 ///
201 /// @param url The URL for the load.
202 ///
203 /// @param description A human-readable description of the error.
204 ///
205 /// @param error_domain The name of the module that triggered the error.
206 ///
207 /// @param error_code Internal error code generated by the module.
208 ///
209 /// @note This will be called for each frame on the page. You can filter for the main frame load
210 /// by checking if `is_main_frame` is `true`.
211 ///
212 virtual void OnFailLoading(ultralight::View* caller, uint64_t frame_id, bool is_main_frame,
213 const String& url, const String& description,
214 const String& error_domain, int error_code) { }
215
216 ///
217 /// Called when the JavaScript window object is reset for a new page load.
218 ///
219 /// This is called before any scripts are executed on the page and is the earliest time to setup
220 /// any initial JavaScript state or bindings.
221 ///
222 /// The document is not guaranteed to be loaded/parsed at this point. If you need to make any
223 /// JavaScript calls that are dependent on DOM elements or scripts on the page, use OnDOMReady
224 /// instead.
225 ///
226 /// The window object is lazily initialized (this will not be called on pages with no scripts).
227 ///
228 /// @param frame_id A unique ID for the frame.
229 ///
230 /// @param is_main_frame Whether or not this is the main frame.
231 ///
232 /// @param url The URL for the load.
233 ///
234 /// @note This will be called for each frame on the page. You can filter for the main frame load
235 /// by checking if `is_main_frame` is `true`.
236 ///
237 virtual void OnWindowObjectReady(ultralight::View* caller, uint64_t frame_id, bool is_main_frame,
238 const String& url) { }
239
240 ///
241 /// Called when all JavaScript has been parsed and the document is ready.
242 ///
243 /// This is the best time to make any JavaScript calls that are dependent on DOM elements or
244 /// scripts on the page.
245 ///
246 /// @param frame_id A unique ID for the frame.
247 ///
248 /// @param is_main_frame Whether or not this is the main frame.
249 ///
250 /// @param url The URL for the load.
251 ///
252 /// @note This will be called for each frame on the page. You can filter for the main frame load
253 /// by checking if `is_main_frame` is `true`.
254 ///
255 virtual void OnDOMReady(ultralight::View* caller, uint64_t frame_id, bool is_main_frame,
256 const String& url) { }
257
258 ///
259 /// Called when the session history (back/forward state) is modified.
260 ///
261 virtual void OnUpdateHistory(ultralight::View* caller) { }
262};
263
264///
265/// A unique identifier representing an active download.
266///
267typedef uint32_t DownloadId;
268
269///
270/// User-defined interface to handle download-related events for a View.
271///
272/// You must implement this interface to handle downloads initiated by a View.
273///
274/// @see View::set_download_listener
275///
277 public:
278 virtual ~DownloadListener() {}
279
280 ///
281 /// Called when the View wants to generate a unique download id.
282 ///
283 /// You should generally return an integer (starting at 0) that is incremented with each call
284 /// to this callback.
285 ///
287
288 ///
289 /// Called when the View wants to start downloading a resource from the network.
290 ///
291 /// You should return true to allow the download, or false to block the download.
292 ///
293 virtual bool OnRequestDownload(ultralight::View* caller, DownloadId id, const String& url) = 0;
294
295 ///
296 /// Called when the View begins downloading a resource from the network.
297 ///
298 /// The View will not actually write any data to disk, you should open a file for writing
299 /// yourself and handle the OnReceiveDataForDownload callback below.
300 ///
301 virtual void OnBeginDownload(ultralight::View* caller, DownloadId id, const String& url,
302 const String& filename, int64_t expected_content_length) = 0;
303
304 ///
305 /// Called when the View receives data for a certain download from the network.
306 ///
307 /// This may be called multiple times for each active download as data is streamed in.
308 ///
309 /// You should write the data to the associated file in this callback.
310 ///
312 RefPtr<Buffer> data) = 0;
313
314 ///
315 /// Called when the View finishes downloading a resource from the network.
316 ///
317 /// You should close the associated file in this callback.
318 ///
319 virtual void OnFinishDownload(ultralight::View* caller, DownloadId id) = 0;
320
321 ///
322 /// Called when the View fails downloading a resource from the network.
323 ///
324 /// You should close the associated file and delete it from disk in this callback.
325 ///
326 virtual void OnFailDownload(ultralight::View* caller, DownloadId id) = 0;
327};
328
329///
330/// User-defined interface to handle network-related events for a View.
331///
332/// @see View::set_network_listener
333///
335 public:
336 virtual ~NetworkListener() { }
337
338 ///
339 /// Called when the View is about to begin a network request.
340 ///
341 /// You can use this to block or modify network requests before they are sent.
342 ///
343 /// Return true to allow the request, return false to block it.
344 ///
345 /// @pre This feature is only available in Ultralight Pro edition and above.
346 ///
347 virtual bool OnNetworkRequest(ultralight::View* caller, NetworkRequest& request) = 0;
348};
349
350} // namespace ultralight
#define UExport
Definition Exports.h:25
Interface for console messages.
Definition ConsoleMessage.h:69
User-defined interface to handle download-related events for a View.
Definition Listener.h:276
virtual void OnFailDownload(ultralight::View *caller, DownloadId id)=0
Called when the View fails downloading a resource from the network.
virtual DownloadId NextDownloadId(ultralight::View *caller)=0
Called when the View wants to generate a unique download id.
virtual void OnFinishDownload(ultralight::View *caller, DownloadId id)=0
Called when the View finishes downloading a resource from the network.
virtual void OnBeginDownload(ultralight::View *caller, DownloadId id, const String &url, const String &filename, int64_t expected_content_length)=0
Called when the View begins downloading a resource from the network.
virtual bool OnRequestDownload(ultralight::View *caller, DownloadId id, const String &url)=0
Called when the View wants to start downloading a resource from the network.
virtual ~DownloadListener()
Definition Listener.h:278
virtual void OnReceiveDataForDownload(ultralight::View *caller, DownloadId id, RefPtr< Buffer > data)=0
Called when the View receives data for a certain download from the network.
User-defined interface to handle load-related events for a View.
Definition Listener.h:160
virtual void OnUpdateHistory(ultralight::View *caller)
Called when the session history (back/forward state) is modified.
Definition Listener.h:261
virtual ~LoadListener()
Definition Listener.h:162
virtual void OnDOMReady(ultralight::View *caller, uint64_t frame_id, bool is_main_frame, const String &url)
Called when all JavaScript has been parsed and the document is ready.
Definition Listener.h:255
virtual void OnFinishLoading(ultralight::View *caller, uint64_t frame_id, bool is_main_frame, const String &url)
Called when the page finishes loading a URL into a frame.
Definition Listener.h:191
virtual void OnFailLoading(ultralight::View *caller, uint64_t frame_id, bool is_main_frame, const String &url, const String &description, const String &error_domain, int error_code)
Called when an error occurs while loading a URL into a frame.
Definition Listener.h:212
virtual void OnBeginLoading(ultralight::View *caller, uint64_t frame_id, bool is_main_frame, const String &url)
Called when the page begins loading a new URL into a frame.
Definition Listener.h:176
virtual void OnWindowObjectReady(ultralight::View *caller, uint64_t frame_id, bool is_main_frame, const String &url)
Called when the JavaScript window object is reset for a new page load.
Definition Listener.h:237
User-defined interface to handle network-related events for a View.
Definition Listener.h:334
virtual ~NetworkListener()
Definition Listener.h:336
virtual bool OnNetworkRequest(ultralight::View *caller, NetworkRequest &request)=0
Called when the View is about to begin a network request.
Interface for Network requests.
Definition NetworkRequest.h:16
A nullable smart pointer.
Definition RefPtr.h:79
Unicode string container with conversions for UTF-8, UTF-16, and UTF-32.
Definition String.h:34
Web-page container rendered to an offscreen surface.
Definition View.h:202
User-defined interface to handle general events for a View.
Definition Listener.h:77
virtual void OnChangeTitle(ultralight::View *caller, const String &title)
Called when the page title changes.
Definition Listener.h:84
virtual void OnChangeURL(ultralight::View *caller, const String &url)
Called when the page URL changes.
Definition Listener.h:89
virtual RefPtr< View > OnCreateInspectorView(ultralight::View *caller, bool is_local, const String &inspected_url)
Called when the page wants to create a new View to display the local inspector in.
virtual void OnAddConsoleMessage(ultralight::View *caller, const ultralight::ConsoleMessage &message)
Called when a message is added to the console (useful for errors / debug)
Definition Listener.h:104
virtual RefPtr< View > OnCreateChildView(ultralight::View *caller, const String &opener_url, const String &target_url, bool is_popup, const IntRect &popup_rect)
Called when the page wants to create a new child View.
virtual void OnChangeTooltip(ultralight::View *caller, const String &tooltip)
Called when the tooltip changes (usually as result of a mouse hover)
Definition Listener.h:94
virtual void OnRequestClose(ultralight::View *caller)
Called when the page requests to be closed.
Definition Listener.h:151
virtual void OnChangeCursor(ultralight::View *caller, Cursor cursor)
Called when the mouse cursor changes.
Definition Listener.h:99
virtual ~ViewListener()
Definition Listener.h:79
Definition App.h:14
uint32_t DownloadId
A unique identifier representing an active download.
Definition Listener.h:267
Cursor
Cursor types,.
Definition Listener.h:25
@ kCursor_NorthEastSouthWestResize
Definition Listener.h:42
@ kCursor_NorthWestSouthEastResize
Definition Listener.h:43
@ kCursor_Cell
Definition Listener.h:57
@ kCursor_SouthWestPanning
Definition Listener.h:53
@ kCursor_Cross
Definition Listener.h:27
@ kCursor_Progress
Definition Listener.h:60
@ kCursor_Copy
Definition Listener.h:62
@ kCursor_SouthEastPanning
Definition Listener.h:52
@ kCursor_ZoomIn
Definition Listener.h:65
@ kCursor_SouthWestResize
Definition Listener.h:38
@ kCursor_NorthWestResize
Definition Listener.h:35
@ kCursor_MiddlePanning
Definition Listener.h:46
@ kCursor_ContextMenu
Definition Listener.h:58
@ kCursor_Hand
Definition Listener.h:28
@ kCursor_ZoomOut
Definition Listener.h:66
@ kCursor_Wait
Definition Listener.h:30
@ kCursor_Grabbing
Definition Listener.h:68
@ kCursor_NoDrop
Definition Listener.h:61
@ kCursor_SouthPanning
Definition Listener.h:51
@ kCursor_Grab
Definition Listener.h:67
@ kCursor_VerticalText
Definition Listener.h:56
@ kCursor_Move
Definition Listener.h:55
@ kCursor_WestPanning
Definition Listener.h:54
@ kCursor_SouthEastResize
Definition Listener.h:37
@ kCursor_EastResize
Definition Listener.h:32
@ kCursor_EastPanning
Definition Listener.h:47
@ kCursor_None
Definition Listener.h:63
@ kCursor_EastWestResize
Definition Listener.h:41
@ kCursor_RowResize
Definition Listener.h:45
@ kCursor_NorthEastPanning
Definition Listener.h:49
@ kCursor_SouthResize
Definition Listener.h:36
@ kCursor_ColumnResize
Definition Listener.h:44
@ kCursor_NorthResize
Definition Listener.h:33
@ kCursor_IBeam
Definition Listener.h:29
@ kCursor_NorthEastResize
Definition Listener.h:34
@ kCursor_WestResize
Definition Listener.h:39
@ kCursor_NorthSouthResize
Definition Listener.h:40
@ kCursor_Pointer
Definition Listener.h:26
@ kCursor_NorthWestPanning
Definition Listener.h:50
@ kCursor_Custom
Definition Listener.h:69
@ kCursor_NotAllowed
Definition Listener.h:64
@ kCursor_Alias
Definition Listener.h:59
@ kCursor_NorthPanning
Definition Listener.h:48
@ kCursor_Help
Definition Listener.h:31
Integer Rectangle Helper.
Definition Geometry.h:529