Skip to content

Commit

Permalink
fix(recovery): reset rules
Browse files Browse the repository at this point in the history
Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com>
  • Loading branch information
whizzzkid committed Aug 25, 2023
1 parent 67bbd4a commit d9b16aa
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 27 deletions.
7 changes: 5 additions & 2 deletions add-on/src/lib/ipfs-companion.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { createRequestModifier } from './ipfs-request.js'
import createNotifier from './notifier.js'
import { runPendingOnInstallTasks } from './on-installed.js'
import { guiURLString, migrateOptions, optionDefaults, safeURL, storeMissingOptions } from './options.js'
import { getExtraInfoSpec } from './redirect-handler/blockOrObserve.js'
import { getExtraInfoSpec, notifyOptionChange } from './redirect-handler/blockOrObserve.js'
import createRuntimeChecks from './runtime-checks.js'
import { initState, offlinePeerCount } from './state.js'

Expand Down Expand Up @@ -577,7 +577,7 @@ export default async function init (inQuickImport = false) {
}
}

async function onStorageChange (changes, area) {
async function onStorageChange (changes) {
let shouldReloadExtension = false
let shouldRestartIpfsClient = false
let shouldStopIpfsClient = false
Expand Down Expand Up @@ -660,6 +660,7 @@ export default async function init (inQuickImport = false) {
break
}
}
await notifyOptionChange()
// ensure consent is set properly on state changes
handleConsentFromState(state)

Expand Down Expand Up @@ -696,6 +697,8 @@ export default async function init (inQuickImport = false) {
browser.tabs.reload() // async reload of options page to keep it alive
await browser.runtime.reload()
}
log('storage change processed')

// Post update to Browser Action (if exists) -- this gives UX a snappy feel
await sendStatusUpdateToBrowserAction()
}
Expand Down
17 changes: 3 additions & 14 deletions add-on/src/lib/redirect-handler/blockOrObserve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ const log = debug('ipfs-companion:redirect-handler:blockOrObserve')
log.error = debug('ipfs-companion:redirect-handler:blockOrObserve:error')

export const DEFAULT_NAMESPACES = new Set(['ipfs', 'ipns'])

export const GLOBAL_STATE_CHANGE = 'GLOBAL_STATE_CHANGE'
export const GLOBAL_STATE_OPTION_CHANGE = 'GLOBAL_STATE_OPTION_CHANGE'
export const DELETE_RULE_REQUEST = 'DELETE_RULE_REQUEST'
export const DELETE_RULE_REQUEST_SUCCESS = 'DELETE_RULE_REQUEST_SUCCESS'
Expand All @@ -32,7 +30,7 @@ interface redirectHandlerInput {
getPort: (state: CompanionState) => string
}

type messageToSelfType = typeof GLOBAL_STATE_CHANGE | typeof GLOBAL_STATE_OPTION_CHANGE | typeof DELETE_RULE_REQUEST
type messageToSelfType = typeof GLOBAL_STATE_OPTION_CHANGE | typeof DELETE_RULE_REQUEST
interface messageToSelf {
type: messageToSelfType
value?: string | Record<string, unknown>
Expand All @@ -49,19 +47,12 @@ export const defaultNSRegexStr = `(${[...DEFAULT_NAMESPACES].join('|')})`
// just check for the existence of the property, we need to call the browser instance at that point.
export const supportsBlock = (): boolean => !(browser.declarativeNetRequest?.MAX_NUMBER_OF_DYNAMIC_AND_SESSION_RULES > 0)

/**
* Notify self about state change.
* @returns void
*/
export async function notifyStateChange (): Promise<void> {
return await sendMessageToSelf(GLOBAL_STATE_CHANGE)
}

/**
* Notify self about option change.
* @returns void
*/
export async function notifyOptionChange (): Promise<void> {
log('notifyOptionChange')
return await sendMessageToSelf(GLOBAL_STATE_OPTION_CHANGE)
}

Expand Down Expand Up @@ -380,10 +371,8 @@ export function addRuleToDynamicRuleSetGenerator (
}

setupListeners({
[GLOBAL_STATE_CHANGE]: async (): Promise<void> => {
await reconcileRulesAndRemoveOld(getState())
},
[GLOBAL_STATE_OPTION_CHANGE]: async (): Promise<void> => {
log('GLOBAL_STATE_OPTION_CHANGE')
await cleanupRules(true)
await reconcileRulesAndRemoveOld(getState())
},
Expand Down
8 changes: 4 additions & 4 deletions add-on/src/options/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ export default function optionsPage (state, emit) {
// when global toggle is in "suspended" state
return html`
<div class="sans-serif">
${globalToggleForm({
active: state.options.active,
onOptionChange
})}
${globalToggleForm({
active: state.options.active,
onOptionChange
})}
</div>
`
}
Expand Down
5 changes: 1 addition & 4 deletions add-on/src/options/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import browser from 'webextension-polyfill'
import { optionDefaults } from '../lib/options.js'
import { DELETE_RULE_REQUEST_SUCCESS, RULE_REGEX_ENDING, notifyDeleteRule, notifyOptionChange, notifyStateChange } from '../lib/redirect-handler/blockOrObserve.js'
import { DELETE_RULE_REQUEST_SUCCESS, RULE_REGEX_ENDING, notifyDeleteRule, notifyOptionChange } from '../lib/redirect-handler/blockOrObserve.js'
import createRuntimeChecks from '../lib/runtime-checks.js'

// The store contains and mutates the state for the app
Expand Down Expand Up @@ -49,9 +49,6 @@ export default function optionStore (state, emitter) {

emitter.on('optionChange', async ({ key, value }) => {
browser.storage.local.set({ [key]: value })
if (key === 'active') {
await notifyStateChange()
}
await notifyOptionChange()
})

Expand Down
6 changes: 3 additions & 3 deletions add-on/src/popup/browser-action/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { optionsPage, welcomePage } from '../../lib/constants.js'
import { contextMenuCopyAddressAtPublicGw, contextMenuCopyCanonicalAddress, contextMenuCopyCidAddress, contextMenuCopyPermalink, contextMenuCopyRawCid, contextMenuViewOnGateway } from '../../lib/context-menus.js'
import { browserActionFilesCpImportCurrentTab } from '../../lib/ipfs-import.js'
import { ipfsContentPath } from '../../lib/ipfs-path.js'
import { notifyStateChange } from '../../lib/redirect-handler/blockOrObserve.js'
import { notifyOptionChange } from '../../lib/redirect-handler/blockOrObserve.js'
import { POSSIBLE_NODE_TYPES } from '../../lib/state.js'

// The store contains and mutates the state for the app
Expand Down Expand Up @@ -179,7 +179,7 @@ export default (state, emitter) => {
}
// console.dir('toggleSiteIntegrations', state)
await browser.storage.local.set({ disabledOn, enabledOn })
await notifyStateChange()
await notifyOptionChange()

const path = ipfsContentPath(currentTab.url, { keepURIParams: true })
// Reload the current tab to apply updated redirect preference
Expand Down Expand Up @@ -216,7 +216,7 @@ export default (state, emitter) => {
state.swarmPeers = null
state.isIpfsOnline = false
}
await notifyStateChange()
await notifyOptionChange()
await browser.storage.local.set({ active: state.active })
} catch (error) {
console.error(`Unable to update global Active flag due to ${error}`)
Expand Down

0 comments on commit d9b16aa

Please sign in to comment.