Skip to content

Commit

Permalink
🚧 Move modules over
Browse files Browse the repository at this point in the history
Does not build
  • Loading branch information
trickypr committed Dec 29, 2023
1 parent c2e1dcc commit 098cd4c
Show file tree
Hide file tree
Showing 27 changed files with 1,262 additions and 759 deletions.
12 changes: 12 additions & 0 deletions app/actors/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "actors",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
12 changes: 12 additions & 0 deletions app/content/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "content",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
12 changes: 12 additions & 0 deletions app/extensions/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "extensions",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
72 changes: 72 additions & 0 deletions app/modules/lib/BrowserGlue.sys.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/// @ts-check
/// <reference types="@browser/link" />
import { lazyESModuleGetters } from 'resource://app/modules/TypedImportUtils.sys.mjs'

const lazy = lazyESModuleGetters({
ActorManagerParent: 'resource://gre/modules/ActorManagerParent.sys.mjs',
})

const JS_PROCESS_ACTORS = {}
const JS_WINDOW_ACTORS = {
ClickHandler: {
parent: { esModuleURI: 'resource://app/actors/ClickHandlerParent.sys.mjs' },
child: {
esModuleURI: 'resource://app/actors/ClickHandlerChild.sys.mjs',
events: {
chromelinkclick: { capture: true, mozSystemGroup: true },
},
},

allFrames: true,
},

ContextMenu: {
parent: {
esModuleURI: 'resource://app/actors/ContextMenuParent.sys.mjs',
},

child: {
esModuleURI: 'resource://app/actors/ContextMenuChild.sys.mjs',
events: {
contextmenu: { mozSystemGroup: true },
},
},

messageManagerGroups: ['browsers'],
allFrames: true,
},

LinkHandler: {
parent: {
esModuleURI: 'resource://app/actors/LinkHandlerParent.sys.mjs',
},
child: {
esModuleURI: 'resource://app/actors/LinkHandlerChild.sys.mjs',
events: {
DOMHeadElementParsed: {},
DOMLinkAdded: {},
DOMLinkChanged: {},
pageshow: {},
// The `pagehide` event is only used to clean up state which will not be
// present if the actor hasn't been created.
pagehide: { createActor: false },
},
},
messageManagerGroups: ['browsers'],
},
}

export class BrowserGlue {
QueryInterface = ChromeUtils.generateQI(['nsIObserver'])

constructor() {
lazy.ActorManagerParent.addJSProcessActors(JS_PROCESS_ACTORS)
lazy.ActorManagerParent.addJSWindowActors(JS_WINDOW_ACTORS)
}

// nsIObserver impl
observe(aSubject, aTopic, aData) {
// eslint-disable-next-line no-console
console.log({ aSubject, aTopic, aData })
}
}
52 changes: 52 additions & 0 deletions app/modules/lib/BrowserWindowTracker.sys.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/// @ts-check
/// <reference types="@browser/link" />
import mitt from 'resource://app/modules/mitt.sys.mjs'

/** @type {import('resource://app/modules/BrowserWindowTracker.sys.mjs')['WindowTracker']} */
export const WindowTracker = {
nextWindowId: 0,

events: mitt(),

activeWindow: null,
registeredWindows: new Map(),

/**
* Registers a new browser window to be tracked
*
* @param w The window to register
*/
registerWindow(w) {
w.windowApi.id = this.nextWindowId++
this.registeredWindows.set(w.windowApi.id, w)
this.events.emit('windowCreated', w)
},

removeWindow(w) {
this.registeredWindows.delete(w.windowApi.id)
this.events.emit('windowDestroyed', w)
},

getWindowById(wid) {
return this.registeredWindows.get(wid)
},

getWindowWithBrowser(browser) {
for (const window of this.registeredWindows.values()) {
const tab = window.windowApi.tabs.tabs.find(
(t) => t.getTabId() === browser.browserId,
)
if (tab) return { window, tab }
}
return null
},

focusWindow(id) {
this.activeWindow = id
this.events.emit('focus', this.getActiveWindow())
},

getActiveWindow() {
return this.registeredWindows.get(this.activeWindow ?? -1)
},
}
108 changes: 108 additions & 0 deletions app/modules/lib/EPageActions.sys.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/// @ts-check
/// <reference types="@browser/link" />
import mitt from 'resource://app/modules/mitt.sys.mjs'

/**
* @typedef {import('resource://app/modules/EPageActions.sys.mjs').PageActionImpl} PageActionImpl
*/

/** @implements {PageActionImpl} */
export class PageAction {
/** @type {import('resource://app/modules/EPageActions.sys.mjs').PageActionImpl['events']} */
events = mitt()

tooltip
popupUrl
showMatches
hideMatches
/** @type {Record<number, string>} */
icons

showTabIds = new Set()
hideTabIds = new Set()

/**
* @param {import('resource://app/modules/EPageActions.sys.mjs').PageActionOptions<string[]>} data
*/
constructor(data) {
this.tooltip = data.tooltip
this.popupUrl = data.popupUrl
this.showMatches = new MatchPatternSet(data.showMatches || [])
this.hideMatches = new MatchPatternSet(data.hideMatches || [])
}

/**
* @param {string} url
* @param {number} tabId
* @returns {boolean}
*/
shouldShow(url, tabId) {
const urlMatch =
this.showMatches.matches(url) &&
!this.hideMatches.matches(url, true) &&
!this.hideTabIds.has(tabId)
const idMatch = this.showTabIds.has(tabId)

return urlMatch || idMatch
}

/**
* @param {Record<number, string>} icons
*/
setIcons(icons) {
this.icons = icons
this.events.emit('updateIcon', icons)
}

/**
* @param {number} id
*/
addShow(id) {
this.showTabIds.add(id)
this.hideTabIds.delete(id)
}

/**
* @param {number} id
*/
addHide(id) {
this.hideTabIds.add(id)
this.showTabIds.delete(id)
}
}

/** @type {import('resource://app/modules/EPageActions.sys.mjs')['EPageActions']} */
export const EPageActions = {
/** @type {import('resource://app/modules/EPageActions.sys.mjs')['EPageActions']['events']} */
events: mitt(),

PageAction,
pageActions: new Map(),

/**
* @param {string} extensionId
* @param {PageAction} pageAction
*/
registerPageAction(extensionId, pageAction) {
if (EPageActions.pageActions.has(extensionId)) {
EPageActions.removePageAction(extensionId)
}

EPageActions.pageActions.set(extensionId, pageAction)
EPageActions.events.emit('register', {
action: pageAction,
extension: extensionId,
})
},

/**
* @param {string} extensionId
*/
removePageAction(extensionId) {
const action = EPageActions.pageActions.get(extensionId)
if (!action) return

EPageActions.pageActions.delete(extensionId)
EPageActions.events.emit('remove', { action, extension: extensionId })
},
}
10 changes: 10 additions & 0 deletions app/modules/lib/TypedImportUtils.sys.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/// @ts-check
/// <reference types="@browser/link" />

/** @type {typeof import('resource://app/modules/TypedImportUtils.sys.mjs').lazyESModuleGetters} */
export const lazyESModuleGetters = (modules) => {
/** @type {any} */
const lazy = {}
ChromeUtils.defineESModuleGetters(lazy, modules)
return lazy
}
12 changes: 12 additions & 0 deletions app/modules/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "@browser/modules",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"@browser/link": "workspace:*"
},
"keywords": [],
"author": "",
"license": "ISC"
}
12 changes: 12 additions & 0 deletions lib/components/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "components",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
13 changes: 13 additions & 0 deletions lib/link/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "@browser/link",
"version": "1.0.0",
"description": "",
"types": "./types/_link.d.ts",
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"gecko-types": "github:quark-platform/gecko-types",
"mitt": "^3.0.1"
}
}
17 changes: 17 additions & 0 deletions lib/link/types/_link.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// This is the root file that will be imported everywhere. Add your new files
// here to have them show up globally

/// <reference types="gecko-types" />

/// <reference path="./globals/Cr.d.ts" />
/// <reference path="./globals/Elements.d.ts" />
/// <reference path="./globals/MatchPattern.d.ts" />
/// <reference path="./globals/MessageManager.d.ts" />

/// <reference path="./interfaces/GenericDeferred.d.ts" />

/// <reference path="./modules/AppConstants.d.ts" />
/// <reference path="./modules/BrowserWindowTracker.d.ts" />
/// <reference path="./modules/EPageActions.d.ts" />
/// <reference path="./modules/mitt.d.ts" />
/// <reference path="./modules/typedImport.d.ts" />
1 change: 1 addition & 0 deletions lib/link/types/globals/Cr.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare let Cr: Record<string, nsresult>
Loading

0 comments on commit 098cd4c

Please sign in to comment.