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_param.h"
17 
18 #include "hilog_tag_wrapper.h"
19 #include "napi_common_util.h"
20 #include "napi_common_want.h"
21 
22 namespace OHOS {
23 namespace AbilityRuntime {
24 using namespace OHOS::AppExecFwk;
UnwrapExecuteParam(napi_env env,napi_value param,InsightIntentExecuteParam & executeParam)25 bool UnwrapExecuteParam(napi_env env, napi_value param, InsightIntentExecuteParam &executeParam)
26 {
27     if (!IsTypeForNapiValue(env, param, napi_object)) {
28         TAG_LOGE(AAFwkTag::JSNAPI, "invalid params");
29         return false;
30     }
31 
32     std::string bundleName {""};
33     if (!UnwrapStringByPropertyName(env, param, "bundleName", bundleName)) {
34         TAG_LOGE(AAFwkTag::JSNAPI, "Wrong argument type bundleName");
35         return false;
36     }
37     executeParam.bundleName_ = bundleName;
38 
39     std::string moduleName {""};
40     if (!UnwrapStringByPropertyName(env, param, "moduleName", moduleName)) {
41         TAG_LOGE(AAFwkTag::JSNAPI, "Wrong argument type moduleName");
42         return false;
43     }
44     executeParam.moduleName_ = moduleName;
45 
46     std::string abilityName {""};
47     if (!UnwrapStringByPropertyName(env, param, "abilityName", abilityName)) {
48         TAG_LOGE(AAFwkTag::JSNAPI, "Wrong argument type abilityName");
49         return false;
50     }
51     executeParam.abilityName_ = abilityName;
52 
53     std::string insightIntentName {""};
54     if (!UnwrapStringByPropertyName(env, param, "insightIntentName", insightIntentName)) {
55         TAG_LOGE(AAFwkTag::JSNAPI, "Wrong argument type insightIntentName");
56         return false;
57     }
58     executeParam.insightIntentName_ = insightIntentName;
59 
60     napi_value napiIntentParam = nullptr;
61     napi_get_named_property(env, param, "insightIntentParam", &napiIntentParam);
62     if (napiIntentParam == nullptr) {
63         TAG_LOGE(AAFwkTag::JSNAPI, "null napiIntentParam");
64         return false;
65     }
66 
67     napi_valuetype valueType = napi_undefined;
68     napi_typeof(env, napiIntentParam, &valueType);
69     if (valueType != napi_object) {
70         TAG_LOGE(AAFwkTag::JSNAPI, "Wrong argument type intentParam.");
71         return false;
72     }
73     auto wp = std::make_shared<WantParams>();
74     if (!AppExecFwk::UnwrapWantParams(env, napiIntentParam, *wp)) {
75         TAG_LOGE(AAFwkTag::JSNAPI, "unwrap want fail");
76         return false;
77     }
78     executeParam.insightIntentParam_ = wp;
79 
80     int32_t executeMode = 0;
81     if (!UnwrapInt32ByPropertyName(env, param, "executeMode", executeMode)) {
82         TAG_LOGE(AAFwkTag::JSNAPI, "Wrong argument type executeMode");
83         return false;
84     }
85     executeParam.executeMode_ = executeMode;
86 
87     int32_t displayId = INVALID_DISPLAY_ID;
88     if (executeMode == ExecuteMode::UI_ABILITY_FOREGROUND &&
89         UnwrapInt32ByPropertyName(env, param, "displayId", displayId)) {
90         if (displayId < 0) {
91             TAG_LOGE(AAFwkTag::JSNAPI, "invalid displayId");
92             return false;
93         }
94         TAG_LOGI(AAFwkTag::JSNAPI, "displayId %{public}d", displayId);
95         executeParam.displayId_ = displayId;
96     }
97 
98     return true;
99 }
100 }  // namespace AbilityRuntime
101 }  // namespace OHOS
102