summaryrefslogtreecommitdiffstats
path: root/browser/base/content/browser-data-submission-info-bar.js
blob: 104414d5823ff9495e56a4cd260d4ff6b9fdee91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/**
 * Represents an info bar that shows a data submission notification.
 */
var gDataNotificationInfoBar = {
  _OBSERVERS: [
    "datareporting:notify-data-policy:request",
    "datareporting:notify-data-policy:close",
  ],

  _DATA_REPORTING_NOTIFICATION: "data-reporting",

  get _log() {
    let { Log } = ChromeUtils.importESModule(
      "resource://gre/modules/Log.sys.mjs"
    );
    delete this._log;
    return (this._log = Log.repository.getLoggerWithMessagePrefix(
      "Toolkit.Telemetry",
      "DataNotificationInfoBar::"
    ));
  },

  init() {
    window.addEventListener("unload", () => {
      for (let o of this._OBSERVERS) {
        Services.obs.removeObserver(this, o);
      }
    });

    for (let o of this._OBSERVERS) {
      Services.obs.addObserver(this, o, true);
    }
  },

  _getDataReportingNotification(name = this._DATA_REPORTING_NOTIFICATION) {
    return gNotificationBox.getNotificationWithValue(name);
  },

  async _displayDataPolicyInfoBar(request) {
    if (this._getDataReportingNotification()) {
      return;
    }

    this._actionTaken = false;

    let buttons = [
      {
        "l10n-id": "data-reporting-notification-button",
        popup: null,
        callback: () => {
          this._actionTaken = true;
          window.openPreferences("privacy-reports");
        },
      },
    ];

    this._log.info("Creating data reporting policy notification.");
    await gNotificationBox.appendNotification(
      this._DATA_REPORTING_NOTIFICATION,
      {
        label: {
          "l10n-id": "data-reporting-notification-message",
        },
        priority: gNotificationBox.PRIORITY_INFO_HIGH,
        eventCallback: event => {
          if (event == "removed") {
            Services.obs.notifyObservers(
              null,
              "datareporting:notify-data-policy:close"
            );
          }
        },
      },
      buttons
    );
    // It is important to defer calling onUserNotifyComplete() until we're
    // actually sure the notification was displayed. If we ever called
    // onUserNotifyComplete() without showing anything to the user, that
    // would be very good for user choice. It may also have legal impact.
    request.onUserNotifyComplete();
  },

  _clearPolicyNotification() {
    let notification = this._getDataReportingNotification();
    if (notification) {
      this._log.debug("Closing notification.");
      notification.close();
    }
  },

  observe(subject, topic) {
    switch (topic) {
      case "datareporting:notify-data-policy:request":
        let request = subject.wrappedJSObject.object;
        try {
          this._displayDataPolicyInfoBar(request);
        } catch (ex) {
          request.onUserNotifyFailed(ex);
        }
        break;

      case "datareporting:notify-data-policy:close":
        // If this observer fires, it means something else took care of
        // responding. Therefore, we don't need to do anything. So, we
        // act like we took action and clear state.
        this._actionTaken = true;
        this._clearPolicyNotification();
        break;

      default:
    }
  },

  QueryInterface: ChromeUtils.generateQI([
    "nsIObserver",
    "nsISupportsWeakReference",
  ]),
};