Skip to content

Commit 4386d50

Browse files
authored
fix: Add lock to prevent double consent tab (#148)
The consent tab seems to be opening twice, once for update and once for install. This lock prevents that from happening. Tested to make sure it probably won't have a race condition where both gets happen before the first set. It seems to work fine.
1 parent 9161570 commit 4386d50

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

src/background/main.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ import {
1313
} from "./dynamic_content_scripts";
1414

1515
async function handleConsent(): Promise<void> {
16-
const consent = await new Codecov().getConsent();
17-
if (consent === "none") {
16+
const codecov = new Codecov();
17+
const consent = await codecov.getConsent();
18+
const canOpenConsentTab = await codecov.canOpenConsentTab();
19+
20+
if (consent === "none" && canOpenConsentTab) {
1821
const url = browser.runtime.getURL("consent.html");
1922
await browser.tabs.create({ url, active: true });
2023
}

src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// We must update the key's version to re-request consent whenever the data we collect changes.
33
export const allConsentStorageKey = "codecov-consent-0.6.3";
44
export const onlyEssentialConsentStorageKey = "codecov-essential-consent-0.6.3";
5+
export const consentTabLock = "codecov-consent-tab-lock";
56

67
export const codecovApiTokenStorageKey = "self_hosted_codecov_api_token";
78
export const selfHostedCodecovURLStorageKey = "self_hosted_codecov_url";

src/service.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
cacheTtlMs,
1212
allConsentStorageKey,
1313
onlyEssentialConsentStorageKey,
14+
consentTabLock,
1415
} from "src/constants";
1516
import { Consent } from "./types";
1617

@@ -286,4 +287,33 @@ export class Codecov {
286287

287288
return consent;
288289
}
290+
291+
async canOpenConsentTab(): Promise<Boolean> {
292+
// Returns false if the consent tab has been opened in the last two seconds.
293+
// Resolves the case where two consent tabs are opened simultaneously.
294+
const locked = await browser.storage.local
295+
.get([consentTabLock])
296+
.then((res) => res[consentTabLock]);
297+
298+
if (locked) {
299+
return false;
300+
}
301+
302+
// Acquire the lock and return true
303+
304+
const storageObject: { [id: string]: boolean } = {};
305+
storageObject[consentTabLock] = true;
306+
307+
await browser.storage.local.set(storageObject);
308+
309+
// After 2 seconds, release the lock
310+
setTimeout(() => {
311+
const storageObject: { [id: string]: boolean } = {};
312+
storageObject[consentTabLock] = false;
313+
314+
browser.storage.local.set(storageObject);
315+
}, 2000);
316+
317+
return true;
318+
}
289319
}

0 commit comments

Comments
 (0)