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

🚧 Provide a TabBase and TabManagerBase implementation #61

Draft
wants to merge 4 commits into
base: artifact-based-browser
Choose a base branch
from
Draft
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
140 changes: 117 additions & 23 deletions apps/extensions/lib/parent/ext-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const lazy = lazyESModuleGetters({
class TabTracker extends TabTrackerBase {
get activeTab() {
const window = lazy.WindowTracker.getActiveWindow()
return window?.activeTab()
return window?.activeTab() || null
}

init() {
Expand Down Expand Up @@ -50,28 +50,6 @@ class TabTracker extends TabTrackerBase {
return tab
}

/**
* @param {import('resource://gre/modules/Extension.sys.mjs').Extension} extension
* @param {import('@browser/tabs').WindowTab} tab
* @param {Window} window
*
* @returns {tabs__tabs.Tab}
*/
serializeTab(extension, tab, window) {
// TODO: Active tab & host permissions
const hasTabPermission = extension.hasPermission('tabs')

return {
id: tab.view.browserId,
index: window.windowTabs().findIndex((wTab) => wTab === tab),
active: window.activeTab() === tab,
highlighted: false, // TODO
title: (hasTabPermission && tab.view.title) || undefined,
url: (hasTabPermission && tab.view.uri.asciiSpec) || undefined,
windowId: window.windowId,
}
}

/**
* @param {XULBrowserElement} browser
*/
Expand All @@ -89,3 +67,119 @@ class TabTracker extends TabTrackerBase {
/** @global */
let tabTracker = new TabTracker()
Object.assign(global, { tabTracker })

class Tab extends TabBase {
get _favIconUrl() {
return this.nativeTab.view.iconUrl
}

get lastAccessed() {
return undefined
}
get audible() {
return undefined
}
get autoDiscardable() {
return false
}
get browser() {
return this.nativeTab.view.browser
}
get cookieStoreId() {
return undefined
}
get discarded() {
return undefined
}
get height() {
return this.nativeTab.view.browser.clientHeight
}
get hidden() {
return false
}
get index() {
const window = this.window
return (
window
?.windowTabs()
.findIndex(
(tab) => tab.view.browserId == this.nativeTab.view.browserId,
) || -1
)
}
get mutedInfo() {
return undefined
}
get sharingState() {
return undefined
}
get pinned() {
return false
}
get active() {
const window = this.window
return window?.activeTabId() == this.nativeTab.view.windowBrowserId
}
get highlighted() {
const window = this.window
return (
window
?.selectedTabIds()
.some((tab) => tab == this.nativeTab.view.windowBrowserId) ?? false
)
}
get status() {
return this.nativeTab.view.websiteState
}
get width() {
return this.browser.clientWidth
}
get window() {
return lazy.WindowTracker.getWindowWithBrowser(this.nativeTab.view.browser)
?.window
}
get windowId() {
return this.window?.windowId || -1
}
get attention() {
return false
}
get isArticle() {
return false
}
get isInReaderMode() {
return false
}
get successorTabId() {
return undefined
}
}

class TabManager extends TabManagerBase {
canAccessTab(_nativeTab) {
throw new Error('Method ')
}
get(tabId) {
const results = lazy.WindowTracker.getWindowWithBrowserId(tabId)
if (!results) return null
return this.wrapTab(results.tab)
}
/**
* @param {NativeTab} nativeTab
*/
wrapTab(nativeTab) {
return new Tab(this.extension, nativeTab, nativeTab.view.browserId || -1)
}

/**
* @param {NativeTab} nativeTab
* @public
*/
publicWrapTab(nativeTab) {
return this.wrapTab(nativeTab)
}
}

extensions.on('startup', (type, extension) => {
defineLazyGetter(extension, 'tabManager', () => new TabManager(extension))
})
16 changes: 3 additions & 13 deletions apps/extensions/lib/parent/ext-browserAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ this.browserAction = class extends ExtensionAPIPersistent {

async onManifestEntry() {
const { extension } = this
/** @type {browser_action__manifest.WebExtensionManifest__extended['browser_action']} */
const options = extension.manifest.browser_action

if (!options) {
Expand Down Expand Up @@ -67,25 +66,16 @@ this.browserAction = class extends ExtensionAPIPersistent {
* running extension context.
*/
onClicked({ fire }) {
const { extension } = this
const /** @type {Extension} */ extension = this.extension

/**
* @param {import("resource://app/modules/EBrowserActions.sys.mjs").IBrowserActionEvents['click']} clickInfo
*/
const callback = async (_name, clickInfo) => {
if (fire.wakeup) await fire.wakeup()
const { tab, window } = lazy.WindowTracker.getWindowWithBrowserId(
clickInfo.tabId,
) || { tab: null, window: null }

if (!tab || !window) {
return fire.sync(null, clickInfo.clickData)
}

fire.sync(
tabTracker.serializeTab(extension, tab, window),
clickInfo.clickData,
)
const tab = extension.tabManager.get(clickInfo.tabId)
fire.sync(tab.convert(), clickInfo.clickData)
}

this.on('click', callback)
Expand Down
Loading
Loading