tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

buffer-full-utilities.js (2588B)


      1 // This script relies on resources/resource-loaders.js. Include it before in order for the below
      2 // methods to work properly.
      3 
      4 // The resources used to trigger new entries.
      5 const scriptResources = [
      6    'resources/empty.js',
      7    'resources/empty_script.js',
      8    'resources/empty.js?id'
      9 ];
     10 
     11 const waitForNextTask = () => {
     12    return new Promise(resolve => {
     13        step_timeout(resolve, 0);
     14    });
     15 };
     16 
     17 const clearBufferAndSetSize = size => {
     18  performance.clearResourceTimings();
     19  performance.setResourceTimingBufferSize(size);
     20 }
     21 
     22 const forceBufferFullEvent = async () => {
     23  clearBufferAndSetSize(1);
     24  return new Promise(async resolve => {
     25    performance.addEventListener('resourcetimingbufferfull', resolve);
     26    // Load 2 resources to ensure onresourcetimingbufferfull is fired.
     27    // Load them in order in order to get the entries in that order!
     28    await load.script(scriptResources[0]);
     29    await load.script(scriptResources[1]);
     30  });
     31 };
     32 
     33 const fillUpTheBufferWithSingleResource = async (src = scriptResources[0]) => {
     34  clearBufferAndSetSize(1);
     35  await load.script(src);
     36 };
     37 
     38 const fillUpTheBufferWithTwoResources = async () => {
     39    clearBufferAndSetSize(2);
     40    // Load them in order in order to get the entries in that order!
     41    await load.script(scriptResources[0]);
     42    await load.script(scriptResources[1]);
     43 };
     44 
     45 const addAssertUnreachedBufferFull = t => {
     46  performance.addEventListener('resourcetimingbufferfull', t.step_func(() => {
     47    assert_unreached("resourcetimingbufferfull should not fire")
     48  }));
     49 };
     50 
     51 const checkEntries = numEntries => {
     52  const entries = performance.getEntriesByType('resource');
     53  assert_equals(entries.length, numEntries,
     54      'Number of entries does not match the expected value.');
     55  assert_true(entries[0].name.includes(scriptResources[0]),
     56      scriptResources[0] + " is in the entries buffer");
     57  if (entries.length > 1) {
     58    assert_true(entries[1].name.includes(scriptResources[1]),
     59        scriptResources[1] + " is in the entries buffer");
     60  }
     61  if (entries.length > 2) {
     62    assert_true(entries[2].name.includes(scriptResources[2]),
     63        scriptResources[2] + " is in the entries buffer");
     64  }
     65 }
     66 
     67 const bufferFullFirePromise = new Promise(resolve => {
     68  performance.addEventListener('resourcetimingbufferfull', async () => {
     69    // Wait for the next task just to ensure that all bufferfull events have fired, and to ensure
     70    // that the secondary buffer is copied (as this is an event, there may be microtask checkpoints
     71    // right after running an event handler).
     72    await waitForNextTask();
     73    resolve();
     74  });
     75 });