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 private checkParams(code: number, data: rpc.MessageSequence): boolean { 35 if (!code || !data) { 36 HiLog.error(TAG, 'ViewAbilityStub params is invalid'); 37 return false; 38 } 39 try { 40 if (data.readInterfaceToken() !== Constants.DLP_CREDMGR_INTERFACE_TOKEN) { 41 HiLog.error(TAG, 'InterfaceToken unmatched.'); 42 return false; 43 } 44 } catch (error) { 45 HiLog.error(TAG, `read data exception, error is ${JSON.stringify(error)}`); 46 return false; 47 } 48 return true; 49 } 50 51 async onRemoteMessageRequest(code: number, data: rpc.MessageSequence): Promise<boolean> { 52 if (!this.checkParams(code, data)) { 53 HiLog.error(TAG, 'onRemoteMessageRequest checkParams failed'); 54 return false; 55 } 56 HiLog.info(TAG, `onRemoteMessageRequest called, code = ${code}`); 57 switch (code) { 58 case Constants.COMMAND_SEARCH_USER_INFO: { 59 HiLog.info(TAG, 'onRemoteMessageRequest command search user info'); 60 let resultVar = ''; 61 try { 62 resultVar = data.readString(); 63 } catch (error) { 64 HiLog.error(TAG, `read string exception, error is ${JSON.stringify(error)}`); 65 return false; 66 } 67 AppStorage.setOrCreate('commandSearchUserInfo', resultVar); 68 this.disconnectServiceShareAbility(`connection_${Constants.COMMAND_SEARCH_USER_INFO}`); 69 return true; 70 } 71 case Constants.COMMAND_GET_ACCOUNT_INFO: { 72 HiLog.info(TAG, 'onRemoteMessageRequest command get account info'); 73 let resultVar = ''; 74 try { 75 resultVar = data.readString(); 76 } catch (error) { 77 HiLog.error(TAG, `read string exception, error is ${JSON.stringify(error)}`); 78 return false; 79 } 80 AppStorage.setOrCreate('commandGetAccountInfo', resultVar); 81 this.disconnectServiceShareAbility(`connection_${Constants.COMMAND_GET_ACCOUNT_INFO}`); 82 return true; 83 } 84 case Constants.COMMAND_BATCH_REFRESH: { 85 HiLog.info(TAG, 'onRemoteMessageRequest command batch refresh'); 86 this.disconnectServiceShareAbility(`connection_${Constants.COMMAND_BATCH_REFRESH}`); 87 return true; 88 } 89 default: { 90 HiLog.error(TAG, `invalid request code: ${code}`); 91 break; 92 } 93 } 94 return false; 95 } 96 97 searchUserInfo(remote: rpc.IRemoteObject, cloudPhone: string) { 98 if (remote === null || cloudPhone === '') { 99 HiLog.error(TAG, 'onConnect remote or cloudPhone is null.'); 100 return; 101 } 102 HiLog.info(TAG, 'searchUserInfo start'); 103 let option = new rpc.MessageOption(Constants.TF_ASYNC); 104 let data = new rpc.MessageSequence(); 105 let reply = new rpc.MessageSequence(); 106 try { 107 data.writeInterfaceToken(Constants.DLP_CREDMGR_INTERFACE_TOKEN); 108 let callback: CredCallbackStub = new CredCallbackStub('CredCallbackStub'); 109 data.writeRemoteObject(callback.asObject()); 110 data.writeString(JSON.stringify({'phone': cloudPhone})); 111 } catch (error) { 112 HiLog.error(TAG, `prepare data exception, error is ${JSON.stringify(error)}`); 113 data.reclaim(); 114 reply.reclaim(); 115 return; 116 } 117 remote.sendMessageRequest(Constants.COMMAND_SEARCH_USER_INFO, data, reply, option).then((result) => { 118 HiLog.info(TAG, 'searchUserInfo success.'); 119 }).catch((e: string) => { 120 HiLog.info(TAG, `searchUserInfo error: ${e}`); 121 }).finally(() => { 122 data.reclaim(); 123 reply.reclaim(); 124 }); 125 } 126 127 getLocalAccountInfo(remote: rpc.IRemoteObject) { 128 HiLog.info(TAG, 'getLocalAccountInfo start'); 129 if (remote === null) { 130 HiLog.error(TAG, 'getLocalAccountInfo onConnect remote is null.'); 131 return; 132 } 133 let option = new rpc.MessageOption(Constants.TF_ASYNC); 134 let data = new rpc.MessageSequence(); 135 let reply = new rpc.MessageSequence(); 136 try { 137 data.writeInterfaceToken(Constants.DLP_CREDMGR_INTERFACE_TOKEN); 138 let callback: CredCallbackStub = new CredCallbackStub('CredCallbackStub'); 139 data.writeRemoteObject(callback.asObject()); 140 } catch (error) { 141 HiLog.error(TAG, `prepare data exception, error is ${JSON.stringify(error)}`); 142 data.reclaim(); 143 reply.reclaim(); 144 return; 145 } 146 remote.sendMessageRequest(Constants.COMMAND_GET_ACCOUNT_INFO, data, reply, option).then((result) => { 147 HiLog.info(TAG, 'getLocalAccountInfo success.'); 148 }).catch((e: string) => { 149 HiLog.error(TAG, `getLocalAccountInfo error: ${e}`); 150 }).finally(() => { 151 data.reclaim(); 152 reply.reclaim(); 153 }); 154 } 155 156 batchRefresh(remote: rpc.IRemoteObject) { 157 HiLog.info(TAG, 'batchRefresh start'); 158 if (remote === null) { 159 HiLog.error(TAG, 'batchRefresh onConnect remote is null.'); 160 return; 161 } 162 let option = new rpc.MessageOption(Constants.TF_ASYNC); 163 let data = new rpc.MessageSequence(); 164 let reply = new rpc.MessageSequence(); 165 try { 166 data.writeInterfaceToken(Constants.DLP_CREDMGR_INTERFACE_TOKEN); 167 let callback: CredCallbackStub = new CredCallbackStub('CredCallbackStub'); 168 data.writeRemoteObject(callback.asObject()); 169 } catch (error) { 170 HiLog.wrapError(TAG, error, 'batchRefresh prepare data exception'); 171 data.reclaim(); 172 reply.reclaim(); 173 return; 174 } 175 remote.sendMessageRequest(Constants.COMMAND_BATCH_REFRESH, data, reply, option).then((result) => { 176 HiLog.info(TAG, 'batchRefresh success.'); 177 }).catch((e: string) => { 178 HiLog.error(TAG, `batchRefresh error: ${e}`); 179 }).finally(() => { 180 data.reclaim(); 181 reply.reclaim(); 182 }); 183 } 184 185 disconnectServiceShareAbility(connectionKey: string) { 186 let connectionId: number | undefined = AppStorage.get(connectionKey); 187 HiLog.info(TAG, `disconnectServiceShareAbility: ${connectionId}`); 188 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; 189 try { 190 context.disconnectServiceExtensionAbility(connectionId); 191 HiLog.info(TAG, 'disconnectServiceExtensionAbility success.'); 192 } catch (error) { 193 HiLog.error(TAG, `disconnectServiceExtensionAbility failed. Error: ${JSON.stringify(error)}`); 194 } 195 }; 196}