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 { CMModelErrorCode, CMModelOptType } from '../model/CertMangerModel'; 18import { CertAbstractVo } from '../model/CertManagerVo/CertAbstractVo'; 19import { CertInfoVo } from '../model/CertManagerVo/CertInfoVo'; 20 21const TAG = 'CMFaShowUserCa Presenter: '; 22 23export default class CmShowUserCaPresenter { 24 private static sInstance: CmShowUserCaPresenter; 25 public certList: CertAbstractVo[] = []; 26 public certInfo: CertInfoVo = new CertInfoVo('', '', false, '', '', '', '', '', '', 27 new Uint8Array(), new Map(), new Map(), new Map()); 28 29 public static getInstance(): CmShowUserCaPresenter { 30 if (CmShowUserCaPresenter.sInstance == null) { 31 CmShowUserCaPresenter.sInstance = new CmShowUserCaPresenter(); 32 } 33 return CmShowUserCaPresenter.sInstance; 34 } 35 36 onAboutToAppear(): void { 37 38 } 39 40 aboutToDisappear(): void { 41 this.certList = []; 42 this.certInfo = new CertInfoVo('', '', false, '', '', '', '', '', '', 43 new Uint8Array(), new Map(), new Map(), new Map()); 44 } 45 46 updateUserTrustedCertificateList(): void { 47 certManagerModel.getCertOrCredList(CMModelOptType.CM_MODEL_OPT_USER_CA, 48 (errCode: CMModelErrorCode, certList: Array<CertAbstractVo>) => { 49 if (errCode === CMModelErrorCode.CM_MODEL_ERROR_SUCCESS) { 50 certList.sort((certAbs, certAbsOther): number => { 51 let certAlias = certAbs.certAlias; 52 let certAliasOther = certAbsOther.certAlias; 53 if (certAlias <= certAliasOther) { 54 return -1; 55 } else { 56 return 1; 57 } 58 }); 59 this.certList = certList; 60 } else { 61 console.error(TAG + 'updateUserTrustedCertificateList fail,errCode is ' + errCode); 62 this.certList = []; 63 } 64 }); 65 } 66 67 getUserTrustedCertificate(uri: string, callback: Function): void { 68 certManagerModel.getCertOrCred(CMModelOptType.CM_MODEL_OPT_USER_CA, uri, 69 (errCode: CMModelErrorCode, certInfo: CertInfoVo) => { 70 if (errCode === CMModelErrorCode.CM_MODEL_ERROR_SUCCESS) { 71 console.info(TAG + 'getUserTrustedCertificate success, errCode is ' + errCode); 72 this.certInfo = certInfo; 73 callback(); 74 } else { 75 console.error(TAG + 'getUserTrustedCertificate fail, errCode is ' + errCode); 76 this.certInfo.clearCertInfoVo(); 77 callback(); 78 } 79 }); 80 } 81 82 setUserCertificateStatus(uri: string, status: boolean): Promise<boolean> { 83 return new Promise<boolean>(resolve => { 84 certManagerModel.setCertStatus(CMModelOptType.CM_MODEL_OPT_USER_CA, uri, status, (errCode: CMModelErrorCode) => { 85 if (errCode === CMModelErrorCode.CM_MODEL_ERROR_SUCCESS) { 86 this.getUserTrustedCertificate(uri, () => { 87 console.info(TAG + 'setCerStatus then getUserTrustedCertificate,errCode is' + errCode); 88 }); 89 return resolve(true); 90 } else { 91 console.error(TAG + 'setUserCertificateStatus fail,errCode is ' + errCode); 92 return resolve(false); 93 } 94 }); 95 }) 96 } 97 98 deleteUserCertificate(uri: string, callback: Function): void { 99 certManagerModel.deleteCertOrCred(CMModelOptType.CM_MODEL_OPT_USER_CA, uri, (errCode: CMModelErrorCode) => { 100 if (errCode === CMModelErrorCode.CM_MODEL_ERROR_SUCCESS) { 101 this.updateUserTrustedCertificateList(); 102 } else { 103 console.error(TAG + 'deleteUserCertificate fail,errCode is ' + errCode); 104 } 105 callback(); 106 }); 107 } 108}