|
|
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 https://mozilla.org/MPL/2.0/. */
|
|
|
4
|
+
|
|
|
5
|
+const lazy = {};
|
|
|
6
|
+
|
|
|
7
|
+ChromeUtils.defineESModuleGetters(lazy, {
|
|
|
8
|
+ BrowserWindowTracker: "resource:///modules/BrowserWindowTracker.sys.mjs",
|
|
|
9
|
+ TorConnect: "moz-src:///toolkit/modules/TorConnect.sys.mjs",
|
|
|
10
|
+ TorProviderBuilder:
|
|
|
11
|
+ "moz-src:///toolkit/components/tor-launcher/TorProviderBuilder.sys.mjs",
|
|
|
12
|
+ TorProviderState:
|
|
|
13
|
+ "moz-src:///toolkit/components/tor-launcher/TorProviderBuilder.sys.mjs",
|
|
|
14
|
+ TorProviderTopics:
|
|
|
15
|
+ "moz-src:///toolkit/components/tor-launcher/TorProviderBuilder.sys.mjs",
|
|
|
16
|
+});
|
|
|
17
|
+
|
|
|
18
|
+export const TorProviderExternalNotification = {
|
|
|
19
|
+ /**
|
|
|
20
|
+ * Whether we have been initialized.
|
|
|
21
|
+ *
|
|
|
22
|
+ * @type {boolean}
|
|
|
23
|
+ */
|
|
|
24
|
+ _initialized: false,
|
|
|
25
|
+
|
|
|
26
|
+ /**
|
|
|
27
|
+ * Callback for browser-window-delayed-startup.
|
|
|
28
|
+ *
|
|
|
29
|
+ * @param {Window} win - The browser window that was just loaded.
|
|
|
30
|
+ */
|
|
|
31
|
+ delayedStartup(win) {
|
|
|
32
|
+ if (lazy.TorConnect.enabled) {
|
|
|
33
|
+ // Let TorConnect handle the ProviderStateChanged.
|
|
|
34
|
+ return;
|
|
|
35
|
+ }
|
|
|
36
|
+
|
|
|
37
|
+ if (!this._initialized) {
|
|
|
38
|
+ this._initialized = true;
|
|
|
39
|
+ this._setProviderState(lazy.TorProviderBuilder.currentState());
|
|
|
40
|
+ Services.obs.addObserver(
|
|
|
41
|
+ this,
|
|
|
42
|
+ lazy.TorProviderTopics.ProviderStateChanged
|
|
|
43
|
+ );
|
|
|
44
|
+ } else {
|
|
|
45
|
+ // Maybe show the notification in the new window.
|
|
|
46
|
+ this._updateNotification(win);
|
|
|
47
|
+ }
|
|
|
48
|
+ },
|
|
|
49
|
+
|
|
|
50
|
+ /**
|
|
|
51
|
+ * Callback for browser-window-unload.
|
|
|
52
|
+ *
|
|
|
53
|
+ * @param {Window} win - The browser window that is being unloaded.
|
|
|
54
|
+ */
|
|
|
55
|
+ unload(win) {
|
|
|
56
|
+ if (this._notification?.window === win) {
|
|
|
57
|
+ this._notification = null;
|
|
|
58
|
+ }
|
|
|
59
|
+ },
|
|
|
60
|
+
|
|
|
61
|
+ observe(subject, topic, data) {
|
|
|
62
|
+ switch (topic) {
|
|
|
63
|
+ case lazy.TorProviderTopics.ProviderStateChanged:
|
|
|
64
|
+ this._setProviderState(data);
|
|
|
65
|
+ break;
|
|
|
66
|
+ }
|
|
|
67
|
+ },
|
|
|
68
|
+
|
|
|
69
|
+ /**
|
|
|
70
|
+ * The last seen `TorProviderState` state.
|
|
|
71
|
+ *
|
|
|
72
|
+ * @type {?string}
|
|
|
73
|
+ */
|
|
|
74
|
+ _providerState: null,
|
|
|
75
|
+
|
|
|
76
|
+ /**
|
|
|
77
|
+ * Set the provider state.
|
|
|
78
|
+ *
|
|
|
79
|
+ * @param {string} state - The `TorProviderState` state we are now in.
|
|
|
80
|
+ */
|
|
|
81
|
+ _setProviderState(state) {
|
|
|
82
|
+ if (this._providerState === state) {
|
|
|
83
|
+ // No change, ignore.
|
|
|
84
|
+ return;
|
|
|
85
|
+ }
|
|
|
86
|
+
|
|
|
87
|
+ this._providerState = state;
|
|
|
88
|
+ // A new instance of a provider, so ignore any previously dismissed
|
|
|
89
|
+ // notifications.
|
|
|
90
|
+ this._notificationUserDismissed = false;
|
|
|
91
|
+ this._updateNotification();
|
|
|
92
|
+ },
|
|
|
93
|
+
|
|
|
94
|
+ /**
|
|
|
95
|
+ * The notification that is currently shown.
|
|
|
96
|
+ *
|
|
|
97
|
+ * @type {?{ window: Window, elementPromise: Promise<NotificationMessage> }}
|
|
|
98
|
+ */
|
|
|
99
|
+ _notification: null,
|
|
|
100
|
+ /**
|
|
|
101
|
+ * Whether the user has dismissed the notification for the current TorProvider
|
|
|
102
|
+ * instance.
|
|
|
103
|
+ *
|
|
|
104
|
+ * @type {boolean}
|
|
|
105
|
+ */
|
|
|
106
|
+ _notificationUserDismissed: false,
|
|
|
107
|
+
|
|
|
108
|
+ /**
|
|
|
109
|
+ * Update the notification depending on the latest `TorProviderState`.
|
|
|
110
|
+ *
|
|
|
111
|
+ * @param {Window} [win] - A newly opened browser window to maybe show the
|
|
|
112
|
+ * notification in. If not given, the notification might be shown in the
|
|
|
113
|
+ * current top window.
|
|
|
114
|
+ */
|
|
|
115
|
+ _updateNotification(win) {
|
|
|
116
|
+ if (this._providerState === lazy.TorProviderState.Starting) {
|
|
|
117
|
+ this._updateNotificationButton(
|
|
|
118
|
+ "tor-external-process-error-retry-button-retrying"
|
|
|
119
|
+ );
|
|
|
120
|
+ return;
|
|
|
121
|
+ }
|
|
|
122
|
+
|
|
|
123
|
+ if (this._providerState === lazy.TorProviderState.Running) {
|
|
|
124
|
+ if (this._notification) {
|
|
|
125
|
+ // If there is a notification, close it.
|
|
|
126
|
+ const { elementPromise } = this._notification;
|
|
|
127
|
+ this._notification = null;
|
|
|
128
|
+ elementPromise.then(el => el.remove());
|
|
|
129
|
+ }
|
|
|
130
|
+ return;
|
|
|
131
|
+ }
|
|
|
132
|
+ // Else, _providerState is TorProviderState.Stopped.
|
|
|
133
|
+
|
|
|
134
|
+ if (this._notificationUserDismissed) {
|
|
|
135
|
+ // User has already dismissed this in a different window.
|
|
|
136
|
+ return;
|
|
|
137
|
+ }
|
|
|
138
|
+
|
|
|
139
|
+ if (this._notification) {
|
|
|
140
|
+ // Already showing.
|
|
|
141
|
+ this._updateNotificationButton("tor-external-process-error-retry-button");
|
|
|
142
|
+ return;
|
|
|
143
|
+ }
|
|
|
144
|
+
|
|
|
145
|
+ if (!win) {
|
|
|
146
|
+ win = lazy.BrowserWindowTracker.getTopWindow();
|
|
|
147
|
+ if (!win) {
|
|
|
148
|
+ // Wait for a window.
|
|
|
149
|
+ return;
|
|
|
150
|
+ }
|
|
|
151
|
+ }
|
|
|
152
|
+
|
|
|
153
|
+ const buttons = [
|
|
|
154
|
+ {
|
|
|
155
|
+ supportPage: "tor-manual:get-in-touch__bug-or-feedback",
|
|
|
156
|
+ },
|
|
|
157
|
+ {
|
|
|
158
|
+ "l10n-id": "tor-external-process-error-retry-button",
|
|
|
159
|
+ callback: () => {
|
|
|
160
|
+ if (this._providerState === lazy.TorProviderState.Stopped) {
|
|
|
161
|
+ lazy.TorProviderBuilder.replace();
|
|
|
162
|
+ }
|
|
|
163
|
+ // Keep open.
|
|
|
164
|
+ return true;
|
|
|
165
|
+ },
|
|
|
166
|
+ },
|
|
|
167
|
+ ];
|
|
|
168
|
+
|
|
|
169
|
+ const notification = {
|
|
|
170
|
+ window: win,
|
|
|
171
|
+ elementPromise: win.gNotificationBox.appendNotification(
|
|
|
172
|
+ "tor-provider-external-process-stopped",
|
|
|
173
|
+ {
|
|
|
174
|
+ label: { "l10n-id": "tor-external-process-error-message" },
|
|
|
175
|
+ priority: win.gNotificationBox.PRIORITY_WARNING_HIGH,
|
|
|
176
|
+ eventCallback: event => {
|
|
|
177
|
+ switch (event) {
|
|
|
178
|
+ case "dismissed":
|
|
|
179
|
+ this._notificationUserDismissed = true;
|
|
|
180
|
+ break;
|
|
|
181
|
+ case "disconnected":
|
|
|
182
|
+ if (this._notification === notification) {
|
|
|
183
|
+ this._notification = null;
|
|
|
184
|
+ }
|
|
|
185
|
+ break;
|
|
|
186
|
+ }
|
|
|
187
|
+ },
|
|
|
188
|
+ },
|
|
|
189
|
+ buttons
|
|
|
190
|
+ ),
|
|
|
191
|
+ };
|
|
|
192
|
+ this._notification = notification;
|
|
|
193
|
+ },
|
|
|
194
|
+
|
|
|
195
|
+ /**
|
|
|
196
|
+ * Update the notification button if it exists.
|
|
|
197
|
+ *
|
|
|
198
|
+ * @param {string} l10nId - The Fluent ID for the string we want to show in
|
|
|
199
|
+ * the button.
|
|
|
200
|
+ */
|
|
|
201
|
+ async _updateNotificationButton(l10nId) {
|
|
|
202
|
+ if (!this._notification) {
|
|
|
203
|
+ return;
|
|
|
204
|
+ }
|
|
|
205
|
+ const state = this._providerState;
|
|
|
206
|
+ const notification = this._notification;
|
|
|
207
|
+ const notificationEl = await notification.elementPromise;
|
|
|
208
|
+
|
|
|
209
|
+ if (this._providerState !== state || this._notification !== notification) {
|
|
|
210
|
+ // Replaced.
|
|
|
211
|
+ return;
|
|
|
212
|
+ }
|
|
|
213
|
+
|
|
|
214
|
+ notificationEl.buttonContainer.firstElementChild.setAttribute(
|
|
|
215
|
+ "data-l10n-id",
|
|
|
216
|
+ l10nId
|
|
|
217
|
+ );
|
|
|
218
|
+ },
|
|
|
219
|
+}; |