• 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 "js_runtime_utils.h"
23 #include "napi_common_ability.h"
24 #include "napi/native_api.h"
25 #include "securec.h"
26 
27 using namespace OHOS::AbilityRuntime;
28 using namespace OHOS::AAFwk;
29 using namespace OHOS::AppExecFwk;
30 
31 namespace OHOS {
32 namespace AppExecFwk {
33 /**
34  * @brief Obtains the type of this application.
35  *
36  * @param env The environment that the Node-API call is invoked under.
37  * @param info The callback info passed into the callback function.
38  *
39  * @return The return value from NAPI C++ to JS for the module.
40  */
NAPI_PAGetAppType(napi_env env,napi_callback_info info)41 napi_value NAPI_PAGetAppType(napi_env env, napi_callback_info info)
42 {
43     HILOG_INFO("%{public}s called.", __func__);
44     return NAPI_GetAppTypeCommon(env, info, AbilityType::UNKNOWN);
45 }
46 
47 /**
48  * @brief Obtains information about the current ability.
49  *
50  * @param env The environment that the Node-API call is invoked under.
51  * @param info The callback info passed into the callback function.
52  *
53  * @return The return value from NAPI C++ to JS for the module.
54  */
NAPI_PAGetAbilityInfo(napi_env env,napi_callback_info info)55 napi_value NAPI_PAGetAbilityInfo(napi_env env, napi_callback_info info)
56 {
57     HILOG_INFO("%{public}s called.", __func__);
58     return NAPI_GetAbilityInfoCommon(env, info, AbilityType::UNKNOWN);
59 }
60 
61 /**
62  * @brief Obtains the HapModuleInfo object of the application.
63  *
64  * @param env The environment that the Node-API call is invoked under.
65  * @param info The callback info passed into the callback function.
66  *
67  * @return The return value from NAPI C++ to JS for the module.
68  */
NAPI_PAGetHapModuleInfo(napi_env env,napi_callback_info info)69 napi_value NAPI_PAGetHapModuleInfo(napi_env env, napi_callback_info info)
70 {
71     HILOG_INFO("%{public}s called.", __func__);
72     return NAPI_GetHapModuleInfoCommon(env, info, AbilityType::UNKNOWN);
73 }
74 
75 /**
76  * @brief Get context.
77  *
78  * @param env The environment that the Node-API call is invoked under.
79  * @param info The callback info passed into the callback function.
80  *
81  * @return The return value from NAPI C++ to JS for the module.
82  */
NAPI_PAGetContext(napi_env env,napi_callback_info info)83 napi_value NAPI_PAGetContext(napi_env env, napi_callback_info info)
84 {
85     HILOG_INFO("%{public}s called.", __func__);
86     return NAPI_GetContextCommon(env, info, AbilityType::UNKNOWN);
87 }
88 
89 /**
90  * @brief Get want.
91  *
92  * @param env The environment that the Node-API call is invoked under.
93  * @param info The callback info passed into the callback function.
94  *
95  * @return The return value from NAPI C++ to JS for the module.
96  */
NAPI_PAGetWant(napi_env env,napi_callback_info info)97 napi_value NAPI_PAGetWant(napi_env env, napi_callback_info info)
98 {
99     HILOG_INFO("%{public}s called.", __func__);
100     return NAPI_GetWantCommon(env, info, AbilityType::UNKNOWN);
101 }
102 
103 /**
104  * @brief Obtains the class name in this ability name, without the prefixed bundle name.
105  *
106  * @param env The environment that the Node-API call is invoked under.
107  * @param info The callback info passed into the callback function.
108  *
109  * @return The return value from NAPI C++ to JS for the module.
110  */
NAPI_PAGetAbilityName(napi_env env,napi_callback_info info)111 napi_value NAPI_PAGetAbilityName(napi_env env, napi_callback_info info)
112 {
113     HILOG_INFO("%{public}s called.", __func__);
114     return NAPI_GetAbilityNameCommon(env, info, AbilityType::UNKNOWN);
115 }
116 
117 /**
118  * @brief ParticleAbility NAPI method : stopAbility.
119  *
120  * @param env The environment that the Node-API call is invoked under.
121  * @param info The callback info passed into the callback function.
122  *
123  * @return The return value from NAPI C++ to JS for the module.
124  */
NAPI_PAStopAbility(napi_env env,napi_callback_info info)125 napi_value NAPI_PAStopAbility(napi_env env, napi_callback_info info)
126 {
127     HILOG_INFO("%{public}s called.", __func__);
128     return NAPI_StopAbilityCommon(env, info, AbilityType::UNKNOWN);
129 }
130 
131 /**
132  * @brief FeatureAbility NAPI method : acquireDataAbilityHelper.
133  *
134  * @param env The environment that the Node-API call is invoked under.
135  * @param info The callback info passed into the callback function.
136  *
137  * @return The return value from NAPI C++ to JS for the module.
138  */
NAPI_PAAcquireDataAbilityHelper(napi_env env,napi_callback_info info)139 napi_value NAPI_PAAcquireDataAbilityHelper(napi_env env, napi_callback_info info)
140 {
141     HILOG_INFO("%{public}s,called", __func__);
142     return NAPI_AcquireDataAbilityHelperCommon(env, info, AbilityType::UNKNOWN);
143 }
144 
145 /**
146  * @brief ParticleAbility NAPI method : startBackgroundRunning.
147  *
148  * @param env The environment that the Node-API call is invoked under.
149  * @param info The callback info passed into the callback function.
150  *
151  * @return The return value from NAPI C++ to JS for the module.
152  */
NAPI_PAStartBackgroundRunning(napi_env env,napi_callback_info info)153 napi_value NAPI_PAStartBackgroundRunning(napi_env env, napi_callback_info info)
154 {
155     HILOG_INFO("%{public}s,called", __func__);
156     return NAPI_StartBackgroundRunningCommon(env, info);
157 }
158 
159 /**
160  * @brief ParticleAbility NAPI method : cancelBackgroundRunning.
161  *
162  * @param env The environment that the Node-API call is invoked under.
163  * @param info The callback info passed into the callback function.
164  *
165  * @return The return value from NAPI C++ to JS for the module.
166  */
NAPI_PACancelBackgroundRunning(napi_env env,napi_callback_info info)167 napi_value NAPI_PACancelBackgroundRunning(napi_env env, napi_callback_info info)
168 {
169     HILOG_INFO("%{public}s,called", __func__);
170     return NAPI_CancelBackgroundRunningCommon(env, info);
171 }
172 
173 /**
174  * @brief ParticleAbility NAPI module registration.
175  *
176  * @param env The environment that the Node-API call is invoked under.
177  * @param exports An empty object via the exports parameter as a convenience.
178  *
179  * @return The return value from Init is treated as the exports object for the module.
180  */
ParticleAbilityInit(napi_env env,napi_value exports)181 napi_value ParticleAbilityInit(napi_env env, napi_value exports)
182 {
183     HILOG_INFO("%{public}s called.", __func__);
184     napi_property_descriptor properties[] = {
185         DECLARE_NAPI_FUNCTION("getAppType", NAPI_PAGetAppType),
186         DECLARE_NAPI_FUNCTION("getAbilityInfo", NAPI_PAGetAbilityInfo),
187         DECLARE_NAPI_FUNCTION("getHapModuleInfo", NAPI_PAGetHapModuleInfo),
188         DECLARE_NAPI_FUNCTION("getContext", NAPI_PAGetContext),
189         DECLARE_NAPI_FUNCTION("getWant", NAPI_PAGetWant),
190         DECLARE_NAPI_FUNCTION("getAbilityName", NAPI_PAGetAbilityName),
191         DECLARE_NAPI_FUNCTION("stopAbility", NAPI_PAStopAbility),
192         DECLARE_NAPI_FUNCTION("acquireDataAbilityHelper", NAPI_PAAcquireDataAbilityHelper),
193         DECLARE_NAPI_FUNCTION("startBackgroundRunning", NAPI_PAStartBackgroundRunning),
194         DECLARE_NAPI_FUNCTION("cancelBackgroundRunning", NAPI_PACancelBackgroundRunning),
195     };
196     napi_define_properties(env, exports, sizeof(properties) / sizeof(properties[0]), properties);
197 
198     return reinterpret_cast<napi_value>(JsParticleAbilityInit(reinterpret_cast<NativeEngine*>(env),
199         reinterpret_cast<NativeValue*>(exports)));
200 }
201 
Finalizer(NativeEngine * engine,void * data,void * hint)202 void JsParticleAbility::Finalizer(NativeEngine *engine, void *data, void *hint)
203 {
204     HILOG_INFO("JsParticleAbility::Finalizer is called");
205     std::unique_ptr<JsParticleAbility>(static_cast<JsParticleAbility*>(data));
206 }
207 
PAConnectAbility(NativeEngine * engine,NativeCallbackInfo * info)208 NativeValue* JsParticleAbility::PAConnectAbility(NativeEngine *engine, NativeCallbackInfo *info)
209 {
210     JsParticleAbility *me = CheckParamsAndGetThis<JsParticleAbility>(engine, info);
211     return (me != nullptr) ? me->JsConnectAbility(*engine, *info, AbilityType::UNKNOWN) : nullptr;
212 }
213 
PADisConnectAbility(NativeEngine * engine,NativeCallbackInfo * info)214 NativeValue* JsParticleAbility::PADisConnectAbility(NativeEngine *engine, NativeCallbackInfo *info)
215 {
216     JsParticleAbility *me = CheckParamsAndGetThis<JsParticleAbility>(engine, info);
217     return (me != nullptr) ? me->JsDisConnectAbility(*engine, *info, AbilityType::UNKNOWN) : nullptr;
218 }
219 
PAStartAbility(NativeEngine * engine,NativeCallbackInfo * info)220 NativeValue* JsParticleAbility::PAStartAbility(NativeEngine *engine, NativeCallbackInfo *info)
221 {
222     JsParticleAbility *me = CheckParamsAndGetThis<JsParticleAbility>(engine, info);
223     return (me != nullptr) ? me->JsStartAbility(*engine, *info, AbilityType::UNKNOWN) : nullptr;
224 }
225 
PATerminateAbility(NativeEngine * engine,NativeCallbackInfo * info)226 NativeValue* JsParticleAbility::PATerminateAbility(NativeEngine *engine, NativeCallbackInfo *info)
227 {
228     JsParticleAbility *me = CheckParamsAndGetThis<JsParticleAbility>(engine, info);
229     return (me != nullptr) ? me->JsTerminateAbility(*engine, *info) : nullptr;
230 }
231 
GetAbility(napi_env env)232 Ability* JsParticleAbility::GetAbility(napi_env env)
233 {
234     napi_status ret;
235     napi_value global = nullptr;
236     const napi_extended_error_info *errorInfo = nullptr;
237     ret = napi_get_global(env, &global);
238     if (ret != napi_ok) {
239         napi_get_last_error_info(env, &errorInfo);
240         HILOG_ERROR("JsParticleAbility::GetAbility, get_global=%{public}d err:%{public}s",
241             ret, errorInfo->error_message);
242         return nullptr;
243     }
244     napi_value abilityObj = nullptr;
245     ret = napi_get_named_property(env, global, "ability", &abilityObj);
246     if (ret != napi_ok) {
247         napi_get_last_error_info(env, &errorInfo);
248         HILOG_ERROR("JsParticleAbility::GetAbility, get_named_property=%{public}d err:%{public}s",
249             ret, errorInfo->error_message);
250         return nullptr;
251     }
252     Ability* ability = nullptr;
253     ret = napi_get_value_external(env, abilityObj, reinterpret_cast<void **>(&ability));
254     if (ret != napi_ok) {
255         napi_get_last_error_info(env, &errorInfo);
256         HILOG_ERROR("JsParticleAbility::GetAbility, get_value_external=%{public}d err:%{public}s",
257             ret, errorInfo->error_message);
258         return nullptr;
259     }
260     return ability;
261 }
262 
JsParticleAbilityInit(NativeEngine * engine,NativeValue * exportObj)263 NativeValue* JsParticleAbilityInit(NativeEngine *engine, NativeValue *exportObj)
264 {
265     HILOG_DEBUG("JsParticleAbility is called");
266 
267     if (engine == nullptr || exportObj == nullptr) {
268         HILOG_ERROR("engine or exportObj null");
269         return nullptr;
270     }
271 
272     NativeObject *object = ConvertNativeValueTo<NativeObject>(exportObj);
273     if (object == nullptr) {
274         HILOG_ERROR("object null");
275         return nullptr;
276     }
277 
278     std::unique_ptr<JsParticleAbility> jsParticleAbility = std::make_unique<JsParticleAbility>();
279     jsParticleAbility->ability_ = jsParticleAbility->GetAbility(reinterpret_cast<napi_env>(engine));
280     object->SetNativePointer(jsParticleAbility.release(), JsParticleAbility::Finalizer, nullptr);
281 
282     HILOG_DEBUG("JsParticleAbility BindNativeFunction called");
283     const char *moduleName = "JsParticleAbility";
284     BindNativeFunction(*engine, *object, "connectAbility", moduleName, JsParticleAbility::PAConnectAbility);
285     BindNativeFunction(*engine, *object, "disconnectAbility", moduleName, JsParticleAbility::PADisConnectAbility);
286     BindNativeFunction(*engine, *object, "startAbility", moduleName, JsParticleAbility::PAStartAbility);
287     BindNativeFunction(*engine, *object, "terminateSelf", moduleName, JsParticleAbility::PATerminateAbility);
288 
289     HILOG_DEBUG("JsParticleAbility end");
290     return exportObj;
291 }
292 }  // namespace AppExecFwk
293 }  // namespace OHOS
294