Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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/.
5 /**
6 * Represents an info bar that shows a data submission notification.
7 */
8 let gDataNotificationInfoBar = {
9 _OBSERVERS: [
10 "datareporting:notify-data-policy:request",
11 "datareporting:notify-data-policy:close",
12 ],
14 _DATA_REPORTING_NOTIFICATION: "data-reporting",
16 get _notificationBox() {
17 delete this._notificationBox;
18 return this._notificationBox = document.getElementById("global-notificationbox");
19 },
21 get _log() {
22 let Log = Cu.import("resource://gre/modules/Log.jsm", {}).Log;
23 delete this._log;
24 return this._log = Log.repository.getLogger("Services.DataReporting.InfoBar");
25 },
27 init: function() {
28 window.addEventListener("unload", function onUnload() {
29 window.removeEventListener("unload", onUnload, false);
31 for (let o of this._OBSERVERS) {
32 Services.obs.removeObserver(this, o);
33 }
34 }.bind(this), false);
36 for (let o of this._OBSERVERS) {
37 Services.obs.addObserver(this, o, true);
38 }
39 },
41 _getDataReportingNotification: function (name=this._DATA_REPORTING_NOTIFICATION) {
42 return this._notificationBox.getNotificationWithValue(name);
43 },
45 _displayDataPolicyInfoBar: function (request) {
46 if (this._getDataReportingNotification()) {
47 return;
48 }
50 let brandBundle = document.getElementById("bundle_brand");
51 let appName = brandBundle.getString("brandShortName");
52 let vendorName = brandBundle.getString("vendorShortName");
54 let message = gNavigatorBundle.getFormattedString(
55 "dataReportingNotification.message",
56 [appName, vendorName]);
58 this._actionTaken = false;
60 let buttons = [{
61 label: gNavigatorBundle.getString("dataReportingNotification.button.label"),
62 accessKey: gNavigatorBundle.getString("dataReportingNotification.button.accessKey"),
63 popup: null,
64 callback: function () {
65 // Clicking the button to go to the preferences tab constitutes
66 // acceptance of the data upload policy for Firefox Health Report.
67 // This will ensure the checkbox is checked. The user has the option of
68 // unchecking it.
69 request.onUserAccept("info-bar-button-pressed");
70 this._actionTaken = true;
71 window.openAdvancedPreferences("dataChoicesTab");
72 }.bind(this),
73 }];
75 this._log.info("Creating data reporting policy notification.");
76 let notification = this._notificationBox.appendNotification(
77 message,
78 this._DATA_REPORTING_NOTIFICATION,
79 null,
80 this._notificationBox.PRIORITY_INFO_HIGH,
81 buttons,
82 function onEvent(event) {
83 if (event == "removed") {
84 if (!this._actionTaken) {
85 request.onUserAccept("info-bar-dismissed");
86 }
88 Services.obs.notifyObservers(null, "datareporting:notify-data-policy:close", null);
89 }
90 }.bind(this)
91 );
93 // Tell the notification request we have displayed the notification.
94 request.onUserNotifyComplete();
95 },
97 _clearPolicyNotification: function () {
98 let notification = this._getDataReportingNotification();
99 if (notification) {
100 this._log.debug("Closing notification.");
101 notification.close();
102 }
103 },
105 onNotifyDataPolicy: function (request) {
106 try {
107 this._displayDataPolicyInfoBar(request);
108 } catch (ex) {
109 request.onUserNotifyFailed(ex);
110 }
111 },
113 observe: function(subject, topic, data) {
114 switch (topic) {
115 case "datareporting:notify-data-policy:request":
116 this.onNotifyDataPolicy(subject.wrappedJSObject.object);
117 break;
119 case "datareporting:notify-data-policy:close":
120 // If this observer fires, it means something else took care of
121 // responding. Therefore, we don't need to do anything. So, we
122 // act like we took action and clear state.
123 this._actionTaken = true;
124 this._clearPolicyNotification();
125 break;
127 default:
128 }
129 },
131 QueryInterface: XPCOMUtils.generateQI([
132 Ci.nsIObserver,
133 Ci.nsISupportsWeakReference,
134 ]),
135 };