1/* 2 * Copyright (c) 2025 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 Result from '../../common/Result' 17import { ResultMsg } from '../../common/ResultMsg' 18import Constants from '../../common/constant'; 19import { checkDomainAccountInfo, getConnectionStatus, getOsAccountInfo, getUserId } from '../../common/FileUtils/utils'; 20import { HiLog } from '../../common/HiLog'; 21import { dlpPermission } from '@kit.DataProtectionKit'; 22import { common } from '@kit.AbilityKit'; 23import DecryptContent from '../data/DecryptContent'; 24import account_osAccount from '@ohos.account.osAccount'; 25import { hiTraceMeter } from '@kit.PerformanceAnalysisKit'; 26import AccountManager from '../../manager/AccountManager'; 27 28const TAG: string = 'AccountHandler'; 29 30abstract class AccountHandlerBase { 31 protected accountInfo?: account_osAccount.OsAccountInfo; 32 protected userId: number = -1; 33 34 protected async getAccountInfo(): Promise<Result<void>> { 35 try { 36 this.accountInfo = await getOsAccountInfo(); 37 this.userId = await getUserId(); 38 } catch (error) { 39 HiLog.wrapError(TAG, error, 'Failed to get account info'); 40 return ResultMsg.getErrMsg(Constants.ERR_JS_GET_ACCOUNT_ERROR); 41 } 42 return ResultMsg.buildSuccess(); 43 } 44 45 public abstract handle(decryptContent: DecryptContent, 46 context: common.ServiceExtensionContext): Promise<Result<void>>; 47} 48 49class CloudAccountHandle extends AccountHandlerBase { 50 public async handle(decryptContent: DecryptContent, context: common.ServiceExtensionContext): 51 Promise<Result<void>> { 52 hiTraceMeter.startTrace('DlpGetOsAccountJs', decryptContent.openDlpFileData.startId); 53 const getAccountInfoRet = await this.getAccountInfo(); 54 hiTraceMeter.finishTrace('DlpGetOsAccountJs', decryptContent.openDlpFileData.startId); 55 if (getAccountInfoRet.errcode !== Constants.ERR_CODE_SUCCESS || !this.accountInfo) { 56 HiLog.error(TAG, 'CloudAccountHandle getAccountInfo error'); 57 return ResultMsg.buildMsg(getAccountInfoRet.errcode, getAccountInfoRet.errmsg); 58 } 59 if (this.accountInfo.distributedInfo.name === 'ohosAnonymousName' && 60 this.accountInfo.distributedInfo.id === 'ohosAnonymousUid') { 61 HiLog.info(TAG, 'Cloud account not login'); 62 const lunchLoginRet = await this.launchLogin(context); 63 if (lunchLoginRet.errcode !== Constants.ERR_CODE_SUCCESS) { 64 HiLog.error(TAG, 'lunchLogin error'); 65 return ResultMsg.buildMsg(lunchLoginRet.errcode, lunchLoginRet.errmsg); 66 } else { 67 HiLog.info(TAG, 'lunchLogin success'); 68 return ResultMsg.getErrMsg(Constants.ERR_JS_ACCOUNT_NOT_LOGIN); 69 } 70 } 71 decryptContent.distributedInfoId = this.accountInfo.distributedInfo.id; 72 decryptContent.userId = this.userId; 73 return ResultMsg.buildSuccess(); 74 } 75 76 private async launchLogin(context: common.ServiceExtensionContext): Promise<Result<void>> { 77 let accountWant: Want = { 78 bundleName: Constants.DLP_CREDMGR_BUNDLE_NAME, 79 abilityName: Constants.DLP_CREDMGR_LOGIN_ABILITY_NAME, 80 }; 81 if (!(await getConnectionStatus())) { 82 HiLog.error(TAG, 'getConnectionStatus error'); 83 return ResultMsg.getErrMsg(Constants.ERR_JS_NETWORK_INVALID); 84 } 85 try { 86 await context.startAbility(accountWant); 87 } catch (error) { 88 HiLog.wrapError(TAG, error, 'Failed to invoke startAbility'); 89 return ResultMsg.getErrMsg(Constants.ERR_CODE_START_ABILITY_ERROR); 90 } 91 return ResultMsg.buildSuccess(); 92 } 93} 94 95class DomainAccountHandle extends AccountHandlerBase { 96 public async handle(decryptContent: DecryptContent, context: common.ServiceExtensionContext): 97 Promise<Result<void>> { 98 AccountManager.connectAbility(context); 99 hiTraceMeter.startTrace('DlpGetOsAccountJs', decryptContent.openDlpFileData.startId); 100 const getAccountInfoRet = await this.getAccountInfo(); 101 hiTraceMeter.finishTrace('DlpGetOsAccountJs', decryptContent.openDlpFileData.startId); 102 if (getAccountInfoRet.errcode !== Constants.ERR_CODE_SUCCESS || !this.accountInfo) { 103 HiLog.error(TAG, 'DomainAccountHandle getAccountInfo error'); 104 return ResultMsg.buildMsg(getAccountInfoRet.errcode, getAccountInfoRet.errmsg); 105 } 106 let checkAccountRet = checkDomainAccountInfo(this.accountInfo); 107 if (checkAccountRet.errcode !== Constants.ERR_CODE_SUCCESS) { 108 HiLog.error(TAG, 'checkDomainAccountInfo error'); 109 return ResultMsg.buildMsg(checkAccountRet.errcode, checkAccountRet.errmsg); 110 } 111 decryptContent.distributedInfoId = this.accountInfo.distributedInfo.id; 112 decryptContent.accountName = this.accountInfo.domainInfo.accountName; 113 decryptContent.userId = this.userId; 114 return ResultMsg.buildSuccess(); 115 } 116} 117 118class PluginAccountHandle extends AccountHandlerBase { 119 public async handle(decryptContent: DecryptContent, context: common.ServiceExtensionContext): 120 Promise<Result<void>> { 121 hiTraceMeter.startTrace('DlpGetOsAccountJs', decryptContent.openDlpFileData.startId); 122 const getAccountInfoRet = await this.getAccountInfo(); 123 hiTraceMeter.finishTrace('DlpGetOsAccountJs', decryptContent.openDlpFileData.startId); 124 if (getAccountInfoRet.errcode !== Constants.ERR_CODE_SUCCESS || !this.accountInfo) { 125 HiLog.error(TAG, 'getAccountInfo error'); 126 return ResultMsg.buildMsg(getAccountInfoRet.errcode, getAccountInfoRet.errmsg); 127 } 128 decryptContent.distributedInfoId = this.accountInfo.distributedInfo.id; 129 decryptContent.userId = this.userId; 130 return ResultMsg.buildSuccess(); 131 } 132} 133 134export class AccountHandlerFactory { 135 static createAccountHandler(decryptContent: DecryptContent): Result<AccountHandlerBase> { 136 if (decryptContent.fileMetaInfo.accountType === dlpPermission.AccountType.CLOUD_ACCOUNT) { 137 HiLog.info(TAG, 'create CloudAccountHandle'); 138 return ResultMsg.buildSuccess(new CloudAccountHandle()); 139 } 140 if (decryptContent.fileMetaInfo.accountType === dlpPermission.AccountType.DOMAIN_ACCOUNT && 141 decryptContent.openDlpFileData.isFromPlugin) { 142 HiLog.info(TAG, 'create PluginAccountHandle'); 143 return ResultMsg.buildSuccess(new PluginAccountHandle()); 144 } 145 if (decryptContent.fileMetaInfo.accountType === dlpPermission.AccountType.DOMAIN_ACCOUNT) { 146 HiLog.info(TAG, 'create DomainAccountHandle'); 147 return ResultMsg.buildSuccess(new DomainAccountHandle()); 148 } 149 HiLog.error(TAG, 'not found AccountHandler'); 150 return ResultMsg.getErrMsg(Constants.ERR_JS_NOT_DLP_FILE); 151 } 152}