michael@0: # This Source Code Form is subject to the terms of the Mozilla Public michael@0: # License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: # file, You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: michael@0: /** michael@0: * Represents an info bar that shows a data submission notification. michael@0: */ michael@0: let gDataNotificationInfoBar = { michael@0: _OBSERVERS: [ michael@0: "datareporting:notify-data-policy:request", michael@0: "datareporting:notify-data-policy:close", michael@0: ], michael@0: michael@0: _DATA_REPORTING_NOTIFICATION: "data-reporting", michael@0: michael@0: get _notificationBox() { michael@0: delete this._notificationBox; michael@0: return this._notificationBox = document.getElementById("global-notificationbox"); michael@0: }, michael@0: michael@0: get _log() { michael@0: let Log = Cu.import("resource://gre/modules/Log.jsm", {}).Log; michael@0: delete this._log; michael@0: return this._log = Log.repository.getLogger("Services.DataReporting.InfoBar"); michael@0: }, michael@0: michael@0: init: function() { michael@0: window.addEventListener("unload", function onUnload() { michael@0: window.removeEventListener("unload", onUnload, false); michael@0: michael@0: for (let o of this._OBSERVERS) { michael@0: Services.obs.removeObserver(this, o); michael@0: } michael@0: }.bind(this), false); michael@0: michael@0: for (let o of this._OBSERVERS) { michael@0: Services.obs.addObserver(this, o, true); michael@0: } michael@0: }, michael@0: michael@0: _getDataReportingNotification: function (name=this._DATA_REPORTING_NOTIFICATION) { michael@0: return this._notificationBox.getNotificationWithValue(name); michael@0: }, michael@0: michael@0: _displayDataPolicyInfoBar: function (request) { michael@0: if (this._getDataReportingNotification()) { michael@0: return; michael@0: } michael@0: michael@0: let brandBundle = document.getElementById("bundle_brand"); michael@0: let appName = brandBundle.getString("brandShortName"); michael@0: let vendorName = brandBundle.getString("vendorShortName"); michael@0: michael@0: let message = gNavigatorBundle.getFormattedString( michael@0: "dataReportingNotification.message", michael@0: [appName, vendorName]); michael@0: michael@0: this._actionTaken = false; michael@0: michael@0: let buttons = [{ michael@0: label: gNavigatorBundle.getString("dataReportingNotification.button.label"), michael@0: accessKey: gNavigatorBundle.getString("dataReportingNotification.button.accessKey"), michael@0: popup: null, michael@0: callback: function () { michael@0: // Clicking the button to go to the preferences tab constitutes michael@0: // acceptance of the data upload policy for Firefox Health Report. michael@0: // This will ensure the checkbox is checked. The user has the option of michael@0: // unchecking it. michael@0: request.onUserAccept("info-bar-button-pressed"); michael@0: this._actionTaken = true; michael@0: window.openAdvancedPreferences("dataChoicesTab"); michael@0: }.bind(this), michael@0: }]; michael@0: michael@0: this._log.info("Creating data reporting policy notification."); michael@0: let notification = this._notificationBox.appendNotification( michael@0: message, michael@0: this._DATA_REPORTING_NOTIFICATION, michael@0: null, michael@0: this._notificationBox.PRIORITY_INFO_HIGH, michael@0: buttons, michael@0: function onEvent(event) { michael@0: if (event == "removed") { michael@0: if (!this._actionTaken) { michael@0: request.onUserAccept("info-bar-dismissed"); michael@0: } michael@0: michael@0: Services.obs.notifyObservers(null, "datareporting:notify-data-policy:close", null); michael@0: } michael@0: }.bind(this) michael@0: ); michael@0: michael@0: // Tell the notification request we have displayed the notification. michael@0: request.onUserNotifyComplete(); michael@0: }, michael@0: michael@0: _clearPolicyNotification: function () { michael@0: let notification = this._getDataReportingNotification(); michael@0: if (notification) { michael@0: this._log.debug("Closing notification."); michael@0: notification.close(); michael@0: } michael@0: }, michael@0: michael@0: onNotifyDataPolicy: function (request) { michael@0: try { michael@0: this._displayDataPolicyInfoBar(request); michael@0: } catch (ex) { michael@0: request.onUserNotifyFailed(ex); michael@0: } michael@0: }, michael@0: michael@0: observe: function(subject, topic, data) { michael@0: switch (topic) { michael@0: case "datareporting:notify-data-policy:request": michael@0: this.onNotifyDataPolicy(subject.wrappedJSObject.object); michael@0: break; michael@0: michael@0: case "datareporting:notify-data-policy:close": michael@0: // If this observer fires, it means something else took care of michael@0: // responding. Therefore, we don't need to do anything. So, we michael@0: // act like we took action and clear state. michael@0: this._actionTaken = true; michael@0: this._clearPolicyNotification(); michael@0: break; michael@0: michael@0: default: michael@0: } michael@0: }, michael@0: michael@0: QueryInterface: XPCOMUtils.generateQI([ michael@0: Ci.nsIObserver, michael@0: Ci.nsISupportsWeakReference, michael@0: ]), michael@0: }; michael@0: