Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Implement nsIAboutModule #45

Merged
merged 2 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading