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_common_execute_result.h"
17
18 #include "hilog_tag_wrapper.h"
19 #include "insight_intent_execute_result.h"
20 #include "ani_common_util.h"
21 #include "ani_common_want.h"
22 #include "want_params.h"
23 #include <memory>
24
25 namespace OHOS {
26 namespace AbilityRuntime {
27 using namespace OHOS::AppExecFwk;
UnwrapResultOfExecuteResult(ani_env * env,ani_object & param,InsightIntentExecuteResult & executeResult)28 bool UnwrapResultOfExecuteResult(ani_env *env, ani_object ¶m, InsightIntentExecuteResult &executeResult)
29 {
30 if (env == nullptr) {
31 TAG_LOGE(AAFwkTag::INTENT, "null env");
32 return false;
33 }
34 ani_status status = ANI_OK;
35 ani_ref wantParamRef = nullptr;
36 if (!GetRefProperty(env, param, "result", wantParamRef)) {
37 TAG_LOGE(AAFwkTag::INTENT, "status: %{public}d", status);
38 return false;
39 }
40
41 auto wantParams = std::make_shared<AAFwk::WantParams>();
42 if (!UnwrapWantParams(env, wantParamRef, *wantParams)) {
43 TAG_LOGE(AAFwkTag::INTENT, "failed to unwrap want parameter");
44 return false;
45 }
46
47 if (!executeResult.CheckResult(wantParams)) {
48 TAG_LOGE(AAFwkTag::INTENT, "Check wp fail");
49 return false;
50 }
51 executeResult.result = wantParams;
52
53 return true;
54 }
55
UnwrapExecuteResult(ani_env * env,ani_object & param,InsightIntentExecuteResult & executeResult)56 bool UnwrapExecuteResult(ani_env *env, ani_object ¶m, InsightIntentExecuteResult &executeResult)
57 {
58 if (env == nullptr) {
59 TAG_LOGE(AAFwkTag::INTENT, "null env");
60 return false;
61 }
62
63 if (param == nullptr) {
64 TAG_LOGE(AAFwkTag::INTENT, "param is nullptr");
65 return false;
66 }
67
68 ani_double code = 0;
69 if (!GetDoublePropertyValue(env, param, "code", code)) {
70 TAG_LOGE(AAFwkTag::INTENT, "parse code fail");
71 return false;
72 }
73 executeResult.code = static_cast<int32_t>(code);
74
75 if (IsExistsProperty(env, param, "result")) {
76 if (!UnwrapResultOfExecuteResult(env, param, executeResult)) {
77 TAG_LOGE(AAFwkTag::INTENT, "unwrap execute result fail");
78 return false;
79 }
80 }
81
82 if (IsExistsProperty(env, param, "uris")) {
83 std::vector<std::string> uris;
84 if (!GetStringArrayProperty(env, param, "uris", uris)) {
85 TAG_LOGE(AAFwkTag::INTENT, "unwrap uris is null");
86 return false;
87 }
88 executeResult.uris = uris;
89 }
90
91 if (IsExistsProperty(env, param, "flags")) {
92 double flags = 0.0;
93 if (!GetDoublePropertyObject(env, param, "flags", flags)) {
94 TAG_LOGE(AAFwkTag::INTENT, "unwrap flags is null");
95 return false;
96 }
97 executeResult.flags = static_cast<int32_t>(flags);
98 }
99
100 return true;
101 }
102
WrapExecuteResult(ani_env * env,const AppExecFwk::InsightIntentExecuteResult & executeResult)103 ani_object WrapExecuteResult(ani_env *env, const AppExecFwk::InsightIntentExecuteResult &executeResult)
104 {
105 if (env == nullptr) {
106 TAG_LOGE(AAFwkTag::INTENT, "null env");
107 return nullptr;
108 }
109
110 ani_class cls = nullptr;
111 ani_status status = ANI_ERROR;
112 ani_method ctor = nullptr;
113 ani_object objValue = {};
114
115 if ((status = env->FindClass("L@ohos/app/ability/insightIntent/insightIntent/ExecuteResultInner;",
116 &cls)) != ANI_OK) {
117 TAG_LOGE(AAFwkTag::INTENT, "status: %{public}d", status);
118 return nullptr;
119 }
120
121 if ((status = env->Class_FindMethod(cls, "<ctor>", nullptr, &ctor)) != ANI_OK) {
122 TAG_LOGE(AAFwkTag::INTENT, "status: %{public}d", status);
123 return nullptr;
124 }
125
126 if ((status = env->Object_New(cls, ctor, &objValue)) != ANI_OK) {
127 TAG_LOGE(AAFwkTag::INTENT, "status: %{public}d", status);
128 return nullptr;
129 }
130
131 if (!SetDoublePropertyValue(env, objValue, "code", static_cast<double>(executeResult.code))) {
132 TAG_LOGE(AAFwkTag::INTENT, "SetDoubleProperty failded");
133 return nullptr;
134 }
135 if (executeResult.result != nullptr) {
136 SetRefProperty(env, objValue, "result", WrapWantParams(env, *executeResult.result));
137 }
138 if (executeResult.uris.size() > 0) {
139 SetStringArrayProperty(env, objValue, "uris", executeResult.uris);
140 }
141 SetDoublePropertyObject(env, objValue, "flags", static_cast<double>(executeResult.flags));
142
143 return objValue;
144 }
145
CreateNullExecuteResult(ani_env * env)146 ani_object CreateNullExecuteResult(ani_env *env)
147 {
148 if (env == nullptr) {
149 TAG_LOGE(AAFwkTag::INTENT, "null env");
150 return nullptr;
151 }
152
153 ani_class cls = nullptr;
154 ani_status status = ANI_ERROR;
155 ani_method ctor = nullptr;
156 ani_object objValue = {};
157
158 if ((status = env->FindClass("L@ohos/app/ability/insightIntent/insightIntent/ExecuteResultInner;",
159 &cls))
160 != ANI_OK) {
161 TAG_LOGE(AAFwkTag::INTENT, "status: %{public}d", status);
162 return nullptr;
163 }
164
165 if ((status = env->Class_FindMethod(cls, "<ctor>", nullptr, &ctor)) != ANI_OK) {
166 TAG_LOGE(AAFwkTag::INTENT, "status: %{public}d", status);
167 return nullptr;
168 }
169
170 if ((status = env->Object_New(cls, ctor, &objValue)) != ANI_OK) {
171 TAG_LOGE(AAFwkTag::INTENT, "status: %{public}d", status);
172 return nullptr;
173 }
174
175 return objValue;
176 }
177 } // namespace AbilityRuntime
178 } // namespace OHOS
179