1 /*
2 * Copyright (c) 2023-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 "napi_common_execute_result.h"
17
18 #include "hilog_tag_wrapper.h"
19 #include "insight_intent_execute_result.h"
20 #include "napi_common_util.h"
21 #include "napi_common_want.h"
22 #include "want_params.h"
23 #include <memory>
24
25 namespace OHOS {
26 namespace AbilityRuntime {
27 using namespace OHOS::AppExecFwk;
UnwrapResultOfExecuteResult(napi_env env,napi_value param,InsightIntentExecuteResult & executeResult)28 bool UnwrapResultOfExecuteResult(napi_env env, napi_value param, InsightIntentExecuteResult &executeResult)
29 {
30 napi_value result = nullptr;
31 napi_get_named_property(env, param, "result", &result);
32 if (result != nullptr) {
33 napi_valuetype valueType = napi_undefined;
34 napi_typeof(env, result, &valueType);
35 if (valueType != napi_object) {
36 TAG_LOGE(AAFwkTag::JSNAPI, "type not function");
37 return false;
38 }
39 auto wp = std::make_shared<AAFwk::WantParams>();
40 if (!AppExecFwk::UnwrapWantParams(env, result, *wp)) {
41 TAG_LOGE(AAFwkTag::JSNAPI, "unwrap want failed");
42 return false;
43 }
44 if (!executeResult.CheckResult(wp)) {
45 TAG_LOGE(AAFwkTag::JSNAPI, "Check wp fail");
46 return false;
47 }
48 executeResult.result = wp;
49 }
50 return true;
51 }
52
UnwrapResultOfDecoratorExecuteResult(napi_env env,napi_value param,InsightIntentExecuteResult & executeResult)53 bool UnwrapResultOfDecoratorExecuteResult(napi_env env, napi_value param, InsightIntentExecuteResult &executeResult)
54 {
55 if (param == nullptr) {
56 TAG_LOGE(AAFwkTag::JSNAPI, "decorator param null");
57 return false;
58 }
59 napi_valuetype valueType = napi_undefined;
60 napi_typeof(env, param, &valueType);
61 if (valueType != napi_object) {
62 TAG_LOGE(AAFwkTag::JSNAPI, "type not object");
63 return false;
64 }
65 auto wp = std::make_shared<AAFwk::WantParams>();
66 if (!AppExecFwk::UnwrapWantParams(env, param, *wp)) {
67 TAG_LOGE(AAFwkTag::JSNAPI, "unwrap want failed");
68 return false;
69 }
70 if (!executeResult.CheckResult(wp)) {
71 TAG_LOGE(AAFwkTag::JSNAPI, "Check wp fail");
72 return false;
73 }
74 executeResult.result = wp;
75 return true;
76 }
77
UnwrapExecuteResult(napi_env env,napi_value param,InsightIntentExecuteResult & executeResult,bool isDecorator)78 bool UnwrapExecuteResult(
79 napi_env env, napi_value param, InsightIntentExecuteResult &executeResult, bool isDecorator)
80 {
81 TAG_LOGD(AAFwkTag::JSNAPI, "called");
82
83 if (!IsTypeForNapiValue(env, param, napi_valuetype::napi_object)) {
84 TAG_LOGE(AAFwkTag::JSNAPI, "UnwrapExecuteResult not object");
85 return false;
86 }
87 if (isDecorator) {
88 executeResult.isDecorator = true;
89 if (!UnwrapResultOfDecoratorExecuteResult(env, param, executeResult)) {
90 TAG_LOGE(AAFwkTag::JSNAPI, "unwrap decorator fail");
91 return false;
92 }
93 return true;
94 }
95
96 int32_t code = 0;
97 if (!UnwrapInt32ByPropertyName(env, param, "code", code)) {
98 TAG_LOGE(AAFwkTag::JSNAPI, "parse code fail");
99 return false;
100 }
101 executeResult.code = code;
102
103 if (IsExistsByPropertyName(env, param, "result")) {
104 if (!UnwrapResultOfExecuteResult(env, param, executeResult)) {
105 TAG_LOGE(AAFwkTag::JSNAPI, "unwrap result fail");
106 return false;
107 }
108 }
109
110 if (IsExistsByPropertyName(env, param, "uris")) {
111 std::vector<std::string> uris;
112 if (!UnwrapStringArrayByPropertyName(env, param, "uris", uris)) {
113 TAG_LOGE(AAFwkTag::JSNAPI, "unwrap uris is null");
114 return false;
115 }
116 executeResult.uris = uris;
117 }
118
119 if (IsExistsByPropertyName(env, param, "flags")) {
120 int32_t flags = 0;
121 if (!UnwrapInt32ByPropertyName(env, param, "flags", flags)) {
122 TAG_LOGE(AAFwkTag::JSNAPI, "unwrap flags is null");
123 return false;
124 }
125 executeResult.flags = flags;
126 }
127
128 return true;
129 }
130 } // namespace AbilityRuntime
131 } // namespace OHOS
132