Skip to content

Commit

Permalink
✨ Implement nsIAboutModule (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
trickypr authored Jan 2, 2024
1 parent 6506795 commit 4097921
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 2 deletions.
1 change: 0 additions & 1 deletion apps/content/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import WebpackLicensePlugin from 'webpack-license-plugin'

const HTML_TEMPLATE_FILE = './src/index.html'

const getDistFile = (file) => resolve('dist', file)
const absolutePackage = (file) => resolve('node_modules', file)

/**
Expand Down
4 changes: 4 additions & 0 deletions apps/misc/static/defaults/pref/prefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ pref('browser.download.clearHistoryOnDelete', 0);
// We do this because we want page actions etc. to have color schemes
pref('svg.context-properties.content.enabled', true);

// Security page preferences
// This requires our own testing server, which we don't have
pref('security.certerrors.mitm.priming.enabled', true);

// =============================================================================
// Multithreading

Expand Down
108 changes: 108 additions & 0 deletions apps/modules/lib/AboutRedirector.sys.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// @ts-check
/// <reference types="@browser/link" />

/**
* Based on Thunderbird's module with the same name
* {@link https://searchfox.org/comm-central/source/mail/components/AboutRedirector.jsm}
*/
export class AboutRedirector {
QueryInterface = ChromeUtils.generateQI(['nsIAboutModule'])

/**
* @type {Record<string, {url: string, flags: number}>}
* @private
*/
redirectMap = {
certerror: {
url: 'chrome://global/content/aboutNetError.xhtml',
flags:
Ci.nsIAboutModule.URI_SAFE_FOR_UNTRUSTED_CONTENT |
Ci.nsIAboutModule.URI_CAN_LOAD_IN_CHILD |
Ci.nsIAboutModule.ALLOW_SCRIPT |
Ci.nsIAboutModule.HIDE_FROM_ABOUTABOUT,
},
tests: {
url: 'chrome://browser/content/tests/index.html',
flags: Ci.nsIAboutModule.ALLOW_SCRIPT,
},
}

/**
* Filters out hashes and query strings from the about url, returning just the name
* @private
* @param {nsIURIType} uri
* @returns {string}
*/
getRedirectName({ pathQueryRef }) {
const [name] = /[^?#]+/.exec(pathQueryRef) || ['invalid']
return name.toLowerCase()
}

/**
* @private
* @param {string} name The name of the missing page
* @returns {never}
*/
notRegistered(name) {
throw new Error(`about:${name} was not registered with AboutRedirector`)
}

/**
* @param {nsIURIType} uri
* @param {nsILoadInfoType} loadInfo
* @returns {nsIChannelType}
*/
newChannel(uri, loadInfo) {
const redirectName = this.getRedirectName(uri)
const redirect = this.redirectMap[redirectName]
if (!redirect) this.notRegistered(redirectName)

const redirectUri = Services.io.newURI(redirect.url)
const channel = Services.io.newChannelFromURIWithLoadInfo(
redirectUri,
loadInfo,
)
channel.originalURI = uri

const safeForUntrustedContent =
redirect.flags & Ci.nsIAboutModule.URI_SAFE_FOR_UNTRUSTED_CONTENT
if (safeForUntrustedContent) {
channel.owner = Services.scriptSecurityManager.createContentPrincipal(
uri,
{},
)
}

return channel
}

/**
* Fetches some combination of flags for some `about:` url
* @param {nsIURIType} uri The url to get flags for
* @returns {number} The assigned flags
*/
getURIFlags(uri) {
const redirectName = this.getRedirectName(uri)
const redirect = this.redirectMap[redirectName]
if (!redirect) this.notRegistered(redirectName)

return redirect.flags
}

/**
* Finds the uri for a paticular `about:` about
* @param {nsIURIType} uri The url to match
* @returns {nsIURIType} The page to redirect to
*/
getChromeURI(uri) {
const redirectName = this.getRedirectName(uri)
const redirect = this.redirectMap[redirectName]
if (!redirect) this.notRegistered(redirectName)

return Services.io.newURI(redirect.url)
}
}
4 changes: 4 additions & 0 deletions docs/about-pages.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Custom about pages

1. You need to update the [runtime](https://github.com/pulse-browser/experiment-runtime/blob/main/src/quark-runtime/components/components.conf) component to include the contract `@mozilla.org/network/protocol/about;1?what=<page_name>`. You will need to wait for CI to build or use `rt:slink`
2. Add your page to `redirectMap` inside of `AboutRedirector.sys.mjs`
1 change: 0 additions & 1 deletion libs/shared/src/utilities/svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ export function resolverStore<T>(
export const dynamicStringPref =
<T>(processor: (value: string) => T) =>
(pref: string): Readable<T> => {
console.log('dynamicStringPref', pref)
return readable(
processor(Services.prefs.getStringPref(pref, '')),
(set) => {
Expand Down

0 comments on commit 4097921

Please sign in to comment.