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 common from '@ohos.app.ability.common'; 17import Want from '@ohos.app.ability.Want'; 18import { BusinessError } from '@ohos.base'; 19import rpc from '@ohos.rpc'; 20import DomainAccountRequest from '../bean/request/DomainAccountRequest'; 21import DomainAccountResponse from '../bean/response/DomainAccountResponse'; 22import DomainAccountConvertor from '../convertor/DomainAccountConvertor'; 23import Constants from '../common/constant'; 24import AppStorageConstant from '../common/AppStorageConstant'; 25import { HiLog } from '../common/HiLog'; 26 27const TAG = 'CredConnectServiceDomain'; 28 29export default class CredConnectServiceDomain { 30 private static readonly REQUEST_TIME_OUT: number = 10; 31 private static readonly CONNECT_TIME_OUT: number = 1000; 32 private context: common.UIAbilityContext | common.UIExtensionContext | common.ServiceExtensionContext; 33 private connectionNum = -1; 34 private commonOption: common.ConnectOptions; 35 private remoteProxy: rpc.IRemoteObject | null = null; 36 37 constructor(context: common.UIAbilityContext | common.UIExtensionContext | common.ServiceExtensionContext) { 38 this.context = context; 39 this.commonOption = { 40 onConnect: (elementName, remote) => { 41 this.remoteProxy = remote; 42 HiLog.info(TAG, `onConnect success`); 43 }, 44 onDisconnect: () => { 45 HiLog.info(TAG, `onDisconnect`); 46 }, 47 onFailed: () => { 48 HiLog.info(TAG, `onFailed`); 49 } 50 } 51 } 52 53 public getRemoteProxy(): rpc.IRemoteObject | null { 54 return this.remoteProxy; 55 } 56 57 connectDomainAccountQueryAbility() { 58 this.connectServiceAbility(Constants.COMMAND_GET_DOMAIN_ACCOUNT_INFO); 59 } 60 61 private connectServiceAbility(code: number) { 62 HiLog.info(TAG, `connectServiceAbility start`); 63 let want: Want = { 64 bundleName: Constants.DLP_CREDMGR_BUNDLE_NAME, 65 abilityName: Constants.DLP_CREDMGR_DATA_ABILITY_NAME, 66 }; 67 try { 68 switch (code) { 69 case Constants.COMMAND_GET_DOMAIN_ACCOUNT_INFO: { 70 this.connectionNum = this.context.connectServiceExtensionAbility(want, this.commonOption); 71 break; 72 } 73 default: { 74 HiLog.error(TAG, `code is not exist ${code}`); 75 } 76 } 77 } catch (err) { 78 HiLog.error(TAG, `connectServiceAbility failed: ${JSON.stringify(err)}`); 79 } 80 AppStorage.setOrCreate(AppStorageConstant.CRED_CONNECTION_DOMAIN_NUM, this.connectionNum); 81 HiLog.info(TAG, `connectServiceAbility result: ${this.connectionNum}`); 82 } 83 84 private waitConnect() { 85 if (this.remoteProxy) { 86 return; 87 } 88 let currentTime = new Date().getTime(); 89 while (!this.remoteProxy && new Date().getTime() < currentTime + CredConnectServiceDomain.CONNECT_TIME_OUT) { 90 continue; 91 } 92 } 93 94 async getDomainAccountInfo(req: DomainAccountRequest): Promise<DomainAccountResponse | undefined> { 95 HiLog.info(TAG, `getDomainAccountInfo start`); 96 let result: DomainAccountResponse | undefined; 97 let option = new rpc.MessageOption(Constants.TF_SYNC, CredConnectServiceDomain.REQUEST_TIME_OUT); 98 let data = new rpc.MessageSequence(); 99 let reply = new rpc.MessageSequence(); 100 try { 101 data.writeInterfaceToken(Constants.DLP_CREDMGR_INTERFACE_TOKEN); 102 data.writeRemoteObject(new rpc.RemoteObject('')); 103 data.writeString(JSON.stringify(req)); 104 this.waitConnect(); 105 if (!this.remoteProxy) { 106 HiLog.error(TAG, `onConnect remote is null.`); 107 return result; 108 } 109 let sendResult = await this.remoteProxy.sendMessageRequest( 110 Constants.COMMAND_GET_DOMAIN_ACCOUNT_INFO, data, reply, option); 111 let code = sendResult.reply.readInt(); 112 if (code !== Constants.INTERFACE_SUCCESS) { 113 HiLog.info(TAG, `getDomainAccountInfo sendMessageRequest is error, code: ${code}`); 114 return result; 115 } 116 result = DomainAccountConvertor.convertToDomainAccountResp(sendResult.reply.readString()); 117 } catch (error) { 118 HiLog.error(TAG, `getDomainAccountInfo result: ${error}`); 119 } finally { 120 data.reclaim(); 121 reply.reclaim(); 122 } 123 HiLog.info(TAG, `getDomainAccountInfo end`); 124 return result; 125 } 126 127 128 disconnectServiceAbility() { 129 HiLog.info(TAG, `disconnectServiceAbility: ${AppStorage.get(AppStorageConstant.CRED_CONNECTION_DOMAIN_NUM)}`); 130 let connectionNum: number | undefined = AppStorage.get(AppStorageConstant.CRED_CONNECTION_DOMAIN_NUM); 131 if (!connectionNum || connectionNum < 0) { 132 return; 133 } 134 this.context.disconnectServiceExtensionAbility(connectionNum).then(() => { 135 HiLog.info(TAG, `disconnectServiceAbility success.`); 136 }).catch((error: BusinessError) => { 137 HiLog.error(TAG, `disconnectServiceAbility failed. Error: ${JSON.stringify(error)}`); 138 }) 139 }; 140}