Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | const Cc = Components.classes; |
michael@0 | 2 | const Ci = Components.interfaces; |
michael@0 | 3 | const Cu = Components.utils; |
michael@0 | 4 | const Cr = Components.results; |
michael@0 | 5 | |
michael@0 | 6 | function newCacheBackEndUsed() |
michael@0 | 7 | { |
michael@0 | 8 | var cache1srv = Components.classes["@mozilla.org/network/cache-service;1"] |
michael@0 | 9 | .getService(Components.interfaces.nsICacheService); |
michael@0 | 10 | var cache2srv = Components.classes["@mozilla.org/netwerk/cache-storage-service;1"] |
michael@0 | 11 | .getService(Components.interfaces.nsICacheStorageService); |
michael@0 | 12 | |
michael@0 | 13 | return cache1srv.cacheIOTarget != cache2srv.ioTarget; |
michael@0 | 14 | } |
michael@0 | 15 | |
michael@0 | 16 | var callbacks = new Array(); |
michael@0 | 17 | |
michael@0 | 18 | // Expect an existing entry |
michael@0 | 19 | const NORMAL = 0; |
michael@0 | 20 | // Expect a new entry |
michael@0 | 21 | const NEW = 1 << 0; |
michael@0 | 22 | // Return early from onCacheEntryCheck and set the callback to state it expects onCacheEntryCheck to happen |
michael@0 | 23 | const NOTVALID = 1 << 1; |
michael@0 | 24 | // Throw from onCacheEntryAvailable |
michael@0 | 25 | const THROWAVAIL = 1 << 2; |
michael@0 | 26 | // Open entry for reading-only |
michael@0 | 27 | const READONLY = 1 << 3; |
michael@0 | 28 | // Expect the entry to not be found |
michael@0 | 29 | const NOTFOUND = 1 << 4; |
michael@0 | 30 | // Return ENTRY_NEEDS_REVALIDATION from onCacheEntryCheck |
michael@0 | 31 | const REVAL = 1 << 5; |
michael@0 | 32 | // Return ENTRY_PARTIAL from onCacheEntryCheck, in combo with NEW or RECREATE bypasses check for emptiness of the entry |
michael@0 | 33 | const PARTIAL = 1 << 6 |
michael@0 | 34 | // Expect the entry is doomed, i.e. the output stream should not be possible to open |
michael@0 | 35 | const DOOMED = 1 << 7; |
michael@0 | 36 | // Don't trigger the go-on callback until the entry is written |
michael@0 | 37 | const WAITFORWRITE = 1 << 8; |
michael@0 | 38 | // Don't write data (i.e. don't open output stream) |
michael@0 | 39 | const METAONLY = 1 << 9; |
michael@0 | 40 | // Do recreation of an existing cache entry |
michael@0 | 41 | const RECREATE = 1 << 10; |
michael@0 | 42 | // Do not give me the entry |
michael@0 | 43 | const NOTWANTED = 1 << 11; |
michael@0 | 44 | // Tell the cache to wait for the entry to be completely written first |
michael@0 | 45 | const COMPLETE = 1 << 12; |
michael@0 | 46 | |
michael@0 | 47 | var log_c2 = true; |
michael@0 | 48 | function LOG_C2(o, m) |
michael@0 | 49 | { |
michael@0 | 50 | if (!log_c2) return; |
michael@0 | 51 | if (!m) |
michael@0 | 52 | dump("TEST-INFO | CACHE2: " + o + "\n"); |
michael@0 | 53 | else |
michael@0 | 54 | dump("TEST-INFO | CACHE2: callback #" + o.order + "(" + (o.workingData || "---") + ") " + m + "\n"); |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | function pumpReadStream(inputStream, goon) |
michael@0 | 58 | { |
michael@0 | 59 | if (inputStream.isNonBlocking()) { |
michael@0 | 60 | // non-blocking stream, must read via pump |
michael@0 | 61 | var pump = Cc["@mozilla.org/network/input-stream-pump;1"] |
michael@0 | 62 | .createInstance(Ci.nsIInputStreamPump); |
michael@0 | 63 | pump.init(inputStream, -1, -1, 0, 0, true); |
michael@0 | 64 | var data = ""; |
michael@0 | 65 | pump.asyncRead({ |
michael@0 | 66 | onStartRequest: function (aRequest, aContext) { }, |
michael@0 | 67 | onDataAvailable: function (aRequest, aContext, aInputStream, aOffset, aCount) |
michael@0 | 68 | { |
michael@0 | 69 | var wrapper = Cc["@mozilla.org/scriptableinputstream;1"]. |
michael@0 | 70 | createInstance(Ci.nsIScriptableInputStream); |
michael@0 | 71 | wrapper.init(aInputStream); |
michael@0 | 72 | var str = wrapper.read(wrapper.available()); |
michael@0 | 73 | LOG_C2("reading data '" + str.substring(0,5) + "'"); |
michael@0 | 74 | data += str; |
michael@0 | 75 | }, |
michael@0 | 76 | onStopRequest: function (aRequest, aContext, aStatusCode) |
michael@0 | 77 | { |
michael@0 | 78 | LOG_C2("done reading data: " + aStatusCode); |
michael@0 | 79 | do_check_eq(aStatusCode, Cr.NS_OK); |
michael@0 | 80 | goon(data); |
michael@0 | 81 | }, |
michael@0 | 82 | }, null); |
michael@0 | 83 | } |
michael@0 | 84 | else { |
michael@0 | 85 | // blocking stream |
michael@0 | 86 | var data = read_stream(inputStream, inputStream.available()); |
michael@0 | 87 | goon(data); |
michael@0 | 88 | } |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | OpenCallback.prototype = |
michael@0 | 92 | { |
michael@0 | 93 | QueryInterface: function listener_qi(iid) { |
michael@0 | 94 | if (iid.equals(Ci.nsISupports) || |
michael@0 | 95 | iid.equals(Ci.nsICacheEntryOpenCallback)) { |
michael@0 | 96 | return this; |
michael@0 | 97 | } |
michael@0 | 98 | throw Components.results.NS_ERROR_NO_INTERFACE; |
michael@0 | 99 | }, |
michael@0 | 100 | onCacheEntryCheck: function(entry, appCache) |
michael@0 | 101 | { |
michael@0 | 102 | LOG_C2(this, "onCacheEntryCheck"); |
michael@0 | 103 | do_check_true(!this.onCheckPassed); |
michael@0 | 104 | this.onCheckPassed = true; |
michael@0 | 105 | |
michael@0 | 106 | if (this.behavior & NOTVALID) { |
michael@0 | 107 | LOG_C2(this, "onCacheEntryCheck DONE, return ENTRY_WANTED"); |
michael@0 | 108 | return Ci.nsICacheEntryOpenCallback.ENTRY_WANTED; |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | if (this.behavior & NOTWANTED) { |
michael@0 | 112 | LOG_C2(this, "onCacheEntryCheck DONE, return ENTRY_NOT_WANTED"); |
michael@0 | 113 | return Ci.nsICacheEntryOpenCallback.ENTRY_NOT_WANTED; |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | do_check_eq(entry.getMetaDataElement("meto"), this.workingMetadata); |
michael@0 | 117 | |
michael@0 | 118 | // check for sane flag combination |
michael@0 | 119 | do_check_neq(this.behavior & (REVAL|PARTIAL), REVAL|PARTIAL); |
michael@0 | 120 | |
michael@0 | 121 | if (this.behavior & (REVAL|PARTIAL)) { |
michael@0 | 122 | LOG_C2(this, "onCacheEntryCheck DONE, return ENTRY_NEEDS_REVALIDATION"); |
michael@0 | 123 | return Ci.nsICacheEntryOpenCallback.ENTRY_NEEDS_REVALIDATION; |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | if (this.behavior & COMPLETE) { |
michael@0 | 127 | LOG_C2(this, "onCacheEntryCheck DONE, return RECHECK_AFTER_WRITE_FINISHED"); |
michael@0 | 128 | if (newCacheBackEndUsed()) { |
michael@0 | 129 | // Specific to the new backend because of concurrent read/write: |
michael@0 | 130 | // when a consumer returns RECHECK_AFTER_WRITE_FINISHED from onCacheEntryCheck |
michael@0 | 131 | // the cache calls this callback again after the entry write has finished. |
michael@0 | 132 | // This gives the consumer a chance to recheck completeness of the entry |
michael@0 | 133 | // again. |
michael@0 | 134 | // Thus, we reset state as onCheck would have never been called. |
michael@0 | 135 | this.onCheckPassed = false; |
michael@0 | 136 | // Don't return RECHECK_AFTER_WRITE_FINISHED on second call of onCacheEntryCheck. |
michael@0 | 137 | this.behavior &= ~COMPLETE; |
michael@0 | 138 | } |
michael@0 | 139 | return Ci.nsICacheEntryOpenCallback.RECHECK_AFTER_WRITE_FINISHED; |
michael@0 | 140 | } |
michael@0 | 141 | |
michael@0 | 142 | LOG_C2(this, "onCacheEntryCheck DONE, return ENTRY_WANTED"); |
michael@0 | 143 | return Ci.nsICacheEntryOpenCallback.ENTRY_WANTED; |
michael@0 | 144 | }, |
michael@0 | 145 | onCacheEntryAvailable: function(entry, isnew, appCache, status) |
michael@0 | 146 | { |
michael@0 | 147 | LOG_C2(this, "onCacheEntryAvailable, " + this.behavior); |
michael@0 | 148 | do_check_true(!this.onAvailPassed); |
michael@0 | 149 | this.onAvailPassed = true; |
michael@0 | 150 | |
michael@0 | 151 | do_check_eq(isnew, !!(this.behavior & NEW)); |
michael@0 | 152 | |
michael@0 | 153 | if (this.behavior & (NOTFOUND|NOTWANTED)) { |
michael@0 | 154 | do_check_eq(status, Cr.NS_ERROR_CACHE_KEY_NOT_FOUND); |
michael@0 | 155 | do_check_false(!!entry); |
michael@0 | 156 | if (this.behavior & THROWAVAIL) |
michael@0 | 157 | this.throwAndNotify(entry); |
michael@0 | 158 | this.goon(entry); |
michael@0 | 159 | } |
michael@0 | 160 | else if (this.behavior & (NEW|RECREATE)) { |
michael@0 | 161 | do_check_true(!!entry); |
michael@0 | 162 | |
michael@0 | 163 | if (this.behavior & RECREATE) { |
michael@0 | 164 | entry = entry.recreate(); |
michael@0 | 165 | do_check_true(!!entry); |
michael@0 | 166 | } |
michael@0 | 167 | |
michael@0 | 168 | if (this.behavior & THROWAVAIL) |
michael@0 | 169 | this.throwAndNotify(entry); |
michael@0 | 170 | |
michael@0 | 171 | if (!(this.behavior & WAITFORWRITE)) |
michael@0 | 172 | this.goon(entry); |
michael@0 | 173 | |
michael@0 | 174 | if (!(this.behavior & PARTIAL)) { |
michael@0 | 175 | try { |
michael@0 | 176 | entry.getMetaDataElement("meto"); |
michael@0 | 177 | do_check_true(false); |
michael@0 | 178 | } |
michael@0 | 179 | catch (ex) {} |
michael@0 | 180 | } |
michael@0 | 181 | |
michael@0 | 182 | var self = this; |
michael@0 | 183 | do_execute_soon(function() { // emulate network latency |
michael@0 | 184 | entry.setMetaDataElement("meto", self.workingMetadata); |
michael@0 | 185 | entry.metaDataReady(); |
michael@0 | 186 | if (self.behavior & METAONLY) { |
michael@0 | 187 | // Since forcing GC/CC doesn't trigger OnWriterClosed, we have to set the entry valid manually :( |
michael@0 | 188 | entry.setValid(); |
michael@0 | 189 | entry.close(); |
michael@0 | 190 | return; |
michael@0 | 191 | } |
michael@0 | 192 | do_execute_soon(function() { // emulate more network latency |
michael@0 | 193 | if (self.behavior & DOOMED) { |
michael@0 | 194 | try { |
michael@0 | 195 | var os = entry.openOutputStream(0); |
michael@0 | 196 | do_check_true(false); |
michael@0 | 197 | } catch (ex) { |
michael@0 | 198 | do_check_true(true); |
michael@0 | 199 | } |
michael@0 | 200 | if (self.behavior & WAITFORWRITE) |
michael@0 | 201 | self.goon(entry); |
michael@0 | 202 | return; |
michael@0 | 203 | } |
michael@0 | 204 | |
michael@0 | 205 | var offset = (self.behavior & PARTIAL) |
michael@0 | 206 | ? entry.dataSize |
michael@0 | 207 | : 0; |
michael@0 | 208 | LOG_C2(self, "openOutputStream @ " + offset); |
michael@0 | 209 | var os = entry.openOutputStream(offset); |
michael@0 | 210 | LOG_C2(self, "writing data"); |
michael@0 | 211 | var wrt = os.write(self.workingData, self.workingData.length); |
michael@0 | 212 | do_check_eq(wrt, self.workingData.length); |
michael@0 | 213 | os.close(); |
michael@0 | 214 | if (self.behavior & WAITFORWRITE) |
michael@0 | 215 | self.goon(entry); |
michael@0 | 216 | |
michael@0 | 217 | entry.close(); |
michael@0 | 218 | }) |
michael@0 | 219 | }) |
michael@0 | 220 | } |
michael@0 | 221 | else { // NORMAL |
michael@0 | 222 | do_check_true(!!entry); |
michael@0 | 223 | do_check_eq(entry.getMetaDataElement("meto"), this.workingMetadata); |
michael@0 | 224 | if (this.behavior & THROWAVAIL) |
michael@0 | 225 | this.throwAndNotify(entry); |
michael@0 | 226 | |
michael@0 | 227 | var wrapper = Cc["@mozilla.org/scriptableinputstream;1"]. |
michael@0 | 228 | createInstance(Ci.nsIScriptableInputStream); |
michael@0 | 229 | var self = this; |
michael@0 | 230 | pumpReadStream(entry.openInputStream(0), function(data) { |
michael@0 | 231 | do_check_eq(data, self.workingData); |
michael@0 | 232 | self.onDataCheckPassed = true; |
michael@0 | 233 | LOG_C2(self, "entry read done"); |
michael@0 | 234 | self.goon(entry); |
michael@0 | 235 | entry.close(); |
michael@0 | 236 | }); |
michael@0 | 237 | } |
michael@0 | 238 | }, |
michael@0 | 239 | selfCheck: function() |
michael@0 | 240 | { |
michael@0 | 241 | LOG_C2(this, "selfCheck"); |
michael@0 | 242 | |
michael@0 | 243 | do_check_true(this.onCheckPassed); |
michael@0 | 244 | do_check_true(this.onAvailPassed); |
michael@0 | 245 | do_check_true(this.onDataCheckPassed); |
michael@0 | 246 | }, |
michael@0 | 247 | throwAndNotify: function(entry) |
michael@0 | 248 | { |
michael@0 | 249 | LOG_C2(this, "Throwing"); |
michael@0 | 250 | var self = this; |
michael@0 | 251 | do_execute_soon(function() { |
michael@0 | 252 | LOG_C2(self, "Notifying"); |
michael@0 | 253 | self.goon(entry); |
michael@0 | 254 | }); |
michael@0 | 255 | throw Cr.NS_ERROR_FAILURE; |
michael@0 | 256 | } |
michael@0 | 257 | }; |
michael@0 | 258 | |
michael@0 | 259 | function OpenCallback(behavior, workingMetadata, workingData, goon) |
michael@0 | 260 | { |
michael@0 | 261 | this.behavior = behavior; |
michael@0 | 262 | this.workingMetadata = workingMetadata; |
michael@0 | 263 | this.workingData = workingData; |
michael@0 | 264 | this.goon = goon; |
michael@0 | 265 | this.onCheckPassed = (!!(behavior & (NEW|RECREATE)) || !workingMetadata) && !(behavior & NOTVALID); |
michael@0 | 266 | this.onAvailPassed = false; |
michael@0 | 267 | this.onDataCheckPassed = !!(behavior & (NEW|RECREATE|NOTWANTED)) || !workingMetadata; |
michael@0 | 268 | callbacks.push(this); |
michael@0 | 269 | this.order = callbacks.length; |
michael@0 | 270 | } |
michael@0 | 271 | |
michael@0 | 272 | VisitCallback.prototype = |
michael@0 | 273 | { |
michael@0 | 274 | QueryInterface: function listener_qi(iid) { |
michael@0 | 275 | if (iid.equals(Ci.nsISupports) || |
michael@0 | 276 | iid.equals(Ci.nsICacheStorageVisitor)) { |
michael@0 | 277 | return this; |
michael@0 | 278 | } |
michael@0 | 279 | throw Components.results.NS_ERROR_NO_INTERFACE; |
michael@0 | 280 | }, |
michael@0 | 281 | onCacheStorageInfo: function(num, consumption) |
michael@0 | 282 | { |
michael@0 | 283 | LOG_C2(this, "onCacheStorageInfo: num=" + num + ", size=" + consumption); |
michael@0 | 284 | do_check_eq(this.num, num); |
michael@0 | 285 | if (newCacheBackEndUsed()) { |
michael@0 | 286 | // Consumption is as expected only in the new backend |
michael@0 | 287 | do_check_eq(this.consumption, consumption); |
michael@0 | 288 | } |
michael@0 | 289 | if (!this.entries) |
michael@0 | 290 | this.notify(); |
michael@0 | 291 | }, |
michael@0 | 292 | onCacheEntryInfo: function(entry) |
michael@0 | 293 | { |
michael@0 | 294 | var key = entry.key; |
michael@0 | 295 | LOG_C2(this, "onCacheEntryInfo: key=" + key); |
michael@0 | 296 | |
michael@0 | 297 | do_check_true(!!this.entries); |
michael@0 | 298 | |
michael@0 | 299 | var index = this.entries.indexOf(key); |
michael@0 | 300 | do_check_true(index > -1); |
michael@0 | 301 | |
michael@0 | 302 | this.entries.splice(index, 1); |
michael@0 | 303 | }, |
michael@0 | 304 | onCacheEntryVisitCompleted: function() |
michael@0 | 305 | { |
michael@0 | 306 | LOG_C2(this, "onCacheEntryVisitCompleted"); |
michael@0 | 307 | if (this.entries) |
michael@0 | 308 | do_check_eq(this.entries.length, 0); |
michael@0 | 309 | this.notify(); |
michael@0 | 310 | }, |
michael@0 | 311 | notify: function() |
michael@0 | 312 | { |
michael@0 | 313 | do_check_true(!!this.goon); |
michael@0 | 314 | var goon = this.goon; |
michael@0 | 315 | this.goon = null; |
michael@0 | 316 | do_execute_soon(goon); |
michael@0 | 317 | }, |
michael@0 | 318 | selfCheck: function() |
michael@0 | 319 | { |
michael@0 | 320 | do_check_true(!this.entries || !this.entries.length); |
michael@0 | 321 | } |
michael@0 | 322 | }; |
michael@0 | 323 | |
michael@0 | 324 | function VisitCallback(num, consumption, entries, goon) |
michael@0 | 325 | { |
michael@0 | 326 | this.num = num; |
michael@0 | 327 | this.consumption = consumption; |
michael@0 | 328 | this.entries = entries; |
michael@0 | 329 | this.goon = goon; |
michael@0 | 330 | callbacks.push(this); |
michael@0 | 331 | this.order = callbacks.length; |
michael@0 | 332 | } |
michael@0 | 333 | |
michael@0 | 334 | EvictionCallback.prototype = |
michael@0 | 335 | { |
michael@0 | 336 | QueryInterface: function listener_qi(iid) { |
michael@0 | 337 | if (iid.equals(Ci.nsISupports) || |
michael@0 | 338 | iid.equals(Ci.nsICacheEntryDoomCallback)) { |
michael@0 | 339 | return this; |
michael@0 | 340 | } |
michael@0 | 341 | throw Components.results.NS_ERROR_NO_INTERFACE; |
michael@0 | 342 | }, |
michael@0 | 343 | onCacheEntryDoomed: function(result) |
michael@0 | 344 | { |
michael@0 | 345 | do_check_eq(this.expectedSuccess, result == Cr.NS_OK); |
michael@0 | 346 | this.goon(); |
michael@0 | 347 | }, |
michael@0 | 348 | selfCheck: function() {} |
michael@0 | 349 | } |
michael@0 | 350 | |
michael@0 | 351 | function EvictionCallback(success, goon) |
michael@0 | 352 | { |
michael@0 | 353 | this.expectedSuccess = success; |
michael@0 | 354 | this.goon = goon; |
michael@0 | 355 | callbacks.push(this); |
michael@0 | 356 | this.order = callbacks.length; |
michael@0 | 357 | } |
michael@0 | 358 | |
michael@0 | 359 | MultipleCallbacks.prototype = |
michael@0 | 360 | { |
michael@0 | 361 | fired: function() |
michael@0 | 362 | { |
michael@0 | 363 | if (--this.pending == 0) |
michael@0 | 364 | { |
michael@0 | 365 | var self = this; |
michael@0 | 366 | if (this.delayed) |
michael@0 | 367 | do_execute_soon(function() { self.goon(); }); |
michael@0 | 368 | else |
michael@0 | 369 | this.goon(); |
michael@0 | 370 | } |
michael@0 | 371 | } |
michael@0 | 372 | } |
michael@0 | 373 | |
michael@0 | 374 | function MultipleCallbacks(number, goon, delayed) |
michael@0 | 375 | { |
michael@0 | 376 | this.pending = number; |
michael@0 | 377 | this.goon = goon; |
michael@0 | 378 | this.delayed = delayed; |
michael@0 | 379 | } |
michael@0 | 380 | |
michael@0 | 381 | function finish_cache2_test() |
michael@0 | 382 | { |
michael@0 | 383 | callbacks.forEach(function(callback, index) { |
michael@0 | 384 | callback.selfCheck(); |
michael@0 | 385 | }); |
michael@0 | 386 | do_test_finished(); |
michael@0 | 387 | } |