Skip to content

Commit e15c9d9

Browse files
committed
Fix more problems caused by the ESM migration
1 parent 63d99a9 commit e15c9d9

File tree

2 files changed

+131
-128
lines changed

2 files changed

+131
-128
lines changed

native/userchrome/profile/chrome/pwa/boot.sys.mjs

+131
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,20 @@ import { AppConstants } from 'resource://gre/modules/AppConstants.sys.mjs';
22
import { NetUtil } from 'resource://gre/modules/NetUtil.sys.mjs';
33
import { nsContentDispatchChooser } from 'resource://gre/modules/ContentDispatchChooser.sys.mjs';
44
import { nsDefaultCommandLineHandler, nsBrowserContentHandler } from 'resource:///modules/BrowserContentHandler.sys.mjs';
5+
import { BrowserGlue } from 'resource:///modules/BrowserGlue.sys.mjs';
56
import { BrowserWindowTracker } from 'resource:///modules/BrowserWindowTracker.sys.mjs';
7+
import { WebProtocolHandlerRegistrar } from 'resource:///modules/WebProtocolHandlerRegistrar.sys.mjs';
8+
import { OnboardingMessageProvider } from 'resource:///modules/asrouter/OnboardingMessageProvider.sys.mjs';
69

710
import { applySystemIntegration } from 'resource://pwa/utils/systemIntegration.sys.mjs';
811

12+
const lazy = {};
13+
14+
ChromeUtils.defineESModuleGetters(lazy, {
15+
sendNativeMessage: 'resource://pwa/utils/nativeMessaging.sys.mjs',
16+
sanitizeString: 'resource://pwa/utils/common.sys.mjs',
17+
});
18+
919
/**
1020
* Reads the PWAsForFirefox config file and parses it as JSON.
1121
*
@@ -117,9 +127,18 @@ Services.prefs.getDefaultBranch(null).setBoolPref('browser.sessionstore.resume_f
117127
Services.prefs.getDefaultBranch(null).setIntPref('browser.sessionstore.max_resumed_crashes', 0);
118128
Services.prefs.getDefaultBranch(null).setIntPref('browser.sessionstore.max_tabs_undo', 0);
119129
Services.prefs.getDefaultBranch(null).setIntPref('browser.sessionstore.max_windows_undo', 0);
130+
Services.prefs.getDefaultBranch(null).setBoolPref('browser.shell.checkDefaultBrowser', false);
131+
Services.prefs.getDefaultBranch(null).setBoolPref('browser.startup.upgradeDialog.enabled', false);
120132
Services.prefs.getDefaultBranch(null).setBoolPref('browser.privateWindowSeparation.enabled', false);
121133
Services.prefs.getDefaultBranch(null).setBoolPref('browser.privacySegmentation.createdShortcut', true);
122134

135+
// Disable default browser prompt
136+
BrowserGlue.prototype._maybeShowDefaultBrowserPrompt = async () => null;
137+
138+
// Disable onboarding messages
139+
OnboardingMessageProvider.getMessages = async () => [];
140+
OnboardingMessageProvider.getUntranslatedMessages = async () => [];
141+
123142
// Override command line helper to intercept PWAsForFirefox arguments and start loading the site
124143
nsDefaultCommandLineHandler.prototype._handle = nsDefaultCommandLineHandler.prototype.handle;
125144
nsDefaultCommandLineHandler.prototype.handle = function (cmdLine) {
@@ -196,6 +215,118 @@ nsContentDispatchChooser.prototype._hasProtocolHandlerPermission = function(sche
196215
return this._hasProtocolHandlerPermissionOriginal(scheme, principal, triggeredExternally);
197216
};
198217

218+
// Handle opening new window from keyboard shortcuts
219+
BrowserWindowTracker._openWindow = BrowserWindowTracker.openWindow;
220+
BrowserWindowTracker.openWindow = function (options) {
221+
if (options.openerWindow && options.openerWindow.gFFPWASiteConfig && !options.args) {
222+
options.args = Cc['@mozilla.org/supports-string;1'].createInstance(Ci.nsISupportsString);
223+
options.args.data = options.openerWindow.HomePage.get(options.openerWindow);
224+
}
225+
return BrowserWindowTracker._openWindow(options);
226+
};
227+
228+
// Some checks for protocol handlers are directly based on the Firefox code, licensed under MPL 2.0
229+
// Original source: https://github.com/mozilla/gecko-dev/blob/a62618baa72cd0ba6c0a5f5fc0b1d63f2866b7c6/browser/components/protocolhandler/WebProtocolHandlerRegistrar.jsm
230+
231+
// Handle registering custom protocol handlers
232+
WebProtocolHandlerRegistrar.prototype.registerProtocolHandler = function (protocol, url, title, documentURI, browserOrWindow) {
233+
protocol = (protocol || '').toLowerCase();
234+
if (!url || !documentURI) return;
235+
236+
// Some special handling for e10s and non-e10s
237+
let browser = browserOrWindow;
238+
if (browserOrWindow instanceof Ci.nsIDOMWindow) {
239+
let rootDocShell = browserOrWindow.docShell.sameTypeRootTreeItem;
240+
browser = rootDocShell.QueryInterface(Ci.nsIDocShell).chromeEventHandler;
241+
}
242+
243+
// Get the browser window
244+
let browserWindow = browser.ownerGlobal;
245+
246+
// Check if protocol handler is allowed
247+
try { browser.ownerGlobal.navigator.checkProtocolHandlerAllowed(protocol, url, documentURI) }
248+
catch (_) { return }
249+
250+
// If the protocol handler is already registered, just return early
251+
// We only allow one handler (either manifest or custom) per protocol scheme
252+
const existingHandlers = new Set([
253+
...browserWindow.gFFPWASiteConfig.config.custom_protocol_handlers,
254+
...browserWindow.gFFPWASiteConfig.manifest.protocol_handlers
255+
].map(handler => lazy.sanitizeString(handler.protocol)).filter(handler => handler).sort());
256+
if (existingHandlers.has(protocol)) return;
257+
258+
// Now ask the user and provide the proper callback
259+
const message = this._getFormattedString('addProtocolHandlerMessage', [url.host, protocol,]);
260+
261+
const notificationBox = browser.getTabBrowser().getNotificationBox(browser);
262+
const notificationIcon = url.prePath + '/favicon.ico';
263+
const notificationValue = 'Protocol Registration: ' + protocol;
264+
265+
const addButton = {
266+
label: this._getString('addProtocolHandlerAddButton'),
267+
accessKey: this._getString('addProtocolHandlerAddButtonAccesskey'),
268+
protocolInfo: { site: browserWindow.gFFPWASiteConfig.ulid, protocol: protocol, url: url.spec },
269+
270+
async callback (notification, buttonInfo) {
271+
// Send a request to the native program to register the handler
272+
const response = await lazy.sendNativeMessage({
273+
cmd: 'RegisterProtocolHandler',
274+
params: {
275+
site: buttonInfo.protocolInfo.site,
276+
protocol: buttonInfo.protocolInfo.protocol,
277+
url: buttonInfo.protocolInfo.url,
278+
}
279+
});
280+
if (response.type === 'Error') throw new Error(response.data);
281+
if (response.type !== 'ProtocolHandlerRegistered') throw new Error(`Received invalid response type: ${response.type}`);
282+
283+
// Reset the handlerInfo to ask before the next use
284+
const eps = Cc['@mozilla.org/uriloader/external-protocol-service;1'].getService(Ci.nsIExternalProtocolService);
285+
const handlerInfo = eps.getProtocolHandlerInfo(buttonInfo.protocolInfo.protocol);
286+
handlerInfo.alwaysAskBeforeHandling = true;
287+
288+
const hs = Cc['@mozilla.org/uriloader/handler-service;1'].getService(Ci.nsIHandlerService);
289+
hs.store(handlerInfo);
290+
291+
// Hide the notification
292+
notificationBox.currentNotification.close();
293+
},
294+
};
295+
296+
notificationBox.appendNotification(
297+
notificationValue,
298+
{
299+
label: message,
300+
image: notificationIcon,
301+
priority: notificationBox.PRIORITY_INFO_LOW
302+
},
303+
[addButton]
304+
);
305+
};
306+
307+
// Handle unregistering custom protocol handlers
308+
// Disabled for now until we figure out how to access window from here
309+
// WebProtocolHandlerRegistrar.prototype.removeProtocolHandler = function (protocol, url) {
310+
// (async () => {
311+
// // Send a request to the native program to unregister the handler
312+
// const response = await lazy.sendNativeMessage({
313+
// cmd: 'UnregisterProtocolHandler',
314+
// params: {
315+
// site: window.gFFPWASiteConfig.ulid,
316+
// protocol,
317+
// url,
318+
// }
319+
// });
320+
// if (response.type === 'Error') throw new Error(response.data);
321+
// if (response.type !== 'ProtocolHandlerUnregistered') throw new Error(`Received invalid response type: ${response.type}`);
322+
//
323+
// // Reset the handlerInfo to ask before the next use
324+
// const eps = Cc['@mozilla.org/uriloader/external-protocol-service;1'].getService(Ci.nsIExternalProtocolService);
325+
// const handlerInfo = eps.getProtocolHandlerInfo(protocol);
326+
// handlerInfo.alwaysAskBeforeHandling = true;
327+
// })()
328+
// };
329+
199330
// Register a localization source for the packaged locales
200331
Services.obs.addObserver(async () => {
201332
const languages = Services.locale.packagedLocales;

native/userchrome/profile/chrome/pwa/content/browser.sys.mjs

-128
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
import { OnboardingMessageProvider } from 'resource:///modules/asrouter/OnboardingMessageProvider.sys.mjs';
21
import { WebNavigationManager } from 'resource://gre/modules/WebNavigation.sys.mjs';
32
import { XPCOMUtils } from 'resource://gre/modules/XPCOMUtils.sys.mjs';
4-
import { BrowserGlue } from 'resource:///modules/BrowserGlue.sys.mjs';
5-
import { BrowserWindowTracker } from 'resource:///modules/BrowserWindowTracker.sys.mjs';
6-
import { WebProtocolHandlerRegistrar } from 'resource:///modules/WebProtocolHandlerRegistrar.sys.mjs';
73

84
import { sanitizeString } from 'resource://pwa/utils/common.sys.mjs';
95
import { hookFunction } from 'resource://pwa/utils/hookFunction.sys.mjs';
@@ -12,7 +8,6 @@ import { xPref } from 'resource://pwa/utils/xPref.sys.mjs';
128
const lazy = {};
139

1410
ChromeUtils.defineESModuleGetters(lazy, {
15-
sendNativeMessage: 'resource://pwa/utils/nativeMessaging.sys.mjs',
1611
applyDynamicThemeColor: 'resource://pwa/utils/systemIntegration.sys.mjs',
1712
applySystemIntegration: 'resource://pwa/utils/systemIntegration.sys.mjs',
1813
buildIconList: 'resource://pwa/utils/systemIntegration.sys.mjs',
@@ -47,7 +42,6 @@ class PwaBrowser {
4742
this.switchPopupSides();
4843
this.makeUrlBarReadOnly();
4944
setTimeout(() => { this.setDisplayModeStandalone() });
50-
this.handleRegisteringProtocols();
5145
this.handleOutOfScopeNavigation();
5246
this.handleOpeningNewWindow();
5347
this.handleDisablingShortcuts();
@@ -368,105 +362,6 @@ class PwaBrowser {
368362
hookCurrentBrowser();
369363
}
370364

371-
handleRegisteringProtocols () {
372-
// Overwrites original Firefox functions with our custom installation process
373-
// Some checks here are directly based on the Firefox code, licensed under MPL 2.0
374-
// Original source: https://github.com/mozilla/gecko-dev/blob/a62618baa72cd0ba6c0a5f5fc0b1d63f2866b7c6/browser/components/protocolhandler/WebProtocolHandlerRegistrar.jsm
375-
376-
WebProtocolHandlerRegistrar.prototype.registerProtocolHandler = function (protocol, url, title, documentURI, browserOrWindow) {
377-
protocol = (protocol || '').toLowerCase();
378-
if (!url || !documentURI) return;
379-
380-
// Some special handling for e10s and non-e10s
381-
let browser = browserOrWindow;
382-
if (browserOrWindow instanceof Ci.nsIDOMWindow) {
383-
let rootDocShell = browserOrWindow.docShell.sameTypeRootTreeItem;
384-
browser = rootDocShell.QueryInterface(Ci.nsIDocShell).chromeEventHandler;
385-
}
386-
387-
// Check if protocol handler is allowed
388-
try { browser.ownerGlobal.navigator.checkProtocolHandlerAllowed(protocol, url, documentURI) }
389-
catch (_) { return }
390-
391-
// If the protocol handler is already registered, just return early
392-
// We only allow one handler (either manifest or custom) per protocol scheme
393-
const existingHandlers = new Set([
394-
...window.gFFPWASiteConfig.config.custom_protocol_handlers,
395-
...window.gFFPWASiteConfig.manifest.protocol_handlers
396-
].map(handler => sanitizeString(handler.protocol)).filter(handler => handler).sort());
397-
if (existingHandlers.has(protocol)) return;
398-
399-
// Now ask the user and provide the proper callback
400-
const message = this._getFormattedString('addProtocolHandlerMessage', [url.host, protocol,]);
401-
402-
const notificationBox = browser.getTabBrowser().getNotificationBox(browser);
403-
const notificationIcon = url.prePath + '/favicon.ico';
404-
const notificationValue = 'Protocol Registration: ' + protocol;
405-
406-
const addButton = {
407-
label: this._getString('addProtocolHandlerAddButton'),
408-
accessKey: this._getString('addProtocolHandlerAddButtonAccesskey'),
409-
protocolInfo: { site: window.gFFPWASiteConfig.ulid, protocol: protocol, url: url.spec },
410-
411-
async callback (notification, buttonInfo) {
412-
// Send a request to the native program to register the handler
413-
const response = await lazy.sendNativeMessage({
414-
cmd: 'RegisterProtocolHandler',
415-
params: {
416-
site: buttonInfo.protocolInfo.site,
417-
protocol: buttonInfo.protocolInfo.protocol,
418-
url: buttonInfo.protocolInfo.url,
419-
}
420-
})
421-
if (response.type === 'Error') throw new Error(response.data)
422-
if (response.type !== 'ProtocolHandlerRegistered') throw new Error(`Received invalid response type: ${response.type}`)
423-
424-
// Reset the handlerInfo to ask before the next use
425-
const eps = Cc['@mozilla.org/uriloader/external-protocol-service;1'].getService(Ci.nsIExternalProtocolService);
426-
const handlerInfo = eps.getProtocolHandlerInfo(buttonInfo.protocolInfo.protocol);
427-
handlerInfo.alwaysAskBeforeHandling = true;
428-
429-
const hs = Cc['@mozilla.org/uriloader/handler-service;1'].getService(Ci.nsIHandlerService);
430-
hs.store(handlerInfo);
431-
432-
// Hide the notification
433-
notificationBox.currentNotification.close();
434-
},
435-
};
436-
437-
notificationBox.appendNotification(
438-
notificationValue,
439-
{
440-
label: message,
441-
image: notificationIcon,
442-
priority: notificationBox.PRIORITY_INFO_LOW
443-
},
444-
[addButton]
445-
);
446-
}
447-
448-
WebProtocolHandlerRegistrar.prototype.removeProtocolHandler = function (protocol, url) {
449-
(async () => {
450-
// Send a request to the native program to unregister the handler
451-
const response = await lazy.sendNativeMessage({
452-
cmd: 'UnregisterProtocolHandler',
453-
params: {
454-
site: window.gFFPWASiteConfig.ulid,
455-
protocol,
456-
url,
457-
}
458-
})
459-
if (response.type === 'Error') throw new Error(response.data)
460-
if (response.type !== 'ProtocolHandlerUnregistered') throw new Error(`Received invalid response type: ${response.type}`)
461-
462-
// Reset the handlerInfo to ask before the next use
463-
const eps = Cc['@mozilla.org/uriloader/external-protocol-service;1'].getService(Ci.nsIExternalProtocolService);
464-
const handlerInfo = eps.getProtocolHandlerInfo(protocol);
465-
handlerInfo.alwaysAskBeforeHandling = true;
466-
})()
467-
}
468-
}
469-
470365
handleOutOfScopeNavigation () {
471366
function matchWildcard(wildcard, string) {
472367
const pattern = wildcard
@@ -572,18 +467,6 @@ class PwaBrowser {
572467
// Return a new window
573468
return win;
574469
};
575-
576-
// Handle opening new window from keyboard shortcuts
577-
if (!('_openWindow' in BrowserWindowTracker)) {
578-
BrowserWindowTracker._openWindow = BrowserWindowTracker.openWindow;
579-
BrowserWindowTracker.openWindow = function (options) {
580-
if (options.openerWindow && options.openerWindow.gFFPWASiteConfig && !options.args) {
581-
options.args = Cc['@mozilla.org/supports-string;1'].createInstance(Ci.nsISupportsString);
582-
options.args.data = options.openerWindow.HomePage.get(options.openerWindow);
583-
}
584-
return BrowserWindowTracker._openWindow(options);
585-
}
586-
}
587470
}
588471

589472
handleDisablingShortcuts () {
@@ -1813,7 +1696,6 @@ class PwaBrowser {
18131696
configureAll () {
18141697
this.configureLayout();
18151698
this.configureSettings();
1816-
this.disableOnboarding();
18171699

18181700
setTimeout(() => { this.configureWidgets() });
18191701
}
@@ -2067,16 +1949,6 @@ class PwaBrowser {
20671949
}
20681950
}
20691951

2070-
disableOnboarding () {
2071-
// Disable default browser prompt
2072-
BrowserGlue.prototype._maybeShowDefaultBrowserPrompt = async () => null;
2073-
2074-
// Disable onboarding messages
2075-
OnboardingMessageProvider.getMessages = async () => [];
2076-
OnboardingMessageProvider.getUntranslatedMessages = async () => [];
2077-
OnboardingMessageProvider.getUntranslatedMessages = async () => null;
2078-
}
2079-
20801952
//////////////////////////////
20811953
// Utils
20821954
//////////////////////////////

0 commit comments

Comments
 (0)