1 /*
2  * Copyright (c) 2022 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 <pthread.h>
17 #include <cstdio>
18 #include <cstring>
19 #include <unistd.h>
20 
21 #include "app_log_wrapper.h"
22 
23 #include "napi/native_api.h"
24 #include "napi/native_node_api.h"
25 
26 namespace OHOS {
27 namespace AppExecFwk {
28 namespace {
29 constexpr int32_t INDEX_ZERO = 0;
30 constexpr int32_t UNSUPPORTED_FEATURE_ERRCODE = 801;
31 const std::string UNSUPPORTED_FEATURE_MESSAGE = "unsupported BundleManagerService feature";
32 constexpr int32_t INDEX_ONE = 1;
33 constexpr int32_t INDEX_TWO = 2;
34 constexpr int32_t INDEX_THREE = 3;
35 constexpr int32_t NAPI_RETURN_ONE = 1;
36 }
JsLauncherCommon(napi_env env,size_t argc,napi_value * argv)37 static napi_value JsLauncherCommon(napi_env env, size_t argc, napi_value *argv)
38 {
39     napi_ref callback = nullptr;
40     if (argc > INDEX_ZERO) {
41         napi_valuetype valueType = napi_undefined;
42         napi_typeof(env, argv[argc - INDEX_ONE], &valueType);
43         if (valueType == napi_function) {
44             napi_create_reference(env, argv[argc - INDEX_ONE], NAPI_RETURN_ONE, &callback);
45         }
46     }
47     napi_value promise = nullptr;
48     napi_deferred deferred = nullptr;
49     if (callback == nullptr) {
50         napi_create_promise(env, &deferred, &promise);
51     } else {
52         napi_get_undefined(env, &promise);
53     }
54 
55     napi_value result[INDEX_TWO] = { 0 };
56     napi_create_int32(env, UNSUPPORTED_FEATURE_ERRCODE, &result[INDEX_ZERO]);
57     napi_create_string_utf8(env, UNSUPPORTED_FEATURE_MESSAGE.c_str(),
58         NAPI_AUTO_LENGTH, &result[INDEX_ONE]);
59     if (callback) {
60         napi_value callbackTemp = nullptr;
61         napi_value placeHolder = nullptr;
62         napi_get_reference_value(env, callback, &callbackTemp);
63         napi_call_function(env, nullptr, callbackTemp,
64             sizeof(result) / sizeof(result[0]), result, &placeHolder);
65     } else {
66         napi_reject_deferred(env, deferred, result[INDEX_ZERO]);
67     }
68     napi_delete_reference(env, callback);
69     return promise;
70 }
71 
JSLauncherServiceOn(napi_env env,napi_callback_info info)72 static napi_value JSLauncherServiceOn(napi_env env, napi_callback_info info)
73 {
74     size_t argc = INDEX_THREE;
75     napi_value argv[INDEX_THREE] = { 0 };
76     napi_value thisArg = nullptr;
77     void *data = nullptr;
78     napi_get_cb_info(env, info, &argc, argv, &thisArg, &data);
79 
80     return JsLauncherCommon(env, argc, argv);
81 }
82 
JSLauncherServiceOff(napi_env env,napi_callback_info info)83 static napi_value JSLauncherServiceOff(napi_env env, napi_callback_info info)
84 {
85     size_t argc = INDEX_TWO;
86     napi_value argv[INDEX_TWO] = { 0 };
87     napi_value thisArg = nullptr;
88     void *data = nullptr;
89     napi_get_cb_info(env, info, &argc, argv, &thisArg, &data);
90 
91     return JsLauncherCommon(env, argc, argv);
92 }
93 
JSGetAllLauncherAbilityInfos(napi_env env,napi_callback_info info)94 static napi_value JSGetAllLauncherAbilityInfos(napi_env env, napi_callback_info info)
95 {
96     size_t argc = INDEX_TWO;
97     napi_value argv[INDEX_TWO] = { 0 };
98     napi_value thisArg = nullptr;
99     void *data = nullptr;
100     napi_get_cb_info(env, info, &argc, argv, &thisArg, &data);
101 
102     return JsLauncherCommon(env, argc, argv);
103 }
104 
JSGetLauncherAbilityInfos(napi_env env,napi_callback_info info)105 static napi_value JSGetLauncherAbilityInfos(napi_env env, napi_callback_info info)
106 {
107     size_t argc = INDEX_THREE;
108     napi_value argv[INDEX_THREE] = { 0 };
109     napi_value thisArg = nullptr;
110     void *data = nullptr;
111     napi_get_cb_info(env, info, &argc, argv, &thisArg, &data);
112 
113     return JsLauncherCommon(env, argc, argv);
114 }
115 
JSGetShortcutInfos(napi_env env,napi_callback_info info)116 static napi_value JSGetShortcutInfos(napi_env env, napi_callback_info info)
117 {
118     size_t argc = INDEX_TWO;
119     napi_value argv[INDEX_TWO] = { 0 };
120     napi_value thisArg = nullptr;
121     void *data = nullptr;
122 
123     napi_get_cb_info(env, info, &argc, argv, &thisArg, &data);
124 
125     return JsLauncherCommon(env, argc, argv);
126 }
127 
LauncherServiceExport(napi_env env,napi_value exports)128 static napi_value LauncherServiceExport(napi_env env, napi_value exports)
129 {
130     static napi_property_descriptor launcherDesc[] = {
131         DECLARE_NAPI_FUNCTION("on", JSLauncherServiceOn),
132         DECLARE_NAPI_FUNCTION("off", JSLauncherServiceOff),
133         DECLARE_NAPI_FUNCTION("getAllLauncherAbilityInfos", JSGetAllLauncherAbilityInfos),
134         DECLARE_NAPI_FUNCTION("getLauncherAbilityInfos", JSGetLauncherAbilityInfos),
135         DECLARE_NAPI_FUNCTION("getShortcutInfos", JSGetShortcutInfos),
136     };
137 
138     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(launcherDesc) / sizeof(launcherDesc[0]), launcherDesc));
139     return exports;
140 }
141 
142 static napi_module launcherServiceModule = {
143     .nm_version = 1,
144     .nm_flags = 0,
145     .nm_filename = nullptr,
146     .nm_register_func = LauncherServiceExport,
147     .nm_modname = "bundle.innerBundleManager",
148     .nm_priv = ((void*)0),
149     .reserved = { 0 },
150 };
151 
LauncherServiceRegister()152 extern "C" __attribute__((constructor)) void LauncherServiceRegister()
153 {
154     napi_module_register(&launcherServiceModule);
155 }
156 } // AppExecFwk
157 } // OHOS