import { Permission } from "../models/permission" ; const permissions = [ { id : "activeTab" , description : chrome . i18n . getMessage ( "permission_active_tab" ) , revocable : false , } , { id : "storage" , description : chrome . i18n . getMessage ( "permission_storage" ) , revocable : false , } , { id : "identity" , description : chrome . i18n . getMessage ( "permission_identity" ) , revocable : false , } , { id : "clipboardWrite" , description : chrome . i18n . getMessage ( "permission_clipboard_write" ) , revocable : true , } , { id : "contextMenus" , description : chrome . i18n . getMessage ( "permission_context_menus" ) , revocable : true , } , { id : "<all_urls>" , description : chrome . i18n . getMessage ( "permission_all_urls" ) , revocable : true , } , { id : "https://www.google.com/*" , description : chrome . i18n . getMessage ( "permission_sync_clock" ) , revocable : true , } , { id : "https://*.dropboxapi.com/*" , description : chrome . i18n . getMessage ( "permission_dropbox" ) , revocable : true , validation : [ ( ) => { if ( localStorage . dropboxToken !== undefined ) { return { valid : false , message : chrome . i18n . getMessage ( "permission_dropbox_cannot_revoke" ) , } ; } return { valid : true , } ; } , ] , } , { id : "https://www.googleapis.com/*" , description : chrome . i18n . getMessage ( "permission_drive" ) , revocable : true , validation : [ ( ) => { if ( localStorage . driveToken !== undefined ) { return { valid : false , message : chrome . i18n . getMessage ( "permission_drive_cannot_revoke" ) , } ; } return { valid : true , } ; } , ] , } , { id : "https://accounts.google.com/*" , description : chrome . i18n . getMessage ( "permission_drive" ) , revocable : true , validation : [ ( ) => { if ( localStorage . driveToken !== undefined ) { return { valid : false , message : chrome . i18n . getMessage ( "permission_drive_cannot_revoke" ) , } ; } return { valid : true , } ; } , ] , } , { id : "https://graph.microsoft.com/*" , description : chrome . i18n . getMessage ( "permission_onedrive" ) , revocable : true , validation : [ ( ) => { if ( localStorage . oneDriveToken !== undefined ) { return { valid : false , message : chrome . i18n . getMessage ( "permission_onedrive_cannot_revoke" ) , } ; } return { valid : true , } ; } , ] , } , { id : "https://login.microsoftonline.com/*" , description : chrome . i18n . getMessage ( "permission_onedrive" ) , revocable : true , validation : [ ( ) => { if ( localStorage . oneDriveToken !== undefined ) { return { valid : false , message : chrome . i18n . getMessage ( "permission_onedrive_cannot_revoke" ) , } ; } return { valid : true , } ; } , ] , } , ] ; export class Permissions implements Module { async getModule ( ) { return { state : { permissions : await this . getPermissions ( ) , } , mutations : { revokePermission : async ( state , permissionId ) => { const permissionObject = this . getPermissionById ( permissionId ) ; const validators = permissionObject . validation ?? [ ] ; const validationResults = validators . map ( ( validator ) => validator ( ) ) . filter ( ( result ) => ! result . valid ) ; if ( validationResults . length > 0 ) { const messages = validationResults . map ( ( result ) => "• " + result . message ) ; alert ( messages . join ( "\n" ) ) ; return ; } await this . revokePermission ( permissionId ) ; state . permissions = await this . getPermissions ( ) ; } , } , namespaced : true , } ; } private async getPermissions ( ) { return new Promise ( ( resolve ) => { chrome . permissions . getAll ( ( permissions ) => { const permissionList = [ ] ; for ( const permissionId of permissions . permissions ?? [ ] ) { const permissionObject = this . getPermissionById ( permissionId ) ; permissionList . push ( permissionObject ) ; } for ( const permissionId of permissions . origins ?? [ ] ) { const permissionObject = this . getPermissionById ( permissionId ) ; permissionList . push ( permissionObject ) ; } permissionList . sort ( ( a , b ) => { return a . revocable !== b . revocable ? ( a . revocable ? 1 : - 1 ) : 0 ; } ) ; return resolve ( permissionList ) ; } ) ; } ) ; } private getPermissionById ( permissionId ) { const permissionObject = permissions . find ( ( p ) => p . id === permissionId ) ; if ( permissionObject === undefined ) { return new Permission ( { id : permissionId , description : chrome . i18n . getMessage ( "permission_unknown_permission" ) , revocable : true , } ) ; } return permissionObject ; } private async revokePermission ( permissionId ) { return new Promise ( ( resolve ) => { chrome . permissions . getAll ( ( permissions ) => { for ( const _permissionId of permissions . permissions ?? [ ] ) { if ( _permissionId === permissionId ) { return chrome . permissions . remove ( { permissions : [ permissionId ] , } , ( ) => { resolve ( ) ; } ) ; } } for ( const _permissionId of permissions . origins ?? [ ] ) { if ( _permissionId === permissionId ) { return chrome . permissions . remove ( { origins : [ permissionId ] , } , ( ) => { resolve ( ) ; } ) ; } } } ) ; setTimeout ( resolve , 100 ) ; } ) ; } }