1 /*
2  * Copyright 2005-2007 James Bursa <bursa@users.sourceforge.net>
3  * Copyright 2003 Philip Pemberton <philpem@users.sourceforge.net>
4  *
5  * This file is part of NetSurf, http://www.netsurf-browser.org/
6  *
7  * NetSurf is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; version 2 of the License.
10  *
11  * NetSurf is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 /**
21  * \file
22  * Content handling interface.
23  *
24  * The content functions manipulate struct contents, which correspond to URLs.
25  */
26 
27 #ifndef NETSURF_CONTENT_CONTENT_H_
28 #define NETSURF_CONTENT_CONTENT_H_
29 
30 #include <libwapcaplet/libwapcaplet.h>
31 
32 #include "desktop/search.h" /* search flags enum */
33 #include "netsurf/content_type.h"
34 #include "netsurf/mouse.h" /* mouse state enums */
35 #include "netsurf/console.h" /* console state and flags enums */
36 
37 struct browser_window;
38 struct browser_window_features;
39 struct content;
40 struct llcache_handle;
41 struct hlcache_handle;
42 struct object_params;
43 struct rect;
44 struct redraw_context;
45 struct cert_chain;
46 
47 
48 /** RFC5988 metadata link */
49 struct content_rfc5988_link {
50 	struct content_rfc5988_link *next; /**< next rfc5988_link in list */
51 
52 	lwc_string *rel; /**< the link relationship - must be present */
53 	struct nsurl *href; /**< the link href - must be present */
54 	lwc_string *hreflang;
55 	lwc_string *type;
56 	lwc_string *media;
57 	lwc_string *sizes;
58 };
59 
60 /** Extra data for some content_msg messages. */
61 union content_msg_data {
62 	/**
63 	 * CONTENT_MSG_LOG - Information for logging
64 	 */
65 	struct {
66 		/** The source of the logging */
67 		browser_window_console_source src;
68 		/** The message to log */
69 		const char *msg;
70 		/** The length of that message */
71 		size_t msglen;
72 		/** The flags of the logging */
73 		browser_window_console_flags flags;
74 	} log;
75 
76 	/**
77 	 * CONTENT_MSG_SSL_CERTS - The certificate chain from the
78 	 *   underlying fetch
79 	 */
80 	const struct cert_chain *chain;
81 
82 	/**
83 	 * CONTENT_MSG_ERROR - Error from content or underlying fetch
84 	 */
85 	struct {
86 		/**
87 		 * The error code to convey meaning
88 		 */
89 		nserror errorcode;
90 		/**
91 		 * The message.  if NSERROR_UNKNOWN then this is the direct
92 		 *   message, otherwise is some kind of metadata (e.g. a
93 		 *   message name or somesuch) but always a null terminated
94 		 *   string.
95 		 */
96 		const char *errormsg;
97 	} errordata;
98 
99 	/**
100 	 * CONTENT_MSG_REDIRECT - Redirect info
101 	 */
102 	struct {
103 		struct nsurl *from;	/**< Redirect origin */
104 		struct nsurl *to;	/**< Redirect target */
105 	} redirect;		/**< Fetch URL redirect occured */
106 
107 	/**
108 	 * CONTENT_MSG_REDRAW - Area of content which needs redrawing
109 	 */
110 	struct {
111 		int x, y, width, height;
112 	} redraw;
113 
114 	/**
115 	 * CONTENT_MSG_REFRESH - Minimum delay
116 	 */
117 	int delay;
118 
119 	/**
120 	 * CONTENT_MSG_REFORMAT - Reformat should not cause a redraw
121 	 */
122 	bool background;
123 
124 	/**
125 	 * CONTENT_MSG_STATUS - Status message update.  If NULL, the
126 	 * content's internal status text has been updated, and
127 	 * listener should use content_get_status_message()
128 	 */
129 	const char *explicit_status_text;
130 
131 	/**
132 	 * CONTENT_MSG_DOWNLOAD - Low-level cache handle
133 	 */
134 	struct llcache_handle *download;
135 
136 	/**
137 	 * CONTENT_MSG_RFC5988_LINK - rfc5988 link data
138 	 */
139 	struct content_rfc5988_link *rfc5988_link;
140 
141 	/**
142 	 * CONTENT_MSG_GETTHREAD - Javascript context (thread)
143 	 */
144 	struct jsthread **jsthread;
145 
146 	/**
147 	 * CONTENT_MSG_GETDIMS - Get the viewport dimensions
148 	 */
149 	struct {
150 		/** \todo Consider getting screen_width, screen_height too. */
151 		unsigned *viewport_width;
152 		unsigned *viewport_height;
153 	} getdims;
154 
155 	/**
156 	 * CONTENT_MSG_SCROLL - Part of content to scroll to show
157 	 */
158 	struct {
159 		/*
160 		 * if true, scroll to show area given by (x0, y0) and (x1,y1).
161 		 * if false, scroll point (x0, y0) to top left of viewport
162 		 */
163 		bool area;
164 		int x0, y0;
165 		int x1, y1;
166 	} scroll;
167 
168 	/**
169 	 * CONTENT_MSG_DRAGSAVE - Drag save a content
170 	 */
171 	struct {
172 		enum {
173 			CONTENT_SAVE_ORIG,
174 			CONTENT_SAVE_NATIVE,
175 			CONTENT_SAVE_COMPLETE,
176 			CONTENT_SAVE_SOURCE
177 		} type;
178 		 /** if NULL, save the content generating the message */
179 		struct hlcache_handle *content;
180 	} dragsave;
181 
182 	/**
183 	 * CONTENT_MSG_SAVELINK - Save a URL
184 	 */
185 	struct {
186 		struct nsurl *url;
187 		const char *title;
188 	} savelink;
189 
190 	/**
191 	 * CONTENT_MSG_POINTER - Mouse pointer to set
192 	 */
193 	browser_pointer_shape pointer;
194 
195 	/**
196 	 * CONTENT_MSG_SELECTION - Selection made or cleared
197 	 */
198 	struct {
199 		bool selection; /**< false for selection cleared */
200 		bool read_only;
201 	} selection;
202 
203 	/**
204 	 * CONTENT_MSG_CARET - set caret position or, hide caret
205 	 */
206 	struct {
207 		enum {
208 			CONTENT_CARET_SET_POS,
209 			CONTENT_CARET_HIDE,
210 			CONTENT_CARET_REMOVE
211 		} type;
212 		struct {
213 			int x;				/**< Carret x-coord */
214 			int y;				/**< Carret y-coord */
215 			int height;			/**< Carret height */
216 			const struct rect *clip;	/**< Carret clip rect */
217 		} pos;			/**< With CONTENT_CARET_SET_POS */
218 	} caret;
219 
220 	/**
221 	 * CONTENT_MSG_DRAG - Drag start or end
222 	 */
223 	struct {
224 		enum {
225 			CONTENT_DRAG_NONE,
226 			CONTENT_DRAG_SCROLL,
227 			CONTENT_DRAG_SELECTION
228 		} type;
229 		const struct rect *rect;
230 	} drag;
231 
232 	/**
233 	 * CONTENT_MSG_SELECTMENU - Create select menu at pointer
234 	 */
235 	struct {
236 		struct form_control *gadget;
237 	} select_menu;
238 
239 	/**
240 	 * CONTENT_MSG_GADGETCLICK - User clicked on a form gadget
241 	 */
242 	struct {
243 		struct form_control *gadget;
244 	} gadget_click;
245 
246 	/**
247 	 * CONTENT_MSG_TEXTSEARCH - Free text search action
248 	 */
249 	struct {
250 		/**
251 		 * The type of text search operation
252 		 */
253 		enum {
254 		      /**
255 		       * Free text search find operation has started or finished
256 		       */
257 		      CONTENT_TEXTSEARCH_FIND,
258 		      /**
259 		       * Free text search match state has changed
260 		       */
261 		      CONTENT_TEXTSEARCH_MATCH,
262 		      /**
263 		       * Free text search back available state changed
264 		       */
265 		      CONTENT_TEXTSEARCH_BACK,
266 		      /**
267 		       * Free text search forward available state changed
268 		       */
269 		      CONTENT_TEXTSEARCH_FORWARD,
270 		      /**
271 		       * add a search query string to the recent searches
272 		       */
273 		      CONTENT_TEXTSEARCH_RECENT
274 		} type;
275 		/**
276 		 * context passed to browser_window_search()
277 		 */
278 		void *ctx;
279 		/**
280 		 * state for operation
281 		 */
282 		bool state;
283 		/**
284 		 * search string
285 		 */
286 		const char *string;
287 	} textsearch;
288 
289 };
290 
291 
292 /**
293  * Get whether a content can reformat
294  *
295  * \param h  content to check
296  * \return whether the content can reformat
297  */
298 bool content_can_reformat(struct hlcache_handle *h);
299 
300 /**
301  * Reformat to new size.
302  *
303  * Calls the reformat function for the content.
304  */
305 void content_reformat(struct hlcache_handle *h, bool background,
306 		int width, int height);
307 
308 /**
309  * Request a redraw of an area of a content
310  *
311  * \param h	  high-level cache handle
312  * \param x	  x co-ord of left edge
313  * \param y	  y co-ord of top edge
314  * \param width	  Width of rectangle
315  * \param height  Height of rectangle
316  */
317 void content_request_redraw(struct hlcache_handle *h,
318 		int x, int y, int width, int height);
319 
320 /**
321  * Handle mouse movements in a content window.
322  *
323  * \param  h	  Content handle
324  * \param  bw	  browser window
325  * \param  mouse  state of mouse buttons and modifier keys
326  * \param  x	  coordinate of mouse
327  * \param  y	  coordinate of mouse
328  */
329 void content_mouse_track(struct hlcache_handle *h, struct browser_window *bw,
330 		browser_mouse_state mouse, int x, int y);
331 
332 /**
333  * Handle mouse clicks and movements in a content window.
334  *
335  * \param  h	  Content handle
336  * \param  bw	  browser window
337  * \param  mouse  state of mouse buttons and modifier keys
338  * \param  x	  coordinate of mouse
339  * \param  y	  coordinate of mouse
340  *
341  * This function handles both hovering and clicking. It is important that the
342  * code path is identical (except that hovering doesn't carry out the action),
343  * so that the status bar reflects exactly what will happen. Having separate
344  * code paths opens the possibility that an attacker will make the status bar
345  * show some harmless action where clicking will be harmful.
346  */
347 void content_mouse_action(struct hlcache_handle *h, struct browser_window *bw,
348 		browser_mouse_state mouse, int x, int y);
349 
350 /**
351  * Handle keypresses.
352  *
353  * \param  h	Content handle
354  * \param  key	The UCS4 character codepoint
355  * \return true if key handled, false otherwise
356  */
357 bool content_keypress(struct hlcache_handle *h, uint32_t key);
358 
359 
360 /**
361  * A window containing the content has been opened.
362  *
363  * \param h	 handle to content that has been opened
364  * \param bw	 browser window containing the content
365  * \param page   content of type CONTENT_HTML containing h, or NULL if not an
366  *		   object within a page
367  * \param params object parameters, or NULL if not an object
368  *
369  * Calls the open function for the content.
370  */
371 nserror content_open(struct hlcache_handle *h, struct browser_window *bw,
372 		struct content *page, struct object_params *params);
373 
374 /**
375  * The window containing the content has been closed.
376  *
377  * Calls the close function for the content.
378  */
379 nserror content_close(struct hlcache_handle *h);
380 
381 /**
382  * Tell a content that any selection it has, or one of its objects
383  * has, must be cleared.
384  */
385 void content_clear_selection(struct hlcache_handle *h);
386 
387 /**
388  * Get a text selection from a content.  Ownership is passed to the caller,
389  * who must free() it.
390  */
391 char * content_get_selection(struct hlcache_handle *h);
392 
393 /**
394  * Get positional contextural information for a content.
395  *
396  * \param[in] h Handle to content to examine.
397  * \param[in] x The x coordinate to examine.
398  * \param[in] y The y coordinate to examine.
399  * \param[out] data The context structure to fill in.
400  */
401 nserror content_get_contextual_content(struct hlcache_handle *h,
402 		int x, int y, struct browser_window_features *data);
403 
404 /**
405  * scroll content at coordnate
406  *
407  * \param[in] h Handle to content to examine.
408  * \param[in] x The x coordinate to examine.
409  * \param[in] y The y coordinate to examine.
410  */
411 bool content_scroll_at_point(struct hlcache_handle *h,
412 		int x, int y, int scrx, int scry);
413 
414 /**
415  * Drag and drop a file at coordinate
416  *
417  * \param[in] h Handle to content to examine.
418  * \param[in] x The x coordinate to examine.
419  * \param[in] y The y coordinate to examine.
420  */
421 bool content_drop_file_at_point(struct hlcache_handle *h,
422 		int x, int y, char *file);
423 
424 /**
425  * Free text search a content
426  *
427  * \param[in] h Handle to content to search.
428  * \param[in] context The context passed to gui table search handlers
429  * \param[in] flags The flags that control the search
430  * \param[in] The string being searched for.
431  * \retun NSERROR_OK on success else error code on faliure
432  */
433 nserror content_textsearch(struct hlcache_handle *h, void *context, search_flags_t flags, const char *string);
434 
435 /**
436  * Clear a search
437  *
438  * \param[in] h Handle to content to clear search from.
439  */
440 nserror content_textsearch_clear(struct hlcache_handle *h);
441 
442 
443 /**
444  * Control debug con a content.
445  *
446  * \param h content handle to debug.
447  * \param op Debug operation type.
448  */
449 nserror content_debug(struct hlcache_handle *h, enum content_debug op);
450 
451 
452 /**
453  * find link in content that matches the rel string.
454  *
455  * \param h handle to the content to retrieve tyoe of.
456  * \param rel The string to match.
457  * \return A matching rfc5988 link or NULL if none is found.
458  *
459  */
460 struct content_rfc5988_link *content_find_rfc5988_link(struct hlcache_handle *h, lwc_string *rel);
461 
462 
463 /**
464  * Retrieve status of content
465  *
466  * \param h handle to the content to retrieve status from
467  * \return Content status
468  */
469 content_status content_get_status(struct hlcache_handle *h);
470 
471 
472 /**
473  * Retrieve status of content
474  *
475  * \param c Content to retrieve status from.
476  * \return Content status
477  */
478 content_status content__get_status(struct content *c);
479 
480 
481 /**
482  * Retrieve status message associated with content
483  *
484  * \param h handle to the content to retrieve status message from
485  * \return Pointer to status message, or NULL if not found.
486  */
487 const char *content_get_status_message(struct hlcache_handle *h);
488 
489 
490 /**
491  * Retrieve available width of content
492  *
493  * \param h handle to the content to get available width of.
494  * \return Available width of content.
495  */
496 int content_get_available_width(struct hlcache_handle *h);
497 
498 
499 /**
500  * Retrieve the refresh URL for a content
501  *
502  * \param h Content to retrieve refresh URL from
503  * \return Pointer to URL, or NULL if none
504  */
505 struct nsurl *content_get_refresh_url(struct hlcache_handle *h);
506 
507 
508 /**
509  * Determine if a content is opaque from handle
510  *
511  * \param h high level cache handle to retrieve opacity from.
512  * \return false if the content is not opaque or information is not
513  *         known else true.
514  */
515 bool content_get_opaque(struct hlcache_handle *h);
516 
517 
518 /**
519  * Retrieve quirkiness of a content
520  *
521  * \param h Content to examine
522  * \return True if content is quirky, false otherwise
523  */
524 bool content_get_quirks(struct hlcache_handle *h);
525 
526 
527 /**
528  * Return whether a content is currently locked
529  *
530  * \param h handle to the content.
531  * \return true iff locked, else false
532  */
533 bool content_is_locked(struct hlcache_handle *h);
534 
535 
536 /**
537  * Execute some JavaScript code inside a content object.
538  *
539  * Runs the passed in JavaScript code in the content object's context.
540  *
541  * \param h The handle to the content
542  * \param src The JavaScript source code
543  * \param srclen The length of the source code
544  * \return Whether the JS function was successfully injected into the content
545  */
546 bool content_exec(struct hlcache_handle *h, const char *src, size_t srclen);
547 
548 /**
549  * Determine if the content referred to any insecure objects.
550  *
551  * Query the content to determine if any of its referred objects were loaded
552  * in a manner not considered secure.  For a content to be recursively
553  * secure it must only load over https and must not have certificate overrides
554  * in place.
555  *
556  * \param h The handle to the content
557  * \return Whether the content referred to any insecure objects
558  */
559 bool content_saw_insecure_objects(struct hlcache_handle *h);
560 
561 #endif
562