tor-browser

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

audit.js (1658B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 "use strict";
      6 
      7 /**
      8 * A helper class that contains the state of the audit progress performed by
      9 * the accessibility panel. Its onProgressForWalker function wraps around the
     10 * onProgress function (see actions/audit.js) that updates the panel state. It
     11 * combines the audits across multiple frames that happen asynchronously. It
     12 * only starts dispatching/calling onProgress after we get an initial progress
     13 * audit-event from all frames (and thus know the combined total).
     14 */
     15 class CombinedProgress {
     16  constructor({ onProgress, totalFrames }) {
     17    this.onProgress = onProgress;
     18    this.totalFrames = totalFrames;
     19    this.combinedProgress = new Map();
     20  }
     21 
     22  onProgressForWalker(walker, progress) {
     23    this.combinedProgress.set(walker, progress);
     24    // We did not get all initial progress events from all frames, do not
     25    // relay them to the client until we can calculate combined total below.
     26    if (this.combinedProgress.size < this.totalFrames) {
     27      return;
     28    }
     29 
     30    let combinedTotal = 0;
     31    let combinedCompleted = 0;
     32 
     33    for (const { completed, total } of this.combinedProgress.values()) {
     34      combinedTotal += total;
     35      combinedCompleted += completed;
     36    }
     37    this.onProgress({
     38      total: combinedTotal,
     39      percentage: Math.round((combinedCompleted / combinedTotal) * 100),
     40    });
     41  }
     42 }
     43 
     44 exports.CombinedProgress = CombinedProgress;
     45 exports.isFiltered = filters => Object.values(filters).some(active => active);