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 "js_ability_auto_startup_manager_utils.h"
17 
18 #include "global_constant.h"
19 #include "hilog_tag_wrapper.h"
20 #include "napi_common_util.h"
21 
22 namespace OHOS {
23 namespace AbilityRuntime {
UnwrapAutoStartupInfo(napi_env env,napi_value param,AutoStartupInfo & info)24 bool UnwrapAutoStartupInfo(napi_env env, napi_value param, AutoStartupInfo &info)
25 {
26     if (!IsNormalObject(env, param)) {
27         TAG_LOGE(AAFwkTag::AUTO_STARTUP, "invalid param");
28         return false;
29     }
30 
31     if (!AppExecFwk::UnwrapStringByPropertyName(env, param, "bundleName", info.bundleName)) {
32         TAG_LOGE(AAFwkTag::AUTO_STARTUP, "convert bundleName failed");
33         return false;
34     }
35 
36     if (!AppExecFwk::UnwrapStringByPropertyName(env, param, "abilityName", info.abilityName)) {
37         TAG_LOGE(AAFwkTag::AUTO_STARTUP, "convert abilityName failed");
38         return false;
39     }
40 
41     if (AppExecFwk::IsExistsByPropertyName(env, param, "appCloneIndex")) {
42         if (!AppExecFwk::UnwrapInt32ByPropertyName(env, param, "appCloneIndex", info.appCloneIndex)) {
43             TAG_LOGE(AAFwkTag::AUTO_STARTUP, "convert appCloneIndex failed");
44             return false;
45         }
46     }
47 
48     AppExecFwk::UnwrapStringByPropertyName(env, param, "moduleName", info.moduleName);
49     return true;
50 }
51 
IsNormalObject(napi_env env,napi_value value)52 bool IsNormalObject(napi_env env, napi_value value)
53 {
54     if (value == nullptr) {
55         TAG_LOGE(AAFwkTag::AUTO_STARTUP, "null value");
56         return false;
57     }
58     napi_valuetype type;
59     napi_typeof(env, value, &type);
60     if (type == napi_undefined || type == napi_null) {
61         TAG_LOGE(AAFwkTag::AUTO_STARTUP, "invalid type");
62         return false;
63     }
64     if (type != napi_object) {
65         TAG_LOGE(AAFwkTag::AUTO_STARTUP, "invalid type");
66         return false;
67     }
68     return true;
69 }
70 
CreateJsAutoStartupInfoArray(napi_env env,const std::vector<AutoStartupInfo> & infoList)71 napi_value CreateJsAutoStartupInfoArray(napi_env env, const std::vector<AutoStartupInfo> &infoList)
72 {
73     TAG_LOGD(AAFwkTag::AUTO_STARTUP, "called");
74     napi_value arrayObj = nullptr;
75     napi_create_array(env, &arrayObj);
76     for (size_t i = 0; i < infoList.size(); ++i) {
77         auto object = CreateJsAutoStartupInfo(env, infoList.at(i));
78         if (object == nullptr) {
79             TAG_LOGE(AAFwkTag::AUTO_STARTUP, "null obj");
80             return nullptr;
81         }
82 
83         if (napi_set_element(env, arrayObj, i, object) != napi_ok) {
84             TAG_LOGE(AAFwkTag::AUTO_STARTUP, "insert object failed");
85             return nullptr;
86         }
87     }
88 
89     return arrayObj;
90 }
91 
CreateJsAutoStartupInfo(napi_env env,const AutoStartupInfo & info)92 napi_value CreateJsAutoStartupInfo(napi_env env, const AutoStartupInfo &info)
93 {
94     TAG_LOGD(AAFwkTag::AUTO_STARTUP, "called");
95     napi_value object = AppExecFwk::CreateJSObject(env);
96     if (object == nullptr) {
97         TAG_LOGE(AAFwkTag::AUTO_STARTUP, "null object");
98         return nullptr;
99     }
100 
101     napi_value bundleName = AppExecFwk::WrapStringToJS(env, info.bundleName);
102     if (bundleName == nullptr) {
103         TAG_LOGE(AAFwkTag::AUTO_STARTUP, "null bundleName");
104         return nullptr;
105     }
106 
107     napi_value abilityName = AppExecFwk::WrapStringToJS(env, info.abilityName);
108     if (abilityName == nullptr) {
109         TAG_LOGE(AAFwkTag::AUTO_STARTUP, "null abilityName");
110         return nullptr;
111     }
112 
113     napi_value moduleName = AppExecFwk::WrapStringToJS(env, info.moduleName);
114     if (moduleName == nullptr) {
115         TAG_LOGE(AAFwkTag::AUTO_STARTUP, "null moduleName");
116         return nullptr;
117     }
118 
119     napi_value abilityTypeName = AppExecFwk::WrapStringToJS(env, info.abilityTypeName);
120     if (abilityTypeName == nullptr) {
121         TAG_LOGE(AAFwkTag::AUTO_STARTUP, "null abilityTypeName");
122         return nullptr;
123     }
124 
125     if (!(AppExecFwk::SetPropertyValueByPropertyName(env, object, "bundleName", bundleName) &&
126         AppExecFwk::SetPropertyValueByPropertyName(env, object, "abilityName", abilityName) &&
127         AppExecFwk::SetPropertyValueByPropertyName(env, object, "moduleName", moduleName) &&
128         AppExecFwk::SetPropertyValueByPropertyName(env, object, "abilityTypeName", abilityTypeName))) {
129         TAG_LOGE(AAFwkTag::AUTO_STARTUP, "create js AutoStartupInfo failed");
130         return nullptr;
131     }
132     if (info.appCloneIndex >= 0 && info.appCloneIndex < GlobalConstant::MAX_APP_CLONE_INDEX) {
133         napi_value appCloneIndex = AppExecFwk::WrapInt32ToJS(env, info.appCloneIndex);
134         if (appCloneIndex == nullptr) {
135             TAG_LOGE(AAFwkTag::AUTO_STARTUP, "null appCloneIndex");
136             return nullptr;
137         }
138         if (!AppExecFwk::SetPropertyValueByPropertyName(env, object, "appCloneIndex", appCloneIndex)) {
139             TAG_LOGE(AAFwkTag::AUTO_STARTUP, "create js AutoStartupInfo failed");
140             return nullptr;
141         }
142     }
143     return object;
144 }
145 } // namespace AbilityRuntime
146 } // namespace OHOS