home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / libnet / prefetch.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  5.7 KB  |  235 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18. /* designed/implemented by Gagan Saksena */
  19.  
  20. #include "mkutils.h"
  21. #include "xp.h"
  22. #include "prefetch.h"
  23. #include "net.h"
  24. #include "xp_list.h"
  25. #include "prefapi.h"
  26.  
  27. /*    The PrefetchURLStruct which holds the URL struct
  28.     and the corresponding prefetch value.
  29. */
  30. typedef struct _PrefetchURLStruct {
  31.     double             prevalue;
  32.     URL_Struct*        URL_s;
  33. } PrefetchURLStruct;
  34.  
  35. PRIVATE XP_List* prefetch_list = 0;
  36.  
  37. #define USER_SETTING 0.0 /* TODO Change this to read off of the prefs.js file */
  38.  
  39. MODULE_PRIVATE void pre_FreePrefetchURLStruct(PrefetchURLStruct *pus);
  40. MODULE_PRIVATE Bool pre_OKToPrefetch(char* url);
  41. MODULE_PRIVATE void pre_Finished(URL_Struct* url_struct, int status, MWContext* context);
  42.  
  43. PRIVATE XP_Bool pre_enabled = TRUE;
  44. PRIVATE int pre_LockNormalizeAndSort(); 
  45. PRIVATE void pre_ProcessList(MWContext* context); 
  46.  
  47. /*    Constructs a URL_Struct from the the specified URL to the prefetch_list based
  48.     on the value of the pre subtag. 
  49. */
  50. PUBLIC void
  51. PRE_AddToList(MWContext* context, char* url, double value) 
  52. {
  53.     /* Construct a new URL_Struct with this url, and Prefetch priority */
  54.     URL_Struct* urls;
  55.     PrefetchURLStruct *pus = XP_NEW(PrefetchURLStruct); 
  56.  
  57.     if (!pre_enabled || !value || !pus || !pre_OKToPrefetch(url))
  58.         return;
  59.  
  60.     urls = NET_CreateURLStruct(url, NET_DONT_RELOAD);
  61.     if (!urls)
  62.         return;
  63.     
  64.     urls->priority = Prefetch_priority;
  65.     urls->load_background = TRUE;
  66.                     
  67.     if (prefetch_list == NULL)
  68.     {
  69.         prefetch_list = XP_ListNew();
  70.     }
  71.     
  72.     pus->prevalue = value;
  73.     pus->URL_s = urls;
  74.  
  75.     XP_ListAddObjectToEnd(prefetch_list, pus);
  76. }
  77.  
  78.  
  79. /*  The main process for each MWContext, should be called after 
  80.     a page is finished loading. This basically extracts all the 
  81.     links on the current page and eliminates the non relevant ones
  82.     and adds the elements to the prefetch_list. If something
  83.     has been added, then it is passed on to NET_GetURL function
  84.     for prefetching and storing in cache. 
  85. */
  86. PUBLIC void 
  87. PRE_Fetch(MWContext* context)
  88. {
  89.     LO_TabFocusData tfd;
  90.     int found ;
  91.  
  92.     if (!pre_enabled)
  93.         return;
  94.  
  95.     tfd.pElement        = NULL;
  96.     tfd.mapAreaIndex    = 0;        /* 0 means no focus, start with index 1. */
  97.     tfd.pAnchor            = NULL;
  98.     
  99.     do 
  100.     {
  101.         if (found = LO_getNextTabableElement(
  102.             context, 
  103.             &tfd, 
  104.             1) != 0)
  105.         {
  106.             if (tfd.pElement && 
  107.                     (tfd.pElement->lo_any.type == LO_TEXT ||    
  108.                         tfd.pElement->lo_any.type == LO_IMAGE))    /* Image Maps */
  109.             {
  110.                 PRE_AddToList(context, (char*)tfd.pAnchor->anchor, tfd.pAnchor->prevalue);
  111.             }
  112.         }
  113.     }
  114.     while (found != 0);
  115.  
  116.     pre_ProcessList(context);
  117.     XP_ListDestroy(prefetch_list); 
  118.     prefetch_list = NULL;
  119. }
  120.  
  121. /*    Returns bool to indicate if its OK to prefetch the specified URL.
  122.     we don't prefetch mailto:, file:, etc. 
  123. */
  124. PRIVATE Bool
  125. pre_OKToPrefetch(char* url)
  126. {
  127.     int type;
  128.     if (url == NULL)
  129.         return FALSE;
  130.  
  131.     /* Add new logic here */
  132.     /* (skip about:/mailto:/telnet:/gopher:/ etc.) */
  133.     type = NET_URL_Type(url);
  134.     if (type && (
  135.         (type == HTTP_TYPE_URL) ||
  136.         (type == GOPHER_TYPE_URL)))
  137.         return TRUE;
  138.  
  139.     return FALSE;
  140. }
  141.  
  142. /*    Lock the prefetch_list and normalize the item values.
  143. */
  144. PRIVATE int 
  145. pre_LockNormalizeAndSort() 
  146. {
  147.     int count;
  148.     double sum=0;
  149.     PrefetchURLStruct* pus = NULL;
  150.     XP_List* pList = prefetch_list;
  151.  
  152.     count = XP_ListCount(prefetch_list);
  153.     if (0 == count)
  154.         return 0;
  155.  
  156.     while((pus = (PrefetchURLStruct*)XP_ListNextObject(pList)))
  157.         sum += pus->prevalue;
  158.     
  159.     if (sum != 0)
  160.     {
  161.         XP_List* pList = prefetch_list;
  162.         while((pus = (PrefetchURLStruct*)XP_ListNextObject(pList)))
  163.             pus->prevalue = pus->prevalue/sum;
  164.     }
  165.     else 
  166.         return -1;
  167.     return count;
  168. }
  169.  
  170. MODULE_PRIVATE void 
  171. pre_FreePrefetchURLStruct(PrefetchURLStruct *pus)
  172. {
  173.     if(pus) 
  174.       {
  175.         FREEIF(pus->URL_s);
  176.         XP_FREE(pus);
  177.       }
  178. }
  179.  
  180. PRIVATE void
  181. pre_Finished(URL_Struct* url_struct, int status, MWContext* context)
  182. {
  183.     /* this should change to update the colors of 
  184.     the prefetched links */
  185.     XP_FREE(url_struct);
  186.     url_struct = 0;
  187. }
  188.  
  189. PRIVATE void
  190. pre_ProcessList(MWContext* context) 
  191. {
  192.     int count = XP_ListCount(prefetch_list);
  193.     if (XP_ListCount(prefetch_list)>0) 
  194.     {
  195.         /* Normalize the prefetch-list based on the values */
  196.         if (pre_LockNormalizeAndSort() > 0) 
  197.         {
  198.             /* Invoke NET_GetURL on the prefetch_list */
  199.             int i;
  200.             for (i=0; i<count; i++) {
  201.                 PrefetchURLStruct* pusTop;
  202.                 PrefetchURLStruct* pus;
  203.                 XP_List* pList = prefetch_list;
  204.  
  205.                 while((pus = (PrefetchURLStruct*)XP_ListNextObject(pList)))
  206.                 {
  207.                     if (pusTop) 
  208.                     {
  209.                         if (pusTop->prevalue < pus->prevalue)
  210.                             pusTop = pus;
  211.                     }
  212.                     else
  213.                         pusTop = pus;
  214.  
  215.                 }
  216.                 if (pusTop->prevalue > USER_SETTING)
  217.                 {
  218.                     NET_GetURL (pus->URL_s,
  219.                         FO_CACHE_ONLY,
  220.                         context,
  221.                         pre_Finished); 
  222.                 }
  223.                 XP_ListRemoveObject(prefetch_list, pusTop);
  224.             }
  225.         }
  226.     }
  227. }
  228.  
  229. /* Enable or disable the prefetching, called from NET_SetupPrefs in mkgeturl.c */
  230. PUBLIC void
  231. PRE_Enable(XP_Bool enabled)
  232. {
  233.     pre_enabled = enabled;
  234. }
  235.