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 '@kit.IPCKit'; 17import Logger from '../log/HiAdLog'; 18import { AdContentRsp } from './AdContentRsp'; 19import { JSON } from '@kit.ArkTS'; 20import { advertising } from '@kit.AdsKit'; 21 22const TAG = 'AdsApiServiceRpcObj'; 23 24/** 25 * AdsApiService返回给调用方的rpc对象,用于调用方给service发送数据 26 */ 27export default class AdsApiServiceRpcObj extends rpc.RemoteObject { 28 constructor(descriptor: string) { 29 super(descriptor); 30 } 31 32 async onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, 33 options: rpc.MessageOption): Promise<boolean> { 34 Logger.i(TAG, `enter onRemoteMessageRequest`); 35 const interfaceToken: string = data.readInterfaceToken(); 36 Logger.i(TAG, `interfaceToken value is : ${interfaceToken}`); 37 Logger.i(TAG, `get the code from rpc value is : ${code}`); 38 39 const replyRpcObj = data.readRemoteObject(); 40 const rpcCallbackFunc = this.buildRpcCallback(code, replyRpcObj); 41 return await this.doCheckCode(code, rpcCallbackFunc, data); 42 } 43 44 async doCheckCode(code: number, rpcCallbackFunc: IRpcCallback, data: rpc.MessageSequence): Promise<boolean> { 45 const ads: Array<advertising.Advertisement> = []; 46 ads.push({ 47 adType: 7, 48 uniqueId: '122222', 49 rewardVerifyConfig: null, 50 rewarded: false, 51 shown: false, 52 clicked: false 53 }); 54 const slot1: string = 'test1'; 55 const resMap: Map<string, Array<advertising.Advertisement>> = new Map(); 56 resMap.set(slot1, ads); 57 if (code === 1) { 58 // 读取并解析请求参数 59 this.checkCode(data, rpcCallbackFunc, resMap); 60 } 61 if (code === 2) { 62 // 读取响应体 63 const buffer: Array<string> = []; 64 data.readStringArray(buffer); 65 const respStr: string = buffer.join(''); 66 Logger.i(TAG, `get the respStr from rpc value is : ${respStr}`); 67 let adContentRsp: AdContentRsp = JSON.parse(respStr); 68 Logger.i(TAG, `json parse to adContentRsp value is : ${JSON.stringify(adContentRsp)}`); 69 Logger.i(TAG, `json parse to testType value is : ${adContentRsp.testType}`); 70 71 if (adContentRsp.testType === '21800001') { 72 Logger.i(TAG, `testType is : 21800001`); 73 rpcCallbackFunc(21800001, 'System internal error.'); 74 } else if (adContentRsp.testType === '21800005') { 75 Logger.i(TAG, `testType is : 21800005`); 76 rpcCallbackFunc(21800005, 'System internal error.'); 77 } else if (adContentRsp.testType === 'error') { 78 Logger.i(TAG, `testType is : error`); 79 return Promise.reject(false); 80 } else if (adContentRsp.testType === 'ok') { 81 Logger.i(TAG, `testType is : ok`); 82 rpcCallbackFunc(200, JSON.stringify(resMap)); 83 } else { 84 Logger.i(TAG, `no testType`); 85 rpcCallbackFunc(200, JSON.stringify(resMap)); 86 } 87 } 88 return true; 89 } 90 91 private checkCode(data: rpc.MessageSequence, rpcCallbackFunc: IRpcCallback, resMap: Map<string, Array<advertising.Advertisement>>): void { 92 const reqData = { 93 // read reqParams 94 reqParams: data.readString(), 95 // read adOptions 96 adOptions: data.readString(), 97 }; 98 let adRequestParams = this.buildAdRequestParams(reqData.reqParams); 99 const adRequestParam = adRequestParams[0]; 100 if (adRequestParam.adType === 2) { 101 rpcCallbackFunc(21800001, 'System internal error.'); 102 } else if (adRequestParam.adType === 7) { 103 rpcCallbackFunc(200, JSON.stringify(resMap)); 104 } else { 105 rpcCallbackFunc(200, JSON.stringify(resMap)); 106 } 107 } 108 109 private buildAdRequestParams(reqParams: string): advertising.AdRequestParams[] { 110 let adRequestParams: advertising.AdRequestParams[] = JSON.parse(reqParams) as advertising.AdRequestParams[]; 111 adRequestParams.forEach((adRequestParam) => { 112 Logger.i(TAG, `adRequestParam: ${adRequestParam}`); 113 adRequestParam.packageName = 'com.acts.AdsServiceDemo'; 114 adRequestParam.requestStartTime = Date.now().toString(); 115 }); 116 return adRequestParams; 117 } 118 119 protected buildRpcCallback(rpcCode: number, replyRpcObj: rpc.IRemoteObject): IRpcCallback { 120 return (respCode: number, respData: string) => { 121 Logger.i(TAG, `resp code is: ${respCode}`); 122 const data = rpc.MessageSequence.create(); 123 data.writeInt(respCode); 124 data.writeString(respData); 125 const reply = rpc.MessageSequence.create(); 126 replyRpcObj.sendMessageRequest(rpcCode, data, reply, new rpc.MessageOption(1)) 127 .catch(e => { 128 Logger.e(TAG, `send message from kit to caller failed, code: ${e.code}, msg: ${e.message}`); 129 }) 130 .finally(() => { 131 data.reclaim(); 132 reply.reclaim(); 133 }); 134 }; 135 } 136} 137 138export interface IRpcCallback { 139 (respCode: number, respData: string): void; 140} 141