1 /*
2 * Copyright (c) 2023 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 "device_control_addon.h"
17 #include "os_account_manager.h"
18 #include "edm_log.h"
19
20 using namespace OHOS::EDM;
21
Init(napi_env env,napi_value exports)22 napi_value DeviceControlAddon::Init(napi_env env, napi_value exports)
23 {
24 napi_property_descriptor property[] = {
25 DECLARE_NAPI_FUNCTION("resetFactory", ResetFactory),
26 DECLARE_NAPI_FUNCTION("shutdown", Shutdown),
27 DECLARE_NAPI_FUNCTION("reboot", Reboot),
28 DECLARE_NAPI_FUNCTION("lockScreen", LockScreen),
29 };
30 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(property) / sizeof(property[0]), property));
31 return exports;
32 }
33
ResetFactory(napi_env env,napi_callback_info info)34 napi_value DeviceControlAddon::ResetFactory(napi_env env, napi_callback_info info)
35 {
36 EDMLOGI("NAPI_resetFactory called");
37 size_t argc = ARGS_SIZE_TWO;
38 napi_value argv[ARGS_SIZE_TWO] = {nullptr};
39 napi_value thisArg = nullptr;
40 void *data = nullptr;
41 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisArg, &data));
42 ASSERT_AND_THROW_PARAM_ERROR(env, argc >= ARGS_SIZE_ONE, "parameter count error");
43 bool matchFlag = MatchValueType(env, argv[ARR_INDEX_ZERO], napi_object);
44 if (argc > ARGS_SIZE_ONE) {
45 matchFlag = matchFlag && MatchValueType(env, argv[ARR_INDEX_ONE], napi_function);
46 }
47 ASSERT_AND_THROW_PARAM_ERROR(env, matchFlag, "parameter type error");
48 auto asyncCallbackInfo = new (std::nothrow) AsyncDeviceControlCallbackInfo();
49 if (asyncCallbackInfo == nullptr) {
50 return nullptr;
51 }
52 std::unique_ptr<AsyncDeviceControlCallbackInfo> callbackPtr {asyncCallbackInfo};
53 bool ret = ParseElementName(env, asyncCallbackInfo->elementName, argv[ARR_INDEX_ZERO]);
54 ASSERT_AND_THROW_PARAM_ERROR(env, ret, "element name param error");
55 EDMLOGD("resetFactory: asyncCallbackInfo->elementName.bundlename %{public}s, "
56 "asyncCallbackInfo->abilityname:%{public}s",
57 asyncCallbackInfo->elementName.GetBundleName().c_str(),
58 asyncCallbackInfo->elementName.GetAbilityName().c_str());
59 if (argc > ARGS_SIZE_ONE) {
60 EDMLOGD("NAPI_resetFactory argc == ARGS_SIZE_TWO");
61 napi_create_reference(env, argv[ARR_INDEX_ONE], NAPI_RETURN_ONE, &asyncCallbackInfo->callback);
62 }
63 napi_value asyncWorkReturn = HandleAsyncWork(env, asyncCallbackInfo, "ResetFactory",
64 NativeResetFactory, NativeVoidCallbackComplete);
65 callbackPtr.release();
66 return asyncWorkReturn;
67 }
68
LockScreen(napi_env env,napi_callback_info info)69 napi_value DeviceControlAddon::LockScreen(napi_env env, napi_callback_info info)
70 {
71 EDMLOGI("NAPI_lockScreen called");
72 size_t argc = ARGS_SIZE_TWO;
73 napi_value argv[ARGS_SIZE_TWO] = {nullptr};
74 napi_value thisArg = nullptr;
75 void *data = nullptr;
76 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisArg, &data));
77 ASSERT_AND_THROW_PARAM_ERROR(env, argc >= ARGS_SIZE_ONE, "parameter count error");
78 bool matchFlag = MatchValueType(env, argv[ARR_INDEX_ZERO], napi_object);
79 ASSERT_AND_THROW_PARAM_ERROR(env, matchFlag, "parameter type error");
80 OHOS::AppExecFwk::ElementName elementName;
81 bool ret = ParseElementName(env, elementName, argv[ARR_INDEX_ZERO]);
82 ASSERT_AND_THROW_PARAM_ERROR(env, ret, "element name param error");
83 EDMLOGD("lockScreen: asyncCallbackInfo->elementName.bundlename %{public}s, "
84 "asyncCallbackInfo->abilityname:%{public}s",
85 elementName.GetBundleName().c_str(),
86 elementName.GetAbilityName().c_str());
87 int32_t userId = 0;
88 AccountSA::OsAccountManager::GetOsAccountLocalIdFromProcess(userId);
89 EDMLOGI("NAPI_lockScreen called userId :%{public}d", userId);
90 int32_t result = DeviceControlProxy::GetDeviceControlProxy()->LockScreen(elementName, userId);
91 if (FAILED(result)) {
92 napi_throw(env, CreateError(env, result));
93 }
94 return nullptr;
95 }
96
Shutdown(napi_env env,napi_callback_info info)97 napi_value DeviceControlAddon::Shutdown(napi_env env, napi_callback_info info)
98 {
99 EDMLOGI("NAPI_shutdown called");
100 size_t argc = ARGS_SIZE_TWO;
101 napi_value argv[ARGS_SIZE_TWO] = {nullptr};
102 napi_value thisArg = nullptr;
103 void *data = nullptr;
104 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisArg, &data));
105 ASSERT_AND_THROW_PARAM_ERROR(env, argc >= ARGS_SIZE_ONE, "parameter count error");
106 bool matchFlag = MatchValueType(env, argv[ARR_INDEX_ZERO], napi_object);
107 ASSERT_AND_THROW_PARAM_ERROR(env, matchFlag, "parameter type error");
108 OHOS::AppExecFwk::ElementName elementName;
109 bool ret = ParseElementName(env, elementName, argv[ARR_INDEX_ZERO]);
110 ASSERT_AND_THROW_PARAM_ERROR(env, ret, "element name param error");
111 EDMLOGD("lockScreen: asyncCallbackInfo->elementName.bundlename %{public}s, "
112 "asyncCallbackInfo->abilityname:%{public}s",
113 elementName.GetBundleName().c_str(),
114 elementName.GetAbilityName().c_str());
115 int32_t result = DeviceControlProxy::GetDeviceControlProxy()->Shutdown(elementName);
116 if (FAILED(result)) {
117 napi_throw(env, CreateError(env, result));
118 }
119 return nullptr;
120 }
121
Reboot(napi_env env,napi_callback_info info)122 napi_value DeviceControlAddon::Reboot(napi_env env, napi_callback_info info)
123 {
124 EDMLOGI("NAPI_reboot called");
125 size_t argc = ARGS_SIZE_TWO;
126 napi_value argv[ARGS_SIZE_TWO] = {nullptr};
127 napi_value thisArg = nullptr;
128 void *data = nullptr;
129 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisArg, &data));
130 ASSERT_AND_THROW_PARAM_ERROR(env, argc >= ARGS_SIZE_ONE, "parameter count error");
131 bool matchFlag = MatchValueType(env, argv[ARR_INDEX_ZERO], napi_object);
132 ASSERT_AND_THROW_PARAM_ERROR(env, matchFlag, "parameter type error");
133 OHOS::AppExecFwk::ElementName elementName;
134 bool ret = ParseElementName(env, elementName, argv[ARR_INDEX_ZERO]);
135 ASSERT_AND_THROW_PARAM_ERROR(env, ret, "element name param error");
136 EDMLOGD("lockScreen: asyncCallbackInfo->elementName.bundlename %{public}s, "
137 "asyncCallbackInfo->abilityname:%{public}s",
138 elementName.GetBundleName().c_str(),
139 elementName.GetAbilityName().c_str());
140 int32_t result = DeviceControlProxy::GetDeviceControlProxy()->Reboot(elementName);
141 if (FAILED(result)) {
142 napi_throw(env, CreateError(env, result));
143 }
144 return nullptr;
145 }
146
NativeResetFactory(napi_env env,void * data)147 void DeviceControlAddon::NativeResetFactory(napi_env env, void *data)
148 {
149 EDMLOGI("NAPI_NativeResetFactory called");
150 if (data == nullptr) {
151 EDMLOGE("data is nullptr");
152 return;
153 }
154 AsyncDeviceControlCallbackInfo *asyncCallbackInfo = static_cast<AsyncDeviceControlCallbackInfo *>(data);
155 if (DeviceControlProxy::GetDeviceControlProxy() == nullptr) {
156 EDMLOGE("can not get GetDeviceControlProxy");
157 return;
158 }
159 asyncCallbackInfo->ret =
160 DeviceControlProxy::GetDeviceControlProxy()->ResetFactory(asyncCallbackInfo->elementName);
161 }
162
163 static napi_module g_deviceControlModule = {
164 .nm_version = 1,
165 .nm_flags = 0,
166 .nm_filename = nullptr,
167 .nm_register_func = DeviceControlAddon::Init,
168 .nm_modname = "enterprise.deviceControl",
169 .nm_priv = ((void *)0),
170 .reserved = { 0 },
171 };
172
DeviceControlManagerRegister()173 extern "C" __attribute__((constructor)) void DeviceControlManagerRegister()
174 {
175 napi_module_register(&g_deviceControlModule);
176 }
177