• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/**
2 * Copyright (c) 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 { CredentialAbstractVo } from '../model/CertManagerVo/CredentialAbstractVo';
19import { CredentialVo } from '../model/CertManagerVo/CredentialVo';
20import { AppAuthorVo } from '../model/CertManagerVo/AppAuthorVo';
21@Observed
22export default class CmShowSysCredPresenter {
23  private static sInstance: CmShowSysCredPresenter;
24  public credList: CredentialAbstractVo[] = [];
25  public appInfoList: AppAuthorVo[] = [];
26  public credInfo: CredentialVo = new CredentialVo('', '', '', 0, 0, new Uint8Array());
27
28  public static getInstance(): CmShowSysCredPresenter {
29    if (CmShowSysCredPresenter.sInstance == null) {
30      CmShowSysCredPresenter.sInstance = new CmShowSysCredPresenter();
31    }
32    return CmShowSysCredPresenter.sInstance;
33  }
34
35  aboutToDisappear(): void {
36    this.credList = [];
37    this.credInfo = new CredentialVo('', '', '', 0, 0, new Uint8Array());
38    this.appInfoList = [];
39  }
40
41  updateSystemCredListCallback(callback: Function): void {
42    certManagerModel.getCertOrCredList(CMModelOptType.CM_MODEL_OPT_SYSTEM_CRED,
43      (errCode: CMModelErrorCode, credList: Array<CredentialAbstractVo>) => {
44        if (errCode === CMModelErrorCode.CM_MODEL_ERROR_SUCCESS) {
45          credList.sort((certAbs, certAbsOther): number => {
46            let certAlias = certAbs.alias;
47            let certAliasOther = certAbsOther.alias;
48            if (certAlias <= certAliasOther) {
49              return -1;
50            } else {
51              return 1;
52            }
53          });
54          this.credList = credList;
55          callback();
56        } else {
57          console.error('updateAppCredList error :' + JSON.stringify(errCode));
58          this.credList = [];
59          callback();
60        }
61      });
62  }
63
64  updateSystemCredList(): void {
65    certManagerModel.getCertOrCredList(CMModelOptType.CM_MODEL_OPT_SYSTEM_CRED,
66      (errCode: CMModelErrorCode, credList: Array<CredentialAbstractVo>) => {
67        if (errCode === CMModelErrorCode.CM_MODEL_ERROR_SUCCESS) {
68          credList.sort((certAbs, certAbsOther): number => {
69            let certAlias = certAbs.alias;
70            let certAliasOther = certAbsOther.alias;
71            if (certAlias <= certAliasOther) {
72              return -1;
73            } else {
74              return 1;
75            }
76          });
77          this.credList = credList;
78        } else {
79          console.error('updateSystemCredList error :' + JSON.stringify(errCode));
80          this.credList = [];
81        }
82      });
83  }
84
85  getSystemCred(uri: string, callback: Function): void {
86    certManagerModel.getCertOrCred(CMModelOptType.CM_MODEL_OPT_SYSTEM_CRED, uri,
87      (errCode: CMModelErrorCode, credInfo: CredentialVo) => {
88        if (errCode === CMModelErrorCode.CM_MODEL_ERROR_SUCCESS) {
89          this.credInfo = credInfo;
90        } else {
91          console.error('getAppCred error :' + JSON.stringify(errCode));
92          this.credInfo.clearCredentialVo();
93        }
94        callback();
95      });
96  }
97
98  deleteSystemCred(uri: string): void {
99    certManagerModel.deleteCertOrCred(CMModelOptType.CM_MODEL_OPT_SYSTEM_CRED, uri, (errCode: CMModelErrorCode) => {
100      if (errCode === CMModelErrorCode.CM_MODEL_ERROR_SUCCESS) {
101        this.updateSystemCredList();
102      } else {
103        console.error('deleteAppCred error :' + JSON.stringify(errCode));
104      }
105    });
106  }
107}