• 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_deserialize.h"
17 #include "event_logger.h"
18 
19 namespace OHOS {
20 namespace AppExecFwk {
21 namespace {
22     DEFINE_EH_HILOG_LABEL("EventsEmitter");
23 }
JsonParse(ani_env * env,std::string jsonStr)24 static ani_ref JsonParse(ani_env *env, std::string jsonStr)
25 {
26     ani_status status = ANI_ERROR;
27     ani_class cls = nullptr;
28     if ((status = env->FindClass("L@ohos/events/json/RecordSerializeTool;", &cls)) != ANI_OK) {
29         HILOGI("FindClass RecordSerializeTool failed, status : %{public}d", status);
30         return nullptr;
31     }
32     if (cls == nullptr) {
33         HILOGI("RecordSerializeTool class null");
34         return nullptr;
35     }
36     ani_static_method parseNoThrowMethod = nullptr;
37     status = env->Class_FindStaticMethod(cls, "parseNoThrow", nullptr, &parseNoThrowMethod);
38     if (status != ANI_OK) {
39         HILOGI("failed to get parseNoThrow method, status : %{public}d", status);
40         return nullptr;
41     }
42 
43     ani_string aniString;
44     status = env->String_NewUTF8(jsonStr.c_str(), jsonStr.length(), &aniString);
45     if (status != ANI_OK) {
46         HILOGI("String_NewUTF8 wantParamsString failed, status : %{public}d", status);
47         return nullptr;
48     }
49 
50     ani_ref aniResult = nullptr;
51     status = env->Class_CallStaticMethod_Ref(cls, parseNoThrowMethod, &aniResult, aniString);
52     if (status != ANI_OK) {
53         HILOGI("failed to call parseNoThrow method, status : %{public}d", status);
54         return nullptr;
55     }
56     return aniResult;
57 }
58 
PeerDeserialize(ani_env * env,ani_ref * peerData,std::shared_ptr<SerializeData> serializeData)59 bool AniDeserialize::PeerDeserialize(ani_env* env, ani_ref* peerData, std::shared_ptr<SerializeData> serializeData)
60 {
61     if (std::holds_alternative<ani_ref>(serializeData->peerData)) {
62         *peerData = std::get<ani_ref>(serializeData->peerData);
63     }
64     return true;
65 }
66 
CrossDeserialize(ani_env * env,ani_ref * crossData,std::shared_ptr<SerializeData> serializeData)67 bool AniDeserialize::CrossDeserialize(ani_env* env, ani_ref* crossData, std::shared_ptr<SerializeData> serializeData)
68 {
69     if (serializeData->crossData.empty()) {
70         return true;
71     }
72     *crossData = JsonParse(env, serializeData->crossData);
73     if (*crossData == nullptr) {
74         HILOGI("json parse failed");
75         return false;
76     }
77     return true;
78 }
79 
80 } // namespace AppExecFwk
81 } // namespace OHOS