Skip to content

Commit

Permalink
fix (quick-import): Duplicate behaviour in MV3 (#1215)
Browse files Browse the repository at this point in the history
* feat(mv3): ✨ ContextMenus MV3 Style

* feat(mv3): 🧪 Adding tests

* fix: test case

* fix(mv3): 🐛 Quick Import
  • Loading branch information
whizzzkid authored Jun 2, 2023
1 parent 554e69f commit 9f4981c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 36 deletions.
17 changes: 11 additions & 6 deletions add-on/src/lib/ipfs-client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@

import debug from 'debug'

import * as external from './external.js'
import * as embedded from './embedded.js'
import * as brave from './brave.js'
import { precache } from '../precache.js'
import * as brave from './brave.js'
import * as embedded from './embedded.js'
import * as external from './external.js'
import {
prepareReloadExtensions, WebUiReloader, LocalGatewayReloader, InternalTabReloader
InternalTabReloader,
LocalGatewayReloader,
WebUiReloader,
prepareReloadExtensions
} from './reloaders/index.js'
const log = debug('ipfs-companion:client')
log.error = debug('ipfs-companion:client:error')

// ensure single client at all times, and no overlap between init and destroy
let client

export async function initIpfsClient (browser, opts) {
export async function initIpfsClient (browser, opts, inQuickImport) {
log('init ipfs client')
if (client) return // await destroyIpfsClient()
let backend
Expand Down Expand Up @@ -48,7 +51,9 @@ export async function initIpfsClient (browser, opts) {
throw new Error(`Unsupported ipfsNodeType: ${opts.ipfsNodeType}`)
}
const instance = await backend.init(browser, opts)
_reloadIpfsClientDependents(browser, instance, opts) // async (API is present)
if (!inQuickImport) {
_reloadIpfsClientDependents(browser, instance, opts) // async (API is present)
}
client = backend
return instance
}
Expand Down
68 changes: 40 additions & 28 deletions add-on/src/lib/ipfs-companion.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@

import debug from 'debug'

import browser from 'webextension-polyfill'
import toMultiaddr from 'uri-to-multiaddr'
import pMemoize from 'p-memoize'
import LRU from 'lru-cache'
import all from 'it-all'
import { optionDefaults, storeMissingOptions, migrateOptions, guiURLString, safeURL } from './options.js'
import { initState, offlinePeerCount } from './state.js'
import { createIpfsPathValidator, dropSlash, sameGateway, safeHostname } from './ipfs-path.js'
import LRU from 'lru-cache'
import pMemoize from 'p-memoize'
import toMultiaddr from 'uri-to-multiaddr'
import browser from 'webextension-polyfill'
import { contextMenuCopyAddressAtPublicGw, contextMenuCopyCanonicalAddress, contextMenuCopyCidAddress, contextMenuCopyPermalink, contextMenuCopyRawCid, contextMenuViewOnGateway, createContextMenus, findValueForContext } from './context-menus.js'
import createCopier from './copier.js'
import createDnslinkResolver from './dnslink.js'
import { registerSubdomainProxy } from './http-proxy.js'
import createInspector from './inspector.js'
import { braveNodeType, releaseBraveEndpoint, useBraveEndpoint } from './ipfs-client/brave.js'
import { destroyIpfsClient, initIpfsClient, reloadIpfsClientOfflinePages } from './ipfs-client/index.js'
import { browserActionFilesCpImportCurrentTab, createIpfsImportHandler, formatImportDirectory } from './ipfs-import.js'
import { createIpfsPathValidator, dropSlash, safeHostname, sameGateway } from './ipfs-path.js'
import { createRequestModifier } from './ipfs-request.js'
import { initIpfsClient, destroyIpfsClient, reloadIpfsClientOfflinePages } from './ipfs-client/index.js'
import { braveNodeType, useBraveEndpoint, releaseBraveEndpoint } from './ipfs-client/brave.js'
import { createIpfsImportHandler, formatImportDirectory, browserActionFilesCpImportCurrentTab } from './ipfs-import.js'
import createNotifier from './notifier.js'
import createCopier from './copier.js'
import createInspector from './inspector.js'
import createRuntimeChecks from './runtime-checks.js'
import { createContextMenus, findValueForContext, contextMenuCopyAddressAtPublicGw, contextMenuCopyRawCid, contextMenuCopyCanonicalAddress, contextMenuViewOnGateway, contextMenuCopyPermalink, contextMenuCopyCidAddress } from './context-menus.js'
import { registerSubdomainProxy } from './http-proxy.js'
import { runPendingOnInstallTasks } from './on-installed.js'
import { guiURLString, migrateOptions, optionDefaults, safeURL, storeMissingOptions } from './options.js'
import { getExtraInfoSpec } from './redirect-handler/blockOrObserve.js'
import createRuntimeChecks from './runtime-checks.js'
import { initState, offlinePeerCount } from './state.js'

// this won't work in webworker context. Needs to be enabled manually
// https://github.com/debug-js/debug/issues/916
Expand All @@ -33,7 +33,7 @@ log.error = debug('ipfs-companion:main:error')
let browserActionPort // reuse instance for status updates between on/off toggles

// init happens on addon load in background/background.js
export default async function init () {
export default async function init (inQuickImport = false) {
// INIT
// ===================================================================
let ipfs // ipfs-api instance
Expand Down Expand Up @@ -65,7 +65,7 @@ export default async function init () {
if (state.active) {
// It's ok for this to fail, node might be unavailable or mis-configured
try {
ipfs = await initIpfsClient(browser, state)
ipfs = await initIpfsClient(browser, state, inQuickImport)
} catch (err) {
console.error('[ipfs-companion] Failed to init IPFS client', err)
notify(
Expand All @@ -81,12 +81,14 @@ export default async function init () {
copier = createCopier(notify, ipfsPathValidator)
ipfsImportHandler = createIpfsImportHandler(getState, getIpfs, ipfsPathValidator, runtime, copier)
inspector = createInspector(notify, ipfsPathValidator, getState)
contextMenus = createContextMenus(getState, runtime, ipfsPathValidator, {
onAddFromContext,
onCopyCanonicalAddress: copier.copyCanonicalAddress,
onCopyRawCid: copier.copyRawCid,
onCopyAddressAtPublicGw: copier.copyAddressAtPublicGw
})
if (!inQuickImport) {
contextMenus = createContextMenus(getState, runtime, ipfsPathValidator, {
onAddFromContext,
onCopyCanonicalAddress: copier.copyCanonicalAddress,
onCopyRawCid: copier.copyRawCid,
onCopyAddressAtPublicGw: copier.copyAddressAtPublicGw
})
}
modifyRequest = createRequestModifier(getState, dnslinkResolver, ipfsPathValidator, runtime)
log('register all listeners')
registerListeners()
Expand Down Expand Up @@ -358,16 +360,22 @@ export default async function init () {
// immediately preceding a switch from one browser window to another.
if (windowId !== browser.windows.WINDOW_ID_NONE) {
const currentTab = await browser.tabs.query({ active: true, windowId }).then(tabs => tabs[0])
await contextMenus.update(currentTab.id)
if (!inQuickImport) {
await contextMenus.update(currentTab.id)
}
}
}

async function onActivatedTab (activeInfo) {
await contextMenus.update(activeInfo.tabId)
if (!inQuickImport) {
await contextMenus.update(activeInfo.tabId)
}
}

async function onNavigationCommitted (details) {
await contextMenus.update(details.tabId)
if (!inQuickImport) {
await contextMenus.update(details.tabId)
}
await updatePageActionIndicator(details.tabId, details.url)
}

Expand Down Expand Up @@ -437,8 +445,12 @@ export default async function init () {
await Promise.all([
updateAutomaticModeRedirectState(oldPeerCount, state.peerCount),
updateBrowserActionBadge(),
contextMenus.update(),
sendStatusUpdateToBrowserAction()
sendStatusUpdateToBrowserAction(),
() => {
if (!inQuickImport) {
contextMenus.update()
}
}
])
}

Expand Down
4 changes: 2 additions & 2 deletions add-on/src/popup/quick-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ async function processFiles (state, emitter, files) {

let ipfs
if (httpStreaming) {
// We create separate instance of http client running in thie same page to
// We create separate instance of http client running in the same page to
// avoid serialization issues in Chromium
// (https://bugs.chromium.org/p/chromium/issues/detail?id=112163) when
// crossing process boundary, which enables streaming upload of big files
Expand Down Expand Up @@ -151,7 +151,7 @@ async function processFiles (state, emitter, files) {
if (state.userChangedImportDir) {
emitter.emit('optionChange', { key: 'importDir', value: state.importDir })
}
// present result to the user using the beast available way
// present result to the user using the best available way
if (!state.openViaWebUI || state.ipfsNodeType.startsWith('embedded')) {
await openFilesAtGateway(importDir)
} else {
Expand Down

0 comments on commit 9f4981c

Please sign in to comment.