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 = 'CMShowSysCa Presenter: '; 22 23export default class CmShowSysCaPresenter { 24 private static sInstance: CmShowSysCaPresenter; 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(): CmShowSysCaPresenter { 30 if (CmShowSysCaPresenter.sInstance == null) { 31 CmShowSysCaPresenter.sInstance = new CmShowSysCaPresenter(); 32 } 33 return CmShowSysCaPresenter.sInstance; 34 } 35 36 onAboutToAppear(): void { 37 this.updateSystemTrustedCertificateList(); 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 updateSystemTrustedCertificateList(): void { 47 certManagerModel.getCertOrCredList(CMModelOptType.CM_MODEL_OPT_SYSTEM_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 + 'updateSystemTrustedCertificateList fail,errCode is' + errCode); 62 this.certList = []; 63 } 64 }); 65 } 66 67 getSystemTrustedCertificate(uri: string, callback: Function): void { 68 certManagerModel.getCertOrCred(CMModelOptType.CM_MODEL_OPT_SYSTEM_CA, uri, 69 (errCode: CMModelErrorCode, certInfo: CertInfoVo) => { 70 if (errCode === CMModelErrorCode.CM_MODEL_ERROR_SUCCESS) { 71 console.info(TAG + 'getSystemTrustedCertificate success,errCode is ' + errCode); 72 this.certInfo = certInfo; 73 callback(); 74 } else { 75 console.error(TAG + 'getSystemTrustedCertificate fail,errCode is' + errCode); 76 this.certInfo.clearCertInfoVo(); 77 callback(); 78 } 79 }); 80 } 81 82 setSystemCertificateStatus(uri: string, status: boolean): void { 83 certManagerModel.setCertStatus(CMModelOptType.CM_MODEL_OPT_SYSTEM_CA, uri, status, 84 (errCode: CMModelErrorCode) => { 85 if (errCode === CMModelErrorCode.CM_MODEL_ERROR_SUCCESS) { 86 console.info(TAG + 'setSystemCertificateStatus success,errCode is' + errCode); 87 this.updateSystemTrustedCertificateList(); 88 } else { 89 console.error(TAG + 'setSystemCertificateStatus fail,errCode is' + errCode); 90 } 91 }); 92 } 93}