Skip to content

Commit

Permalink
add confVersion to sync new conf to frozen / history pages
Browse files Browse the repository at this point in the history
  • Loading branch information
gdh1995 committed Sep 9, 2023
1 parent 49dc848 commit 35eb596
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 49 deletions.
5 changes: 3 additions & 2 deletions background/key_mappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -914,9 +914,10 @@ updateHooks_.keyMappings = (value: string | null): void => {
updatesInKeyFSM = !!oldFSM && f(keyFSM_) !== f(oldFSM),
updatesInMappedKeys = oldMappedKeys ? !curMapped || f(oldMappedKeys) !== f(curMapped) : !!oldFSM && !!curMapped;
(updatesInMappedKeys || updatesInKeyFSM) && settings_.broadcast_({
N: kBgReq.keyFSM, m: mappedKeyRegistry_, t: mappedKeyTypes_, k: updatesInKeyFSM ? keyFSM_ : null
N: kBgReq.keyFSM, m: mappedKeyRegistry_, t: mappedKeyTypes_, k: updatesInKeyFSM ? keyFSM_ : null,
v: BgUtils_.nextConfUpdate(0)
});
if (oldOmniMapKeys ? !curOmniMapped || f(oldOmniMapKeys) !== f(curOmniMapped) : curOmniMapped) {
settings_.broadcastOmni_({ N: kBgReq.omni_updateOptions, d: { m: curOmniMapped } })
settings_.broadcastOmniConf_({ m: curOmniMapped })
}
};
8 changes: 5 additions & 3 deletions background/page_handlers.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import {
contentPayload_, evalVimiumUrl_, keyFSM_, keyToCommandMap_, mappedKeyRegistry_, newTabUrls_, restoreSettings_,
CONST_, settingsCache_, shownHash_, substitute_, framesForTab_, curTabId_, extAllowList_, OnChrome, reqH_, OnEdge,
storageCache_, os_, framesForOmni_, updateHooks_, Origin2_, CurCVer_
storageCache_, os_, framesForOmni_, updateHooks_, Origin2_, CurCVer_, omniConfVer_
} from "./store"
import { deferPromise_, protocolRe_, safeObj_ } from "./utils"
import { browserWebNav_, browser_, getCurTab, getTabUrl, Q_, runContentScriptsOn_, runtimeError_, tabsCreate, tabsGet } from "./browser"
import {
browserWebNav_, browser_, getCurTab, getTabUrl, Q_, runContentScriptsOn_, runtimeError_, tabsCreate, tabsGet
} from "./browser"
import { convertToUrl_, lastUrlType_, reformatURL_ } from "./normalize_urls"
import { findUrlInText_, parseSearchEngines_ } from "./parse_urls"
import * as settings_ from "./settings"
Expand Down Expand Up @@ -270,7 +272,7 @@ const pageRequestHandlers_: {
/** kPgReq.updateOmniPayload: */ ({ key, val }, port): void => {
const tabId = port && port.s && port.s.tabId_ || curTabId_
const omniPort = framesForOmni_.find(i => i.s.tabId_ === tabId)
omniPort && omniPort.postMessage({ N: kBgReq.omni_updateOptions, d: { [key]: val } })
omniPort && omniPort.postMessage({ N: kBgReq.omni_updateOptions, d: { [key]: val }, v: omniConfVer_ })
},
/** kPgReq.saveToSyncAtOnce: */ (): void => {
settingsCache_.vimSync && updateHooks_.vimSync!(true, "vimSync")
Expand Down
30 changes: 21 additions & 9 deletions background/ports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
needIcon_, cPort, set_cPort, reqH_, contentPayload_, omniPayload_, innerCSS_, extAllowList_, framesForTab_, findCSS_,
framesForOmni_, getNextFakeTabId, curTabId_, vomnibarPage_f, OnChrome, CurCVer_, OnEdge, setIcon_, lastKeptTabId_,
keyFSM_, mappedKeyRegistry_, CONST_, mappedKeyTypes_, recencyForTab_, setTeeTask_, OnFirefox, UseZhLang_, blank_,
set_lastKeptTabId_
set_lastKeptTabId_, omniConfVer_, contentConfVer_
} from "./store"
import { asyncIter_, deferPromise_, getOmniSecret_, isNotPriviledged, keys_ } from "./utils"
import {
Expand Down Expand Up @@ -123,10 +123,12 @@ export const OnConnect = (port: Frames.Port, type: PortType): void => {
/*#__NOINLINE__*/ _recoverStates(ref, port, type)
} else {
port.postMessage<kBgReq.init>(type & PortType.confInherited ? {
N: kBgReq.init, c: null as never as SettingsNS.FrontendSettingCache, d: isInactive, f: flags, p: passKeys
} satisfies Omit<Req.bg<kBgReq.init>, keyof BgReq[kBgReq.keyFSM]> as Req.bg<kBgReq.init> : {
N: kBgReq.init, c: null as never as SettingsNS.FrontendSettingCache, d: isInactive, f: flags, p: passKeys,
v: contentConfVer_
} satisfies Omit<Req.bg<kBgReq.init>, keyof Omit<BgReq[kBgReq.keyFSM], keyof ConfVersionReq>> as Req.bg<kBgReq.init>
: {
N: kBgReq.init, c: contentPayload_, d: isInactive, f: flags,
k: keyFSM_!, m: mappedKeyRegistry_, p: passKeys, t: mappedKeyTypes_
k: keyFSM_!, m: mappedKeyRegistry_, p: passKeys, t: mappedKeyTypes_, v: contentConfVer_
})
if (isInactive) {
port.disconnect()
Expand Down Expand Up @@ -207,8 +209,14 @@ const _onOmniConnect = (port: Frames.Port, type: PortType, isOmniUrl: boolean):
if (!OnChrome) { (port as Frames.BrowserPort).sender.tab = null as never }
port.onDisconnect.addListener(/*#__NOINLINE__*/ onOmniDisconnect)
port.onMessage.addListener(/*#__NOINLINE__*/ onMessage)
type & PortType.reconnect ||
port.postMessage({ N: kBgReq.omni_init, l: omniPayload_, s: getOmniSecret_(false) })
if (type & PortType.reconnect) {
const oldConfVer = type >> PortType.OFFSET_SETTINGS
if (oldConfVer !== omniConfVer_) {
port.postMessage({ N: kBgReq.omni_updateOptions, d: omniPayload_, v: omniConfVer_ })
}
} else {
port.postMessage({ N: kBgReq.omni_init, l: omniPayload_, s: getOmniSecret_(false), v: omniConfVer_ })
}
return
}
} else if (port.s.tabId_ < 0 // e.g.: inside a sidebar on MS Edge
Expand Down Expand Up @@ -699,7 +707,11 @@ const _recoverStates = (frames: Frames.Frames | undefined, port: Port, type: Por
: (type & PortType.hasCSS) && Frames.Flags.hasCSS
frames || refreshPorts_({ cur_: port, top_: null, ports_: [], lock_: null, flags_: Frames.Flags.HadIFrames }, 0)
let flag = type as Frames.Flags
if (!(type & PortType.refreshInBatch)) {
if (type & PortType.refreshInBatch) { /* empty */ }
else if ((type >> PortType.OFFSET_SETTINGS) !== contentConfVer_
&& !(frames && frames.flags_ & Frames.Flags.MASK_UPDATES)) {
flag = Frames.Flags.MASK_UPDATES
} else {
if (!(type & PortType.hasFocus) // frame is not focused - on refreshing ports of inactive tabs
|| !frames || !(frames.flags_ & Frames.Flags.ResReleased)) { // no old data to sync
return
Expand All @@ -708,11 +720,11 @@ const _recoverStates = (frames: Frames.Frames | undefined, port: Port, type: Por
if (flag & Frames.Flags.HadIFrames || port.s.frameId_) { refreshPorts_(frames, 0) }
}
if (flag & Frames.Flags.SettingsUpdated) {
port.postMessage({ N: kBgReq.settingsUpdate, d: contentPayload_ })
port.postMessage({ N: kBgReq.settingsUpdate, d: contentPayload_, v: contentConfVer_ })
}
if (flag & Frames.Flags.KeyMappingsUpdated) {
port.postMessage({ N: kBgReq.keyFSM, m: mappedKeyRegistry_, t: mappedKeyTypes_
, k: flag & Frames.Flags.KeyFSMUpdated ? keyFSM_ : null })
, k: flag & Frames.Flags.KeyFSMUpdated ? keyFSM_ : null, v: contentConfVer_ })
}
if (flag & Frames.Flags.CssUpdated && port.s.flags_ & Frames.Flags.hasCSS) {
(port.s.flags_ satisfies Frames.Flags) |= Frames.Flags.hasFindCSS
Expand Down
23 changes: 15 additions & 8 deletions background/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import {
contentPayload_, extAllowList_, newTabUrls_, omniPayload_, OnChrome, OnEdge, OnFirefox, framesForOmni_, sync_, IsEdg_,
settingsCache_, bgIniting_, set_bgIniting_, CurCVer_, CONST_, OnOther_, onInit_, storageCache_, searchEngines_,
set_hasEmptyLocalStorage_, set_newTabUrl_f, newTabUrl_f, set_vomnibarPage_f, updateHooks_,set_CurFFVer_, UseZhLang_,
CurFFVer_, set_os_, os_
CurFFVer_, set_os_, os_, contentConfVer_
} from "./store"
import { asyncIter_, nextTick_, safeObj_ } from "./utils"
import { asyncIter_, nextTick_, safeObj_, nextConfUpdate } from "./utils"
import { browser_, normalizeExtOrigin_, Qs_ } from "./browser"
import { convertToUrl_, reformatURL_ } from "./normalize_urls"
import { parseSearchEngines_ } from "./parse_urls"
Expand Down Expand Up @@ -162,27 +162,34 @@ export const broadcast_ = <K extends kBgReq.settingsUpdate | kBgReq.url | kBgReq

const _BroadcastSettingsUpdates = <K extends keyof BgReq> (
request: K extends kBgReq.settingsUpdate ? SettingsUpdateMsg : Req.bg<K>): void => {
if (request.N === kBgReq.settingsUpdate && !request.d) {
const reqName = request.N
if (reqName === kBgReq.settingsUpdate && !(request.d as typeof request.d | null)) {
const obj = newSettingsToBroadcast_!
const d: BgReq[kBgReq.settingsUpdate]["d"] = (request as Req.bg<kBgReq.settingsUpdate>).d = {}
for (const key of obj) {
(d as Generalized<typeof d>)[key] = contentPayload_[key]
}
newSettingsToBroadcast_ = null
}
asyncIterFrames_(request.N === kBgReq.url ? Frames.Flags.UrlUpdated
: request.N === kBgReq.keyFSM ? Frames.Flags.KeyMappingsUpdated | (request.k ? Frames.Flags.KeyFSMUpdated : 0)
const needConfVer = reqName === kBgReq.keyFSM || kBgReq.settingsUpdate
asyncIterFrames_(reqName === kBgReq.url ? Frames.Flags.UrlUpdated
: reqName === kBgReq.keyFSM ? Frames.Flags.KeyMappingsUpdated | (request.k ? Frames.Flags.KeyFSMUpdated : 0)
: Frames.Flags.SettingsUpdated
, (frames: Frames.Frames): void => {
needConfVer && ((request as Req.bg<kBgReq.keyFSM | kBgReq.settingsUpdate>).v = contentConfVer_)
for (const port of frames.ports_) {
port.postMessage(request as Req.baseBg<K> as Req.bg<K>)
}
})
}

export const broadcastOmni_ = <K extends ValidBgVomnibarReq> (request: Req.bg<K>): void => {
export const broadcastOmniConf_ = (payload: BgVomnibarSpecialReq[kBgReq.omni_updateOptions]["d"]
, excluded?: Port): void => {
const msg: Req.bg<kBgReq.omni_updateOptions> = { N: kBgReq.omni_updateOptions, d: payload, v: nextConfUpdate(1) }
asyncIter_(framesForOmni_.slice(0), (frame): number => {
framesForOmni_.includes(frame) && frame.postMessage(request)
framesForOmni_.includes(frame) && frame.postMessage(frame !== excluded ? msg : {
N: kBgReq.omni_updateOptions, d: {}, v: msg.v
})
return 1
})
}
Expand Down Expand Up @@ -252,7 +259,7 @@ Object.assign<typeof updateHooks_, { [key in SettingsNS.DeclaredUpdateHooks]: Se
grabBackFocus (value: SettingsWithDefaults["grabBackFocus"]): void { contentPayload_.g = value },
keyLayout (value): void {
omniPayload_.l = contentPayload_.l
broadcastOmni_({ N: kBgReq.omni_updateOptions, d: { l: contentPayload_.l } })
broadcastOmniConf_({ l: contentPayload_.l })
if (needToUpgradeSettings_ & 1 && !(value & kKeyLayout.fromOld)) {
const hasInLocal = needToUpgradeSettings_ & 2
needToUpgradeSettings_ &= ~(1 | 2)
Expand Down
4 changes: 4 additions & 0 deletions background/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ export const omniPayload_ = {
c: "", i: 0, l: 0, m: null, n: 0, s: "", t: ""
} satisfies SettingsNS.DeclaredVomnibarPayload as SettingsNS.VomnibarPayload
export const vomnibarBgOptions_ = { actions: [] as string[] }
export let contentConfVer_ = 0
export let omniConfVer_ = 0
export let omniStyleOverridden_ = false
export let findCSS_: FindCSS
export let innerCSS_: string
Expand Down Expand Up @@ -190,6 +192,8 @@ export const set_onInit_ = (_newInit: typeof onInit_): void => { onInit_ = _newI
export const set_iconData_ = (_newIconData: typeof iconData_): void => { iconData_ = _newIconData }
export const set_hasGroupPermission_ff_ = (_newAllowed: boolean | 0): void => { hasGroupPermission_ff_ = _newAllowed }
export const set_lastKeptTabId_ = (_newKeptTabId: number): void => { lastKeptTabId_ = _newKeptTabId }
export const set_contentConfVer_ = (_newContConfVer: number): number => { return contentConfVer_ = _newContConfVer }
export const set_omniConfVer_ = (_newOmniConfVer: number): number => { return omniConfVer_ = _newOmniConfVer }

export const set_reqH_ = (_newRH: BackendHandlersNS.FgRequestHandlers): void => { reqH_ = _newRH }
export const set_bgC_ = (_newBgC: typeof bgC_): void => { bgC_ = _newBgC }
Expand Down
4 changes: 2 additions & 2 deletions background/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -762,10 +762,10 @@ updateHooks_.vomnibarOptions = (options: SettingsNS.BackendSettings["vomnibarOpt
MediaWatcher_.update_(MediaNS.kName.PrefersReduceMotion, 1)
MediaWatcher_.update_(MediaNS.kName.PrefersColorScheme, 1)
}
settings_.broadcastOmni_({ N: kBgReq.omni_updateOptions, d: {
settings_.broadcastOmniConf_({
n: maxMatches,
i: queryInterval,
s: sizes,
t: payload.t
} })
})
}
16 changes: 7 additions & 9 deletions background/ui_css.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {
findCSS_, innerCSS_, omniPayload_, set_findCSS_, set_innerCSS_, CurCVer_, CurFFVer_, IsEdg_, omniStyleOverridden_,
OnChrome, OnEdge, OnFirefox, isHighContrast_ff_, set_isHighContrast_ff_, bgIniting_, CONST_, set_helpDialogData_,
framesForOmni_, settingsCache_, set_omniStyleOverridden_, updateHooks_, storageCache_, installation_
settingsCache_, set_omniStyleOverridden_, updateHooks_, storageCache_, installation_, contentConfVer_
} from "./store"
import { asyncIter_, fetchFile_, spacesRe_ } from "./utils"
import { fetchFile_, nextConfUpdate, spacesRe_ } from "./utils"
import { getFindCSS_cr_, set_getFindCSS_cr_ } from "./browser"
import { ready_, broadcastOmni_, postUpdate_, setInLocal_ } from "./settings"
import { ready_, broadcastOmniConf_, postUpdate_, setInLocal_ } from "./settings"
import { asyncIterFrames_ } from "./ports"

export declare const enum MergeAction {
Expand Down Expand Up @@ -208,6 +208,7 @@ export const mergeCSS = (css2Str: string, action: MergeAction | "userDefinedCss"
setInLocal_(O, omni2 || null)
reloadCSS_(MergeAction.readFromCache, newInnerCSS)
if (action !== MergeAction.readFromCache && action !== MergeAction.rebuildWhenInit) {
nextConfUpdate(0)
asyncIterFrames_(Frames.Flags.CssUpdated, (frames: Frames.Frames): void => {
for (const port of frames.ports_) {
const flags = port.s.flags_
Expand All @@ -216,10 +217,11 @@ export const mergeCSS = (css2Str: string, action: MergeAction | "userDefinedCss"
N: kBgReq.showHUD, H: innerCSS_,
f: flags & Frames.Flags.hasFindCSS ? OnChrome ? getFindCSS_cr_!(port.s) : findCSS_ : void 0
})
port.postMessage({ N: kBgReq.settingsUpdate, d: {}, v: contentConfVer_ })
}
}
})
broadcastOmni_({ N: kBgReq.omni_updateOptions, d: { c: omniPayload_.c } })
broadcastOmniConf_({ c: omniPayload_.c })
}
}

Expand All @@ -240,11 +242,7 @@ export const setOmniStyle_ = (req: FgReq[kFgReq.setOmniStyle], port?: Port): voi
}
if (styles === curStyles) { return }
omniPayload_.t = styles
const request2: Req.bg<kBgReq.omni_updateOptions> = { N: kBgReq.omni_updateOptions, d: { t: styles } }
asyncIter_(framesForOmni_.slice(0), (frame): number => {
frame !== port && framesForOmni_.includes(frame) && frame.postMessage(request2)
return 1
})
broadcastOmniConf_({ t: styles }, port)
}

OnChrome && set_getFindCSS_cr_(((sender: Frames.Sender): FindCSS => {
Expand Down
11 changes: 10 additions & 1 deletion background/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { CurCVer_, CurFFVer_, framesForTab_, OnChrome, OnEdge, OnFirefox } from "./store"
import {
contentConfVer_, CurCVer_, CurFFVer_, framesForTab_, omniConfVer_, OnChrome, OnEdge, OnFirefox, set_contentConfVer_,
set_omniConfVer_
} from "./store"

export const spacesRe_ = <RegExpG & RegExpSearchable<0>> /\s+/g
export const protocolRe_ = <RegExpOne> /^[a-z][\+\-\.\da-z]+:\/\//
Expand Down Expand Up @@ -486,3 +489,9 @@ export const splitWhenKeepExpressions = (src: string, sep: string): string[] =>
results.push(src.slice(lastInd))
return results
}

export const nextConfUpdate = (useOmni: 0 | 1): number => {
let version = useOmni ? omniConfVer_ : contentConfVer_
version = ((version + 1) & 0xfff) || 1
return useOmni ? set_omniConfVer_(version) : set_contentConfVer_(version)
}
5 changes: 3 additions & 2 deletions content/port.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
injector, safer, timeout_, isAlive_, isTop, set_i18n_getMsg, locHref, OnEdge, OnChrome, OnFirefox, isTY, fgCache,
interval_, setupTimerFunc_cr, noRAF_old_cr_, runtime_ff, isIFrameInAbout_, isLocked_, inherited_, chromeVer_
interval_, setupTimerFunc_cr, noRAF_old_cr_, runtime_ff, isIFrameInAbout_, isLocked_, inherited_, chromeVer_, confVersion
} from "../lib/utils"
import { suppressTail_ } from "../lib/keyboard_utils"
import { docHasFocus_, rAF_ } from "../lib/dom_utils"
Expand Down Expand Up @@ -88,7 +88,8 @@ export const runtimeConnect = (function (this: void, extraFlags?: number): void
: PortType.reconnect | extraFlags! | PortType.hasCSS * <number> <boolean | number> !!style_ui,
name = (PortType.isTop === 1 ? <number> <boolean | number> isTop : PortType.isTop * <number> <number | boolean> isTop)
| PortType.aboutIframe * <number> <number | boolean> isIFrameInAbout_ | onceFreezed | status
| PortType.hasFocus * <number> <number | boolean> docHasFocus_(),
| PortType.hasFocus * <number> <number | boolean> docHasFocus_()
| (confVersion << PortType.OFFSET_SETTINGS),
data = { name: injector ? injector.$h(name) : OnEdge ? name + PortNameEnum.Delimiter + locHref() : "" + name },
connect = (OnFirefox ? runtime_ff! : api.runtime).connect
port_ = (injector ? connect(injector.id, data) : connect(data)) as ContentNS.Port
Expand Down
6 changes: 4 additions & 2 deletions content/request_handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
keydownEvents_, set_chromeVer_, set_clickable_, set_fgCache, set_isLocked_, OnChrome, OnFirefox, safeCall, recordLog,
set_isEnabled_, set_onWndFocus, onWndFocus, timeout_, safer, set_os_, safeObj, set_keydownEvents_, setupEventListener,
interval_, getTime, vApi, clearInterval_, locHref, set_firefoxVer_, firefoxVer_, os_, isAsContent, isIFrameInAbout_,
OnEdge, inherited_, clearTimeout_, setupTimerFunc_cr_mv3, set_weakRef_ff, weakRef_ff, deref_
OnEdge, inherited_, clearTimeout_, setupTimerFunc_cr_mv3, set_weakRef_ff, weakRef_ff, deref_, set_confVersion
} from "../lib/utils"
import { set_keyIdCorrectionOffset_old_cr_, handler_stack, suppressTail_ } from "../lib/keyboard_utils"
import {
Expand Down Expand Up @@ -166,7 +166,7 @@ set_requestHandlers([
},
/* kBgReq.msg: */ onPortRes_,
/* kBgReq.eval: */ evalIfOK,
/* kBgReq.settingsUpdate: */ ({ d: delta }: BgReq[kBgReq.settingsUpdate]): void => {
/* kBgReq.settingsUpdate: */ ({ d: delta, v: newConfVersion }: BgReq[kBgReq.settingsUpdate]): void => {
type Keys = keyof typeof delta;
safer(delta);
for (const i in delta) {
Expand All @@ -175,6 +175,7 @@ set_requestHandlers([
(i2 in fgCache) && delete safer(fgCache)[i2]
}
delta.d != null && hud_box && toggleClass_s(hud_box, "D", !!delta.d)
newConfVersion && set_confVersion(newConfVersion)
},
/* kBgReq.focusFrame: */ (req: BgReq[kBgReq.focusFrame]): void => {
// Note: .c, .S are ensured to exist
Expand Down Expand Up @@ -202,6 +203,7 @@ set_requestHandlers([
set_mapKeyTypes(request.t)
set_mappedKeys(request.m)
mappedKeys && safer(mappedKeys)
set_confVersion(request.v)
esc!(HandlerResult.Nothing) // so that passNextKey#normal refreshes nextKeys to the new keyFSM
},
/* kBgReq.execute: */ function<O extends keyof CmdOptions> (request: BaseExecute<CmdOptions[O], O>): void {
Expand Down
Loading

0 comments on commit 35eb596

Please sign in to comment.