1/** 2 * Copyright (c) 2022-2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import certManagerModel from '../model/CertMangerModel'; 17import bundleModel from '../model/BundleModel'; 18import { CMModelErrorCode, CMModelOptType } from '../model/CertMangerModel'; 19import { CredentialAbstractVo } from '../model/CertManagerVo/CredentialAbstractVo'; 20import { CredentialVo } from '../model/CertManagerVo/CredentialVo'; 21import { AppAuthorVo } from '../model/CertManagerVo/AppAuthorVo'; 22import { AppInfoVo } from '../model/CertManagerVo/AppInfoVo'; 23import { BusinessError } from '@ohos.base'; 24 25export default class CmShowAppCredPresenter { 26 private static sInstance: CmShowAppCredPresenter; 27 public credList: CredentialAbstractVo[] = []; 28 public appInfoList: AppAuthorVo[] = []; 29 public credInfo: CredentialVo = new CredentialVo('', '', '', 0, 0, new Uint8Array()); 30 31 public static getInstance(): CmShowAppCredPresenter { 32 if (CmShowAppCredPresenter.sInstance == null) { 33 CmShowAppCredPresenter.sInstance = new CmShowAppCredPresenter(); 34 } 35 return CmShowAppCredPresenter.sInstance; 36 } 37 38 aboutToDisappear(): void { 39 this.credList = []; 40 this.credInfo = new CredentialVo('', '', '', 0, 0, new Uint8Array()); 41 this.appInfoList = []; 42 } 43 44 updateAppCredListCallback(callback: Function): void { 45 certManagerModel.getCertOrCredList(CMModelOptType.CM_MODEL_OPT_APP_CRED, 46 (errCode: CMModelErrorCode, credList: Array<CredentialAbstractVo>) => { 47 if (errCode === CMModelErrorCode.CM_MODEL_ERROR_SUCCESS) { 48 credList.sort((certAbs, certAbsOther): number => { 49 let certAlias = certAbs.alias; 50 let certAliasOther = certAbsOther.alias; 51 if (certAlias <= certAliasOther) { 52 return -1; 53 } else { 54 return 1; 55 } 56 }); 57 this.credList = credList; 58 callback(); 59 } else { 60 console.error('updateAppCredList error :' + JSON.stringify(errCode)); 61 this.credList = []; 62 callback(); 63 } 64 }); 65 } 66 67 updateAppCredList(): void { 68 certManagerModel.getCertOrCredList(CMModelOptType.CM_MODEL_OPT_APP_CRED, 69 (errCode: CMModelErrorCode, credList: Array<CredentialAbstractVo>) => { 70 if (errCode === CMModelErrorCode.CM_MODEL_ERROR_SUCCESS) { 71 credList.sort((certAbs, certAbsOther): number => { 72 let certAlias = certAbs.alias; 73 let certAliasOther = certAbsOther.alias; 74 if (certAlias <= certAliasOther) { 75 return -1; 76 } else { 77 return 1; 78 } 79 }); 80 this.credList = credList; 81 } else { 82 console.error('updateAppCredList error :' + JSON.stringify(errCode)); 83 this.credList = []; 84 } 85 }); 86 } 87 88 getAppCred(uri: string, callback: Function): void { 89 certManagerModel.getCertOrCred(CMModelOptType.CM_MODEL_OPT_APP_CRED, uri, 90 (errCode: CMModelErrorCode, credInfo: CredentialVo) => { 91 if (errCode === CMModelErrorCode.CM_MODEL_ERROR_SUCCESS) { 92 this.credInfo = credInfo; 93 } else { 94 console.error('getAppCred error :' + JSON.stringify(errCode)); 95 this.credInfo.clearCredentialVo(); 96 } 97 callback(); 98 }); 99 } 100 101 deleteAppCred(uri: string): void { 102 certManagerModel.deleteCertOrCred(CMModelOptType.CM_MODEL_OPT_APP_CRED, uri, (errCode: CMModelErrorCode) => { 103 if (errCode === CMModelErrorCode.CM_MODEL_ERROR_SUCCESS) { 104 this.updateAppCredList(); 105 } else { 106 console.error('deleteAppCred error :' + JSON.stringify(errCode)); 107 } 108 }); 109 } 110 111 getAuthorizedAppList(uri: string): void { 112 this.appInfoList = []; 113 certManagerModel.getAuthAppList(CMModelOptType.CM_MODEL_OPT_APP_CRED, uri, 114 (errCode: CMModelErrorCode, appUidList: Array<string>) => { 115 if (errCode === CMModelErrorCode.CM_MODEL_ERROR_SUCCESS) { 116 for (let i = 0; i < appUidList.length; i++) { 117 bundleModel.getAppInfoList(Number(appUidList[i]), (errCode: CMModelErrorCode, appInfo: AppInfoVo) => { 118 if (errCode === CMModelErrorCode.CM_MODEL_ERROR_SUCCESS) { 119 this.appInfoList.push( 120 new AppAuthorVo(String(appInfo.appImage), String(appUidList[i]), String(appInfo.appName), true)); 121 } 122 }); 123 } 124 } else { 125 console.error('getAuthorizedAppList error :' + JSON.stringify(errCode)); 126 this.appInfoList = []; 127 } 128 }); 129 } 130 131 async removeGrantedAppList(uri: string): Promise<void> { 132 console.info('enter removeGrantedAppList'); 133 for (let i = 0; i < this.appInfoList.length; i++) { 134 if (!this.appInfoList[i].status) { 135 try { 136 let res = await certManagerModel.setAppAuthPromise(CMModelOptType.CM_MODEL_OPT_APP_CRED, uri, 137 this.appInfoList[i].uid, false); 138 console.info('removeGrantedAppList succeed'); 139 } catch (error) { 140 let e: BusinessError = error as BusinessError; 141 console.error('removeGrantedAppList error, message: ' + e.message + ', code: ' + e.code); 142 } 143 } 144 } 145 console.info('leave removeGrantedAppList'); 146 } 147}