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 rpc from '@ohos.rpc'; 17import { HiLog } from '../common/HiLog'; 18import Constants from '../common/constant'; 19import { common } from '@kit.AbilityKit'; 20 21const TAG = 'CredCallbackStub'; 22const DEFAULT_DES = 'CredCallbackStub'; 23 24export default class CredCallbackStub extends rpc.RemoteObject { 25 26 constructor(des?: string) { 27 super(des ?? DEFAULT_DES); 28 } 29 30 asObject(): rpc.IRemoteObject { 31 return this; 32 } 33 34 async onRemoteMessageRequest(code: number, data: rpc.MessageSequence): Promise<boolean> { 35 HiLog.info(TAG, `onRemoteMessageRequest called, code = ${code}`); 36 try { 37 if (data.readInterfaceToken() !== Constants.DLP_CREDMGR_INTERFACE_TOKEN) { 38 HiLog.error(TAG, `InterfaceToken unmatched.`); 39 return false; 40 } 41 } catch (error) { 42 HiLog.error(TAG, `read data exception, error is ${JSON.stringify(error)}`); 43 return false; 44 } 45 switch (code) { 46 case Constants.COMMAND_SEARCH_USER_INFO: { 47 let storage = LocalStorage.getShared(); 48 HiLog.info(TAG, `onRemoteMessageRequest command search user info`); 49 let resultVar = ''; 50 try { 51 resultVar = data.readString(); 52 } catch (error) { 53 HiLog.error(TAG, `read string exception, error is ${JSON.stringify(error)}`); 54 return false; 55 } 56 storage.setOrCreate('commandSearchUserInfo', resultVar); 57 this.disconnectServiceShareAbility(`connection_${Constants.COMMAND_SEARCH_USER_INFO}`); 58 return true; 59 } 60 case Constants.COMMAND_GET_ACCOUNT_INFO: { 61 let storage = LocalStorage.getShared(); 62 HiLog.info(TAG, `onRemoteMessageRequest command get account info`); 63 let resultVar = ''; 64 try { 65 resultVar = data.readString(); 66 } catch (error) { 67 HiLog.error(TAG, `read string exception, error is ${JSON.stringify(error)}`); 68 return false; 69 } 70 storage.setOrCreate('commandGetAccountInfo', resultVar); 71 this.disconnectServiceShareAbility(`connection_${Constants.COMMAND_GET_ACCOUNT_INFO}`); 72 return true; 73 } 74 default: { 75 HiLog.error(TAG, `invalid request code: ${code}`); 76 break; 77 } 78 } 79 return false; 80 } 81 82 searchUserInfo(remote: rpc.IRemoteObject, cloudPhone: string) { 83 if (remote === null || cloudPhone === '') { 84 HiLog.error(TAG, `onConnect remote or cloudPhone is null.`); 85 return; 86 } 87 HiLog.info(TAG, `searchUserInfo start`); 88 let option = new rpc.MessageOption(Constants.TF_ASYNC); 89 let data = new rpc.MessageSequence(); 90 let reply = new rpc.MessageSequence(); 91 try { 92 data.writeInterfaceToken(Constants.DLP_CREDMGR_INTERFACE_TOKEN); 93 let callback: CredCallbackStub = new CredCallbackStub('CredCallbackStub'); 94 data.writeRemoteObject(callback.asObject()); 95 data.writeString(JSON.stringify({'phone': cloudPhone})); 96 } catch (error) { 97 HiLog.error(TAG, `prepare data exception, error is ${JSON.stringify(error)}`); 98 return; 99 } 100 remote.sendMessageRequest(Constants.COMMAND_SEARCH_USER_INFO, data, reply, option).then((result) => { 101 HiLog.info(TAG, `searchUserInfo success.`); 102 }).catch((e: string) => { 103 HiLog.info(TAG, `searchUserInfo error: ${e}`); 104 }).finally(() => { 105 data.reclaim(); 106 reply.reclaim(); 107 }); 108 } 109 110 getLocalAccountInfo(remote: rpc.IRemoteObject) { 111 HiLog.info(TAG, `getLocalAccountInfo start`); 112 if (remote === null) { 113 HiLog.error(TAG, `onConnect remote is null.`); 114 return; 115 } 116 let option = new rpc.MessageOption(Constants.TF_ASYNC); 117 let data = new rpc.MessageSequence(); 118 let reply = new rpc.MessageSequence(); 119 try { 120 data.writeInterfaceToken(Constants.DLP_CREDMGR_INTERFACE_TOKEN); 121 let callback: CredCallbackStub = new CredCallbackStub('CredCallbackStub'); 122 data.writeRemoteObject(callback.asObject()); 123 } catch (error) { 124 HiLog.error(TAG, `prepare data exception, error is ${JSON.stringify(error)}`); 125 return; 126 } 127 remote.sendMessageRequest(Constants.COMMAND_GET_ACCOUNT_INFO, data, reply, option).then((result) => { 128 HiLog.info(TAG, `getLocalAccountInfo success.`); 129 }).catch((e: string) => { 130 HiLog.error(TAG, `getLocalAccountInfo error: ${e}`); 131 }).finally(() => { 132 data.reclaim(); 133 reply.reclaim(); 134 }); 135 } 136 137 disconnectServiceShareAbility(connectionKey: string) { 138 let connectionId: number | undefined = AppStorage.get(connectionKey); 139 HiLog.info(TAG, `disconnectServiceShareAbility: ${connectionId}`); 140 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; 141 try { 142 context.disconnectServiceExtensionAbility(connectionId); 143 HiLog.info(TAG, `disconnectServiceExtensionAbility success.`); 144 } catch (error) { 145 HiLog.error(TAG, `disconnectServiceExtensionAbility failed. Error: ${JSON.stringify(error)}`); 146 } 147 }; 148}