tor-browser

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

uptake-telemetry.sys.mjs (4155B)


      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 /**
      6 * A Telemetry helper to report uptake of remote content.
      7 */
      8 export class UptakeTelemetry {
      9  /**
     10   * Supported uptake statuses:
     11   *
     12   * - `UP_TO_DATE`: Local content was already up-to-date with remote content.
     13   * - `SUCCESS`: Local content was updated successfully.
     14   * - `BACKOFF`: Remote server asked clients to backoff.
     15   * - `PARSE_ERROR`: Parsing server response has failed.
     16   * - `CONTENT_ERROR`: Server response has unexpected content.
     17   * - `PREF_DISABLED`: Update is disabled in user preferences.
     18   * - `SIGNATURE_ERROR`: Signature verification after diff-based sync has failed.
     19   * - `SIGNATURE_RETRY_ERROR`: Signature verification after full fetch has failed.
     20   * - `CONFLICT_ERROR`: Some remote changes are in conflict with local changes.
     21   * - `CORRUPTION_ERROR`: Error related to corrupted local data.
     22   * - `SYNC_ERROR`: Synchronization of remote changes has failed.
     23   * - `APPLY_ERROR`: Application of changes locally has failed.
     24   * - `SERVER_ERROR`: Server failed to respond.
     25   * - `CERTIFICATE_ERROR`: Server certificate verification has failed.
     26   * - `DOWNLOAD_ERROR`: Data could not be fully retrieved.
     27   * - `TIMEOUT_ERROR`: Server response has timed out.
     28   * - `NETWORK_ERROR`: Communication with server has failed.
     29   * - `NETWORK_OFFLINE_ERROR`: Network not available.
     30   * - `SHUTDOWN_ERROR`: Error occuring during shutdown.
     31   * - `UNKNOWN_ERROR`: Uncategorized error.
     32   * - `CLEANUP_ERROR`: Clean-up of temporary files has failed.
     33   * - `SYNC_BROKEN_ERROR`: Synchronization is broken.
     34   * - `CUSTOM_1_ERROR`: Update source specific error #1.
     35   * - `CUSTOM_2_ERROR`: Update source specific error #2.
     36   * - `CUSTOM_3_ERROR`: Update source specific error #3.
     37   * - `CUSTOM_4_ERROR`: Update source specific error #4.
     38   * - `CUSTOM_5_ERROR`: Update source specific error #5.
     39   *
     40   * @type {object}
     41   */
     42  static get STATUS() {
     43    return {
     44      UP_TO_DATE: "up_to_date",
     45      SUCCESS: "success",
     46      BACKOFF: "backoff",
     47      PARSE_ERROR: "parse_error",
     48      CONTENT_ERROR: "content_error",
     49      PREF_DISABLED: "pref_disabled",
     50      SIGNATURE_ERROR: "sign_error",
     51      SIGNATURE_RETRY_ERROR: "sign_retry_error",
     52      CONFLICT_ERROR: "conflict_error",
     53      CORRUPTION_ERROR: "corruption_error",
     54      SYNC_START: "sync_start",
     55      SYNC_ERROR: "sync_error",
     56      APPLY_ERROR: "apply_error",
     57      SERVER_ERROR: "server_error",
     58      CERTIFICATE_ERROR: "certificate_error",
     59      DOWNLOAD_START: "download_start",
     60      DOWNLOAD_ERROR: "download_error",
     61      TIMEOUT_ERROR: "timeout_error",
     62      NETWORK_ERROR: "network_error",
     63      NETWORK_OFFLINE_ERROR: "offline_error",
     64      SHUTDOWN_ERROR: "shutdown_error",
     65      UNKNOWN_ERROR: "unknown_error",
     66      CLEANUP_ERROR: "cleanup_error",
     67      SYNC_BROKEN_ERROR: "sync_broken_error",
     68      CUSTOM_1_ERROR: "custom_1_error",
     69      CUSTOM_2_ERROR: "custom_2_error",
     70      CUSTOM_3_ERROR: "custom_3_error",
     71      CUSTOM_4_ERROR: "custom_4_error",
     72      CUSTOM_5_ERROR: "custom_5_error",
     73    };
     74  }
     75 
     76  /**
     77   * Reports the uptake status for the specified source.
     78   *
     79   * @param {string} component     the component reporting the uptake (eg. "Normandy").
     80   * @param {string} status        the uptake status (eg. "network_error")
     81   * @param {object} extra         extra values to report
     82   * @param {string} extra.source  the update source (eg. "recipe-42").
     83   * @param {string} extra.trigger what triggered the polling/fetching (eg. "broadcast", "timer").
     84   * @param {int}    extra.age     age of pulled data in seconds
     85   */
     86  static async report(component, status, extra = {}) {
     87    const { source } = extra;
     88 
     89    if (!source) {
     90      throw new Error("`source` value is mandatory.");
     91    }
     92 
     93    if (!Object.values(UptakeTelemetry.STATUS).includes(status)) {
     94      throw new Error(`Unknown status '${status}'`);
     95    }
     96 
     97    extra.value = status;
     98    Glean.uptakeRemotecontentResult["uptake" + component].record(extra);
     99  }
    100 }