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