Skip to content

Commit 379c87a

Browse files
committed
Guard against updating notifications preferences without permissions
1 parent 93e02f3 commit 379c87a

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"
@@ -275,11 +276,21 @@ export default class PreferenceService extends BaseService<Events> {
275276
return (await this.db.getPreferences()).shouldShowNotifications
276277
}
277278

279+
/**
280+
* This guards against invalid state by checking if permissions have
281+
* been granted by the browser
282+
*/
278283
async setShouldShowNotifications(shouldShowNotifications: boolean) {
279-
await this.db.setShouldShowNotifications(shouldShowNotifications)
280-
this.emitter.emit("setNotificationsPermission", shouldShowNotifications)
284+
const hasBrowserPermissions = await browser.permissions.contains({
285+
permissions: ["notifications"],
286+
})
287+
288+
const updatedValue = hasBrowserPermissions && shouldShowNotifications
289+
290+
await this.db.setShouldShowNotifications(updatedValue)
291+
this.emitter.emit("setNotificationsPermission", updatedValue)
281292

282-
return shouldShowNotifications
293+
return updatedValue
283294
}
284295

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

background/services/redux/index.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -1699,10 +1699,12 @@ export default class ReduxService extends BaseService<never> {
16991699
uiSliceEmitter.on(
17001700
"shouldShowNotifications",
17011701
async (shouldShowNotifications: boolean) => {
1702-
await this.preferenceService.setShouldShowNotifications(
1703-
shouldShowNotifications,
1704-
)
1705-
this.store.dispatch(toggleNotifications(shouldShowNotifications))
1702+
const notificationsEnabled =
1703+
await this.preferenceService.setShouldShowNotifications(
1704+
shouldShowNotifications,
1705+
)
1706+
1707+
this.store.dispatch(toggleNotifications(notificationsEnabled))
17061708
},
17071709
)
17081710

0 commit comments

Comments
 (0)