netwerk/test/unit/head_cache.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 Components.utils.import('resource://gre/modules/XPCOMUtils.jsm');
     2 Components.utils.import('resource://gre/modules/LoadContextInfo.jsm');
     4 var _CSvc;
     5 function get_cache_service() {
     6   if (_CSvc)
     7     return _CSvc;
     9   return _CSvc = Components.classes["@mozilla.org/netwerk/cache-storage-service;1"]
    10                            .getService(Components.interfaces.nsICacheStorageService);
    11 }
    13 function evict_cache_entries(where)
    14 {
    15   var clearDisk = (!where || where == "disk" || where == "all");
    16   var clearMem = (!where || where == "memory" || where == "all");
    17   var clearAppCache = (where == "appcache");
    19   var svc = get_cache_service();
    20   var storage;
    22   if (clearMem) {
    23     storage = svc.memoryCacheStorage(LoadContextInfo.default);
    24     storage.asyncEvictStorage(null);
    25   }
    27   if (clearDisk) {
    28     storage = svc.diskCacheStorage(LoadContextInfo.default, false);
    29     storage.asyncEvictStorage(null);
    30   }
    32   if (clearAppCache) {
    33     storage = svc.appCacheStorage(LoadContextInfo.default, null);
    34     storage.asyncEvictStorage(null);
    35   }
    36 }
    38 function createURI(urispec)
    39 {
    40   var ioServ = Components.classes["@mozilla.org/network/io-service;1"]
    41                          .getService(Components.interfaces.nsIIOService);
    42   return ioServ.newURI(urispec, null, null);
    43 }
    45 function getCacheStorage(where, lci, appcache)
    46 {
    47   if (!lci) lci = LoadContextInfo.default;
    48   var svc = get_cache_service();
    49   switch (where) {
    50     case "disk": return svc.diskCacheStorage(lci, false);
    51     case "memory": return svc.memoryCacheStorage(lci);
    52     case "appcache": return svc.appCacheStorage(lci, appcache);
    53   }
    54   return null;
    55 }
    57 function asyncOpenCacheEntry(key, where, flags, lci, callback, appcache)
    58 {
    59   key = createURI(key);
    61   function CacheListener() { }
    62   CacheListener.prototype = {
    63     _appCache: appcache,
    65     QueryInterface: function (iid) {
    66       if (iid.equals(Components.interfaces.nsICacheEntryOpenCallback) ||
    67           iid.equals(Components.interfaces.nsISupports))
    68         return this;
    69       throw Components.results.NS_ERROR_NO_INTERFACE;
    70     },
    72     onCacheEntryCheck: function(entry, appCache) {
    73       if (typeof callback === "object")
    74         return callback.onCacheEntryCheck(entry, appCache);
    75       return Ci.nsICacheEntryOpenCallback.ENTRY_WANTED;
    76     },
    78     onCacheEntryAvailable: function (entry, isnew, appCache, status) {
    79       if (typeof callback === "object") {
    80         // Root us at the callback
    81         callback.__cache_listener_root = this;
    82         callback.onCacheEntryAvailable(entry, isnew, appCache, status);
    83       }
    84       else
    85         callback(status, entry, appCache);
    86     },
    88     run: function () {
    89       var storage = getCacheStorage(where, lci, this._appCache);
    90       storage.asyncOpenURI(key, "", flags, this);
    91     }
    92   };
    94   (new CacheListener()).run();
    95 }
    97 function syncWithCacheIOThread(callback)
    98 {
    99   if (!newCacheBackEndUsed()) {
   100     asyncOpenCacheEntry(
   101       "http://nonexistententry/", "disk", Ci.nsICacheStorage.OPEN_READONLY, null,
   102       function(status, entry) {
   103         do_check_eq(status, Components.results.NS_ERROR_CACHE_KEY_NOT_FOUND);
   104         callback();
   105       });
   106   }
   107   else {
   108     callback();
   109   }
   110 }
   112 function get_device_entry_count(where, lci, continuation) {
   113   var storage = getCacheStorage(where, lci);
   114   if (!storage) {
   115     continuation(-1, 0);
   116     return;
   117   }
   119   var visitor = {
   120     onCacheStorageInfo: function (entryCount, consumption) {
   121       do_execute_soon(function() {
   122         continuation(entryCount, consumption);
   123       });
   124     },
   125   };
   127   // get the device entry count
   128   storage.asyncVisitStorage(visitor, false);
   129 }
   131 function asyncCheckCacheEntryPresence(key, where, shouldExist, continuation, appCache)
   132 {
   133   asyncOpenCacheEntry(key, where, Ci.nsICacheStorage.OPEN_READONLY, null,
   134     function(status, entry) {
   135       if (shouldExist) {
   136         dump("TEST-INFO | checking cache key " + key + " exists @ " + where);
   137         do_check_eq(status, Cr.NS_OK);
   138         do_check_true(!!entry);
   139       } else {
   140         dump("TEST-INFO | checking cache key " + key + " doesn't exist @ " + where);
   141         do_check_eq(status, Cr.NS_ERROR_CACHE_KEY_NOT_FOUND);
   142         do_check_null(entry);
   143       }
   144       continuation();
   145     }, appCache);
   146 }

mercurial