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 "ohos_nweb/cpptoc/ark_web_hap_value_vector_cpptoc.h"
17
18 #include "ohos_nweb/bridge/ark_web_hap_value_impl.h"
19 #include "ohos_nweb/cpptoc/ark_web_hap_value_cpptoc.h"
20
21 namespace OHOS::ArkWeb {
22
ArkWebHapValueMapClassToStruct(const std::map<std::string,std::shared_ptr<OHOS::NWeb::NWebHapValue>> & class_value)23 ARK_WEB_NO_SANITIZE ArkWebHapValueMap ArkWebHapValueMapClassToStruct(
24 const std::map<std::string, std::shared_ptr<OHOS::NWeb::NWebHapValue>>& class_value)
25 {
26 ArkWebHapValueMap struct_value = { .size = class_value.size(), .ark_web_mem_free_func = ArkWebMemFree };
27 if (struct_value.size > 0) {
28 struct_value.key = (ArkWebString*)ArkWebMemMalloc(sizeof(ArkWebString) * struct_value.size);
29 struct_value.value = (ark_web_hap_value_t**)ArkWebMemMalloc(sizeof(ark_web_hap_value_t*) * struct_value.size);
30 if ((struct_value.key == nullptr) || (struct_value.value == nullptr)) {
31 struct_value.size = 0;
32 SAFE_FREE(struct_value.key, struct_value.ark_web_mem_free_func);
33 SAFE_FREE(struct_value.value, struct_value.ark_web_mem_free_func);
34 return struct_value;
35 }
36
37 int count = 0;
38 for (auto it = class_value.begin(); it != class_value.end(); it++) {
39 struct_value.key[count] = ArkWebStringClassToStruct(it->first);
40 ArkWebRefPtr<ArkWebHapValue> ark_web_hap_value = new ArkWebHapValueImpl(it->second);
41 struct_value.value[count] = ArkWebHapValueCppToC::Invert(ark_web_hap_value);
42 count++;
43 }
44 }
45
46 return struct_value;
47 }
48
ArkWebHapValueVectorClassToStruct(const std::vector<std::shared_ptr<OHOS::NWeb::NWebHapValue>> & class_value)49 ARK_WEB_NO_SANITIZE ArkWebHapValueVector ArkWebHapValueVectorClassToStruct(
50 const std::vector<std::shared_ptr<OHOS::NWeb::NWebHapValue>>& class_value)
51 {
52 ArkWebHapValueVector struct_value = { .size = class_value.size(), .ark_web_mem_free_func = ArkWebMemFree };
53 if (struct_value.size > 0) {
54 struct_value.value = (ark_web_hap_value_t**)ArkWebMemMalloc(sizeof(ark_web_hap_value_t*) * struct_value.size);
55 if (struct_value.value == nullptr) {
56 struct_value.size = 0;
57 return struct_value;
58 }
59
60 int count = 0;
61 for (auto it = class_value.begin(); it != class_value.end(); it++) {
62 ArkWebRefPtr<ArkWebHapValue> ark_web_hap_value = new ArkWebHapValueImpl(*it);
63 struct_value.value[count] = ArkWebHapValueCppToC::Invert(ark_web_hap_value);
64 count++;
65 }
66 }
67
68 return struct_value;
69 }
70
ArkWebHapValueVectorStructRelease(ArkWebHapValueVector & struct_value)71 ARK_WEB_NO_SANITIZE void ArkWebHapValueVectorStructRelease(ArkWebHapValueVector& struct_value)
72 {
73 struct_value.size = 0;
74 SAFE_FREE(struct_value.value, struct_value.ark_web_mem_free_func);
75 }
76
77 } // namespace OHOS::ArkWeb
78