NetSurf
netsurf.c
Go to the documentation of this file.
1/*
2 * Copyright 2003 Phil Mellor <monkeyson@users.sourceforge.net>
3 * Copyright 2007 James Bursa <bursa@users.sourceforge.net>
4 * Copyright 2004 Andrew Timmins <atimmins@blueyonder.co.uk>
5 *
6 * This file is part of NetSurf, http://www.netsurf-browser.org/
7 *
8 * NetSurf is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
11 *
12 * NetSurf is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <locale.h>
22#include <signal.h>
23#include <stdbool.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <libwapcaplet/libwapcaplet.h>
27#include <dom/dom.h>
28
29#include "netsurf/inttypes.h"
30#include "utils/config.h"
31#include "utils/errors.h"
32#include "utils/nscolour.h"
33#include "utils/nsoption.h"
34#include "utils/corestrings.h"
35#include "utils/log.h"
36#include "utils/string.h"
37#include "utils/utf8.h"
38#include "utils/messages.h"
39#include "utils/useragent.h"
41#include "content/fetchers.h"
42#include "content/hlcache.h"
43#include "content/mimesniff.h"
44#include "content/urldb.h"
45#include "css/css.h"
46#include "image/image.h"
47#include "image/image_cache.h"
48#include "javascript/js.h"
49#include "html/html.h"
50#include "text/textplain.h"
51
54#include "desktop/page-info.h"
55#include "desktop/searchweb.h"
56#include "netsurf/misc.h"
58#include "netsurf/netsurf.h"
59
60
61/** \todo QUERY - Remove this import later */
63
64/** speculative pre-conversion small image size
65 *
66 * Experimenting by visiting every page from default page in order and
67 * then netsurf homepage
68 *
69 * 0 : Cache hit/miss/speculative miss/fail 604/147/ 0/0 (80%/19%/ 0%/ 0%)
70 * 2048 : Cache hit/miss/speculative miss/fail 622/119/ 17/0 (82%/15%/ 2%/ 0%)
71 * 4096 : Cache hit/miss/speculative miss/fail 656/109/ 25/0 (83%/13%/ 3%/ 0%)
72 * 8192 : Cache hit/miss/speculative miss/fail 648/104/ 40/0 (81%/13%/ 5%/ 0%)
73 * ALL : Cache hit/miss/speculative miss/fail 775/ 0/161/0 (82%/ 0%/17%/ 0%)
74*/
75#define SPECULATE_SMALL 4096
76
77/** the time between image cache clean runs in ms. */
78#define IMAGE_CACHE_CLEAN_TIME (10 * 1000)
79
80/** default time between content cache cleans. */
81#define HL_CACHE_CLEAN_TIME (2 * IMAGE_CACHE_CLEAN_TIME)
82
83/** ensure there is a minimal amount of memory for source objetcs and
84 * decoded bitmaps.
85 */
86#define MINIMUM_MEMORY_CACHE_SIZE (2 * 1024 * 1024)
87
88/** default minimum object time before object is pushed to backing store. (s) */
89#define LLCACHE_STORE_MIN_LIFETIME (60 * 30)
90
91/** default minimum bandwidth for backing store writeout. (byte/s) */
92#define LLCACHE_STORE_MIN_BANDWIDTH (128 * 1024)
93
94/** default maximum bandwidth for backing store writeout. (byte/s) */
95#define LLCACHE_STORE_MAX_BANDWIDTH (1024 * 1024)
96
97/** default time quantum with which to calculate bandwidth (ms) */
98#define LLCACHE_STORE_TIME_QUANTUM (100)
99
100static void netsurf_lwc_iterator(lwc_string *str, void *pw)
101{
102 NSLOG(netsurf, WARNING, "[%3"PRIu32"] %.*s", str->refcnt,
103 (int)lwc_string_length(str), lwc_string_data(str));
104}
105
106/* exported interface documented in netsurf/netsurf.h */
107nserror netsurf_init(const char *store_path)
108{
109 nserror ret;
112 .llcache = {
113 .minimum_lifetime = LLCACHE_STORE_MIN_LIFETIME,
114 .minimum_bandwidth = LLCACHE_STORE_MIN_BANDWIDTH,
115 .maximum_bandwidth = LLCACHE_STORE_MAX_BANDWIDTH,
116 .time_quantum = LLCACHE_STORE_TIME_QUANTUM,
117 }
118 };
121 .speculative_small = SPECULATE_SMALL
122 };
123
124#ifdef HAVE_SIGPIPE
125 /* Ignore SIGPIPE - this is necessary as OpenSSL can generate these
126 * and the default action is to terminate the app. There's no easy
127 * way of determining the cause of the SIGPIPE (other than using
128 * sigaction() and some mechanism for getting the file descriptor
129 * out of libcurl). However, we expect nothing else to generate a
130 * SIGPIPE, anyway, so may as well just ignore them all.
131 */
132 signal(SIGPIPE, SIG_IGN);
133#endif
134
135 /* corestrings init */
136 ret = corestrings_init();
137 if (ret != NSERROR_OK)
138 return ret;
139
140 ret = nscolour_update();
141 if (ret != NSERROR_OK)
142 return ret;
143
144 /* set up cache limits based on the memory cache size option */
145 hlcache_parameters.llcache.limit = nsoption_int(memory_cache_size);
146
149 NSLOG(netsurf, INFO,
150 "Setting minimum memory cache size %"PRIsizet,
152 }
153
154 /* Set up the max attempts made to fetch a timing out resource */
156
157 /* image cache is 25% of total memory cache size */
159
160 /* image cache hysteresis is 20% of the image cache size */
162
163 /* account for image cache use from total */
165
166 /* set backing store target limit */
168
169 /* set backing store hysterissi to 20% */
171
172 /* set the path to the backing store */
174 nsoption_charp(disc_cache_path) ?
175 nsoption_charp(disc_cache_path) :
176 store_path;
177
178 /* image handler bitmap cache */
180 if (ret != NSERROR_OK)
181 return ret;
182
183 /* content handler initialisation */
184 ret = nscss_init();
185 if (ret != NSERROR_OK)
186 return ret;
187
188 ret = html_init();
189 if (ret != NSERROR_OK)
190 return ret;
191
192 ret = image_init();
193 if (ret != NSERROR_OK)
194 return ret;
195
196 ret = textplain_init();
197 if (ret != NSERROR_OK)
198 return ret;
199
200 setlocale(LC_ALL, "");
201
202 /* initialise the fetchers */
203 ret = fetcher_init();
204 if (ret != NSERROR_OK)
205 return ret;
206
207 /* Initialise the hlcache and allow it to init the llcache for us */
209 if (ret != NSERROR_OK)
210 return ret;
211
212 /* Initialize system colours */
213 ret = ns_system_colour_init();
214 if (ret != NSERROR_OK)
215 return ret;
216
218
219 ret = page_info_init();
220 if (ret != NSERROR_OK) {
221 return ret;
222 }
223
224 return NSERROR_OK;
225}
226
227
228/**
229 * Clean up components used by gui NetSurf.
230 */
231
232void netsurf_exit(void)
233{
234 hlcache_stop();
235
236 NSLOG(netsurf, INFO, "Closing GUI");
237 guit->misc->quit();
238
239 NSLOG(netsurf, INFO, "Finalising page-info module");
241
242 NSLOG(netsurf, INFO, "Finalising JavaScript");
243 js_finalise();
244
245 NSLOG(netsurf, INFO, "Finalising Web Search");
247
248 NSLOG(netsurf, INFO, "Finalising high-level cache");
250
251 NSLOG(netsurf, INFO, "Closing fetches");
252 fetcher_quit();
253 /* Now the fetchers are done, our user-agent string can go */
255
256 /* dump any remaining cache entries */
258
259 /* Clean up after content handlers */
261
262 NSLOG(netsurf, INFO, "Closing utf8");
264
265 NSLOG(netsurf, INFO, "Destroying URLdb");
267
268 NSLOG(netsurf, INFO, "Destroying System colours");
270
271 NSLOG(netsurf, INFO, "Destroying Messages");
273
275 if (dom_namespace_finalise() != DOM_NO_ERR) {
276 NSLOG(netsurf, WARNING, "Unable to finalise DOM namespace strings");
277 }
278 NSLOG(netsurf, INFO, "Remaining lwc strings:");
279 lwc_iterate_strings(netsurf_lwc_iterator, NULL);
280
281 NSLOG(netsurf, INFO, "Exited successfully");
282}
Browser window private structure.
Browser window creation and manipulation interface.
void fetcher_quit(void)
Clean up for quit.
Definition: fetch.c:322
nserror fetcher_init(void)
Initialise all registered fetchers.
Definition: fetch.c:285
nserror image_init(void)
Initialise image content handlers.
Definition: image.c:48
Initialisation/finalisation of image handlers.
void content_factory_fini(void)
Clean up after the content factory.
nserror corestrings_fini(void)
free resources of core string tables.
Definition: corestrings.c:40
nserror corestrings_init(void)
Initialise the core string tables.
Definition: corestrings.c:78
Useful interned string pointers (interface).
nserror nscss_init(void)
Initialise the CSS content handler.
Definition: css.c:816
nserror search_web_finalise(void)
Finalise the web search operations freeing all resources.
Definition: searchweb.c:582
Error codes.
nserror
Enumeration of error codes.
Definition: errors.h:29
@ NSERROR_OK
No error.
Definition: errors.h:30
Interface for fetchers factory.
struct netsurf_table * guit
The global interface table.
Definition: gui_factory.c:50
Interface to core interface table.
nserror hlcache_initialise(const struct hlcache_parameters *hlcache_parameters)
Initialise the high-level cache, preparing the llcache also.
Definition: hlcache.c:552
void hlcache_finalise(void)
Finalise the high-level cache, destroying any remaining contents.
Definition: hlcache.c:584
void hlcache_stop(void)
Stop the high-level cache periodic functionality so that the exit sequence can run.
Definition: hlcache.c:577
High-level resource cache interface.
nserror html_init(void)
initialise content handler
Definition: html.c:2382
Interface to text/html content handler.
nserror image_cache_fini(void)
Definition: image_cache.c:438
nserror image_cache_init(const struct image_cache_parameters *image_cache_parameters)
Initialise the image cache.
Definition: image_cache.c:416
The image content handler intermediate image cache.
Interface to platform-specific miscellaneous browser operation table.
Netsurf additional integer type formatting macros.
#define PRIsizet
c99 standard printf formatting for size_t type
Definition: inttypes.h:53
Interface to javascript engine functions.
void js_finalise(void)
finalise javascript interpreter
Definition: dukky.c:590
void js_initialise(void)
Initialise javascript interpreter.
Definition: dukky.c:577
#define NSLOG(catname, level, logmsg, args...)
Definition: log.h:116
void messages_destroy(void)
Free memory used by the standard Messages hash.
Definition: messages.c:463
Localised message support (interface).
MIME type sniffer interface.
#define LLCACHE_STORE_MIN_BANDWIDTH
default minimum bandwidth for backing store writeout.
Definition: netsurf.c:92
void netsurf_exit(void)
Clean up components used by gui NetSurf.
Definition: netsurf.c:232
#define MINIMUM_MEMORY_CACHE_SIZE
ensure there is a minimal amount of memory for source objetcs and decoded bitmaps.
Definition: netsurf.c:86
#define HL_CACHE_CLEAN_TIME
default time between content cache cleans.
Definition: netsurf.c:81
static void netsurf_lwc_iterator(lwc_string *str, void *pw)
Definition: netsurf.c:100
#define IMAGE_CACHE_CLEAN_TIME
the time between image cache clean runs in ms.
Definition: netsurf.c:78
#define LLCACHE_STORE_MAX_BANDWIDTH
default maximum bandwidth for backing store writeout.
Definition: netsurf.c:95
nserror netsurf_init(const char *store_path)
Initialise netsurf core.
Definition: netsurf.c:107
#define SPECULATE_SMALL
speculative pre-conversion small image size
Definition: netsurf.c:75
#define LLCACHE_STORE_MIN_LIFETIME
default minimum object time before object is pushed to backing store.
Definition: netsurf.c:89
#define LLCACHE_STORE_TIME_QUANTUM
default time quantum with which to calculate bandwidth (ms)
Definition: netsurf.c:98
NetSurf core interface registration, construction and destruction.
nserror nscolour_update(void)
Update the nscolour table from the current nsoptions.
Definition: nscolour.c:132
NetSurf UI colours (interface).
nserror page_info_fini(void)
Finalise the page_info module.
Definition: page-info.c:291
nserror page_info_init(void)
Initialise the page_info module.
Definition: page-info.c:258
Pave info viewer window interface.
core web search facilities interface.
Interface to utility string handling.
void(* quit)(void)
called to allow the gui to cleanup.
Definition: misc.h:66
unsigned int bg_clean_time
How frequently the background cache clean process is run (ms)
Definition: hlcache.h:50
struct llcache_parameters llcache
Definition: hlcache.h:52
unsigned int bg_clean_time
How frequently the background cache clean process is run (ms)
Definition: image_cache.h:51
size_t limit
The target upper bound for the image cache size.
Definition: image_cache.h:54
size_t hysteresis
The hysteresis allowed round the target size.
Definition: image_cache.h:57
struct llcache_store_parameters store
Definition: llcache.h:153
size_t limit
The target upper bound for the RAM cache size.
Definition: llcache.h:130
uint32_t fetch_attempts
The number of fetches to attempt when timing out.
Definition: llcache.h:151
size_t hysteresis
The hysteresis around the target size.
Definition: llcache.h:123
size_t limit
The backing store upper bound target size.
Definition: llcache.h:122
const char * path
The path to the backing store.
Definition: llcache.h:120
struct gui_misc_table * misc
Browser table.
Definition: gui_table.h:57
nserror ns_system_colour_init(void)
Initialise the system colours.
Definition: system_colour.c:42
void ns_system_colour_finalize(void)
release any resources associated with the system colours.
Definition: system_colour.c:67
Interface to system colour values.
nserror textplain_init(void)
Initialise the text content handler.
Definition: textplain.c:1654
Interface to content handler for plain text.
void urldb_destroy(void)
Destroy urldb.
Definition: urldb.c:2847
Unified URL information database internal interface.
void free_user_agent_string(void)
Free any memory allocated for the user_agent_string.
Definition: useragent.c:88
Option reading and saving interface.
#define nsoption_charp(OPTION)
Get the value of a string option.
Definition: nsoption.h:331
#define nsoption_int(OPTION)
Get the value of an integer option.
Definition: nsoption.h:313
#define nsoption_uint(OPTION)
Get the value of an unsigned integer option.
Definition: nsoption.h:322
nserror utf8_finalise(void)
Finalise the UTF-8 library.
Definition: utf8.c:197
UTF-8 manipulation functions (interface).