summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/head_cache.js
blob: 388cb184a24904cdd5cdcd46402692589871bba1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
"use strict";

var { XPCOMUtils } = ChromeUtils.import(
  "resource://gre/modules/XPCOMUtils.jsm"
);
var { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");

var _CSvc;
function get_cache_service() {
  if (_CSvc) {
    return _CSvc;
  }

  return (_CSvc = Cc["@mozilla.org/netwerk/cache-storage-service;1"].getService(
    Ci.nsICacheStorageService
  ));
}

function evict_cache_entries(where) {
  var clearDisk = !where || where == "disk" || where == "all";
  var clearMem = !where || where == "memory" || where == "all";
  var clearAppCache = where == "appcache";

  var svc = get_cache_service();
  var storage;

  if (clearMem) {
    storage = svc.memoryCacheStorage(Services.loadContextInfo.default);
    storage.asyncEvictStorage(null);
  }

  if (clearDisk) {
    storage = svc.diskCacheStorage(Services.loadContextInfo.default, false);
    storage.asyncEvictStorage(null);
  }

  if (clearAppCache) {
    storage = svc.appCacheStorage(Services.loadContextInfo.default, null);
    storage.asyncEvictStorage(null);
  }
}

function createURI(urispec) {
  var ioServ = Cc["@mozilla.org/network/io-service;1"].getService(
    Ci.nsIIOService
  );
  return ioServ.newURI(urispec);
}

function getCacheStorage(where, lci, appcache) {
  if (!lci) {
    lci = Services.loadContextInfo.default;
  }
  var svc = get_cache_service();
  switch (where) {
    case "disk":
      return svc.diskCacheStorage(lci, false);
    case "memory":
      return svc.memoryCacheStorage(lci);
    case "appcache":
      return svc.appCacheStorage(lci, appcache);
    case "pin":
      return svc.pinningCacheStorage(lci);
  }
  return null;
}

function asyncOpenCacheEntry(key, where, flags, lci, callback, appcache) {
  key = createURI(key);

  function CacheListener() {}
  CacheListener.prototype = {
    _appCache: appcache,

    QueryInterface: ChromeUtils.generateQI(["nsICacheEntryOpenCallback"]),

    onCacheEntryCheck(entry, appCache) {
      if (typeof callback === "object") {
        return callback.onCacheEntryCheck(entry, appCache);
      }
      return Ci.nsICacheEntryOpenCallback.ENTRY_WANTED;
    },

    onCacheEntryAvailable(entry, isnew, appCache, status) {
      if (typeof callback === "object") {
        // Root us at the callback
        callback.__cache_listener_root = this;
        callback.onCacheEntryAvailable(entry, isnew, appCache, status);
      } else {
        callback(status, entry, appCache);
      }
    },

    run() {
      var storage = getCacheStorage(where, lci, this._appCache);
      storage.asyncOpenURI(key, "", flags, this);
    },
  };

  new CacheListener().run();
}

function syncWithCacheIOThread(callback, force) {
  if (force) {
    asyncOpenCacheEntry(
      "http://nonexistententry/",
      "disk",
      Ci.nsICacheStorage.OPEN_READONLY,
      null,
      function(status, entry) {
        Assert.equal(status, Cr.NS_ERROR_CACHE_KEY_NOT_FOUND);
        callback();
      }
    );
  } else {
    callback();
  }
}

function get_device_entry_count(where, lci, continuation) {
  var storage = getCacheStorage(where, lci);
  if (!storage) {
    continuation(-1, 0);
    return;
  }

  var visitor = {
    onCacheStorageInfo(entryCount, consumption) {
      executeSoon(function() {
        continuation(entryCount, consumption);
      });
    },
  };

  // get the device entry count
  storage.asyncVisitStorage(visitor, false);
}

function asyncCheckCacheEntryPresence(
  key,
  where,
  shouldExist,
  continuation,
  appCache
) {
  asyncOpenCacheEntry(
    key,
    where,
    Ci.nsICacheStorage.OPEN_READONLY,
    null,
    function(status, entry) {
      if (shouldExist) {
        dump("TEST-INFO | checking cache key " + key + " exists @ " + where);
        Assert.equal(status, Cr.NS_OK);
        Assert.ok(!!entry);
      } else {
        dump(
          "TEST-INFO | checking cache key " + key + " doesn't exist @ " + where
        );
        Assert.equal(status, Cr.NS_ERROR_CACHE_KEY_NOT_FOUND);
        Assert.equal(null, entry);
      }
      continuation();
    },
    appCache
  );
}