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

feat(mv3): Config Migration #1228

Closed
wants to merge 5 commits into from
Closed
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
3 changes: 2 additions & 1 deletion add-on/src/background/background.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
'use strict'
/* eslint-env browser, webextensions */

import browser from 'webextension-polyfill'
import createIpfsCompanion from '../lib/ipfs-companion.js'
import { onInstalled } from '../lib/on-installed.js'
import { getUninstallURL } from '../lib/on-uninstalled.js'
import { debuggerInit } from '../lib/debugger/debugger.js'

// register lifecycle hooks early, otherwise we miss first install event
browser.runtime.onInstalled.addListener(onInstalled)
browser.runtime.setUninstallURL(getUninstallURL(browser))

const init = async () => {
await createIpfsCompanion()
await debuggerInit()
}

init()
1 change: 1 addition & 0 deletions add-on/src/lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
export const welcomePage = '/dist/landing-pages/welcome/index.html'
export const optionsPage = '/dist/options/options.html'
export const recoveryPagePath = '/dist/recovery/recovery.html'
export const mv3updatePage = '/dist/migrate/mv3update.html'
export const tickMs = 250 // no CPU spike, but still responsive enough
22 changes: 22 additions & 0 deletions add-on/src/lib/debugger/debugger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import browser from 'webextension-polyfill'
import debug from 'debug'
import { optionDefaults } from '../options.js'

export async function debuggerInit (): Promise<void> {
await enableDebugger()
await setupListeners()
}

function setupListeners (): void {
browser.storage.onChanged.addListener((changes, areaName): void => {
if (areaName === 'local' && ('logNamespaces' in changes)) {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
enableDebugger()
}
})
}

async function enableDebugger (): Promise<void> {
const debugNs = (await browser.storage.local.get({ logNamespaces: optionDefaults.logNamespaces })).logNamespaces
debug.enable(debugNs)
}
22 changes: 18 additions & 4 deletions add-on/src/lib/on-installed.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import browser from 'webextension-polyfill'
import debug from 'debug'
import { welcomePage } from './constants.js'
import { welcomePage, mv3updatePage } from './constants.js'
import { brave, braveNodeType } from './ipfs-client/brave.js'

const { version } = browser.runtime.getManifest()
Expand All @@ -19,7 +19,7 @@ export async function onInstalled (details) {
}

export async function runPendingOnInstallTasks () {
const { onInstallTasks, displayReleaseNotes } = await browser.storage.local.get(['onInstallTasks', 'displayReleaseNotes'])
const { onInstallTasks } = await browser.storage.local.get(['onInstallTasks', 'displayReleaseNotes'])
await browser.storage.local.remove('onInstallTasks')
switch (onInstallTasks) {
case 'onFirstInstall':
Expand All @@ -28,9 +28,23 @@ export async function runPendingOnInstallTasks () {
url: welcomePage
})
case 'onVersionUpdate':
if (!displayReleaseNotes) return
// temporary disabled due to upgrade to MV3.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to double check the version to ensure mv3 update is what's needed?

// TODO(whizzzkid): revert this logic to it's previous state once v3.0.0 is released.
// if (!displayReleaseNotes) return
await browser.storage.local.set({ dismissedUpdate: version })
return browser.tabs.create({ url: updatePage + version })
// return browser.tabs.create({ url: updatePage + version })
browser.runtime.onMessage.addListener((message) => {
if (message.type === 'ipfs-companion-migrate') {
browser.runtime.onMessage.removeListener(this)
Comment on lines +36 to +38
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is browser.runtime.onMessage.removeListener(this) supposed to be removing the anon/lambda function listener? this won't be the function. Change to non-anonymous function if that's the intention


browser.storage.local.set({
logNamespaces: message.payload.localStorage.logNamespaces,
countly: message.payload.localStorage.countly
})
browser.tabs.remove(message.sender.tab.id)
}
})
return browser.tabs.create({ url: mv3updatePage })
}
}

Expand Down
5 changes: 5 additions & 0 deletions add-on/src/migrate/mv3update.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<head>
<script src="/dist/bundles/mv3update.bundle.js"></script>
</head>
</html>
41 changes: 41 additions & 0 deletions add-on/src/migrate/mv3update.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import browser from 'webextension-polyfill'
import debug from 'debug'

const log = debug('ipfs-companion:mv3update')
log.error = debug('ipfs-companion:mv3update:error')

interface IResult {
logNamespaces?: string
countly?: {
container: string
[key: string]: string
}
}

function parseLocalStorage (): IResult {
const data = localStorage
const result: IResult = {}
Object.entries(data).forEach(([key, value]) => {
if (key === 'debug') {
result.logNamespaces = value
return
}
if (key.includes('cly')) {
const [container, clyKey] = key.split('/')
if (!('countly' in result)) {
result.countly = {
container
}
}
result.countly[clyKey] = value
}
})
return result
}

browser.runtime.sendMessage({
type: 'ipfs-companion-migrate',
payload: {
localStorage: parseLocalStorage()
}
}).catch(e => log.error(e))