• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "system_manager_addon.h"
16 
17 #include "securec.h"
18 
19 #include "edm_constants.h"
20 #include "edm_log.h"
21 #include "napi_edm_adapter.h"
22 
23 using namespace OHOS::EDM;
24 
Init(napi_env env,napi_value exports)25 napi_value SystemManagerAddon::Init(napi_env env, napi_value exports)
26 {
27     napi_value nPolicyType = nullptr;
28     NAPI_CALL(env, napi_create_object(env, &nPolicyType));
29     CreatePolicyTypeObject(env, nPolicyType);
30 
31     napi_value nPackageType = nullptr;
32     NAPI_CALL(env, napi_create_object(env, &nPackageType));
33     CreatePackageTypeObject(env, nPackageType);
34 
35     napi_value nUpgradeStatus = nullptr;
36     NAPI_CALL(env, napi_create_object(env, &nUpgradeStatus));
37     CreateUpgradeStatusObject(env, nUpgradeStatus);
38 
39     napi_value nProtocol = nullptr;
40     NAPI_CALL(env, napi_create_object(env, &nProtocol));
41     CreateProtocolObject(env, nProtocol);
42 
43     napi_property_descriptor property[] = {
44         DECLARE_NAPI_FUNCTION("setNTPServer", SetNTPServer),
45         DECLARE_NAPI_FUNCTION("getNTPServer", GetNTPServer),
46         DECLARE_NAPI_FUNCTION("setOtaUpdatePolicy", SetOTAUpdatePolicy),
47         DECLARE_NAPI_FUNCTION("getOtaUpdatePolicy", GetOTAUpdatePolicy),
48         DECLARE_NAPI_FUNCTION("notifyUpdatePackages", NotifyUpdatePackages),
49         DECLARE_NAPI_FUNCTION("getUpdateResult", GetUpgradeResult),
50         DECLARE_NAPI_FUNCTION("getUpdateAuthData", GetUpdateAuthData),
51         DECLARE_NAPI_FUNCTION("setAutoUnlockAfterReboot", SetAutoUnlockAfterReboot),
52         DECLARE_NAPI_FUNCTION("getAutoUnlockAfterReboot", GetAutoUnlockAfterReboot),
53         DECLARE_NAPI_FUNCTION("setInstallLocalEnterpriseAppEnabled", SetInstallLocalEnterpriseAppEnabled),
54         DECLARE_NAPI_FUNCTION("getInstallLocalEnterpriseAppEnabled", GetInstallLocalEnterpriseAppEnabled),
55         DECLARE_NAPI_FUNCTION("addDisallowedNearLinkProtocols", AddDisallowedNearlinkProtocols),
56         DECLARE_NAPI_FUNCTION("getDisallowedNearLinkProtocols", GetDisallowedNearlinkProtocols),
57         DECLARE_NAPI_FUNCTION("removeDisallowedNearLinkProtocols", RemoveDisallowedNearlinkProtocols),
58 
59         DECLARE_NAPI_PROPERTY("PolicyType", nPolicyType),
60         DECLARE_NAPI_PROPERTY("PackageType", nPackageType),
61         DECLARE_NAPI_PROPERTY("UpdateStatus", nUpgradeStatus),
62         DECLARE_NAPI_PROPERTY("NearLinkProtocol", nProtocol),
63     };
64     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(property) / sizeof(property[0]), property));
65     return exports;
66 }
67 
CreatePolicyTypeObject(napi_env env,napi_value value)68 void SystemManagerAddon::CreatePolicyTypeObject(napi_env env, napi_value value)
69 {
70     napi_value nDefault;
71     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, static_cast<int32_t>(UpdatePolicyType::DEFAULT), &nDefault));
72     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "DEFAULT", nDefault));
73     napi_value nProhibit;
74     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, static_cast<int32_t>(UpdatePolicyType::PROHIBIT), &nProhibit));
75     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "PROHIBIT", nProhibit));
76     napi_value nUpdateToSpecificVersion;
77     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env,
78         static_cast<int32_t>(UpdatePolicyType::UPDATE_TO_SPECIFIC_VERSION), &nUpdateToSpecificVersion));
79     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "UPDATE_TO_SPECIFIC_VERSION",
80         nUpdateToSpecificVersion));
81     napi_value nWindows;
82     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, static_cast<int32_t>(UpdatePolicyType::WINDOWS), &nWindows));
83     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "WINDOWS", nWindows));
84     napi_value nPostpone;
85     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, static_cast<int32_t>(UpdatePolicyType::POSTPONE), &nPostpone));
86     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "POSTPONE", nPostpone));
87 }
88 
CreatePackageTypeObject(napi_env env,napi_value value)89 void SystemManagerAddon::CreatePackageTypeObject(napi_env env, napi_value value)
90 {
91     napi_value nFirmware;
92     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, static_cast<int32_t>(PackageType::FIRMWARE), &nFirmware));
93     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "FIRMWARE", nFirmware));
94 }
95 
CreateUpgradeStatusObject(napi_env env,napi_value value)96 void SystemManagerAddon::CreateUpgradeStatusObject(napi_env env, napi_value value)
97 {
98     napi_value nNoUpgradePackage;
99     NAPI_CALL_RETURN_VOID(env,
100         napi_create_int32(env, static_cast<int32_t>(UpgradeStatus::NO_UPGRADE_PACKAGE), &nNoUpgradePackage));
101     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "NO_UPDATE_PACKAGE", nNoUpgradePackage));
102 
103     napi_value nUpgradeWaiting;
104     NAPI_CALL_RETURN_VOID(env,
105         napi_create_int32(env, static_cast<int32_t>(UpgradeStatus::UPGRADE_WAITING), &nUpgradeWaiting));
106     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "UPDATE_WAITING", nUpgradeWaiting));
107 
108     napi_value nUpgrading;
109     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, static_cast<int32_t>(UpgradeStatus::UPGRADING), &nUpgrading));
110     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "UPDATING", nUpgrading));
111 
112     napi_value nUpgradeFailure;
113     NAPI_CALL_RETURN_VOID(env,
114         napi_create_int32(env, static_cast<int32_t>(UpgradeStatus::UPGRADE_FAILURE), &nUpgradeFailure));
115     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "UPDATE_FAILURE", nUpgradeFailure));
116 
117     napi_value nUpgradeSuccess;
118     NAPI_CALL_RETURN_VOID(env,
119         napi_create_int32(env, static_cast<int32_t>(UpgradeStatus::UPGRADE_SUCCESS), &nUpgradeSuccess));
120     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "UPDATE_SUCCESS", nUpgradeSuccess));
121 }
122 
SetNTPServer(napi_env env,napi_callback_info info)123 napi_value SystemManagerAddon::SetNTPServer(napi_env env, napi_callback_info info)
124 {
125     EDMLOGI("SetNTPServer Addon called");
126     AddonMethodSign addonMethodSign;
127     addonMethodSign.name = "SetNTPServer";
128     addonMethodSign.argsType = {EdmAddonCommonType::ELEMENT, EdmAddonCommonType::STRING};
129     addonMethodSign.methodAttribute = MethodAttribute::HANDLE;
130     AdapterAddonData adapterAddonData{};
131     napi_value result = JsObjectToData(env, info, addonMethodSign, &adapterAddonData);
132     if (result == nullptr) {
133         return nullptr;
134     }
135     int32_t ret = SystemManagerProxy::GetSystemManagerProxy()->SetNTPServer(adapterAddonData.data);
136     if (FAILED(ret)) {
137         napi_throw(env, CreateError(env, ret));
138         EDMLOGE("SetNTPServer failed!");
139     }
140     return nullptr;
141 }
142 
GetNTPServer(napi_env env,napi_callback_info info)143 napi_value SystemManagerAddon::GetNTPServer(napi_env env, napi_callback_info info)
144 {
145     EDMLOGI("GetNTPServer Addon called");
146     AddonMethodSign addonMethodSign;
147     addonMethodSign.name = "GetNTPServer";
148     addonMethodSign.argsType = {EdmAddonCommonType::ELEMENT};
149     addonMethodSign.methodAttribute = MethodAttribute::GET;
150     AdapterAddonData adapterAddonData{};
151     napi_value result = JsObjectToData(env, info, addonMethodSign, &adapterAddonData);
152     if (result == nullptr) {
153         return nullptr;
154     }
155     std::string ntpParm;
156     int32_t ret = SystemManagerProxy::GetSystemManagerProxy()->GetNTPServer(adapterAddonData.data, ntpParm);
157     if (FAILED(ret)) {
158         napi_throw(env, CreateError(env, ret));
159         EDMLOGE("GetNTPServer failed!");
160         return nullptr;
161     }
162     napi_value ntpServerString = nullptr;
163     NAPI_CALL(env, napi_create_string_utf8(env, ntpParm.c_str(), ntpParm.size(), &ntpServerString));
164     return ntpServerString;
165 }
166 
SetOTAUpdatePolicy(napi_env env,napi_callback_info info)167 napi_value SystemManagerAddon::SetOTAUpdatePolicy(napi_env env, napi_callback_info info)
168 {
169     auto convertupdatePolicy2Data = [](napi_env env, napi_value argv, MessageParcel &data,
170         const AddonMethodSign &methodSign) {
171             UpdatePolicy updatePolicy;
172             std::string errorMsg;
173             if (!JsObjToUpdatePolicy(env, argv, updatePolicy, errorMsg)) {
174                 EDMLOGE("%{public}s", errorMsg.c_str());
175                 return false;
176             }
177             UpdatePolicyUtils::WriteUpdatePolicy(data, updatePolicy);
178             return true;
179     };
180     AddonMethodSign addonMethodSign;
181     addonMethodSign.name = "SetOTAUpdatePolicy";
182     addonMethodSign.argsType = {EdmAddonCommonType::ELEMENT, EdmAddonCommonType::CUSTOM};
183     addonMethodSign.argsConvert = {nullptr, convertupdatePolicy2Data};
184     addonMethodSign.methodAttribute = MethodAttribute::HANDLE;
185     AdapterAddonData adapterAddonData{};
186     napi_value result = JsObjectToData(env, info, addonMethodSign, &adapterAddonData);
187     if (result == nullptr) {
188         return nullptr;
189     }
190     std::string message;
191     int32_t ret = SystemManagerProxy::GetSystemManagerProxy()->SetOTAUpdatePolicy(adapterAddonData.data, message);
192     if (ret == EdmReturnErrCode::PARAM_ERROR) {
193         napi_throw(env, CreateError(env, ret, message));
194     } else if (FAILED(ret)) {
195         napi_throw(env, CreateError(env, ret));
196     }
197     return nullptr;
198 }
199 
GetOTAUpdatePolicy(napi_env env,napi_callback_info info)200 napi_value SystemManagerAddon::GetOTAUpdatePolicy(napi_env env, napi_callback_info info)
201 {
202     AddonMethodSign addonMethodSign;
203     addonMethodSign.name = "GetOTAUpdatePolicy";
204     addonMethodSign.argsType = {EdmAddonCommonType::ELEMENT};
205     addonMethodSign.methodAttribute = MethodAttribute::GET;
206     AdapterAddonData adapterAddonData{};
207     napi_value result = JsObjectToData(env, info, addonMethodSign, &adapterAddonData);
208     if (result == nullptr) {
209         return nullptr;
210     }
211     UpdatePolicy updatePolicy;
212     int32_t ret = SystemManagerProxy::GetSystemManagerProxy()->GetOTAUpdatePolicy(adapterAddonData.data, updatePolicy);
213     if (FAILED(ret)) {
214         napi_throw(env, CreateError(env, ret));
215         return nullptr;
216     }
217     return ConvertUpdatePolicyToJs(env, updatePolicy);
218 }
219 
NotifyUpdatePackages(napi_env env,napi_callback_info info)220 napi_value SystemManagerAddon::NotifyUpdatePackages(napi_env env, napi_callback_info info)
221 {
222     EDMLOGI("NAPI_NotifyUpdatePackages called");
223     size_t argc = ARGS_SIZE_TWO;
224     napi_value argv[ARGS_SIZE_TWO] = {nullptr};
225     napi_value thisArg = nullptr;
226     void *data = nullptr;
227     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisArg, &data));
228     ASSERT_AND_THROW_PARAM_ERROR(env, argc >= ARGS_SIZE_TWO, "parameter count error");
229     ASSERT_AND_THROW_PARAM_ERROR(env, MatchValueType(env, argv[ARR_INDEX_ZERO], napi_object), "parameter admin error");
230     ASSERT_AND_THROW_PARAM_ERROR(env, MatchValueType(env, argv[ARR_INDEX_ONE], napi_object), "parameter policy error");
231     auto asyncCallbackInfo = new (std::nothrow) AsyncNotifyUpdatePackagesCallbackInfo();
232     if (asyncCallbackInfo == nullptr) {
233         return nullptr;
234     }
235     std::unique_ptr<AsyncNotifyUpdatePackagesCallbackInfo> callbackPtr{asyncCallbackInfo};
236     ASSERT_AND_THROW_PARAM_ERROR(env, ParseElementName(env, asyncCallbackInfo->elementName, argv[ARR_INDEX_ZERO]),
237         "element name param error");
238     EDMLOGD(
239         "IsAdminEnabled::asyncCallbackInfo->elementName.bundlename %{public}s, "
240         "asyncCallbackInfo->abilityname:%{public}s",
241         asyncCallbackInfo->elementName.GetBundleName().c_str(),
242         asyncCallbackInfo->elementName.GetAbilityName().c_str());
243     ASSERT_AND_THROW_PARAM_ERROR(env, JsObjToUpgradePackageInfo(env, argv[ARR_INDEX_ONE],
244         asyncCallbackInfo->packageInfo), "parameter packageInfo parse error");
245 
246     napi_value asyncWorkReturn = HandleAsyncWork(env, asyncCallbackInfo, "NotifyUpdatePackages",
247         NativeNotifyUpdatePackages, NativeVoidCallbackComplete);
248     callbackPtr.release();
249     return asyncWorkReturn;
250 }
251 
NativeNotifyUpdatePackages(napi_env env,void * data)252 void SystemManagerAddon::NativeNotifyUpdatePackages(napi_env env, void *data)
253 {
254     EDMLOGI("NAPI_NativeNotifyUpdatePackages called");
255     if (data == nullptr) {
256         EDMLOGE("data is nullptr");
257         return;
258     }
259     auto *asyncCallbackInfo = static_cast<AsyncNotifyUpdatePackagesCallbackInfo *>(data);
260     auto proxy = SystemManagerProxy::GetSystemManagerProxy();
261     if (proxy == nullptr) {
262         EDMLOGE("can not get EnterpriseDeviceMgrProxy");
263         return;
264     }
265     asyncCallbackInfo->ret = proxy->NotifyUpdatePackages(asyncCallbackInfo->elementName, asyncCallbackInfo->packageInfo,
266         asyncCallbackInfo->innerCodeMsg);
267 }
268 
GetUpgradeResult(napi_env env,napi_callback_info info)269 napi_value SystemManagerAddon::GetUpgradeResult(napi_env env, napi_callback_info info)
270 {
271     EDMLOGI("NAPI_GetOTAUpdatePolicy called");
272     size_t argc = ARGS_SIZE_TWO;
273     napi_value argv[ARGS_SIZE_TWO] = {nullptr};
274     napi_value thisArg = nullptr;
275     void *data = nullptr;
276     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisArg, &data));
277     ASSERT_AND_THROW_PARAM_ERROR(env, argc >= ARGS_SIZE_TWO, "parameter count error");
278     ASSERT_AND_THROW_PARAM_ERROR(env, MatchValueType(env, argv[ARR_INDEX_ZERO], napi_object), "parameter admin error");
279     ASSERT_AND_THROW_PARAM_ERROR(env, MatchValueType(env, argv[ARR_INDEX_ONE], napi_string), "parameter version error");
280 
281     auto asyncCallbackInfo = new (std::nothrow) AsyncGetUpgradeResultCallbackInfo();
282     if (asyncCallbackInfo == nullptr) {
283         return nullptr;
284     }
285     std::unique_ptr<AsyncGetUpgradeResultCallbackInfo> callbackPtr{asyncCallbackInfo};
286     ASSERT_AND_THROW_PARAM_ERROR(env, ParseElementName(env, asyncCallbackInfo->elementName, argv[ARR_INDEX_ZERO]),
287         "element name param error");
288     EDMLOGD(
289         "IsAdminEnabled::asyncCallbackInfo->elementName.bundlename %{public}s, "
290         "asyncCallbackInfo->abilityname:%{public}s",
291         asyncCallbackInfo->elementName.GetBundleName().c_str(),
292         asyncCallbackInfo->elementName.GetAbilityName().c_str());
293     ASSERT_AND_THROW_PARAM_ERROR(env, ParseString(env, asyncCallbackInfo->version, argv[ARR_INDEX_ONE]),
294         "version param error");
295 
296     napi_value asyncWorkReturn = HandleAsyncWork(env, asyncCallbackInfo, "GetUpgradeResult", NativeGetUpgradeResult,
297         NativeUpgradeResultComplete);
298     callbackPtr.release();
299     return asyncWorkReturn;
300 }
301 
GetUpdateAuthData(napi_env env,napi_callback_info info)302 napi_value SystemManagerAddon::GetUpdateAuthData(napi_env env, napi_callback_info info)
303 {
304     EDMLOGI("NAPI_GetUpdateAuthData called");
305     AddonMethodSign addonMethodSign;
306     addonMethodSign.name = "GetUpdateAuthData";
307     addonMethodSign.argsType = {EdmAddonCommonType::ELEMENT};
308     addonMethodSign.methodAttribute = MethodAttribute::GET;
309     return AddonMethodAdapter(env, info, addonMethodSign, NativeGetUpdateAuthData, NativeStringCallbackComplete);
310 }
311 
NativeGetUpgradeResult(napi_env env,void * data)312 void SystemManagerAddon::NativeGetUpgradeResult(napi_env env, void *data)
313 {
314     EDMLOGI("NAPI_NativeGetUpgradeResult called");
315     if (data == nullptr) {
316         EDMLOGE("data is nullptr");
317         return;
318     }
319     AsyncGetUpgradeResultCallbackInfo *asyncCallbackInfo = static_cast<AsyncGetUpgradeResultCallbackInfo *>(data);
320     auto proxy = SystemManagerProxy::GetSystemManagerProxy();
321     if (proxy == nullptr) {
322         EDMLOGE("can not get EnterpriseDeviceMgrProxy");
323         return;
324     }
325     asyncCallbackInfo->ret = proxy->GetUpgradeResult(asyncCallbackInfo->elementName, asyncCallbackInfo->version,
326         asyncCallbackInfo->upgradeResult);
327 }
328 
NativeGetUpdateAuthData(napi_env env,void * data)329 void SystemManagerAddon::NativeGetUpdateAuthData(napi_env env, void *data)
330 {
331     EDMLOGI("NAPI_NativeGetUpdateAuthData called");
332     if (data == nullptr) {
333         EDMLOGE("data is nullptr");
334         return;
335     }
336     auto *asyncCallbakInfo = static_cast<AdapterAddonData *>(data);
337     asyncCallbakInfo->ret = SystemManagerProxy::GetSystemManagerProxy()->GetUpdateAuthData(asyncCallbakInfo->data,
338         asyncCallbakInfo->stringRet);
339 }
340 
NativeUpgradeResultComplete(napi_env env,napi_status status,void * data)341 void SystemManagerAddon::NativeUpgradeResultComplete(napi_env env, napi_status status, void *data)
342 {
343     if (data == nullptr) {
344         EDMLOGE("data is nullptr");
345         return;
346     }
347     auto *asyncCallbackInfo = static_cast<AsyncGetUpgradeResultCallbackInfo *>(data);
348     if (asyncCallbackInfo->deferred != nullptr) {
349         EDMLOGD("asyncCallbackInfo->deferred != nullptr");
350         if (asyncCallbackInfo->ret == ERR_OK) {
351             napi_value result = ConvertUpdateResultToJs(env, asyncCallbackInfo->upgradeResult);
352             napi_resolve_deferred(env, asyncCallbackInfo->deferred, result);
353         } else {
354             napi_reject_deferred(env, asyncCallbackInfo->deferred, CreateError(env, asyncCallbackInfo->ret));
355         }
356     }
357     napi_delete_async_work(env, asyncCallbackInfo->asyncWork);
358     delete asyncCallbackInfo;
359 }
360 
SetAutoUnlockAfterReboot(napi_env env,napi_callback_info info)361 napi_value SystemManagerAddon::SetAutoUnlockAfterReboot(napi_env env, napi_callback_info info)
362 {
363 #ifdef FEATURE_PC_ONLY
364     EDMLOGI("SystemManagerAddon::SetAutoUnlockAfterReboot called");
365     AddonMethodSign addonMethodSign;
366     addonMethodSign.name = "SetAutoUnlockAfterReboot";
367     addonMethodSign.argsType = {EdmAddonCommonType::ELEMENT, EdmAddonCommonType::BOOLEAN};
368     addonMethodSign.methodAttribute = MethodAttribute::HANDLE;
369     AdapterAddonData adapterAddonData{};
370     napi_value result = JsObjectToData(env, info, addonMethodSign, &adapterAddonData);
371     if (result == nullptr) {
372         return nullptr;
373     }
374     int32_t ret = SystemManagerProxy::GetSystemManagerProxy()->SetAutoUnlockAfterReboot(adapterAddonData.data);
375     if (FAILED(ret)) {
376         napi_throw(env, CreateError(env, ret));
377         EDMLOGE("SystemManagerAddon::SetAutoUnlockAfterReboot failed!");
378     }
379     return nullptr;
380 #else
381     EDMLOGW("SystemManagerAddon::SetAutoUnlockAfterReboot Unsupported Capabilities");
382     napi_throw(env, CreateError(env, EdmReturnErrCode::INTERFACE_UNSUPPORTED));
383     return nullptr;
384 #endif
385 }
386 
GetAutoUnlockAfterReboot(napi_env env,napi_callback_info info)387 napi_value SystemManagerAddon::GetAutoUnlockAfterReboot(napi_env env, napi_callback_info info)
388 {
389 #ifdef FEATURE_PC_ONLY
390     EDMLOGI("SystemManagerAddon::GetAutoUnlockAfterReboot called");
391     AddonMethodSign addonMethodSign;
392     addonMethodSign.name = "GetAutoUnlockAfterReboot";
393     addonMethodSign.argsType = {EdmAddonCommonType::ELEMENT};
394     addonMethodSign.methodAttribute = MethodAttribute::GET;
395     AdapterAddonData adapterAddonData{};
396     napi_value result = JsObjectToData(env, info, addonMethodSign, &adapterAddonData);
397     if (result == nullptr) {
398         return nullptr;
399     }
400     bool isAllowed = false;
401     int32_t ret =
402         SystemManagerProxy::GetSystemManagerProxy()->GetAutoUnlockAfterReboot(adapterAddonData.data, isAllowed);
403     if (FAILED(ret)) {
404         napi_throw(env, CreateError(env, ret));
405         EDMLOGE("SystemManagerAddon::GetAutoUnlockAfterReboot failed!");
406         return nullptr;
407     }
408     napi_value resultBool = nullptr;
409     NAPI_CALL(env, napi_get_boolean(env, isAllowed, &resultBool));
410     return resultBool;
411 #else
412     EDMLOGW("SystemManagerAddon::GetAutoUnlockAfterReboot Unsupported Capabilities");
413     napi_throw(env, CreateError(env, EdmReturnErrCode::INTERFACE_UNSUPPORTED));
414     return nullptr;
415 #endif
416 }
417 
JsObjToUpdatePolicy(napi_env env,napi_value object,UpdatePolicy & updatePolicy,std::string & errorMsg)418 bool SystemManagerAddon::JsObjToUpdatePolicy(napi_env env, napi_value object, UpdatePolicy &updatePolicy,
419     std::string &errorMsg)
420 {
421     int32_t policyType = -1;
422     if (!JsObjectToInt(env, object, "policyType", true, policyType) ||
423         !UpdatePolicyUtils::ProcessUpdatePolicyType(policyType, updatePolicy.type)) {
424         errorMsg = "the property 'policyType' in type 'OtaUpdatePolicy' is necessary";
425         return false;
426     }
427 
428     if (!JsObjectToString(env, object, "version", true, updatePolicy.version)) {
429         errorMsg = "the property 'version' in type 'OtaUpdatePolicy' is necessary";
430         return false;
431     }
432 
433     if (!JsObjectToLong(env, object, "latestUpdateTime", false, updatePolicy.installTime.latestUpdateTime)) {
434         errorMsg = "the property 'latestUpdateTime' in type 'OtaUpdatePolicy' is check failed";
435         return false;
436     }
437 
438     if (!JsObjectToLong(env, object, "installStartTime", updatePolicy.type == UpdatePolicyType::WINDOWS,
439         updatePolicy.installTime.installWindowStart)) {
440         errorMsg = "the property 'installStartTime' in type 'OtaUpdatePolicy' is check failed";
441         return false;
442     }
443 
444     if (!JsObjectToLong(env, object, "installEndTime", updatePolicy.type == UpdatePolicyType::WINDOWS,
445         updatePolicy.installTime.installWindowEnd)) {
446         errorMsg = "the property 'installEndTime' in type 'OtaUpdatePolicy' is check failed";
447         return false;
448     }
449 
450     if (!JsObjectToLong(env, object, "delayUpdateTime", updatePolicy.type == UpdatePolicyType::POSTPONE,
451         updatePolicy.installTime.delayUpdateTime)) {
452         errorMsg = "the property 'delayUpdateTime' in type 'OtaUpdatePolicy' is check failed";
453         return false;
454     }
455     if (!JsDisableSystemOtaUpdateToUpdatePolicy(env, object, "disableSystemOtaUpdate", updatePolicy.otaPolicyType)) {
456         errorMsg = "the property 'disableSystemOtaUpdate' in type 'OtaUpdatePolicy' is check failed";
457         return false;
458     }
459     return true;
460 }
461 
JsDisableSystemOtaUpdateToUpdatePolicy(napi_env env,napi_value object,const char * filedStr,OtaPolicyType & otaPolicyType)462 bool SystemManagerAddon::JsDisableSystemOtaUpdateToUpdatePolicy(napi_env env, napi_value object, const char *filedStr,
463     OtaPolicyType &otaPolicyType)
464 {
465     bool hasProperty = false;
466     if (napi_has_named_property(env, object, filedStr, &hasProperty) != napi_ok) {
467         EDMLOGE("get js property failed.");
468         return false;
469     }
470     if (!hasProperty) {
471         otaPolicyType = OtaPolicyType::DEFAULT;
472         return true;
473     }
474     napi_value prop = nullptr;
475     bool res = false;
476     if (!(napi_get_named_property(env, object, filedStr, &prop) == napi_ok && ParseBool(env, res, prop))) {
477         EDMLOGE("get js ParseBool failed.");
478         return false;
479     }
480     otaPolicyType = res ? OtaPolicyType::DISABLE_OTA : OtaPolicyType::ENABLE_OTA;
481     return true;
482 }
483 
ConvertUpdatePolicyToJs(napi_env env,const UpdatePolicy & updatePolicy)484 napi_value SystemManagerAddon::ConvertUpdatePolicyToJs(napi_env env, const UpdatePolicy &updatePolicy)
485 {
486     napi_value otaUpdatePolicy = nullptr;
487     NAPI_CALL(env, napi_create_object(env, &otaUpdatePolicy));
488 
489     napi_value policyType = nullptr;
490     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(updatePolicy.type), &policyType));
491     NAPI_CALL(env, napi_set_named_property(env, otaUpdatePolicy, "policyType", policyType));
492 
493     napi_value version = nullptr;
494     NAPI_CALL(env, napi_create_string_utf8(env, updatePolicy.version.c_str(), updatePolicy.version.length(), &version));
495     NAPI_CALL(env, napi_set_named_property(env, otaUpdatePolicy, "version", version));
496 
497     napi_value latestUpdateTime = nullptr;
498     NAPI_CALL(env, napi_create_int64(env, updatePolicy.installTime.latestUpdateTime, &latestUpdateTime));
499     NAPI_CALL(env, napi_set_named_property(env, otaUpdatePolicy, "latestUpdateTime", latestUpdateTime));
500 
501     napi_value delayUpdateTime = nullptr;
502     NAPI_CALL(env, napi_create_int64(env, updatePolicy.installTime.delayUpdateTime, &delayUpdateTime));
503     NAPI_CALL(env, napi_set_named_property(env, otaUpdatePolicy, "delayUpdateTime", delayUpdateTime));
504 
505     napi_value installStartTime = nullptr;
506     NAPI_CALL(env, napi_create_int64(env, updatePolicy.installTime.installWindowStart, &installStartTime));
507     NAPI_CALL(env, napi_set_named_property(env, otaUpdatePolicy, "installStartTime", installStartTime));
508 
509     napi_value installEndTime = nullptr;
510     NAPI_CALL(env, napi_create_int64(env, updatePolicy.installTime.installWindowEnd, &installEndTime));
511     NAPI_CALL(env, napi_set_named_property(env, otaUpdatePolicy, "installEndTime", installEndTime));
512 
513     napi_value disableSystemOtaUpdate = nullptr;
514     bool isDisableSystemOtaUpdate = false;
515     if (updatePolicy.otaPolicyType == OtaPolicyType::DISABLE_OTA) {
516         isDisableSystemOtaUpdate = true;
517     }
518     NAPI_CALL(env, napi_get_boolean(env, isDisableSystemOtaUpdate, &disableSystemOtaUpdate));
519     NAPI_CALL(env, napi_set_named_property(env, otaUpdatePolicy, "disableSystemOtaUpdate", disableSystemOtaUpdate));
520     return otaUpdatePolicy;
521 }
522 
JsObjToUpgradePackageInfo(napi_env env,napi_value object,UpgradePackageInfo & packageInfo)523 bool SystemManagerAddon::JsObjToUpgradePackageInfo(napi_env env, napi_value object, UpgradePackageInfo &packageInfo)
524 {
525     if (!JsObjectToString(env, object, "version", true, packageInfo.version)) {
526         EDMLOGE("JsObjToUpgradePackageInfo version trans failed!");
527         return false;
528     }
529 
530     napi_value nPackages;
531     if (!GetJsProperty(env, object, "packages", nPackages) || !ParsePackages(env, nPackages, packageInfo.packages)) {
532         return false;
533     }
534 
535     napi_value nDescription;
536     if (GetJsProperty(env, object, "description", nDescription) &&
537         !ParseDescription(env, nDescription, packageInfo.description)) {
538         return false;
539     }
540     std::tuple<int, bool> charArrayProp = {EdmConstants::AUTH_INFO_MAX_SIZE, false};
541     std::vector<char> ret;
542     if (!JsObjectToCharArray(env, object, "authInfo", charArrayProp, ret)) {
543         EDMLOGE("JsObjToUpgradePackageInfo authInfo trans failed!");
544         return false;
545     }
546     if (ret.size() == 0) {
547         return true;
548     }
549     errno_t err = memcpy_s(packageInfo.authInfo, sizeof(packageInfo.authInfo), ret.data(), ret.size());
550     memset_s(ret.data(), ret.size(), 0, ret.size());
551     if (err != EOK) {
552         return false;
553     }
554     packageInfo.authInfoSize = ret.size() - 1;
555     return true;
556 }
557 
ParsePackages(napi_env env,napi_value object,std::vector<Package> & packages)558 bool SystemManagerAddon::ParsePackages(napi_env env, napi_value object, std::vector<Package> &packages)
559 {
560     bool isArray = false;
561     if (napi_is_array(env, object, &isArray) != napi_ok || !isArray) {
562         return false;
563     }
564     uint32_t len = 0;
565     if (napi_get_array_length(env, object, &len) != napi_ok) {
566         return false;
567     }
568     for (uint32_t i = 0; i < len; i++) {
569         napi_value nPackage;
570         if (napi_get_element(env, object, i, &nPackage) != napi_ok) {
571             return false;
572         }
573         Package package;
574         if (!ParsePackage(env, nPackage, package)) {
575             return false;
576         }
577         packages.push_back(package);
578     }
579     return true;
580 }
581 
ParsePackage(napi_env env,napi_value object,Package & package)582 bool SystemManagerAddon::ParsePackage(napi_env env, napi_value object, Package &package)
583 {
584     int32_t type = static_cast<int32_t>(PackageType::UNKNOWN);
585     if (!JsObjectToInt(env, object, "type", true, type)) {
586         return false;
587     }
588     if (type != static_cast<int32_t>(PackageType::FIRMWARE)) {
589         return false;
590     }
591     package.type = static_cast<PackageType>(type);
592     return JsObjectToString(env, object, "path", true, package.path) &&
593         JsObjectToInt(env, object, "fd", false, package.fd);
594 }
595 
ParseDescription(napi_env env,napi_value object,PackageDescription & description)596 bool SystemManagerAddon::ParseDescription(napi_env env, napi_value object, PackageDescription &description)
597 {
598     napi_value nNotify;
599     if (GetJsProperty(env, object, "notify", nNotify)) {
600         if (!JsObjectToString(env, nNotify, "installTips", false, description.notify.installTips) ||
601             !JsObjectToString(env, nNotify, "installTipsDetails", false, description.notify.installTipsDetail)) {
602             return false;
603         }
604     }
605     return true;
606 }
607 
ConvertUpdateResultToJs(napi_env env,const UpgradeResult & updateResult)608 napi_value SystemManagerAddon::ConvertUpdateResultToJs(napi_env env, const UpgradeResult &updateResult)
609 {
610     napi_value nUpgradeResult = nullptr;
611     NAPI_CALL(env, napi_create_object(env, &nUpgradeResult));
612 
613     napi_value version = nullptr;
614     NAPI_CALL(env, napi_create_string_utf8(env, updateResult.version.c_str(), updateResult.version.length(), &version));
615     NAPI_CALL(env, napi_set_named_property(env, nUpgradeResult, "version", version));
616 
617     napi_value status = nullptr;
618     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(updateResult.status), &status));
619     NAPI_CALL(env, napi_set_named_property(env, nUpgradeResult, "status", status));
620 
621     napi_value nErrorInfo = nullptr;
622     NAPI_CALL(env, napi_create_object(env, &nErrorInfo));
623 
624     napi_value errorCode = nullptr;
625     NAPI_CALL(env, napi_create_int32(env, updateResult.errorCode, &errorCode));
626     NAPI_CALL(env, napi_set_named_property(env, nErrorInfo, "code", errorCode));
627 
628     napi_value errorMessage = nullptr;
629     NAPI_CALL(env, napi_create_string_utf8(env, updateResult.errorMessage.c_str(), updateResult.errorMessage.length(),
630         &errorMessage));
631     NAPI_CALL(env, napi_set_named_property(env, nErrorInfo, "message", errorMessage));
632 
633     NAPI_CALL(env, napi_set_named_property(env, nUpgradeResult, "errorInfo", nErrorInfo));
634     return nUpgradeResult;
635 }
636 
SetInstallLocalEnterpriseAppEnabled(napi_env env,napi_callback_info info)637 napi_value SystemManagerAddon::SetInstallLocalEnterpriseAppEnabled(napi_env env, napi_callback_info info)
638 {
639 #ifdef FEATURE_PC_ONLY
640     EDMLOGI("SystemManagerAddon::SetInstallLocalEnterpriseAppEnabled called");
641     AddonMethodSign addonMethodSign;
642     addonMethodSign.name = "SetInstallLocalEnterpriseAppEnabled";
643     addonMethodSign.argsType = {EdmAddonCommonType::ELEMENT, EdmAddonCommonType::BOOLEAN};
644     addonMethodSign.methodAttribute = MethodAttribute::HANDLE;
645     AdapterAddonData adapterAddonData{};
646     napi_value result = JsObjectToData(env, info, addonMethodSign, &adapterAddonData);
647     if (result == nullptr) {
648         return nullptr;
649     }
650     int32_t ret =
651         SystemManagerProxy::GetSystemManagerProxy()->SetInstallLocalEnterpriseAppEnabled(adapterAddonData.data);
652     if (FAILED(ret)) {
653         napi_throw(env, CreateError(env, ret));
654         EDMLOGE("SystemManagerAddon::SetInstallLocalEnterpriseAppEnabled failed!");
655     }
656     return nullptr;
657 #else
658     EDMLOGW("SystemManagerAddon::SetInstallLocalEnterpriseAppEnabled Unsupported Capabilities");
659     napi_throw(env, CreateError(env, EdmReturnErrCode::INTERFACE_UNSUPPORTED));
660     return nullptr;
661 #endif
662 }
663 
GetInstallLocalEnterpriseAppEnabled(napi_env env,napi_callback_info info)664 napi_value SystemManagerAddon::GetInstallLocalEnterpriseAppEnabled(napi_env env, napi_callback_info info)
665 {
666 #ifdef FEATURE_PC_ONLY
667     EDMLOGI("SystemManagerAddon::GetInstallLocalEnterpriseAppEnabled called");
668     AddonMethodSign addonMethodSign;
669     addonMethodSign.name = "GetInstallLocalEnterpriseAppEnabled";
670     addonMethodSign.argsType = {EdmAddonCommonType::ELEMENT};
671     addonMethodSign.methodAttribute = MethodAttribute::GET;
672     AdapterAddonData adapterAddonData{};
673     napi_value result = JsObjectToData(env, info, addonMethodSign, &adapterAddonData);
674     if (result == nullptr) {
675         return nullptr;
676     }
677     bool isAllowedInstall = false;
678     int32_t ret =
679         SystemManagerProxy::GetSystemManagerProxy()->GetInstallLocalEnterpriseAppEnabled(adapterAddonData.data,
680             isAllowedInstall);
681     if (FAILED(ret)) {
682         napi_throw(env, CreateError(env, ret));
683         EDMLOGE("SystemManagerAddon::GetInstallLocalEnterpriseAppEnabled failed!");
684         return nullptr;
685     }
686     napi_value resultBool = nullptr;
687     NAPI_CALL(env, napi_get_boolean(env, isAllowedInstall, &resultBool));
688     return resultBool;
689 #else
690     EDMLOGW("SystemManagerAddon::GetInstallLocalEnterpriseAppEnabled Unsupported Capabilities");
691     napi_throw(env, CreateError(env, EdmReturnErrCode::INTERFACE_UNSUPPORTED));
692     return nullptr;
693 #endif
694 }
695 
AddDisallowedNearlinkProtocols(napi_env env,napi_callback_info info)696 napi_value SystemManagerAddon::AddDisallowedNearlinkProtocols(napi_env env, napi_callback_info info)
697 {
698 #if defined(FEATURE_PC_ONLY)
699     EDMLOGI("NAPI_AddDisallowedNearlinkProtocols called");
700     return AddOrRemoveDisallowedNearlinkProtocols(env, info, FuncOperateType::SET);
701 #else
702     EDMLOGW("SystemManagerAddon::AddDisallowedNearlinkProtocols Unsupported Capabilities.");
703     napi_throw(env, CreateError(env, EdmReturnErrCode::INTERFACE_UNSUPPORTED));
704     return nullptr;
705 #endif
706 }
707 
GetDisallowedNearlinkProtocols(napi_env env,napi_callback_info info)708 napi_value SystemManagerAddon::GetDisallowedNearlinkProtocols(napi_env env, napi_callback_info info)
709 {
710 #if defined(FEATURE_PC_ONLY)
711     EDMLOGI("NAPI_GetDisallowedNearlinkProtocols called");
712     AddonMethodSign addonMethodSign;
713     addonMethodSign.name = "GetDisallowedNearlinkProtocols";
714     addonMethodSign.argsType = {EdmAddonCommonType::ELEMENT, EdmAddonCommonType::USERID};
715     addonMethodSign.argsConvert = {nullptr, nullptr};
716     addonMethodSign.methodAttribute = MethodAttribute::GET;
717     AdapterAddonData adapterAddonData{};
718     if (JsObjectToData(env, info, addonMethodSign, &adapterAddonData) == nullptr) {
719         return nullptr;
720     }
721     std::vector<int32_t> protocols;
722     int32_t retCode = SystemManagerProxy::GetSystemManagerProxy()->
723         GetDisallowedNearlinkProtocols(adapterAddonData.data, protocols);
724     if (FAILED(retCode)) {
725         napi_throw(env, CreateError(env, retCode));
726         return nullptr;
727     }
728     napi_value jsList = nullptr;
729     NAPI_CALL(env, napi_create_array_with_length(env, protocols.size(), &jsList));
730     for (size_t i = 0; i < protocols.size(); i++) {
731         napi_value item;
732         NAPI_CALL(env, napi_create_int32(env, protocols[i], &item));
733         NAPI_CALL(env, napi_set_element(env, jsList, i, item));
734     }
735     return jsList;
736 #else
737     EDMLOGW("SystemManagerAddon::GetDisallowedNearlinkProtocols Unsupported Capabilities.");
738     napi_throw(env, CreateError(env, EdmReturnErrCode::INTERFACE_UNSUPPORTED));
739     return nullptr;
740 #endif
741 }
742 
RemoveDisallowedNearlinkProtocols(napi_env env,napi_callback_info info)743 napi_value SystemManagerAddon::RemoveDisallowedNearlinkProtocols(napi_env env, napi_callback_info info)
744 {
745 #if defined(FEATURE_PC_ONLY)
746     EDMLOGI("NAPI_RemoveDisallowedNearlinkProtocols called");
747     return AddOrRemoveDisallowedNearlinkProtocols(env, info, FuncOperateType::REMOVE);
748 #else
749     EDMLOGW("SystemManagerAddon::RemoveDisallowedNearlinkProtocols Unsupported Capabilities.");
750     napi_throw(env, CreateError(env, EdmReturnErrCode::INTERFACE_UNSUPPORTED));
751     return nullptr;
752 #endif
753 }
754 
AddOrRemoveDisallowedNearlinkProtocols(napi_env env,napi_callback_info info,FuncOperateType operateType)755 napi_value SystemManagerAddon::AddOrRemoveDisallowedNearlinkProtocols(napi_env env, napi_callback_info info,
756     FuncOperateType operateType)
757 {
758 #if defined(FEATURE_PC_ONLY)
759     EDMLOGI("NAPI_AddOrRemoveDisallowedNearlinkProtocols called");
760     auto convertNearlinkProtocol2Data = [](napi_env env, napi_value argv, MessageParcel &data,
761         const AddonMethodSign &methodSign) {
762         std::vector<int32_t> nearlinkProtocols;
763         if (!ParseIntArray(env, nearlinkProtocols, argv)) {
764             EDMLOGE("NAPI_AddOrRemoveDisallowedNearlinkProtocols ParseIntArray fail");
765             return false;
766         }
767         data.WriteInt32Vector(nearlinkProtocols);
768         return true;
769     };
770     AddonMethodSign addonMethodSign;
771     addonMethodSign.name = "AddOrRemoveDisallowedNearlinkProtocols";
772     addonMethodSign.argsType = {EdmAddonCommonType::ELEMENT, EdmAddonCommonType::CUSTOM, EdmAddonCommonType::USERID};
773     addonMethodSign.argsConvert = {nullptr, convertNearlinkProtocol2Data, nullptr};
774     addonMethodSign.methodAttribute = MethodAttribute::HANDLE;
775     AdapterAddonData adapterAddonData{};
776     if (JsObjectToData(env, info, addonMethodSign, &adapterAddonData) == nullptr) {
777         return nullptr;
778     }
779     int32_t retCode = SystemManagerProxy::GetSystemManagerProxy()->
780         AddOrRemoveDisallowedNearlinkProtocols(adapterAddonData.data, operateType);
781     if (FAILED(retCode)) {
782         napi_throw(env, CreateError(env, retCode));
783     }
784 #else
785     EDMLOGW("SystemManagerAddon::AddOrRemoveDisallowedNearlinkProtocols Unsupported Capabilities.");
786     napi_throw(env, CreateError(env, EdmReturnErrCode::INTERFACE_UNSUPPORTED));
787 #endif
788     return nullptr;
789 }
790 
CreateProtocolObject(napi_env env,napi_value value)791 void SystemManagerAddon::CreateProtocolObject(napi_env env, napi_value value)
792 {
793     napi_value nSsap;
794     NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, static_cast<uint32_t>(NearlinkProtocol::SSAP), &nSsap));
795     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "SSAP", nSsap));
796     napi_value nDataTransfer;
797     NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, static_cast<uint32_t>(NearlinkProtocol::DATA_TRANSFER),
798     &nDataTransfer));
799     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "DATA_TRANSFER", nDataTransfer));
800 }
801 
802 static napi_module g_systemManagerModule = {
803     .nm_version = 1,
804     .nm_flags = 0,
805     .nm_filename = nullptr,
806     .nm_register_func = SystemManagerAddon::Init,
807     .nm_modname = "enterprise.systemManager",
808     .nm_priv = ((void *)0),
809     .reserved = { 0 },
810 };
811 
SystemManagerRegister()812 extern "C" __attribute__((constructor)) void SystemManagerRegister()
813 {
814     napi_module_register(&g_systemManagerModule);
815 }