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  * Protected interface to Content handling.
23  *
24  * The content functions manipulate struct contents, which correspond to URLs.
25  */
26 
27 #ifndef NETSURF_CONTENT_CONTENT_PROTECTED_H_
28 #define NETSURF_CONTENT_CONTENT_PROTECTED_H_
29 
30 #include <stdio.h>
31 #include <libwapcaplet/libwapcaplet.h>
32 
33 #include "netsurf/content_type.h"
34 #include "desktop/search.h" /* search flags enum */
35 #include "netsurf/mouse.h" /* mouse state enums */
36 
37 struct nsurl;
38 struct content_redraw_data;
39 union content_msg_data;
40 struct http_parameter;
41 struct llcache_handle;
42 struct object_params;
43 struct content;
44 struct redraw_context;
45 struct rect;
46 struct browser_window_features;
47 struct textsearch_context;
48 struct box;
49 struct selection;
50 struct selection_string;
51 
52 typedef struct content_handler content_handler;
53 
54 /**
55  * Content operation function table
56  *
57  * function table implementing a content type.
58  */
59 struct content_handler {
60 	void (*fini)(void);
61 
62 	nserror (*create)(const struct content_handler *handler,
63                           lwc_string *imime_type,
64                           const struct http_parameter *params,
65                           struct llcache_handle *llcache,
66                           const char *fallback_charset, bool quirks,
67                           struct content **c);
68 
69 	bool (*process_data)(struct content *c,
70 			const char *data, unsigned int size);
71 	bool (*data_complete)(struct content *c);
72 	void (*reformat)(struct content *c, int width, int height);
73 	void (*destroy)(struct content *c);
74 	void (*stop)(struct content *c);
75 	nserror (*mouse_track)(struct content *c, struct browser_window *bw,
76 			browser_mouse_state mouse, int x, int y);
77 	nserror (*mouse_action)(struct content *c, struct browser_window *bw,
78 			browser_mouse_state mouse, int x, int y);
79 	bool (*keypress)(struct content *c, uint32_t key);
80 	bool (*redraw)(struct content *c, struct content_redraw_data *data,
81 			const struct rect *clip,
82 			const struct redraw_context *ctx);
83 	nserror (*open)(struct content *c, struct browser_window *bw,
84 			struct content *page, struct object_params *params);
85 	nserror (*close)(struct content *c);
86 	void (*clear_selection)(struct content *c);
87 	char * (*get_selection)(struct content *c);
88 	nserror (*get_contextual_content)(struct content *c, int x, int y,
89 			struct browser_window_features *data);
90 	bool (*scroll_at_point)(struct content *c, int x, int y,
91 			int scrx, int scry);
92 	bool (*drop_file_at_point)(struct content *c, int x, int y,
93 			char *file);
94 	nserror (*debug_dump)(struct content *c, FILE *f, enum content_debug op);
95 	nserror (*debug)(struct content *c, enum content_debug op);
96 	nserror (*clone)(const struct content *old, struct content **newc);
97 	bool (*matches_quirks)(const struct content *c, bool quirks);
98 	const char *(*get_encoding)(const struct content *c, enum content_encoding_type op);
99 	content_type (*type)(void);
100 	void (*add_user)(struct content *c);
101 	void (*remove_user)(struct content *c);
102 	bool (*exec)(struct content *c, const char *src, size_t srclen);
103 	bool (*saw_insecure_objects)(struct content *c);
104 
105 	/**
106 	 * content specific free text search find
107 	 */
108 	nserror (*textsearch_find)(struct content *c, struct textsearch_context *context, const char *pattern, int p_len, bool case_sens);
109 
110 	/**
111 	 * get bounds of free text search match
112 	 */
113 	nserror (*textsearch_bounds)(struct content *c, unsigned start_idx, unsigned end_idx, struct box *start_ptr, struct box *end_ptr, struct rect *bounds_out);
114 
115 	/**
116 	 * redraw an area of selected text
117 	 *
118 	 * The defined text selection will cause an area of the
119 	 *   content to be marked as invalid and hence redrawn.
120 	 *
121 	 * \param c The content being redrawn
122 	 * \param start_idx The start index of the text region to be redrawn
123 	 * \param end_idx The end index of teh text region to be redrawn
124 	 * \return NSERROR_OK on success else error code
125 	 */
126 	nserror (*textselection_redraw)(struct content *c, unsigned start_idx, unsigned end_idx);
127 
128 	/**
129 	 * copy selected text into selection string possibly with formatting
130 	 */
131 	nserror (*textselection_copy)(struct content *c, unsigned start_idx, unsigned end_idx, struct selection_string *selstr);
132 
133 	/**
134 	 * get maximum index of text section.
135 	 *
136 	 * \param[in] c The content to measure
137 	 * \param[out] end_idx pointer to value to recive result
138 	 * \return NSERROR_OK and \a end_idx updated else error code
139 	 */
140 	nserror (*textselection_get_end)(struct content *c, unsigned *end_idx);
141 
142         /**
143 	 * handler dependant content sensitive internal data interface.
144 	 */
145 	void *(*get_internal)(const struct content *c, void *context);
146 
147 	/**
148 	 * are the content contents opaque.
149 	 *
150 	 * Determine if this content would obscure (not mix with) any background
151 	 *
152 	 * \param c The content to check
153 	 */
154 	bool (*is_opaque)(struct content *c);
155 
156 	/**
157 	 * There must be one content per user for this type.
158 	 */
159 	bool no_share;
160 };
161 
162 /**
163  * Linked list of users of a content.
164  */
165 struct content_user
166 {
167 	void (*callback)(
168 			struct content *c,
169 			content_msg msg,
170 			const union content_msg_data *data,
171 			void *pw);
172 	void *pw;
173 
174 	struct content_user *next;
175 };
176 
177 /**
178  * Content which corresponds to a single URL.
179  */
180 struct content {
181 	/**
182 	 * Low-level cache object
183 	 */
184 	struct llcache_handle *llcache;
185 
186 	/**
187 	 * Original MIME type of data
188 	 */
189 	lwc_string *mime_type;
190 
191 	/**
192 	 * Handler for content
193 	 */
194 	const struct content_handler *handler;
195 
196 	/**
197 	 * Current status.
198 	 */
199 	content_status status;
200 
201 	/**
202 	 * Width dimension, if applicable.
203 	 */
204 	int width;
205 	/**
206 	 * Height dimension, if applicable.
207 	 */
208 	int height;
209 	/**
210 	 * Viewport width.
211 	 */
212 	int available_width;
213 	/**
214 	 * Viewport height.
215 	 */
216 	int available_height;
217 
218 	/**
219 	 * Content is in quirks mode
220 	 */
221 	bool quirks;
222 	/**
223 	 * Fallback charset, or NULL
224 	 */
225 	char *fallback_charset;
226 
227 	/**
228 	 * URL for refresh request
229 	 */
230 	struct nsurl *refresh;
231 
232 	/**
233 	 * list of metadata links
234 	 */
235 	struct content_rfc5988_link *links;
236 
237 	/**
238 	 * Creation timestamp when LOADING or READY.  Total time in ms
239 	 * when DONE.
240 	 */
241 	uint64_t time;
242 
243 	/**
244 	 * Earliest time to attempt a period reflow while fetching a
245 	 * page's objects.
246 	 */
247 	uint64_t reformat_time;
248 
249 	/**
250 	 * Estimated size of all data associated with this content
251 	 */
252 	unsigned int size;
253 	/**
254 	 * Title for browser window.
255 	 */
256 	char *title;
257 	/**
258 	 * Number of child fetches or conversions currently in progress.
259 	 */
260 	unsigned int active;
261 	/**
262 	 * List of users.
263 	 */
264 	struct content_user *user_list;
265 	/**
266 	 * Full text for status bar.
267 	 */
268 	char status_message[120];
269 	/**
270 	 * Status of content.
271 	 */
272 	char sub_status[80];
273 	/**
274 	 * Content is being processed: data structures may be
275 	 * inconsistent and content must not be redrawn or modified.
276 	 */
277 	bool locked;
278 
279 	/**
280 	 * Total data size, 0 if unknown.
281 	 */
282 	unsigned long total_size;
283 	/**
284 	 * HTTP status code, 0 if not HTTP.
285 	 */
286 	long http_code;
287 
288 	/**
289 	 * Free text search state
290 	 */
291 	struct {
292 		char *string;
293 		struct textsearch_context *context;
294 	} textsearch;
295 };
296 
297 extern const char * const content_type_name[];
298 extern const char * const content_status_name[];
299 
300 
301 /**
302  * Initialise a new base content structure.
303  *
304  * \param c                 Content to initialise
305  * \param handler           Content handler
306  * \param imime_type        MIME type of content
307  * \param params            HTTP parameters
308  * \param llcache           Source data handle
309  * \param fallback_charset  Fallback charset
310  * \param quirks            Quirkiness of content
311  * \return NSERROR_OK on success, appropriate error otherwise
312  */
313 nserror content__init(struct content *c, const struct content_handler *handler,
314 		lwc_string *imime_type, const struct http_parameter *params,
315 		struct llcache_handle *llcache, const char *fallback_charset,
316 		bool quirks);
317 
318 /**
319  * Clone a content's data members
320  *
321  * \param c   Content to clone
322  * \param nc  Content to populate
323  * \return NSERROR_OK on success, appropriate error otherwise
324  */
325 nserror content__clone(const struct content *c, struct content *nc);
326 
327 /**
328  * Put a content in status CONTENT_STATUS_READY and unlock the content.
329  */
330 void content_set_ready(struct content *c);
331 
332 /**
333  * Put a content in status CONTENT_STATUS_DONE.
334  */
335 void content_set_done(struct content *c);
336 
337 /**
338  * Put a content in status CONTENT_STATUS_ERROR and unlock the content.
339  *
340  * \note We expect the caller to broadcast an error report if needed.
341  */
342 void content_set_error(struct content *c);
343 
344 /**
345  * Updates content with new status.
346  *
347  * The textual status contained in the content is updated with given string.
348  *
349  * \param c The content to set status in.
350  * \param status_message new textual status
351  */
352 void content_set_status(struct content *c, const char *status_message);
353 
354 /**
355  * Send a message to all users.
356  */
357 void content_broadcast(struct content *c, content_msg msg, const union content_msg_data *data);
358 
359 /**
360  * Send an error message to all users.
361  *
362  * \param c The content whose users should be informed of an error
363  * \param errorcode The nserror code to send
364  * \param msg The error message to send alongside
365  */
366 void content_broadcast_error(struct content *c, nserror errorcode, const char *msg);
367 
368 /**
369  * associate a metadata link with a content.
370  *
371  * \param c content to add link to
372  * \param link The rfc5988 link to add
373  */
374 bool content__add_rfc5988_link(struct content *c, const struct content_rfc5988_link *link);
375 
376 /**
377  * free a rfc5988 link
378  *
379  * \param link The link to free
380  * \return The next link in the chain
381  */
382 struct content_rfc5988_link *content__free_rfc5988_link(struct content_rfc5988_link *link);
383 
384 /**
385  * cause a content to be reformatted.
386  *
387  * \param c content to be reformatted
388  * \param background perform reformat in background
389  * \param width The available width to reformat content in
390  * \param height The available height to reformat content in
391  */
392 void content__reformat(struct content *c, bool background, int width, int height);
393 
394 /**
395  * Request a redraw of an area of a content
396  *
397  * \param c	  Content
398  * \param x	  x co-ord of left edge
399  * \param y	  y co-ord of top edge
400  * \param width	  Width of rectangle
401  * \param height  Height of rectangle
402  */
403 void content__request_redraw(struct content *c, int x, int y, int width, int height);
404 
405 /**
406  * Retrieve mime-type of content
407  *
408  * \param c Content to retrieve mime-type of
409  * \return Pointer to referenced mime-type, or NULL if not found.
410  */
411 lwc_string *content__get_mime_type(struct content *c);
412 
413 /**
414  * Set title associated with content
415  *
416  * \param c Content to set title on.
417  * \param title The new title to set.
418  * \return true on sucess else false.
419  */
420 bool content__set_title(struct content *c, const char *title);
421 
422 /**
423  * Retrieve title associated with content
424  *
425  * \param c Content to retrieve title from
426  * \return Pointer to title, or NULL if not found.
427  */
428 const char *content__get_title(struct content *c);
429 
430 /**
431  * Retrieve status message associated with content
432  *
433  * \param c Content to retrieve status message from
434  * \return Pointer to status message, or NULL if not found.
435  */
436 const char *content__get_status_message(struct content *c);
437 
438 /**
439  * Retrieve width of content
440  *
441  * \param c Content to retrieve width of
442  * \return Content width
443  */
444 int content__get_width(struct content *c);
445 
446 /**
447  * Retrieve height of content
448  *
449  * \param c Content to retrieve height of
450  * \return Content height
451  */
452 int content__get_height(struct content *c);
453 
454 /**
455  * Retrieve available width of content
456  *
457  * \param c content to get available width of.
458  * \return Available width of content.
459  */
460 int content__get_available_width(struct content *c);
461 
462 /**
463  * Retrieve source of content.
464  *
465  * \param c    Content to retrieve source of.
466  * \param size Pointer to location to receive byte size of source.
467  * \return Pointer to source data.
468  */
469 const uint8_t *content__get_source_data(struct content *c, size_t *size);
470 
471 /**
472  * Invalidate content reuse data.
473  *
474  * causes subsequent requests for content URL to query server to
475  * determine if content can be reused. This is required behaviour for
476  * forced reloads etc.
477  *
478  * \param c Content to invalidate.
479  */
480 void content__invalidate_reuse_data(struct content *c);
481 
482 /**
483  * Retrieve the refresh URL for a content
484  *
485  * \param c Content to retrieve refresh URL from
486  * \return Pointer to URL or NULL if none
487  */
488 struct nsurl *content__get_refresh_url(struct content *c);
489 
490 /**
491  * Retrieve the bitmap contained in an image content
492  *
493  * \param c Content to retrieve opacity from
494  * \return Pointer to bitmap or NULL if none.
495  */
496 struct bitmap *content__get_bitmap(struct content *c);
497 
498 /**
499  * Determine if a content is opaque
500  *
501  * \param c Content to retrieve opacity from
502  * \return false if the content is not opaque or information is not
503  *         known else true.
504  */
505 bool content__get_opaque(struct content *c);
506 
507 /**
508  * Retrieve the encoding of a content
509  *
510  * \param c the content to examine the encoding of.
511  * \param op encoding operation.
512  * \return Pointer to content info or NULL if none.
513  */
514 const char *content__get_encoding(struct content *c, enum content_encoding_type op);
515 
516 /**
517  * Return whether a content is currently locked
518  *
519  * \param c Content to test
520  * \return true iff locked, else false
521  */
522 bool content__is_locked(struct content *c);
523 
524 /**
525  * Destroy and free a content.
526  *
527  * Calls the destroy function for the content, and frees the structure.
528  */
529 void content_destroy(struct content *c);
530 
531 /**
532  * Register a user for callbacks.
533  *
534  * \param c the content to register
535  * \param callback the user callback function
536  * \param pw callback private data
537  * \return true on success, false otherwise on memory exhaustion
538  *
539  * The callback will be called when content_broadcast() is
540  * called with the content.
541  */
542 bool content_add_user(struct content *h,
543 		      void (*callback)(
544 				       struct content *c,
545 				       content_msg msg,
546 				       const union content_msg_data *data,
547 				       void *pw),
548 		      void *pw);
549 
550 /**
551  * Remove a callback user.
552  *
553  * The callback function and pw must be identical to those passed to
554  * content_add_user().
555  *
556  * \param c Content to remove user from
557  * \param callback passed when added
558  * \param ctx Context passed when added
559  */
560 void content_remove_user(struct content *c,
561 			 void (*callback)(
562 					  struct content *c,
563 					  content_msg msg,
564 					  const union content_msg_data *data,
565 					  void *pw),
566 			 void *ctx);
567 
568 
569 /**
570  * Count users for the content.
571  *
572  * \param c Content to consider
573  */
574 uint32_t content_count_users(struct content *c);
575 
576 
577 /**
578  * Determine if quirks mode matches
579  *
580  * \param c Content to consider
581  * \param quirks  Quirks mode to match
582  * \return True if quirks match, false otherwise
583  */
584 bool content_matches_quirks(struct content *c, bool quirks);
585 
586 /**
587  * Determine if a content is shareable
588  *
589  * \param c  Content to consider
590  * \return True if content is shareable, false otherwise
591  */
592 bool content_is_shareable(struct content *c);
593 
594 /**
595  * Retrieve the low-level cache handle for a content
596  *
597  * \note only used by hlcache
598  *
599  * \param c Content to retrieve from
600  * \return Low-level cache handle
601  */
602 const struct llcache_handle *content_get_llcache_handle(struct content *c);
603 
604 /**
605  * Retrieve URL associated with content
606  *
607  * \param c  Content to retrieve URL from
608  * \return Pointer to URL, or NULL if not found.
609  */
610 struct nsurl *content_get_url(struct content *c);
611 
612 /**
613  * Clone a content object in its current state.
614  *
615  * \param c  Content to clone
616  * \return Clone of \a c
617  */
618 struct content *content_clone(struct content *c);
619 
620 /**
621  * Abort a content object
622  *
623  * \param c The content object to abort
624  * \return NSERROR_OK on success, otherwise appropriate error
625  */
626 nserror content_abort(struct content *c);
627 
628 #endif
629