Skip to content

Commit 77f650b

Browse files
committed
Guard against updating notifications preferences without permissions
1 parent d519213 commit 77f650b

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

background/services/preferences/index.ts

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import browser from "webextension-polyfill"
12
import { FiatCurrency } from "../../assets"
23
import { AddressOnNetwork, NameOnNetwork } from "../../accounts"
34
import { ServiceLifecycleEvents, ServiceCreatorFunction } from "../types"
@@ -281,11 +282,21 @@ export default class PreferenceService extends BaseService<Events> {
281282
return (await this.db.getPreferences()).shouldShowNotifications
282283
}
283284

285+
/**
286+
* This guards against invalid state by checking if permissions have
287+
* been granted by the browser
288+
*/
284289
async setShouldShowNotifications(shouldShowNotifications: boolean) {
285-
await this.db.setShouldShowNotifications(shouldShowNotifications)
286-
this.emitter.emit("setNotificationsPermission", shouldShowNotifications)
290+
const hasBrowserPermissions = await browser.permissions.contains({
291+
permissions: ["notifications"],
292+
})
293+
294+
const updatedValue = hasBrowserPermissions && shouldShowNotifications
295+
296+
await this.db.setShouldShowNotifications(updatedValue)
297+
this.emitter.emit("setNotificationsPermission", updatedValue)
287298

288-
return shouldShowNotifications
299+
return updatedValue
289300
}
290301

291302
async getAccountSignerSettings(): Promise<AccountSignerSettings[]> {

background/services/redux/index.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -1709,10 +1709,12 @@ export default class ReduxService extends BaseService<never> {
17091709
uiSliceEmitter.on(
17101710
"shouldShowNotifications",
17111711
async (shouldShowNotifications: boolean) => {
1712-
await this.preferenceService.setShouldShowNotifications(
1713-
shouldShowNotifications,
1714-
)
1715-
this.store.dispatch(toggleNotifications(shouldShowNotifications))
1712+
const notificationsEnabled =
1713+
await this.preferenceService.setShouldShowNotifications(
1714+
shouldShowNotifications,
1715+
)
1716+
1717+
this.store.dispatch(toggleNotifications(notificationsEnabled))
17161718
},
17171719
)
17181720

0 commit comments

Comments
 (0)