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 
16 #include "webview_message_ffi.h"
17 
18 #include "webview_controller_impl.h"
19 #include "webview_function.h"
20 #include "webview_utils.h"
21 #include "webview_javascript_execute_callback.h"
22 #include "nweb_helper.h"
23 #include "nweb_init_params.h"
24 #include "web_errors.h"
25 #include "webview_log.h"
26 #include "parameters.h"
27 #include "pixel_map.h"
28 #include "cj_lambda.h"
29 
30 using namespace OHOS::FFI;
31 using namespace OHOS::NWeb;
32 
33 namespace OHOS {
34 namespace Webview {
35 
36 extern "C" {
37     // WebMessagePort
FfiOHOSWebMessagePortPostMessageEvent(int64_t msgPortId, char* stringValue, int32_t *errCode)38     void FfiOHOSWebMessagePortPostMessageEvent(int64_t msgPortId, char* stringValue, int32_t *errCode)
39     {
40         WEBVIEWLOGD("message port post message");
41         auto webMsg = std::make_shared<OHOS::NWeb::NWebMessage>(NWebValue::Type::NONE);
42         std::string message(stringValue);
43         webMsg->SetType(NWebValue::Type::STRING);
44         webMsg->SetString(message);
45         WebMessagePortImpl *msgPort = FFIData::GetData<WebMessagePortImpl>(msgPortId);
46         if (msgPort == nullptr) {
47             WEBVIEWLOGE("post message failed, ffi unwrap msg port failed");
48             *errCode = NWebError::CAN_NOT_POST_MESSAGE;
49             return;
50         }
51         *errCode = msgPort->PostPortMessage(webMsg);
52         return;
53     }
54 
FfiOHOSWebMessagePortPostMessageEventArr(int64_t msgPortId, CArrUI8 arrBuf, int32_t *errCode)55     void FfiOHOSWebMessagePortPostMessageEventArr(int64_t msgPortId, CArrUI8 arrBuf, int32_t *errCode)
56     {
57         WEBVIEWLOGD("message port post message");
58         auto webMsg = std::make_shared<OHOS::NWeb::NWebMessage>(NWebValue::Type::NONE);
59         std::vector<uint8_t> vecData(arrBuf.head, arrBuf.head + arrBuf.size);
60         webMsg->SetType(NWebValue::Type::BINARY);
61         webMsg->SetBinary(vecData);
62         WebMessagePortImpl *msgPort = FFIData::GetData<WebMessagePortImpl>(msgPortId);
63         if (msgPort == nullptr) {
64             WEBVIEWLOGE("post message failed, ffi unwrap msg port failed");
65             *errCode = NWebError::CAN_NOT_POST_MESSAGE;
66             return;
67         }
68         *errCode = msgPort->PostPortMessage(webMsg);
69         return;
70     }
71 
FfiOHOSWebMessagePortPostMessageEventExt(int64_t msgPortId, int64_t msgExtId, int32_t *errCode)72     void FfiOHOSWebMessagePortPostMessageEventExt(int64_t msgPortId, int64_t msgExtId, int32_t *errCode)
73     {
74         WEBVIEWLOGD("message PostMessageEventExt start");
75         WebMessageExtImpl* webMessageExt = FFIData::GetData<WebMessageExtImpl>(msgExtId);
76         if (webMessageExt == nullptr) {
77             WEBVIEWLOGE("FfiOHOSWebMessagePortPostMessageEventExt error");
78             *errCode = NWebError::PARAM_CHECK_ERROR;
79             return;
80         }
81         WebMessagePortImpl *msgPort = FFIData::GetData<WebMessagePortImpl>(msgPortId);
82         if (msgPort == nullptr) {
83             WEBVIEWLOGE("post message failed, ffi unwrap msg port failed");
84             *errCode = NWebError::CAN_NOT_POST_MESSAGE;
85             return;
86         }
87         if (!msgPort->IsExtentionType()) {
88             *errCode = NWebError::PARAM_CHECK_ERROR;
89             return;
90         }
91         *errCode = msgPort->PostPortMessage(webMessageExt->GetData());
92         return;
93     }
94 
FfiOHOSWebMessagePortIsExtentionType(int64_t msgPortId)95     bool FfiOHOSWebMessagePortIsExtentionType(int64_t msgPortId)
96     {
97         WebMessagePortImpl *msgPort = FFIData::GetData<WebMessagePortImpl>(msgPortId);
98         if (msgPort == nullptr) {
99             WEBVIEWLOGE("post message failed, ffi unwrap msg port failed");
100             return false;
101         }
102         return msgPort->IsExtentionType();
103     }
104 
FfiOHOSWebMessagePortOnMessageEvent(int64_t msgPortId, void (*callback)(RetWebMessage), int32_t *errCode)105     void FfiOHOSWebMessagePortOnMessageEvent(int64_t msgPortId, void (*callback)(RetWebMessage), int32_t *errCode)
106     {
107         WEBVIEWLOGD("message port set OnMessageEvent callback");
108         std::function<void(RetWebMessage)> onMsgEventFunc = CJLambda::Create(callback);
109         auto callbackImpl = std::make_shared<NWebMessageCallbackImpl>(onMsgEventFunc);
110         WebMessagePortImpl *msgPort = FFIData::GetData<WebMessagePortImpl>(msgPortId);
111         if (msgPort == nullptr) {
112             WEBVIEWLOGE("post message failed, ffi unwrap msg port failed");
113             *errCode = NWebError::CAN_NOT_REGISTER_MESSAGE_EVENT;
114             return;
115         }
116         *errCode = msgPort->SetPortMessageCallback(callbackImpl);
117         return;
118     }
119 
FfiOHOSWebMessagePortOnMessageEventExt(int64_t msgPortId, void (*callback)(int64_t), int32_t *errCode)120     void FfiOHOSWebMessagePortOnMessageEventExt(int64_t msgPortId, void (*callback)(int64_t), int32_t *errCode)
121     {
122         WEBVIEWLOGD("message port set OnMessageEventExt callback");
123         std::function<void(int64_t)> onMsgEventFunc = CJLambda::Create(callback);
124         auto callbackImpl = std::make_shared<NWebWebMessageExtCallbackImpl>(onMsgEventFunc);
125         WebMessagePortImpl *msgPort = FFIData::GetData<WebMessagePortImpl>(msgPortId);
126         if (msgPort == nullptr) {
127             WEBVIEWLOGE("post message failed, ffi unwrap msg port failed");
128             *errCode = NWebError::CAN_NOT_REGISTER_MESSAGE_EVENT;
129             return;
130         }
131         *errCode = msgPort->SetPortMessageCallback(callbackImpl);
132         return;
133     }
134 
FfiOHOSWebMessagePortClose(int64_t msgPortId, int32_t *errCode)135     void FfiOHOSWebMessagePortClose(int64_t msgPortId, int32_t *errCode)
136     {
137         WebMessagePortImpl *msgPort = FFIData::GetData<WebMessagePortImpl>(msgPortId);
138         if (msgPort == nullptr) {
139             WEBVIEWLOGE("close message failed, ffi unwrap msg port failed");
140             return;
141         }
142         *errCode = msgPort->ClosePort();
143         return;
144     }
145     // WebMessageExt
FfiOHOSWebMessageExtImplConstructor()146     int64_t FfiOHOSWebMessageExtImplConstructor()
147     {
148         auto webMsg = std::make_shared<OHOS::NWeb::NWebMessage>(NWebValue::Type::NONE);
149         WebMessageExtImpl* nativeWebMessageExtImpl = FFIData::Create<WebMessageExtImpl>(webMsg);
150         if (nativeWebMessageExtImpl == nullptr) {
151             WEBVIEWLOGE("new webMessageExt failed");
152             return -1;
153         }
154         return nativeWebMessageExtImpl->GetID();
155     }
156 
FfiOHOSWebMessageExtImplGetType(int64_t msgExtId, int32_t *errCode)157     int32_t FfiOHOSWebMessageExtImplGetType(int64_t msgExtId, int32_t *errCode)
158     {
159         WEBVIEWLOGD("FfiOHOSWebMessageExtImplGetType::GetType start");
160         WebMessageExtImpl* webMessageExt = FFIData::GetData<WebMessageExtImpl>(msgExtId);
161         if (webMessageExt == nullptr) {
162             WEBVIEWLOGE("FfiOHOSWebMessageExtImplGetType::GetType error");
163             *errCode = NWebError::PARAM_CHECK_ERROR;
164             return -1;
165         }
166         int32_t type = webMessageExt->GetType();
167         *errCode = NWebError::NO_ERROR;
168         return type;
169     }
170 
FfiOHOSWebMessageExtImplGetString(int64_t msgExtId, int32_t *errCode)171     char* FfiOHOSWebMessageExtImplGetString(int64_t msgExtId, int32_t *errCode)
172     {
173         WEBVIEWLOGD("FfiOHOSWebMessageExtImplGetString::GetString start");
174         WebMessageExtImpl* webMessageExt = FFIData::GetData<WebMessageExtImpl>(msgExtId);
175         if (webMessageExt == nullptr) {
176             WEBVIEWLOGE("FfiOHOSWebMessageExtImplGetString::GetString error");
177             *errCode = NWebError::INIT_ERROR;
178             return nullptr;
179         }
180 
181         if (webMessageExt->GetType() != static_cast<int32_t>(WebMessageType::STRING)) {
182             *errCode = NWebError::TYPE_NOT_MATCH_WITCH_VALUE;
183             return nullptr;
184         }
185         auto data = webMessageExt->GetData();
186         if (data == nullptr) {
187             WEBVIEWLOGE("FfiOHOSWebMessageExtImplGetString::GetString error");
188             *errCode = NWebError::INIT_ERROR;
189             return nullptr;
190         }
191         std::string msgStr = data->GetString();
192         *errCode = NWebError::NO_ERROR;
193         return MallocCString(msgStr);
194     }
195 
FfiOHOSWebMessageExtImplGetNumber(int64_t msgExtId, int32_t *errCode)196     RetNumber FfiOHOSWebMessageExtImplGetNumber(int64_t msgExtId, int32_t *errCode)
197     {
198         WEBVIEWLOGD("FfiOHOSWebMessageExtImplGetNumber::GetNumber start");
199         WebMessageExtImpl* webMessageExt = FFIData::GetData<WebMessageExtImpl>(msgExtId);
200         RetNumber ret = { .numberInt = 0, .numberDouble = 0.0 };
201         if (webMessageExt == nullptr) {
202             WEBVIEWLOGE("FfiOHOSWebMessageExtImplGetNumber::GetNumber error");
203             *errCode = NWebError::INIT_ERROR;
204             return ret;
205         }
206 
207         if (webMessageExt->GetType() != static_cast<int32_t>(WebMessageType::NUMBER)) {
208             *errCode = NWebError::TYPE_NOT_MATCH_WITCH_VALUE;
209             return ret;
210         }
211         auto data = webMessageExt->GetData();
212         if (data == nullptr) {
213             WEBVIEWLOGE("FfiOHOSWebMessageExtImplGetNumber::GetNumber error");
214             *errCode = NWebError::INIT_ERROR;
215             return ret;
216         }
217         if (data->GetType() == NWebValue::Type::INTEGER) {
218             ret.numberInt = data->GetInt64();
219         } else {
220             ret.numberDouble = data->GetDouble();
221         }
222         return ret;
223     }
224 
FfiOHOSWebMessageExtImplGetBoolean(int64_t msgExtId, int32_t *errCode)225     bool FfiOHOSWebMessageExtImplGetBoolean(int64_t msgExtId, int32_t *errCode)
226     {
227         WEBVIEWLOGD("FfiOHOSWebMessageExtImplGetBoolean::GetBoolean start");
228         WebMessageExtImpl* webMessageExt = FFIData::GetData<WebMessageExtImpl>(msgExtId);
229         if (webMessageExt == nullptr) {
230             WEBVIEWLOGE("FfiOHOSWebMessageExtImplGetBoolean::GetBoolean error");
231             *errCode = NWebError::INIT_ERROR;
232             return false;
233         }
234 
235         if (webMessageExt->GetType() != static_cast<int32_t>(WebMessageType::BOOLEAN)) {
236             *errCode = NWebError::TYPE_NOT_MATCH_WITCH_VALUE;
237             return false;
238         }
239         auto data = webMessageExt->GetData();
240         if (data == nullptr) {
241             WEBVIEWLOGE("FfiOHOSWebMessageExtImplGetBoolean::GetBoolean error");
242             *errCode = NWebError::INIT_ERROR;
243             return false;
244         }
245         double boolean = data->GetBoolean();
246         *errCode = NWebError::NO_ERROR;
247         return boolean;
248     }
249 
FfiOHOSWebMessageExtImplGetArrayBuffer(int64_t msgExtId, int32_t *errCode)250     CArrUI8 FfiOHOSWebMessageExtImplGetArrayBuffer(int64_t msgExtId, int32_t *errCode)
251     {
252         WEBVIEWLOGD("FfiOHOSWebMessageExtImplGetArrayBuffer::GetArrayBuffer start");
253         WebMessageExtImpl* webMessageExt = FFIData::GetData<WebMessageExtImpl>(msgExtId);
254         if (webMessageExt == nullptr) {
255             WEBVIEWLOGE("FfiOHOSWebMessageExtImplGetArrayBuffer::GetArrayBuffer error");
256             *errCode = NWebError::INIT_ERROR;
257             return CArrUI8{nullptr, 0};
258         }
259 
260         if (webMessageExt->GetType() != static_cast<int32_t>(WebMessageType::ARRAYBUFFER)) {
261             *errCode = NWebError::TYPE_NOT_MATCH_WITCH_VALUE;
262             return CArrUI8{nullptr, 0};
263         }
264         auto data = webMessageExt->GetData();
265         if (data == nullptr) {
266             WEBVIEWLOGE("FfiOHOSWebMessageExtImplGetArrayBuffer::GetArrayBuffer error");
267             *errCode = NWebError::INIT_ERROR;
268             return CArrUI8{nullptr, 0};
269         }
270         std::vector<uint8_t> msgArr = data->GetBinary();
271         uint8_t* result = VectorToCArrUI8(msgArr);
272         if (result == nullptr) {
273             WEBVIEWLOGE("FfiOHOSWebMessageExtImplGetArrayBuffer malloc failed");
274             *errCode = NWebError::NEW_OOM;
275             return CArrUI8{nullptr, 0};
276         }
277         *errCode = NWebError::NO_ERROR;
278         return CArrUI8{result, msgArr.size()};
279     }
280 
ConvertToCArr(std::shared_ptr<NWebMessage> data, int32_t *errCode)281     CArrValue ConvertToCArr(std::shared_ptr<NWebMessage> data, int32_t *errCode)
282     {
283         CArrValue ret = { .strHead = nullptr, .intHead = nullptr, .doubleHead = nullptr,
284             .boolHead = nullptr, .size = 0 };
285         if (data->GetType() == NWebValue::Type::STRINGARRAY) {
286             std::vector<std::string> values = data->GetStringArray();
287             ret.size = static_cast<int64_t>(values.size());
288             ret.strHead = OHOS::Webview::VectorToCArrString(values);
289         } else if (data->GetType() == NWebValue::Type::BOOLEANARRAY) {
290             std::vector<bool> values = data->GetBooleanArray();
291             auto arr = static_cast<bool*>(malloc(sizeof(bool) * values.size()));
292             if (!arr) {
293                 *errCode = NWebError::NEW_OOM;
294                 return ret;
295             }
296             for (uint32_t i = 0; i < values.size(); i++) {
297                 arr[i] = values[i];
298             }
299             ret.boolHead = arr;
300             ret.size = static_cast<int64_t>(values.size());
301         } else if (data->GetType() == NWebValue::Type::DOUBLEARRAY) {
302             std::vector<double> values = data->GetDoubleArray();
303             auto arr = static_cast<double*>(malloc(sizeof(double) * values.size()));
304             if (!arr) {
305                 *errCode = NWebError::NEW_OOM;
306                 return ret;
307             }
308             for (uint32_t i = 0; i < values.size(); i++) {
309                 arr[i] = values[i];
310             }
311             ret.doubleHead = arr;
312             ret.size = static_cast<int64_t>(values.size());
313         } else {
314             std::vector<int64_t> values = data->GetInt64Array();
315             auto arr = static_cast<int64_t*>(malloc(sizeof(int64_t) * values.size()));
316             if (!arr) {
317                 *errCode = NWebError::NEW_OOM;
318                 return ret;
319             }
320             for (uint32_t i = 0; i < values.size(); i++) {
321                 arr[i] = values[i];
322             }
323             ret.intHead = arr;
324             ret.size = static_cast<int64_t>(values.size());
325         }
326         return ret;
327     }
328 
FfiOHOSWebMessageExtImplGetArray(int64_t msgExtId, int32_t *errCode)329     CArrValue FfiOHOSWebMessageExtImplGetArray(int64_t msgExtId, int32_t *errCode)
330     {
331         CArrValue ret = { .strHead = nullptr, .intHead = nullptr, .doubleHead = nullptr,
332             .boolHead = nullptr, .size = 0 };
333         WebMessageExtImpl* webMessageExt = FFIData::GetData<WebMessageExtImpl>(msgExtId);
334         if (webMessageExt == nullptr) {
335             *errCode = NWebError::INIT_ERROR;
336             return ret;
337         }
338         if (webMessageExt->GetType() != static_cast<int32_t>(WebMessageType::ARRAY)) {
339             *errCode = NWebError::TYPE_NOT_MATCH_WITCH_VALUE;
340             return ret;
341         }
342         auto data = webMessageExt->GetData();
343         if (data == nullptr) {
344             *errCode = NWebError::INIT_ERROR;
345             return ret;
346         }
347         return ConvertToCArr(data, errCode);
348     }
349 
FfiOHOSWebMessageExtImplGetError(int64_t msgExtId, int32_t *errCode)350     CError FfiOHOSWebMessageExtImplGetError(int64_t msgExtId, int32_t *errCode)
351     {
352         WEBVIEWLOGD("FfiOHOSWebMessageExtImplGetError::GetError start");
353         WebMessageExtImpl* webMessageExt = FFIData::GetData<WebMessageExtImpl>(msgExtId);
354         auto err = CError{.errorName = nullptr, .errorMsg = nullptr};
355         if (webMessageExt == nullptr) {
356             WEBVIEWLOGE("FfiOHOSWebMessageExtImplGetError::GetError error");
357             *errCode = NWebError::INIT_ERROR;
358             return err;
359         }
360         if (webMessageExt->GetType() != static_cast<int32_t>(WebMessageType::ERROR)) {
361             *errCode = NWebError::TYPE_NOT_MATCH_WITCH_VALUE;
362             return err;
363         }
364         auto data = webMessageExt->GetData();
365         if (data == nullptr) {
366             WEBVIEWLOGE("FfiOHOSWebMessageExtImplGetError::GetError error");
367             *errCode = NWebError::INIT_ERROR;
368             return err;
369         }
370         *errCode = NWebError::NO_ERROR;
371         std::string errorName = data->GetErrName();
372         std::string errorMsg = data->GetErrName() + ": " + data->GetErrMsg();
373         err.errorName = MallocCString(errorName);
374         err.errorMsg = MallocCString(errorMsg);
375         return err;
376     }
377 
FfiOHOSWebMessageExtImplSetType(int64_t msgExtId, int32_t type, int32_t *errCode)378     void FfiOHOSWebMessageExtImplSetType(int64_t msgExtId, int32_t type, int32_t *errCode)
379     {
380         WEBVIEWLOGD("FfiOHOSWebMessageExtImplSetType::SetType");
381         if (type <= static_cast<int>(WebMessageType::NOTSUPPORT) || type > static_cast<int>(WebMessageType::ERROR)) {
382             *errCode = NWebError::TYPE_NOT_MATCH_WITCH_VALUE;
383             return;
384         }
385         WebMessageExtImpl* webMessageExt = FFIData::GetData<WebMessageExtImpl>(msgExtId);
386         if (webMessageExt == nullptr) {
387             WEBVIEWLOGE("FfiOHOSWebMessageExtImplGetArrayBuffer::GetArrayBuffer error");
388             *errCode = NWebError::PARAM_CHECK_ERROR;
389             return;
390         }
391         webMessageExt->SetType(type);
392         *errCode = NWebError::NO_ERROR;
393         return;
394     }
395 
FfiOHOSWebMessageExtImplSetString(int64_t msgExtId, char* message, int32_t *errCode)396     void FfiOHOSWebMessageExtImplSetString(int64_t msgExtId, char* message, int32_t *errCode)
397     {
398         WEBVIEWLOGD("FfiOHOSWebMessageExtImplSetString::SetString start");
399         WebMessageExtImpl* webMessageExt = FFIData::GetData<WebMessageExtImpl>(msgExtId);
400         if (webMessageExt == nullptr) {
401             WEBVIEWLOGE("FfiOHOSWebMessageExtImplSetString::SetString error");
402             *errCode = NWebError::PARAM_CHECK_ERROR;
403             return;
404         }
405         if (webMessageExt->GetType() != static_cast<int32_t>(WebMessageType::STRING)) {
406             *errCode = NWebError::TYPE_NOT_MATCH_WITCH_VALUE;
407             return ;
408         }
409         std::string value = std::string(message);
410         webMessageExt->SetString(value);
411         *errCode = NWebError::NO_ERROR;
412         return;
413     }
414 
FfiOHOSWebMessageExtImplSetNumber(int64_t msgExtId, double value, int32_t *errCode)415     void FfiOHOSWebMessageExtImplSetNumber(int64_t msgExtId, double value, int32_t *errCode)
416     {
417         WebMessageExtImpl* webMessageExt = FFIData::GetData<WebMessageExtImpl>(msgExtId);
418         if (webMessageExt == nullptr) {
419             WEBVIEWLOGE("FfiOHOSWebMessageExtImplSetNumber::SetNumber error");
420             *errCode = NWebError::PARAM_CHECK_ERROR;
421             return;
422         }
423         if (webMessageExt->GetType() != static_cast<int32_t>(WebMessageType::NUMBER)) {
424             *errCode = NWebError::TYPE_NOT_MATCH_WITCH_VALUE;
425             return;
426         }
427         webMessageExt->SetNumber(value);
428         *errCode = NWebError::NO_ERROR;
429         return;
430     }
431 
FfiOHOSWebMessageExtImplSetBoolean(int64_t msgExtId, bool value, int32_t *errCode)432     void FfiOHOSWebMessageExtImplSetBoolean(int64_t msgExtId, bool value, int32_t *errCode)
433     {
434         WebMessageExtImpl* webMessageExt = FFIData::GetData<WebMessageExtImpl>(msgExtId);
435         if (webMessageExt == nullptr) {
436             WEBVIEWLOGE("FfiOHOSWebMessageExtImplSetBoolean::SetBoolean error");
437             *errCode = NWebError::PARAM_CHECK_ERROR;
438             return;
439         }
440         if (webMessageExt->GetType() != static_cast<int32_t>(WebMessageType::BOOLEAN)) {
441             *errCode = NWebError::TYPE_NOT_MATCH_WITCH_VALUE;
442             return;
443         }
444         webMessageExt->SetBoolean(value);
445         *errCode = NWebError::NO_ERROR;
446         return;
447     }
448 
FfiOHOSWebMessageExtImplSetArrayBuffer(int64_t msgExtId, CArrUI8 value, int32_t *errCode)449     void FfiOHOSWebMessageExtImplSetArrayBuffer(int64_t msgExtId, CArrUI8 value, int32_t *errCode)
450     {
451         WebMessageExtImpl* webMessageExt = FFIData::GetData<WebMessageExtImpl>(msgExtId);
452         if (webMessageExt == nullptr) {
453             WEBVIEWLOGE("FfiOHOSWebMessageExtImplSetArrayBuffer::SetArrayBuffer error");
454             *errCode = NWebError::PARAM_CHECK_ERROR;
455             return;
456         }
457         if (webMessageExt->GetType() != static_cast<int32_t>(WebMessageType::ARRAYBUFFER)) {
458             *errCode = NWebError::TYPE_NOT_MATCH_WITCH_VALUE;
459             return;
460         }
461         uint8_t *arrBuf = value.head;
462         size_t byteLength = static_cast<size_t>(value.size);
463         std::vector<uint8_t> vecData(arrBuf, arrBuf + byteLength);
464         webMessageExt->SetArrayBuffer(vecData);
465         *errCode = NWebError::NO_ERROR;
466         return;
467     }
468 
FfiOHOSWebMessageExtImplSetArrayString(int64_t msgExtId, CArrString value, int32_t *errCode)469     void FfiOHOSWebMessageExtImplSetArrayString(int64_t msgExtId, CArrString value, int32_t *errCode)
470     {
471         WebMessageExtImpl* webMessageExt = FFIData::GetData<WebMessageExtImpl>(msgExtId);
472         if (webMessageExt == nullptr) {
473             WEBVIEWLOGE("FfiOHOSWebMessageExtImplSetArrayString error");
474             *errCode = NWebError::PARAM_CHECK_ERROR;
475             return;
476         }
477         if (webMessageExt->GetType() != static_cast<int32_t>(WebMessageType::ARRAY)) {
478             *errCode = NWebError::TYPE_NOT_MATCH_WITCH_VALUE;
479             return;
480         }
481         std::vector<std::string> outValue;
482         for (int64_t i = 0; i < value.size; i++) {
483             outValue.push_back(std::string(value.head[i]));
484         }
485         webMessageExt->SetStringArray(outValue);
486         *errCode = NWebError::NO_ERROR;
487         return;
488     }
489 
FfiOHOSWebMessageExtImplSetArrayInt(int64_t msgExtId, CArrI64 value, int32_t *errCode)490     void FfiOHOSWebMessageExtImplSetArrayInt(int64_t msgExtId, CArrI64 value, int32_t *errCode)
491     {
492         WebMessageExtImpl* webMessageExt = FFIData::GetData<WebMessageExtImpl>(msgExtId);
493         if (webMessageExt == nullptr) {
494             WEBVIEWLOGE("FfiOHOSWebMessageExtImplSetArrayInt error");
495             *errCode = NWebError::PARAM_CHECK_ERROR;
496             return;
497         }
498         if (webMessageExt->GetType() != static_cast<int32_t>(WebMessageType::ARRAY)) {
499             *errCode = NWebError::TYPE_NOT_MATCH_WITCH_VALUE;
500             return;
501         }
502         std::vector<int64_t> outValue;
503         for (int64_t i = 0; i < value.size; i++) {
504             outValue.push_back(value.head[i]);
505         }
506         webMessageExt->SetInt64Array(outValue);
507         *errCode = NWebError::NO_ERROR;
508         return;
509     }
510 
FfiOHOSWebMessageExtImplSetArrayDouble(int64_t msgExtId, OHOS::Webview::CArrDouble value, int32_t *errCode)511     void FfiOHOSWebMessageExtImplSetArrayDouble(int64_t msgExtId,
512         OHOS::Webview::CArrDouble value, int32_t *errCode)
513     {
514         WebMessageExtImpl* webMessageExt = FFIData::GetData<WebMessageExtImpl>(msgExtId);
515         if (webMessageExt == nullptr) {
516             WEBVIEWLOGE("FfiOHOSWebMessageExtImplSetArrayDouble error");
517             *errCode = NWebError::PARAM_CHECK_ERROR;
518             return;
519         }
520         if (webMessageExt->GetType() != static_cast<int32_t>(WebMessageType::ARRAY)) {
521             *errCode = NWebError::TYPE_NOT_MATCH_WITCH_VALUE;
522             return;
523         }
524         std::vector<double> outValue;
525         for (int64_t i = 0; i < value.size; i++) {
526             outValue.push_back(value.head[i]);
527         }
528         webMessageExt->SetDoubleArray(outValue);
529         *errCode = NWebError::NO_ERROR;
530         return;
531     }
532 
FfiOHOSWebMessageExtImplSetArrayBoolean(int64_t msgExtId, OHOS::Webview::CArrBool value, int32_t *errCode)533     void FfiOHOSWebMessageExtImplSetArrayBoolean(int64_t msgExtId,
534         OHOS::Webview::CArrBool value, int32_t *errCode)
535     {
536         WebMessageExtImpl* webMessageExt = FFIData::GetData<WebMessageExtImpl>(msgExtId);
537         if (webMessageExt == nullptr) {
538             WEBVIEWLOGE("FfiOHOSWebMessageExtImplSetArrayBoolean error");
539             *errCode = NWebError::PARAM_CHECK_ERROR;
540             return;
541         }
542         if (webMessageExt->GetType() != static_cast<int32_t>(WebMessageType::ARRAY)) {
543             *errCode = NWebError::TYPE_NOT_MATCH_WITCH_VALUE;
544             return;
545         }
546         std::vector<bool> outValue;
547         for (int64_t i = 0; i < value.size; i++) {
548             outValue.push_back(value.head[i]);
549         }
550         webMessageExt->SetBooleanArray(outValue);
551         *errCode = NWebError::NO_ERROR;
552         return;
553     }
554 
FfiOHOSWebMessageExtImplSetError(int64_t msgExtId, OHOS::Webview::CError value, int32_t *errCode)555     void FfiOHOSWebMessageExtImplSetError(int64_t msgExtId, OHOS::Webview::CError value, int32_t *errCode)
556     {
557         WebMessageExtImpl* webMessageExt = FFIData::GetData<WebMessageExtImpl>(msgExtId);
558         if (webMessageExt == nullptr) {
559             WEBVIEWLOGE("FfiOHOSWebMessageExtImplGetError::GetError error");
560             *errCode = NWebError::PARAM_CHECK_ERROR;
561             return;
562         }
563         if (webMessageExt->GetType() != static_cast<int32_t>(WebMessageType::ERROR)) {
564             *errCode = NWebError::TYPE_NOT_MATCH_WITCH_VALUE;
565             return;
566         }
567         std::string nameVal = std::string(value.errorName);
568         std::string msgVal = std::string(value.errorMsg);
569         *errCode = NWebError::NO_ERROR;
570         webMessageExt->SetError(nameVal, msgVal);
571         return;
572     }
573 
574     // WebJsMessageExtImpl
FfiOHOSJsMessageExtImplGetType(int64_t jsExtId, int32_t *errCode)575     int32_t FfiOHOSJsMessageExtImplGetType(int64_t jsExtId, int32_t *errCode)
576     {
577         WEBVIEWLOGD("FfiOHOSJsMessageExtImplGetType::GetType start");
578         WebJsMessageExtImpl* webJsMessageExt = FFIData::GetData<WebJsMessageExtImpl>(jsExtId);
579         if (webJsMessageExt == nullptr) {
580             WEBVIEWLOGE("FfiOHOSJsMessageExtImplGetType::GetType error");
581             *errCode = NWebError::PARAM_CHECK_ERROR;
582             return -1;
583         }
584         int32_t type = webJsMessageExt->GetType();
585         *errCode = NWebError::NO_ERROR;
586         return type;
587     }
588 
FfiOHOSJsMessageExtImplGetString(int64_t jsExtId, int32_t *errCode)589     char* FfiOHOSJsMessageExtImplGetString(int64_t jsExtId, int32_t *errCode)
590     {
591         WEBVIEWLOGD("FfiOHOSJsMessageExtImplGetString::GetString start");
592         WebJsMessageExtImpl* webJsMessageExt = FFIData::GetData<WebJsMessageExtImpl>(jsExtId);
593         if (webJsMessageExt == nullptr) {
594             WEBVIEWLOGE("FfiOHOSJsMessageExtImplGetString::GetString error");
595             *errCode = NWebError::INIT_ERROR;
596             return nullptr;
597         }
598 
599         if (webJsMessageExt->GetType() != static_cast<int32_t>(JsMessageType::STRING)) {
600             *errCode = NWebError::TYPE_NOT_MATCH_WITCH_VALUE;
601             return nullptr;
602         }
603         auto data = webJsMessageExt->GetJsMsgResult();
604         if (data == nullptr) {
605             WEBVIEWLOGE("FfiOHOSJsMessageExtImplGetString::GetString error");
606             *errCode = NWebError::INIT_ERROR;
607             return nullptr;
608         }
609         std::string msgStr = data->GetString();
610         *errCode = NWebError::NO_ERROR;
611         return MallocCString(msgStr);
612     }
613 
FfiOHOSJsMessageExtImplGetNumber(int64_t jsExtId, int32_t *errCode)614     RetNumber FfiOHOSJsMessageExtImplGetNumber(int64_t jsExtId, int32_t *errCode)
615     {
616         WEBVIEWLOGD("FfiOHOSJsMessageExtImplGetNumber::GetNumber start");
617         WebJsMessageExtImpl* webJsMessageExt = FFIData::GetData<WebJsMessageExtImpl>(jsExtId);
618         RetNumber ret = { .numberInt = 0, .numberDouble = 0.0 };
619         if (webJsMessageExt == nullptr) {
620             WEBVIEWLOGE("FfiOHOSJsMessageExtImplGetNumber::GetNumber error");
621             *errCode = NWebError::INIT_ERROR;
622             return ret;
623         }
624 
625         if (webJsMessageExt->GetType() != static_cast<int32_t>(JsMessageType::NUMBER)) {
626             *errCode = NWebError::TYPE_NOT_MATCH_WITCH_VALUE;
627             return ret;
628         }
629         auto data = webJsMessageExt->GetJsMsgResult();
630         if (data == nullptr) {
631             WEBVIEWLOGE("FfiOHOSJsMessageExtImplGetNumber::GetNumber error");
632             *errCode = NWebError::INIT_ERROR;
633             return ret;
634         }
635         if (data->GetType() == NWebValue::Type::INTEGER) {
636             ret.numberInt = data->GetInt64();
637         } else {
638             ret.numberDouble = data->GetDouble();
639         }
640         return ret;
641     }
642 
FfiOHOSJsMessageExtImplGetBoolean(int64_t jsExtId, int32_t *errCode)643     bool FfiOHOSJsMessageExtImplGetBoolean(int64_t jsExtId, int32_t *errCode)
644     {
645         WEBVIEWLOGD("FfiOHOSJsMessageExtImplGetBoolean::GetBoolean start");
646         WebJsMessageExtImpl* webJsMessageExt = FFIData::GetData<WebJsMessageExtImpl>(jsExtId);
647         if (webJsMessageExt == nullptr) {
648             WEBVIEWLOGE("FfiOHOSJsMessageExtImplGetBoolean::GetBoolean error");
649             *errCode = NWebError::INIT_ERROR;
650             return false;
651         }
652 
653         if (webJsMessageExt->GetType() != static_cast<int32_t>(JsMessageType::BOOLEAN)) {
654             *errCode = NWebError::TYPE_NOT_MATCH_WITCH_VALUE;
655             return false;
656         }
657         auto data = webJsMessageExt->GetJsMsgResult();
658         if (data == nullptr) {
659             WEBVIEWLOGE("FfiOHOSJsMessageExtImplGetBoolean::GetBoolean error");
660             *errCode = NWebError::INIT_ERROR;
661             return false;
662         }
663         double boolean = data->GetBoolean();
664         *errCode = NWebError::NO_ERROR;
665         return boolean;
666     }
667 
FfiOHOSJsMessageExtImplGetArrayBuffer(int64_t jsExtId, int32_t *errCode)668     CArrUI8 FfiOHOSJsMessageExtImplGetArrayBuffer(int64_t jsExtId, int32_t *errCode)
669     {
670         WEBVIEWLOGD("FfiOHOSJsMessageExtImplGetArrayBuffer::GetArrayBuffer start");
671         WebJsMessageExtImpl* webJsMessageExt = FFIData::GetData<WebJsMessageExtImpl>(jsExtId);
672         if (webJsMessageExt == nullptr) {
673             WEBVIEWLOGE("FfiOHOSJsMessageExtImplGetArrayBuffer::GetArrayBuffer error");
674             *errCode = NWebError::INIT_ERROR;
675             return CArrUI8{nullptr, 0};
676         }
677 
678         if (webJsMessageExt->GetType() != static_cast<int32_t>(JsMessageType::ARRAYBUFFER)) {
679             *errCode = NWebError::TYPE_NOT_MATCH_WITCH_VALUE;
680             return CArrUI8{nullptr, 0};
681         }
682         auto data = webJsMessageExt->GetJsMsgResult();
683         if (data == nullptr) {
684             WEBVIEWLOGE("FfiOHOSJsMessageExtImplGetArrayBuffer::GetArrayBuffer error");
685             *errCode = NWebError::INIT_ERROR;
686             return CArrUI8{nullptr, 0};
687         }
688         std::vector<uint8_t> msgArr = data->GetBinary();
689         uint8_t* result = VectorToCArrUI8(msgArr);
690         if (result == nullptr) {
691             WEBVIEWLOGE("FfiOHOSJsMessageExtImplGetArrayBuffer malloc failed");
692             *errCode = NWebError::NEW_OOM;
693             return CArrUI8{nullptr, 0};
694         }
695         *errCode = NWebError::NO_ERROR;
696         return CArrUI8{result, msgArr.size()};
697     }
698 
FfiOHOSJsMessageExtImplGetArray(int64_t jsExtId, int32_t *errCode)699     CArrValue FfiOHOSJsMessageExtImplGetArray(int64_t jsExtId, int32_t *errCode)
700     {
701         CArrValue ret = { .strHead = nullptr, .intHead = nullptr, .doubleHead = nullptr,
702             .boolHead = nullptr, .size = 0 };
703         WebJsMessageExtImpl* webJsMessageExt = FFIData::GetData<WebJsMessageExtImpl>(jsExtId);
704         if (webJsMessageExt == nullptr) {
705             *errCode = NWebError::INIT_ERROR;
706             return ret;
707         }
708         if (webJsMessageExt->GetType() != static_cast<int32_t>(JsMessageType::ARRAY)) {
709             *errCode = NWebError::TYPE_NOT_MATCH_WITCH_VALUE;
710             return ret;
711         }
712         auto data = webJsMessageExt->GetJsMsgResult();
713         if (data == nullptr) {
714             *errCode = NWebError::INIT_ERROR;
715             return ret;
716         }
717         return ConvertToCArr(data, errCode);
718     }
719 }
720 }
721 }