File tree Expand file tree Collapse file tree 3 files changed +36
-2
lines changed Expand file tree Collapse file tree 3 files changed +36
-2
lines changed Original file line number Diff line number Diff line change @@ -13,8 +13,11 @@ import {
1313} from "./dynamic_content_scripts" ;
1414
1515async 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 }
Original file line number Diff line number Diff line change 22// We must update the key's version to re-request consent whenever the data we collect changes.
33export const allConsentStorageKey = "codecov-consent-0.6.3" ;
44export const onlyEssentialConsentStorageKey = "codecov-essential-consent-0.6.3" ;
5+ export const consentTabLock = "codecov-consent-tab-lock" ;
56
67export const codecovApiTokenStorageKey = "self_hosted_codecov_api_token" ;
78export const selfHostedCodecovURLStorageKey = "self_hosted_codecov_url" ;
Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ import {
1111 cacheTtlMs ,
1212 allConsentStorageKey ,
1313 onlyEssentialConsentStorageKey ,
14+ consentTabLock ,
1415} from "src/constants" ;
1516import { Consent } from "./types" ;
1617
@@ -286,4 +287,33 @@ export class Codecov {
286287
287288 return consent ;
288289 }
290+
291+ async canOpenConsentTab ( ) : Promise < Boolean > {
292+ // Returns whether the consent tab was opened this session. Resolves the
293+ // 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}
You can’t perform that action at this time.
0 commit comments