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(callback?: Function): 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 callback?.(errCode); 76 }); 77 } 78 79 sheetRequestAuthorize(uri: string, appUid: string, callback: Function): void { 80 if (appUid == null || appUid.length === 0 || uri == null || uri.length === 0) { 81 callback(CMModelErrorCode.CM_MODEL_ERROR_FAILED); 82 return; 83 } 84 certManagerModel.setAppAuth(CMModelOptType.CM_MODEL_OPT_APP_CRED, uri, appUid, true, callback); 85 } 86 87 requestAuthorize(uri: string, appUid: string): void { 88 let want = GlobalContext.getContext().getAbilityWant(); 89 certManagerModel.setAppAuth(CMModelOptType.CM_MODEL_OPT_APP_CRED, uri, appUid, 90 true, (errCode: CMModelErrorCode, result: string) => { 91 router.clear(); 92 if (errCode === CMModelErrorCode.CM_MODEL_ERROR_SUCCESS) { 93 console.info('requestAuthorize success result: ' + result); 94 if (want.parameters != undefined) { 95 want.parameters.authUri = result; 96 let ret1: Common.AbilityResult = { 97 resultCode: SUCCESS, 98 want: want 99 }; 100 GlobalContext.getContext().getCmContext().terminateSelfWithResult(ret1); 101 } else { 102 let ret2: Common.AbilityResult = { 103 resultCode: FAIL, 104 want: want 105 }; 106 console.error(TAG + 'requestAuthorize failed, undefined'); 107 GlobalContext.getContext().getCmContext().terminateSelfWithResult(ret2); 108 } 109 } else { 110 console.error('requestAuthorize fail'); 111 let ret3: Common.AbilityResult = { 112 resultCode: FAIL, 113 want: want 114 }; 115 GlobalContext.getContext().getCmContext().terminateSelfWithResult(ret3); 116 } 117 }); 118 } 119 120 cancelProcess(): void { 121 console.info('cancelProcess start'); 122 router.clear(); 123 let ret: Common.AbilityResult = { 124 resultCode: FAIL, 125 want: GlobalContext.getContext().getAbilityWant() 126 }; 127 GlobalContext.getContext().getCmContext().terminateSelfWithResult(ret); 128 } 129}