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

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:16982f3c53c0
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/.
4
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 ],
13
14 _DATA_REPORTING_NOTIFICATION: "data-reporting",
15
16 get _notificationBox() {
17 delete this._notificationBox;
18 return this._notificationBox = document.getElementById("global-notificationbox");
19 },
20
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 },
26
27 init: function() {
28 window.addEventListener("unload", function onUnload() {
29 window.removeEventListener("unload", onUnload, false);
30
31 for (let o of this._OBSERVERS) {
32 Services.obs.removeObserver(this, o);
33 }
34 }.bind(this), false);
35
36 for (let o of this._OBSERVERS) {
37 Services.obs.addObserver(this, o, true);
38 }
39 },
40
41 _getDataReportingNotification: function (name=this._DATA_REPORTING_NOTIFICATION) {
42 return this._notificationBox.getNotificationWithValue(name);
43 },
44
45 _displayDataPolicyInfoBar: function (request) {
46 if (this._getDataReportingNotification()) {
47 return;
48 }
49
50 let brandBundle = document.getElementById("bundle_brand");
51 let appName = brandBundle.getString("brandShortName");
52 let vendorName = brandBundle.getString("vendorShortName");
53
54 let message = gNavigatorBundle.getFormattedString(
55 "dataReportingNotification.message",
56 [appName, vendorName]);
57
58 this._actionTaken = false;
59
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 }];
74
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 }
87
88 Services.obs.notifyObservers(null, "datareporting:notify-data-policy:close", null);
89 }
90 }.bind(this)
91 );
92
93 // Tell the notification request we have displayed the notification.
94 request.onUserNotifyComplete();
95 },
96
97 _clearPolicyNotification: function () {
98 let notification = this._getDataReportingNotification();
99 if (notification) {
100 this._log.debug("Closing notification.");
101 notification.close();
102 }
103 },
104
105 onNotifyDataPolicy: function (request) {
106 try {
107 this._displayDataPolicyInfoBar(request);
108 } catch (ex) {
109 request.onUserNotifyFailed(ex);
110 }
111 },
112
113 observe: function(subject, topic, data) {
114 switch (topic) {
115 case "datareporting:notify-data-policy:request":
116 this.onNotifyDataPolicy(subject.wrappedJSObject.object);
117 break;
118
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;
126
127 default:
128 }
129 },
130
131 QueryInterface: XPCOMUtils.generateQI([
132 Ci.nsIObserver,
133 Ci.nsISupportsWeakReference,
134 ]),
135 };
136

mercurial