Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  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.  
  27. #include <libwapcaplet/libwapcaplet.h>
  28.  
  29. #include "utils/config.h"
  30. #include "utils/utsname.h"
  31. #include "content/content_factory.h"
  32. #include "content/fetch.h"
  33. #include "content/hlcache.h"
  34. #include "content/mimesniff.h"
  35. #include "content/urldb.h"
  36. #include "css/css.h"
  37. #include "image/image.h"
  38. #include "image/image_cache.h"
  39. #include "desktop/netsurf.h"
  40. #include "desktop/401login.h"
  41. #include "desktop/browser.h"
  42. #include "desktop/gui.h"
  43. #include "desktop/options.h"
  44. #include "desktop/searchweb.h"
  45.  
  46. #include "javascript/js.h"
  47.  
  48. #include "render/html.h"
  49. #include "render/textplain.h"
  50. #include "utils/corestrings.h"
  51. #include "utils/log.h"
  52. #include "utils/url.h"
  53. #include "utils/utf8.h"
  54. #include "utils/utils.h"
  55. #include "utils/messages.h"
  56.  
  57. /** speculative pre-conversion small image size
  58.  *
  59.  * Experimenting by visiting every page from default page in order and
  60.  * then netsurf homepage
  61.  *
  62.  * 0    : Cache hit/miss/speculative miss/fail 604/147/  0/0 (80%/19%/ 0%/ 0%)
  63.  * 2048 : Cache hit/miss/speculative miss/fail 622/119/ 17/0 (82%/15%/ 2%/ 0%)
  64.  * 4096 : Cache hit/miss/speculative miss/fail 656/109/ 25/0 (83%/13%/ 3%/ 0%)
  65.  * 8192 : Cache hit/miss/speculative miss/fail 648/104/ 40/0 (81%/13%/ 5%/ 0%)
  66.  * ALL  : Cache hit/miss/speculative miss/fail 775/  0/161/0 (82%/ 0%/17%/ 0%)
  67. */
  68. #define SPECULATE_SMALL 4096
  69.  
  70. /* the time between cache clean runs in ms */
  71. #define IMAGE_CACHE_CLEAN_TIME (10 * 1000)
  72.  
  73. #define HL_CACHE_CLEAN_TIME (2 * IMAGE_CACHE_CLEAN_TIME)
  74.  
  75. bool netsurf_quit = false;
  76. bool verbose_log = false;
  77.  
  78. static void netsurf_lwc_iterator(lwc_string *str, void *pw)
  79. {
  80.         LOG(("[%3u] %.*s", str->refcnt, (int) lwc_string_length(str), lwc_string_data(str)));
  81. }
  82.  
  83. /**
  84.  * Dispatch a low-level cache query to the frontend
  85.  *
  86.  * \param query  Query descriptor
  87.  * \param pw     Private data
  88.  * \param cb     Continuation callback
  89.  * \param cbpw   Private data for continuation
  90.  * \return NSERROR_OK
  91.  */
  92. static nserror netsurf_llcache_query_handler(const llcache_query *query,
  93.                 void *pw, llcache_query_response cb, void *cbpw)
  94. {
  95.         switch (query->type) {
  96.         case LLCACHE_QUERY_AUTH:
  97.                 gui_401login_open(query->url, query->data.auth.realm, cb, cbpw);
  98.                 break;
  99.         case LLCACHE_QUERY_REDIRECT:
  100.                 /** \todo Need redirect query dialog */
  101.                 /* For now, do nothing, as this query type isn't emitted yet */
  102.                 break;
  103.         case LLCACHE_QUERY_SSL:
  104.                 gui_cert_verify(query->url, query->data.ssl.certs,
  105.                                 query->data.ssl.num, cb, cbpw);
  106.                 break;
  107.         }
  108.  
  109.         return NSERROR_OK;
  110. }
  111.  
  112. #define MINIMUM_MEMORY_CACHE_SIZE (2 * 1024 * 1024)
  113.  
  114. /**
  115.  * Initialise components used by gui NetSurf.
  116.  */
  117.  
  118. nserror netsurf_init(int *pargc,
  119.                      char ***pargv,
  120.                      const char *options,
  121.                      const char *messages)
  122. {
  123.         nserror error;
  124.         struct utsname utsname;
  125.         nserror ret = NSERROR_OK;
  126.         struct hlcache_parameters hlcache_parameters = {
  127.                 .bg_clean_time = HL_CACHE_CLEAN_TIME,
  128.                 .cb = netsurf_llcache_query_handler,
  129.         };
  130.         struct image_cache_parameters image_cache_parameters = {
  131.                 .bg_clean_time = IMAGE_CACHE_CLEAN_TIME,
  132.                 .speculative_small = SPECULATE_SMALL
  133.         };
  134.        
  135. #ifdef HAVE_SIGPIPE
  136.         /* Ignore SIGPIPE - this is necessary as OpenSSL can generate these
  137.          * and the default action is to terminate the app. There's no easy
  138.          * way of determining the cause of the SIGPIPE (other than using
  139.          * sigaction() and some mechanism for getting the file descriptor
  140.          * out of libcurl). However, we expect nothing else to generate a
  141.          * SIGPIPE, anyway, so may as well just ignore them all. */
  142.        
  143.         signal(SIGPIPE, SIG_IGN);
  144. #endif
  145.  
  146. #ifndef HAVE_STDOUT
  147.         ret = nslog_init(nslog_ensure, pargc, *pargv);
  148. #else
  149.         ret = nslog_init(NULL, pargc, *pargv);
  150. #endif
  151.  
  152.         if (ret != NSERROR_OK)
  153.                 return ret;
  154.  
  155. #ifdef _MEMDEBUG_H_
  156.         memdebug_memdebug("memdump");
  157. #endif
  158.         LOG(("version '%s'", netsurf_version));
  159.         if (uname(&utsname) < 0)
  160.                 LOG(("Failed to extract machine information"));
  161.         else
  162.                 LOG(("NetSurf on <%s>, node <%s>, release <%s>, version <%s>, "
  163.                                 "machine <%s>", utsname.sysname,
  164.                                 utsname.nodename, utsname.release,
  165.                                 utsname.version, utsname.machine));
  166.  
  167.         LOG(("Using '%s' for Options file", options));
  168.         nsoption_read(options);
  169.         gui_options_init_defaults();
  170.  
  171.         messages_load(messages);
  172.  
  173.         /* corestrings init */
  174.         error = corestrings_init();
  175.         if (error != NSERROR_OK)
  176.                 return error;
  177.  
  178.         /* set up cache limits based on the memory cache size option */
  179.         hlcache_parameters.limit = nsoption_int(memory_cache_size);
  180.  
  181.         if (hlcache_parameters.limit < MINIMUM_MEMORY_CACHE_SIZE) {
  182.                 hlcache_parameters.limit = MINIMUM_MEMORY_CACHE_SIZE;
  183.                 LOG(("Setting minimum memory cache size to %d",
  184.                      hlcache_parameters.limit));
  185.         }
  186.  
  187.         /* image cache is 25% of total memory cache size */
  188.         image_cache_parameters.limit = (hlcache_parameters.limit * 25) / 100;
  189.  
  190.         /* image cache hysteresis is 20% of teh image cache size */
  191.         image_cache_parameters.hysteresis = (image_cache_parameters.limit * 20) / 100;
  192.  
  193.         /* account for image cache use from total */
  194.         hlcache_parameters.limit -= image_cache_parameters.limit;
  195.  
  196.         /* image handler bitmap cache */
  197.         error = image_cache_init(&image_cache_parameters);
  198.         if (error != NSERROR_OK)
  199.                 return error;
  200.  
  201.         /* content handler initialisation */
  202.         error = nscss_init();
  203.         if (error != NSERROR_OK)
  204.                 return error;
  205.  
  206.         error = html_init();
  207.         if (error != NSERROR_OK)
  208.                 return error;
  209.  
  210.         error = image_init();
  211.         if (error != NSERROR_OK)
  212.                 return error;
  213.  
  214.         error = textplain_init();
  215.         if (error != NSERROR_OK)
  216.                 return error;
  217.  
  218.  
  219.         error = mimesniff_init();
  220.         if (error != NSERROR_OK)
  221.                 return error;
  222.  
  223.         url_init();
  224.  
  225.         setlocale(LC_ALL, "C");
  226.  
  227.         fetch_init();
  228.        
  229.         /* Initialise the hlcache and allow it to init the llcache for us */
  230.         hlcache_initialise(&hlcache_parameters);
  231.  
  232.         /* Initialize system colours */
  233.         gui_system_colour_init();
  234.  
  235.         nsoption_commandline(pargc, *pargv);
  236.  
  237.         js_initialise();
  238.  
  239.         return ret;
  240. }
  241.  
  242.  
  243. /**
  244.  * Gui NetSurf main loop.
  245.  */
  246. int netsurf_main_loop(void)
  247. {
  248.         while (!netsurf_quit) {
  249.                 LOG(("GUI POLL"));
  250.                 gui_poll(fetch_active);
  251.                 LOG(("CACHE POLL"));
  252.                 hlcache_poll();
  253.         }
  254.  
  255.         return 0;
  256. }
  257.  
  258. /**
  259.  * Clean up components used by gui NetSurf.
  260.  */
  261.  
  262. void netsurf_exit(void)
  263. {
  264.         hlcache_stop();
  265.        
  266.         LOG(("Closing GUI"));
  267.         gui_quit();
  268.        
  269.         LOG(("Finalising JavaScript"));
  270.         js_finalise();
  271.  
  272.         LOG(("Closing search and related resources"));
  273.         search_web_cleanup();
  274.  
  275.         LOG(("Finalising high-level cache"));
  276.         hlcache_finalise();
  277.  
  278.         LOG(("Closing fetches"));
  279.         fetch_quit();
  280.  
  281.         mimesniff_fini();
  282.  
  283.         /* dump any remaining cache entries */
  284.         image_cache_fini();
  285.  
  286.         /* Clean up after content handlers */
  287.         content_factory_fini();
  288.  
  289.         LOG(("Closing utf8"));
  290.         utf8_finalise();
  291.  
  292.         LOG(("Destroying URLdb"));
  293.         urldb_destroy();
  294.  
  295.         LOG(("Destroying System colours"));
  296.         gui_system_colour_finalize();
  297.  
  298.         corestrings_fini();
  299.         LOG(("Remaining lwc strings:"));
  300.         lwc_iterate_strings(netsurf_lwc_iterator, NULL);
  301.  
  302.         LOG(("Exited successfully"));
  303. }
  304.  
  305.  
  306.