/* 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. Each * window has its own instance, created by `init`. */ export class DataNotificationInfoBar { static #instances = new WeakMap(); /** * @param {Window} window */ static init(window) { let instance = new DataNotificationInfoBar(window); DataNotificationInfoBar.#instances.set(window, instance); instance.init(); } #window; static #OBSERVERS = [ "datareporting:notify-data-policy:request", "datareporting:notify-data-policy:close", ]; static #DATA_REPORTING_NOTIFICATION = "data-reporting"; #logger = null; /** * @param {Window} window The browser window that shows the info bar. */ constructor(window) { this.#window = window; } get #log() { if (!this.#logger) { let { Log } = ChromeUtils.importESModule( "resource://gre/modules/Log.sys.mjs" ); this.#logger = Log.repository.getLoggerWithMessagePrefix( "Toolkit.Telemetry", "DataNotificationInfoBar::" ); } return this.#logger; } init() { this.#window.addEventListener("unload", () => { for (let o of DataNotificationInfoBar.#OBSERVERS) { Services.obs.removeObserver(this, o); } }); for (let o of DataNotificationInfoBar.#OBSERVERS) { Services.obs.addObserver(this, o, true); } } #getDataReportingNotification( name = DataNotificationInfoBar.#DATA_REPORTING_NOTIFICATION ) { return this.#window.gNotificationBox.getNotificationWithValue(name); } async #displayDataPolicyInfoBar(request) { if (this.#getDataReportingNotification()) { return; } let buttons = [ { "l10n-id": "data-reporting-notification-button", popup: null, callback: () => { this.#window.openPreferences("privacy-reports"); }, }, ]; this.#log.info("Creating data reporting policy notification."); await this.#window.gNotificationBox.appendNotification( DataNotificationInfoBar.#DATA_REPORTING_NOTIFICATION, { label: { "l10n-id": "data-reporting-notification-message", }, priority: this.#window.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 only need to clear state. this.#clearPolicyNotification(); break; default: } } QueryInterface = ChromeUtils.generateQI([ "nsIObserver", "nsISupportsWeakReference", ]); }