tor-browser

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

browser-data-submission-info-bar.js (3444B)


      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 * Represents an info bar that shows a data submission notification.
      7 */
      8 var gDataNotificationInfoBar = {
      9  _OBSERVERS: [
     10    "datareporting:notify-data-policy:request",
     11    "datareporting:notify-data-policy:close",
     12  ],
     13 
     14  _DATA_REPORTING_NOTIFICATION: "data-reporting",
     15 
     16  get _log() {
     17    let { Log } = ChromeUtils.importESModule(
     18      "resource://gre/modules/Log.sys.mjs"
     19    );
     20    delete this._log;
     21    return (this._log = Log.repository.getLoggerWithMessagePrefix(
     22      "Toolkit.Telemetry",
     23      "DataNotificationInfoBar::"
     24    ));
     25  },
     26 
     27  init() {
     28    window.addEventListener("unload", () => {
     29      for (let o of this._OBSERVERS) {
     30        Services.obs.removeObserver(this, o);
     31      }
     32    });
     33 
     34    for (let o of this._OBSERVERS) {
     35      Services.obs.addObserver(this, o, true);
     36    }
     37  },
     38 
     39  _getDataReportingNotification(name = this._DATA_REPORTING_NOTIFICATION) {
     40    return gNotificationBox.getNotificationWithValue(name);
     41  },
     42 
     43  async _displayDataPolicyInfoBar(request) {
     44    if (this._getDataReportingNotification()) {
     45      return;
     46    }
     47 
     48    this._actionTaken = false;
     49 
     50    let buttons = [
     51      {
     52        "l10n-id": "data-reporting-notification-button",
     53        popup: null,
     54        callback: () => {
     55          this._actionTaken = true;
     56          window.openPreferences("privacy-reports");
     57        },
     58      },
     59    ];
     60 
     61    this._log.info("Creating data reporting policy notification.");
     62    await gNotificationBox.appendNotification(
     63      this._DATA_REPORTING_NOTIFICATION,
     64      {
     65        label: {
     66          "l10n-id": "data-reporting-notification-message",
     67        },
     68        priority: gNotificationBox.PRIORITY_INFO_HIGH,
     69        eventCallback: event => {
     70          if (event == "removed") {
     71            Services.obs.notifyObservers(
     72              null,
     73              "datareporting:notify-data-policy:close"
     74            );
     75          }
     76        },
     77      },
     78      buttons
     79    );
     80    // It is important to defer calling onUserNotifyComplete() until we're
     81    // actually sure the notification was displayed. If we ever called
     82    // onUserNotifyComplete() without showing anything to the user, that
     83    // would be very good for user choice. It may also have legal impact.
     84    request.onUserNotifyComplete();
     85  },
     86 
     87  _clearPolicyNotification() {
     88    let notification = this._getDataReportingNotification();
     89    if (notification) {
     90      this._log.debug("Closing notification.");
     91      notification.close();
     92    }
     93  },
     94 
     95  observe(subject, topic) {
     96    switch (topic) {
     97      case "datareporting:notify-data-policy:request": {
     98        let request = subject.wrappedJSObject.object;
     99        try {
    100          this._displayDataPolicyInfoBar(request);
    101        } catch (ex) {
    102          request.onUserNotifyFailed(ex);
    103        }
    104        break;
    105      }
    106 
    107      case "datareporting:notify-data-policy:close":
    108        // If this observer fires, it means something else took care of
    109        // responding. Therefore, we don't need to do anything. So, we
    110        // act like we took action and clear state.
    111        this._actionTaken = true;
    112        this._clearPolicyNotification();
    113        break;
    114 
    115      default:
    116    }
    117  },
    118 
    119  QueryInterface: ChromeUtils.generateQI([
    120    "nsIObserver",
    121    "nsISupportsWeakReference",
    122  ]),
    123 };