• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 #include "particle_ability.h"
16 
17 #include <cstring>
18 #include <uv.h>
19 #include <vector>
20 
21 #include "hilog_wrapper.h"
22 #include "napi_common_ability.h"
23 #include "securec.h"
24 
25 using namespace OHOS::AAFwk;
26 using namespace OHOS::AppExecFwk;
27 
28 namespace OHOS {
29 namespace AppExecFwk {
30 /**
31  * @brief Obtains the type of this application.
32  *
33  * @param env The environment that the Node-API call is invoked under.
34  * @param info The callback info passed into the callback function.
35  *
36  * @return The return value from NAPI C++ to JS for the module.
37  */
NAPI_PAGetAppType(napi_env env,napi_callback_info info)38 napi_value NAPI_PAGetAppType(napi_env env, napi_callback_info info)
39 {
40     HILOG_INFO("%{public}s called.", __func__);
41     return NAPI_GetAppTypeCommon(env, info, AbilityType::UNKNOWN);
42 }
43 
44 /**
45  * @brief Obtains information about the current ability.
46  *
47  * @param env The environment that the Node-API call is invoked under.
48  * @param info The callback info passed into the callback function.
49  *
50  * @return The return value from NAPI C++ to JS for the module.
51  */
NAPI_PAGetAbilityInfo(napi_env env,napi_callback_info info)52 napi_value NAPI_PAGetAbilityInfo(napi_env env, napi_callback_info info)
53 {
54     HILOG_INFO("%{public}s called.", __func__);
55     return NAPI_GetAbilityInfoCommon(env, info, AbilityType::UNKNOWN);
56 }
57 
58 /**
59  * @brief Obtains the HapModuleInfo object of the application.
60  *
61  * @param env The environment that the Node-API call is invoked under.
62  * @param info The callback info passed into the callback function.
63  *
64  * @return The return value from NAPI C++ to JS for the module.
65  */
NAPI_PAGetHapModuleInfo(napi_env env,napi_callback_info info)66 napi_value NAPI_PAGetHapModuleInfo(napi_env env, napi_callback_info info)
67 {
68     HILOG_INFO("%{public}s called.", __func__);
69     return NAPI_GetHapModuleInfoCommon(env, info, AbilityType::UNKNOWN);
70 }
71 
72 /**
73  * @brief Get context.
74  *
75  * @param env The environment that the Node-API call is invoked under.
76  * @param info The callback info passed into the callback function.
77  *
78  * @return The return value from NAPI C++ to JS for the module.
79  */
NAPI_PAGetContext(napi_env env,napi_callback_info info)80 napi_value NAPI_PAGetContext(napi_env env, napi_callback_info info)
81 {
82     HILOG_INFO("%{public}s called.", __func__);
83     return NAPI_GetContextCommon(env, info, AbilityType::UNKNOWN);
84 }
85 
86 /**
87  * @brief Get want.
88  *
89  * @param env The environment that the Node-API call is invoked under.
90  * @param info The callback info passed into the callback function.
91  *
92  * @return The return value from NAPI C++ to JS for the module.
93  */
NAPI_PAGetWant(napi_env env,napi_callback_info info)94 napi_value NAPI_PAGetWant(napi_env env, napi_callback_info info)
95 {
96     HILOG_INFO("%{public}s called.", __func__);
97     return NAPI_GetWantCommon(env, info, AbilityType::UNKNOWN);
98 }
99 
100 /**
101  * @brief Obtains the class name in this ability name, without the prefixed bundle name.
102  *
103  * @param env The environment that the Node-API call is invoked under.
104  * @param info The callback info passed into the callback function.
105  *
106  * @return The return value from NAPI C++ to JS for the module.
107  */
NAPI_PAGetAbilityName(napi_env env,napi_callback_info info)108 napi_value NAPI_PAGetAbilityName(napi_env env, napi_callback_info info)
109 {
110     HILOG_INFO("%{public}s called.", __func__);
111     return NAPI_GetAbilityNameCommon(env, info, AbilityType::UNKNOWN);
112 }
113 
114 /**
115  * @brief ParticleAbility NAPI method : startAbility.
116  *
117  * @param env The environment that the Node-API call is invoked under.
118  * @param info The callback info passed into the callback function.
119  *
120  * @return The return value from NAPI C++ to JS for the module.
121  */
NAPI_PAStartAbility(napi_env env,napi_callback_info info)122 napi_value NAPI_PAStartAbility(napi_env env, napi_callback_info info)
123 {
124     HILOG_INFO("%{public}s called.", __func__);
125     return NAPI_StartAbilityCommon(env, info, AbilityType::UNKNOWN);
126 }
127 
128 /**
129  * @brief ParticleAbility NAPI method : stopAbility.
130  *
131  * @param env The environment that the Node-API call is invoked under.
132  * @param info The callback info passed into the callback function.
133  *
134  * @return The return value from NAPI C++ to JS for the module.
135  */
NAPI_PAStopAbility(napi_env env,napi_callback_info info)136 napi_value NAPI_PAStopAbility(napi_env env, napi_callback_info info)
137 {
138     HILOG_INFO("%{public}s called.", __func__);
139     return NAPI_StopAbilityCommon(env, info, AbilityType::UNKNOWN);
140 }
141 
142 /**
143  * @brief ParticleAbility NAPI method : connectAbility.
144  *
145  * @param env The environment that the Node-API call is invoked under.
146  * @param info The callback info passed into the callback function.
147  *
148  * @return The return value from NAPI C++ to JS for the module.
149  */
NAPI_PAConnectAbility(napi_env env,napi_callback_info info)150 napi_value NAPI_PAConnectAbility(napi_env env, napi_callback_info info)
151 {
152     HILOG_INFO("%{public}s called.", __func__);
153     return NAPI_ConnectAbilityCommon(env, info, AbilityType::UNKNOWN);
154 }
155 
156 /**
157  * @brief ParticleAbility NAPI method : disconnectAbility.
158  *
159  * @param env The environment that the Node-API call is invoked under.
160  * @param info The callback info passed into the callback function.
161  *
162  * @return The return value from NAPI C++ to JS for the module.
163  */
NAPI_PADisConnectAbility(napi_env env,napi_callback_info info)164 napi_value NAPI_PADisConnectAbility(napi_env env, napi_callback_info info)
165 {
166     HILOG_INFO("%{public}s called.", __func__);
167     return NAPI_DisConnectAbilityCommon(env, info, AbilityType::UNKNOWN);
168 }
169 
170 /**
171  * @brief FeatureAbility NAPI method : acquireDataAbilityHelper.
172  *
173  * @param env The environment that the Node-API call is invoked under.
174  * @param info The callback info passed into the callback function.
175  *
176  * @return The return value from NAPI C++ to JS for the module.
177  */
NAPI_PAAcquireDataAbilityHelper(napi_env env,napi_callback_info info)178 napi_value NAPI_PAAcquireDataAbilityHelper(napi_env env, napi_callback_info info)
179 {
180     HILOG_INFO("%{public}s,called", __func__);
181     return NAPI_AcquireDataAbilityHelperCommon(env, info, AbilityType::UNKNOWN);
182 }
183 
184 /**
185  * @brief ParticleAbility NAPI method : startBackgroundRunning.
186  *
187  * @param env The environment that the Node-API call is invoked under.
188  * @param info The callback info passed into the callback function.
189  *
190  * @return The return value from NAPI C++ to JS for the module.
191  */
NAPI_PAStartBackgroundRunning(napi_env env,napi_callback_info info)192 napi_value NAPI_PAStartBackgroundRunning(napi_env env, napi_callback_info info)
193 {
194     HILOG_INFO("%{public}s,called", __func__);
195     return NAPI_StartBackgroundRunningCommon(env, info);
196 }
197 
198 /**
199  * @brief ParticleAbility NAPI method : cancelBackgroundRunning.
200  *
201  * @param env The environment that the Node-API call is invoked under.
202  * @param info The callback info passed into the callback function.
203  *
204  * @return The return value from NAPI C++ to JS for the module.
205  */
NAPI_PACancelBackgroundRunning(napi_env env,napi_callback_info info)206 napi_value NAPI_PACancelBackgroundRunning(napi_env env, napi_callback_info info)
207 {
208     HILOG_INFO("%{public}s,called", __func__);
209     return NAPI_CancelBackgroundRunningCommon(env, info);
210 }
211 
NAPI_PATerminateAbility(napi_env env,napi_callback_info info)212 napi_value NAPI_PATerminateAbility(napi_env env, napi_callback_info info)
213 {
214     HILOG_INFO("%{public}s,called", __func__);
215     return NAPI_TerminateAbilityCommon(env, info);
216 }
217 
218 /**
219  * @brief ParticleAbility NAPI module registration.
220  *
221  * @param env The environment that the Node-API call is invoked under.
222  * @param exports An empty object via the exports parameter as a convenience.
223  *
224  * @return The return value from Init is treated as the exports object for the module.
225  */
ParticleAbilityInit(napi_env env,napi_value exports)226 napi_value ParticleAbilityInit(napi_env env, napi_value exports)
227 {
228     HILOG_INFO("%{public}s called.", __func__);
229     napi_property_descriptor properties[] = {
230         DECLARE_NAPI_FUNCTION("getAppType", NAPI_PAGetAppType),
231         DECLARE_NAPI_FUNCTION("getAbilityInfo", NAPI_PAGetAbilityInfo),
232         DECLARE_NAPI_FUNCTION("getHapModuleInfo", NAPI_PAGetHapModuleInfo),
233         DECLARE_NAPI_FUNCTION("getContext", NAPI_PAGetContext),
234         DECLARE_NAPI_FUNCTION("getWant", NAPI_PAGetWant),
235         DECLARE_NAPI_FUNCTION("getAbilityName", NAPI_PAGetAbilityName),
236         DECLARE_NAPI_FUNCTION("startAbility", NAPI_PAStartAbility),
237         DECLARE_NAPI_FUNCTION("stopAbility", NAPI_PAStopAbility),
238         DECLARE_NAPI_FUNCTION("connectAbility", NAPI_PAConnectAbility),
239         DECLARE_NAPI_FUNCTION("disconnectAbility", NAPI_PADisConnectAbility),
240         DECLARE_NAPI_FUNCTION("acquireDataAbilityHelper", NAPI_PAAcquireDataAbilityHelper),
241         DECLARE_NAPI_FUNCTION("startBackgroundRunning", NAPI_PAStartBackgroundRunning),
242         DECLARE_NAPI_FUNCTION("cancelBackgroundRunning", NAPI_PACancelBackgroundRunning),
243         DECLARE_NAPI_FUNCTION("terminateSelf", NAPI_PATerminateAbility),
244     };
245     napi_define_properties(env, exports, sizeof(properties) / sizeof(properties[0]), properties);
246 
247     return exports;
248 }
249 }  // namespace AppExecFwk
250 }  // namespace OHOS
251