browser/base/content/browser-data-submission-info-bar.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/base/content/browser-data-submission-info-bar.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,136 @@
     1.4 +# This Source Code Form is subject to the terms of the Mozilla Public
     1.5 +# License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 +# file, You can obtain one at http://mozilla.org/MPL/2.0/.
     1.7 +
     1.8 +/**
     1.9 + * Represents an info bar that shows a data submission notification.
    1.10 + */
    1.11 +let gDataNotificationInfoBar = {
    1.12 +  _OBSERVERS: [
    1.13 +    "datareporting:notify-data-policy:request",
    1.14 +    "datareporting:notify-data-policy:close",
    1.15 +  ],
    1.16 +
    1.17 +  _DATA_REPORTING_NOTIFICATION: "data-reporting",
    1.18 +
    1.19 +  get _notificationBox() {
    1.20 +    delete this._notificationBox;
    1.21 +    return this._notificationBox = document.getElementById("global-notificationbox");
    1.22 +  },
    1.23 +
    1.24 +  get _log() {
    1.25 +    let Log = Cu.import("resource://gre/modules/Log.jsm", {}).Log;
    1.26 +    delete this._log;
    1.27 +    return this._log = Log.repository.getLogger("Services.DataReporting.InfoBar");
    1.28 +  },
    1.29 +
    1.30 +  init: function() {
    1.31 +    window.addEventListener("unload", function onUnload() {
    1.32 +      window.removeEventListener("unload", onUnload, false);
    1.33 +
    1.34 +      for (let o of this._OBSERVERS) {
    1.35 +        Services.obs.removeObserver(this, o);
    1.36 +      }
    1.37 +    }.bind(this), false);
    1.38 +
    1.39 +    for (let o of this._OBSERVERS) {
    1.40 +      Services.obs.addObserver(this, o, true);
    1.41 +    }
    1.42 +  },
    1.43 +
    1.44 +  _getDataReportingNotification: function (name=this._DATA_REPORTING_NOTIFICATION) {
    1.45 +    return this._notificationBox.getNotificationWithValue(name);
    1.46 +  },
    1.47 +
    1.48 +  _displayDataPolicyInfoBar: function (request) {
    1.49 +    if (this._getDataReportingNotification()) {
    1.50 +      return;
    1.51 +    }
    1.52 +
    1.53 +    let brandBundle = document.getElementById("bundle_brand");
    1.54 +    let appName = brandBundle.getString("brandShortName");
    1.55 +    let vendorName = brandBundle.getString("vendorShortName");
    1.56 +
    1.57 +    let message = gNavigatorBundle.getFormattedString(
    1.58 +      "dataReportingNotification.message",
    1.59 +      [appName, vendorName]);
    1.60 +
    1.61 +    this._actionTaken = false;
    1.62 +
    1.63 +    let buttons = [{
    1.64 +      label: gNavigatorBundle.getString("dataReportingNotification.button.label"),
    1.65 +      accessKey: gNavigatorBundle.getString("dataReportingNotification.button.accessKey"),
    1.66 +      popup: null,
    1.67 +      callback: function () {
    1.68 +        // Clicking the button to go to the preferences tab constitutes
    1.69 +        // acceptance of the data upload policy for Firefox Health Report.
    1.70 +        // This will ensure the checkbox is checked. The user has the option of
    1.71 +        // unchecking it.
    1.72 +        request.onUserAccept("info-bar-button-pressed");
    1.73 +        this._actionTaken = true;
    1.74 +        window.openAdvancedPreferences("dataChoicesTab");
    1.75 +      }.bind(this),
    1.76 +    }];
    1.77 +
    1.78 +    this._log.info("Creating data reporting policy notification.");
    1.79 +    let notification = this._notificationBox.appendNotification(
    1.80 +      message,
    1.81 +      this._DATA_REPORTING_NOTIFICATION,
    1.82 +      null,
    1.83 +      this._notificationBox.PRIORITY_INFO_HIGH,
    1.84 +      buttons,
    1.85 +      function onEvent(event) {
    1.86 +        if (event == "removed") {
    1.87 +          if (!this._actionTaken) {
    1.88 +            request.onUserAccept("info-bar-dismissed");
    1.89 +          }
    1.90 +
    1.91 +          Services.obs.notifyObservers(null, "datareporting:notify-data-policy:close", null);
    1.92 +        }
    1.93 +      }.bind(this)
    1.94 +    );
    1.95 +
    1.96 +    // Tell the notification request we have displayed the notification.
    1.97 +    request.onUserNotifyComplete();
    1.98 +  },
    1.99 +
   1.100 +  _clearPolicyNotification: function () {
   1.101 +    let notification = this._getDataReportingNotification();
   1.102 +    if (notification) {
   1.103 +      this._log.debug("Closing notification.");
   1.104 +      notification.close();
   1.105 +    }
   1.106 +  },
   1.107 +
   1.108 +  onNotifyDataPolicy: function (request) {
   1.109 +    try {
   1.110 +      this._displayDataPolicyInfoBar(request);
   1.111 +    } catch (ex) {
   1.112 +      request.onUserNotifyFailed(ex);
   1.113 +    }
   1.114 +  },
   1.115 +
   1.116 +  observe: function(subject, topic, data) {
   1.117 +    switch (topic) {
   1.118 +      case "datareporting:notify-data-policy:request":
   1.119 +        this.onNotifyDataPolicy(subject.wrappedJSObject.object);
   1.120 +        break;
   1.121 +
   1.122 +      case "datareporting:notify-data-policy:close":
   1.123 +        // If this observer fires, it means something else took care of
   1.124 +        // responding. Therefore, we don't need to do anything. So, we
   1.125 +        // act like we took action and clear state.
   1.126 +        this._actionTaken = true;
   1.127 +        this._clearPolicyNotification();
   1.128 +        break;
   1.129 +
   1.130 +      default:
   1.131 +    }
   1.132 +  },
   1.133 +
   1.134 +  QueryInterface: XPCOMUtils.generateQI([
   1.135 +    Ci.nsIObserver,
   1.136 +    Ci.nsISupportsWeakReference,
   1.137 +  ]),
   1.138 +};
   1.139 +

mercurial