 |
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", () => {
29 for (let o of this._OBSERVERS) {
30 Services.obs.removeObserver(this, o);
31 }
32 }, false);
33
34 for (let o of this._OBSERVERS) {
35 Services.obs.addObserver(this, o, true);
36 }
37 },
38
39 _getDataReportingNotification: function (name=this._DATA_REPORTING_NOTIFICATION) {
40 return this._notificationBox.getNotificationWithValue(name);
41 },
42
43 _displayDataPolicyInfoBar: function (request) {
44 if (this._getDataReportingNotification()) {
45 return;
46 }
47
48 let brandBundle = document.getElementById("bundle_brand");
49 let appName = brandBundle.getString("brandShortName");
50 let vendorName = brandBundle.getString("vendorShortName");
51
52 let message = gNavigatorBundle.getFormattedString(
53 "dataReportingNotification.message",
54 [appName, vendorName]);
55
56 this._actionTaken = false;
57
58 let buttons = [{
59 label: gNavigatorBundle.getString("dataReportingNotification.button.label"),
60 accessKey: gNavigatorBundle.getString("dataReportingNotification.button.accessKey"),
61 popup: null,
62 callback: () => {
63 this._actionTaken = true;
64 window.openAdvancedPreferences("dataChoicesTab");
65 },
66 }];
67
68 this._log.info("Creating data reporting policy notification.");
69 let notification = this._notificationBox.appendNotification(
70 message,
71 this._DATA_REPORTING_NOTIFICATION,
72 null,
73 this._notificationBox.PRIORITY_INFO_HIGH,
74 buttons,
75 event => {
76 if (event == "removed") {
77 Services.obs.notifyObservers(null, "datareporting:notify-data-policy:close", null);
78 }
79 }
80 );
81 // It is important to defer calling onUserNotifyComplete() until we're
82 // actually sure the notification was displayed. If we ever called
83 // onUserNotifyComplete() without showing anything to the user, that
84 // would be very good for user choice. It may also have legal impact.
85 request.onUserNotifyComplete();
86 },
87
88 _clearPolicyNotification: function () {
89 let notification = this._getDataReportingNotification();
90 if (notification) {
91 this._log.debug("Closing notification.");
92 notification.close();
93 }
94 },
95
96 observe: function(subject, topic, data) {
97 switch (topic) {
98 case "datareporting:notify-data-policy:request":
99 let request = subject.wrappedJSObject.object;
100 try {
101 this._displayDataPolicyInfoBar(request);
102 } catch (ex) {
103 request.onUserNotifyFailed(ex);
104 return;
105 }
106 break;
107
108 case "datareporting:notify-data-policy:close":
109 // If this observer fires, it means something else took care of
110 // responding. Therefore, we don't need to do anything. So, we
111 // act like we took action and clear state.
112 this._actionTaken = true;
113 this._clearPolicyNotification();
114 break;
115
116 default:
117 }
118 },
119
120 QueryInterface: XPCOMUtils.generateQI([
121 Ci.nsIObserver,
122 Ci.nsISupportsWeakReference,
123 ]),
124 };
125
126