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 router from '@ohos.router'; 21import { GlobalContext } from '../common/GlobalContext'; 22import { AppInfoVo } from '../model/CertManagerVo/AppInfoVo'; 23import { BusinessError } from '@ohos.base'; 24import Common from '@ohos.app.ability.common' 25 26const TAG = 'CMAppCredAuthPresenter Presenter: '; 27const SUCCESS = 0; 28const FAIL = -1; 29 30export default class CmAppCredAuthPresenter { 31 private static sInstance: CmAppCredAuthPresenter; 32 public credList: CredentialAbstractVo[] = []; 33 public appName: string = ''; 34 35 public static getInstance(): CmAppCredAuthPresenter { 36 if (CmAppCredAuthPresenter.sInstance == null) { 37 CmAppCredAuthPresenter.sInstance = new CmAppCredAuthPresenter(); 38 } 39 return CmAppCredAuthPresenter.sInstance; 40 } 41 42 onAboutToAppear(): void { 43 44 } 45 46 aboutToDisappear(): void { 47 this.credList = []; 48 } 49 50 updateAppNameFromUid(uid: string): void { 51 try { 52 BundleModel.getAppInfoList(Number(uid), (errCode: CMModelErrorCode, appInfo: AppInfoVo) => { 53 if (errCode === CMModelErrorCode.CM_MODEL_ERROR_SUCCESS) { 54 this.appName = appInfo.appName; 55 console.info('getAppNameFromUid success, appName = ' + this.appName); 56 } else { 57 console.error('getAppNameFromUid fail, uid = ' + uid); 58 } 59 }); 60 } catch (err) { 61 let e: BusinessError = err as BusinessError; 62 console.error('updateAppNameFromUid failed with err, message: ' + e.message + ', code: ' + e.code); 63 } 64 } 65 66 updateAppCredList(): void { 67 certManagerModel.getCertOrCredList(CMModelOptType.CM_MODEL_OPT_APP_CRED, 68 (errCode: CMModelErrorCode, credList: Array<CredentialAbstractVo>) => { 69 if (errCode === CMModelErrorCode.CM_MODEL_ERROR_SUCCESS) { 70 this.credList = credList; 71 console.info('updateAppCredList success.'); 72 } else { 73 console.error('updateAppCredList failed'); 74 } 75 }); 76 } 77 78 requestAuthorize(uri: string, appUid: string): void { 79 let want = GlobalContext.getContext().getAbilityWant(); 80 certManagerModel.setAppAuth(CMModelOptType.CM_MODEL_OPT_APP_CRED, uri, appUid, 81 true, (errCode: CMModelErrorCode, result: string) => { 82 router.clear(); 83 if (errCode === CMModelErrorCode.CM_MODEL_ERROR_SUCCESS) { 84 console.info('requestAuthorize success result: ' + result); 85 if (want.parameters != undefined) { 86 want.parameters.authUri = result; 87 let ret1: Common.AbilityResult = { 88 resultCode: SUCCESS, 89 want: want 90 }; 91 GlobalContext.getContext().getCmContext().terminateSelfWithResult(ret1); 92 } else { 93 let ret2: Common.AbilityResult = { 94 resultCode: FAIL, 95 want: want 96 }; 97 console.error(TAG + 'requestAuthorize failed, undefined'); 98 GlobalContext.getContext().getCmContext().terminateSelfWithResult(ret2); 99 } 100 } else { 101 console.error('requestAuthorize fail'); 102 let ret3: Common.AbilityResult = { 103 resultCode: FAIL, 104 want: want 105 }; 106 GlobalContext.getContext().getCmContext().terminateSelfWithResult(ret3); 107 } 108 }); 109 } 110 111 cancelProcess(): void { 112 console.info('cancelProcess start'); 113 router.clear(); 114 let ret: Common.AbilityResult = { 115 resultCode: FAIL, 116 want: GlobalContext.getContext().getAbilityWant() 117 }; 118 GlobalContext.getContext().getCmContext().terminateSelfWithResult(ret); 119 } 120}