• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "ani_serialize.h"
17 #include "sts_events_json_common.h"
18 #include "event_logger.h"
19 
20 namespace OHOS {
21 namespace AppExecFwk {
22 namespace {
23 DEFINE_EH_HILOG_LABEL("EventsEmitter");
24 }
25 
PeerSerialize(ani_env * env,ani_object argv,std::shared_ptr<SerializeData> serializeData)26 bool AniSerialize::PeerSerialize(ani_env* env, ani_object argv, std::shared_ptr<SerializeData> serializeData)
27 {
28     if (argv == nullptr) {
29         // emit with no data
30         return true;
31     }
32     ani_ref record = nullptr;
33     if (GetRefPropertyByName(env, argv, "data", record)) {
34         if (record == nullptr) {
35             return false;
36         }
37         ani_ref peerData = nullptr;
38         if (env->GlobalReference_Create(record, &peerData) != ANI_OK) {
39             HILOGI("Json stringify failed");
40             return false;
41         }
42         serializeData->peerData = peerData;
43         return true;
44     }
45     return false;
46 }
47 
CrossSerialize(ani_env * env,ani_object argv,std::shared_ptr<SerializeData> serializeData)48 bool AniSerialize::CrossSerialize(ani_env* env, ani_object argv, std::shared_ptr<SerializeData> serializeData)
49 {
50     if (argv == nullptr) {
51         // emit with no data
52         return true;
53     }
54     ani_ref record = nullptr;
55     if (GetRefPropertyByName(env, argv, "data", record)) {
56         if (record == nullptr) {
57             return false;
58         }
59         ani_status status = ANI_OK;
60         ani_namespace ns {};
61         status = env->FindNamespace("L@ohos/util/json/json;", &ns);
62         if (status != ANI_OK) {
63             HILOGI("Failed to find namespace");
64             return false;
65         }
66         ani_function fnStringify {};
67         status = env->Namespace_FindFunction(ns, "stringify", nullptr, &fnStringify);
68         if (status != ANI_OK) {
69             HILOGI("Failed to find stringify");
70             return false;
71         }
72         ani_ref ref {};
73         ani_value args[] = {{.r = record}, {.r = nullptr}, {.r = nullptr}};
74         status = env->Function_Call_Ref_A(fnStringify, &ref, args);
75         if (status != ANI_OK) {
76             HILOGI("Failed to call stringify");
77             return false;
78         }
79         ani_size sz {};
80         ani_string str = static_cast<ani_string>(ref);
81         if (env->String_GetUTF8Size(str, &sz) != ANI_OK) {
82             HILOGI("Failed to get string size");
83             return false;
84         }
85         serializeData->crossData.resize(sz + 1);
86         status = env->String_GetUTF8SubString(
87             str, 0, sz, serializeData->crossData.data(), serializeData->crossData.size(), &sz);
88         if (status != ANI_OK) {
89             HILOGI("Failed to convert ani string to c++ string");
90             return false;
91         }
92         serializeData->crossData.resize(sz);
93         return true;
94     }
95     return false;
96 }
97 
98 } // namespace AppExecFwk
99 } // namespace OHOS