1 /* 2 * Copyright (c) 2021 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 #ifndef OHOS_ABILITY_RUNTIME_NAPI_COMMON_ABILITY_H 17 #define OHOS_ABILITY_RUNTIME_NAPI_COMMON_ABILITY_H 18 19 #include <memory> 20 #include <mutex> 21 #include <list> 22 23 #include "ability_connect_callback_stub.h" 24 #include "ability_info.h" 25 #include "ability_manager_errors.h" 26 #include "application_info.h" 27 #include "feature_ability_common.h" 28 29 namespace OHOS { 30 namespace AppExecFwk { 31 const std::int32_t STR_MAX_SIZE = 128; 32 napi_status SetGlobalClassContext(napi_env env, napi_value constructor); 33 napi_value GetGlobalClassContext(napi_env env); 34 35 napi_status SaveGlobalDataAbilityHelper(napi_env env, napi_value constructor); 36 napi_value GetGlobalDataAbilityHelper(napi_env env); 37 bool& GetDataAbilityHelperStatus(); 38 39 napi_value WrapAppInfo(napi_env env, const ApplicationInfo &appInfo); 40 napi_value WrapProperties(napi_env env, const std::vector<std::string> properties, const std::string &proName, 41 napi_value &result); 42 napi_value WrapModuleInfos(napi_env env, const ApplicationInfo &appInfo, napi_value &result); 43 int32_t GetStartAbilityErrorCode(ErrCode innerErrorCode); 44 45 /** 46 * @brief Get Files Dir. 47 * 48 * @param env The environment that the Node-API call is invoked under. 49 * @param info The callback info passed into the callback function. 50 * 51 * @return The return value from NAPI C++ to JS for the module. 52 */ 53 napi_value NAPI_GetFilesDirCommon(napi_env env, napi_callback_info info, AbilityType abilityType); 54 55 /** 56 * @brief Get OrCreateDistribute Dir. 57 * 58 * @param env The environment that the Node-API call is invoked under. 59 * @param info The callback info passed into the callback function. 60 * 61 * @return The return value from NAPI C++ to JS for the module. 62 */ 63 napi_value NAPI_GetOrCreateDistributedDirCommon(napi_env env, napi_callback_info info, AbilityType abilityType); 64 65 napi_value NAPI_GetCacheDirCommon(napi_env env, napi_callback_info info, AbilityType abilityType); 66 67 napi_value NAPI_GetExternalCacheDirCommon(napi_env env, napi_callback_info info, AbilityType abilityType); 68 69 napi_value NAPI_IsUpdatingConfigurationsCommon(napi_env env, napi_callback_info info, AbilityType abilityType); 70 71 napi_value NAPI_PrintDrawnCompletedCommon(napi_env env, napi_callback_info info, AbilityType abilityType); 72 73 /** 74 * @brief Obtains the type of this application. 75 * 76 * @param env The environment that the Node-API call is invoked under. 77 * @param info The callback info passed into the callback function. 78 * 79 * @return The return value from NAPI C++ to JS for the module. 80 */ 81 napi_value NAPI_GetAppTypeCommon(napi_env env, napi_callback_info info, AbilityType abilityType); 82 83 /** 84 * @brief Get the display orientation of the main window. 85 * 86 * @param env The environment that the Node-API call is invoked under. 87 * @param info The callback info passed into the callback function. 88 * 89 * @return The return value from NAPI C++ to JS for the module. 90 */ 91 #ifdef SUPPORT_GRAPHICS 92 napi_value NAPI_GetDisplayOrientationCommon(napi_env env, napi_callback_info info, AbilityType abilityType); 93 bool UnwrapParamGetDisplayOrientationWrap(napi_env env, size_t argc, napi_value *argv, 94 AsyncJSCallbackInfo *asyncCallbackInfo); 95 void GetDisplayOrientationExecuteCallback(napi_env env, void *data); 96 #endif 97 98 /** 99 * @brief Obtains information about the current ability. 100 * 101 * @param env The environment that the Node-API call is invoked under. 102 * @param info The callback info passed into the callback function. 103 * 104 * @return The return value from NAPI C++ to JS for the module. 105 */ 106 napi_value NAPI_GetAbilityInfoCommon(napi_env env, napi_callback_info info, AbilityType abilityType); 107 108 /** 109 * @brief Obtains the HapModuleInfo object of the application. 110 * 111 * @param env The environment that the Node-API call is invoked under. 112 * @param info The callback info passed into the callback function. 113 * 114 * @return The return value from NAPI C++ to JS for the module. 115 */ 116 napi_value NAPI_GetHapModuleInfoCommon(napi_env env, napi_callback_info info, AbilityType abilityType); 117 118 /** 119 * @brief Obtains the AppVersionInfo object of the application. 120 * 121 * @param env The environment that the Node-API call is invoked under. 122 * @param info The callback info passed into the callback function. 123 * 124 * @return The return value from NAPI C++ to JS for the module. 125 */ 126 napi_value NAPI_GetAppVersionInfoCommon(napi_env env, napi_callback_info info, AbilityType abilityType); 127 128 /** 129 * @brief Create asynchronous data. 130 * 131 * @param env The environment that the Node-API call is invoked under. 132 * 133 * @return Return a pointer to AsyncCallbackInfo on success, nullptr on failure 134 */ 135 AsyncCallbackInfo *CreateAsyncCallbackInfo(napi_env env); 136 /** 137 * @brief Get context. 138 * 139 * @param env The environment that the Node-API call is invoked under. 140 * @param info The callback info passed into the callback function. 141 * 142 * @return The return value from NAPI C++ to JS for the module. 143 */ 144 napi_value NAPI_GetContextCommon(napi_env env, napi_callback_info info, AbilityType abilityType); 145 146 /** 147 * @brief Get want. 148 * 149 * @param env The environment that the Node-API call is invoked under. 150 * @param info The callback info passed into the callback function. 151 * 152 * @return The return value from NAPI C++ to JS for the module. 153 */ 154 napi_value NAPI_GetWantCommon(napi_env env, napi_callback_info info, AbilityType abilityType); 155 156 /** 157 * @brief Obtains the class name in this ability name, without the prefixed bundle name. 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 */ 164 napi_value NAPI_GetAbilityNameCommon(napi_env env, napi_callback_info info, AbilityType abilityType); 165 166 /** 167 * @brief startAbility. 168 * 169 * @param env The environment that the Node-API call is invoked under. 170 * @param info The callback info passed into the callback function. 171 * 172 * @return The return value from NAPI C++ to JS for the module. 173 */ 174 napi_value NAPI_StartAbilityCommon(napi_env env, napi_callback_info info, AbilityType abilityType); 175 176 /** 177 * @brief stopAbility. 178 * 179 * @param env The environment that the Node-API call is invoked under. 180 * @param info The callback info passed into the callback function. 181 * 182 * @return The return value from NAPI C++ to JS for the module. 183 */ 184 napi_value NAPI_StopAbilityCommon(napi_env env, napi_callback_info info, AbilityType abilityType); 185 186 /** 187 * @brief Obtains the continue ability Info this application. 188 * 189 * @param env The environment that the Node-API call is invoked under. 190 * @param value The value passed into the info. 191 * @param info The continue ability options info 192 * 193 * @return The return value from NAPI C++ to JS for the module. 194 */ 195 napi_value GetContinueAbilityOptionsInfoCommon( 196 const napi_env &env, const napi_value &value, ContinueAbilityOptionsInfo &info); 197 198 /** 199 * @brief Obtains the continue ability can reversible or not 200 * 201 * @param env The environment that the Node-API call is invoked under. 202 * @param value The value passed into the info. 203 * @param info The continue ability options info 204 * 205 * @return The return value from NAPI C++ to JS for the module. 206 */ 207 napi_value GetContinueAbilityOptionsReversible( 208 const napi_env &env, const napi_value &value, ContinueAbilityOptionsInfo &info); 209 210 /** 211 * @brief Obtains the continue ability Info this application. 212 * 213 * @param env The environment that the Node-API call is invoked under. 214 * @param value The value passed into the info. 215 * @param info The continue ability options info 216 * 217 * @return The return value from NAPI C++ to JS for the module. 218 */ 219 napi_value GetContinueAbilityOptionsDeviceID( 220 const napi_env &env, const napi_value &value, ContinueAbilityOptionsInfo &info); 221 222 bool UnwrapAbilityStartSetting(napi_env env, napi_value param, AAFwk::AbilityStartSetting &setting); 223 224 /** 225 * @brief terminateAbility. 226 * 227 * @param env The environment that the Node-API call is invoked under. 228 * @param info The callback info passed into the callback function. 229 * 230 * @return The return value from NAPI C++ to JS for the module. 231 */ 232 napi_value NAPI_TerminateAbilityCommon(napi_env env, napi_callback_info info); 233 234 /** 235 * @brief TerminateAbility processing function. 236 * 237 * @param env The environment that the Node-API call is invoked under. 238 * @param asyncCallbackInfo Process data asynchronously. 239 * 240 * @return Return JS data successfully, otherwise return nullptr. 241 */ 242 napi_value TerminateAbilityWrap(napi_env env, napi_callback_info info, AsyncCallbackInfo *asyncCallbackInfo); 243 napi_value TerminateAbilityAsync( 244 napi_env env, napi_value *args, const size_t argCallback, AsyncCallbackInfo *asyncCallbackInfo); 245 napi_value TerminateAbilityPromise(napi_env env, AsyncCallbackInfo *asyncCallbackInfo); 246 247 enum { 248 CONNECTION_STATE_DISCONNECTED = -1, 249 250 CONNECTION_STATE_CONNECTED = 0, 251 252 CONNECTION_STATE_CONNECTING = 1 253 }; 254 255 class JsNapiCommon; 256 using ConnectRemoveKeyType = JsNapiCommon*; 257 struct ConnectionCallback { ConnectionCallbackConnectionCallback258 ConnectionCallback(napi_env env, napi_value cbInfo, ConnectRemoveKeyType key) 259 { 260 this->env = env; 261 napi_value jsMethod = nullptr; 262 napi_get_named_property(env, cbInfo, "onConnect", &jsMethod); 263 napi_create_reference(env, jsMethod, 1, &connectCallbackRef); 264 napi_get_named_property(env, cbInfo, "onDisconnect", &jsMethod); 265 napi_create_reference(env, jsMethod, 1, &disconnectCallbackRef); 266 napi_get_named_property(env, cbInfo, "onFailed", &jsMethod); 267 napi_create_reference(env, jsMethod, 1, &failedCallbackRef); 268 removeKey = key; 269 } 270 ConnectionCallback(ConnectionCallback &) = delete; ConnectionCallbackConnectionCallback271 ConnectionCallback(ConnectionCallback &&other) 272 : env(other.env), connectCallbackRef(other.connectCallbackRef), 273 disconnectCallbackRef(other.disconnectCallbackRef), failedCallbackRef(other.failedCallbackRef), 274 removeKey(other.removeKey) 275 { 276 other.env = nullptr; 277 other.connectCallbackRef = nullptr; 278 other.disconnectCallbackRef = nullptr; 279 other.failedCallbackRef = nullptr; 280 other.removeKey = nullptr; 281 } 282 const ConnectionCallback &operator=(ConnectionCallback &) = delete; 283 const ConnectionCallback &operator=(ConnectionCallback &&other) 284 { 285 Reset(); 286 env = other.env; 287 connectCallbackRef = other.connectCallbackRef; 288 disconnectCallbackRef = other.disconnectCallbackRef; 289 failedCallbackRef = other.failedCallbackRef; 290 other.env = nullptr; 291 other.connectCallbackRef = nullptr; 292 other.disconnectCallbackRef = nullptr; 293 other.failedCallbackRef = nullptr; 294 other.removeKey = nullptr; 295 return *this; 296 } ~ConnectionCallbackConnectionCallback297 ~ConnectionCallback() 298 { 299 Reset(); 300 } ResetConnectionCallback301 void Reset() 302 { 303 if (env) { 304 if (connectCallbackRef) { 305 napi_delete_reference(env, connectCallbackRef); 306 connectCallbackRef = nullptr; 307 } 308 if (disconnectCallbackRef) { 309 napi_delete_reference(env, disconnectCallbackRef); 310 disconnectCallbackRef = nullptr; 311 } 312 if (failedCallbackRef) { 313 napi_delete_reference(env, failedCallbackRef); 314 failedCallbackRef = nullptr; 315 } 316 env = nullptr; 317 } 318 removeKey = nullptr; 319 } 320 321 napi_env env = nullptr; 322 napi_ref connectCallbackRef = nullptr; 323 napi_ref disconnectCallbackRef = nullptr; 324 napi_ref failedCallbackRef = nullptr; 325 ConnectRemoveKeyType removeKey = nullptr; 326 }; 327 328 class NAPIAbilityConnection : public AAFwk::AbilityConnectionStub { 329 public: 330 void OnAbilityConnectDone( 331 const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject, int resultCode) override; 332 void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) override; 333 void AddConnectionCallback(std::shared_ptr<ConnectionCallback> callback); 334 void HandleOnAbilityConnectDone(ConnectionCallback &callback, int resultCode); 335 void HandleOnAbilityDisconnectDone(ConnectionCallback &callback, int resultCode); 336 int GetConnectionState() const; 337 void SetConnectionState(int connectionState); 338 size_t GetCallbackSize(); 339 size_t ReomveAllCallbacks(ConnectRemoveKeyType key); 340 341 private: 342 std::list<std::shared_ptr<ConnectionCallback>> callbacks_; 343 AppExecFwk::ElementName element_; 344 sptr<IRemoteObject> serviceRemoteObject_ = nullptr; 345 int connectionState_ = CONNECTION_STATE_DISCONNECTED; 346 mutable std::mutex lock_; 347 }; 348 349 /** 350 * @brief acquireDataAbilityHelper processing function. 351 * 352 * @param env The environment that the Node-API call is invoked under. 353 * @param dataAbilityHelperCB Process data asynchronously. 354 * 355 * @return Return JS data successfully, otherwise return nullptr. 356 */ 357 napi_value AcquireDataAbilityHelperWrap( 358 napi_env env, napi_callback_info info, DataAbilityHelperCB *dataAbilityHelperCB); 359 360 /** 361 * @brief AcquireDataAbilityHelper. 362 * 363 * @param env The environment that the Node-API call is invoked under. 364 * @param info The callback info passed into the callback function. 365 * 366 * @return The return value from NAPI C++ to JS for the module. 367 */ 368 napi_value NAPI_AcquireDataAbilityHelperCommon(napi_env env, napi_callback_info info, AbilityType abilityType); 369 370 napi_value ConvertAbilityInfo(napi_env env, const AbilityInfo &abilityInfo); 371 372 /** 373 * @brief start background running. 374 * 375 * @param env The environment that the Node-API call is invoked under. 376 * @param info The callback info passed into the callback function. 377 * 378 * @return The return value from NAPI C++ to JS for the module. 379 */ 380 napi_value NAPI_StartBackgroundRunningCommon(napi_env env, napi_callback_info info); 381 382 /** 383 * @brief cancel background running. 384 * 385 * @param env The environment that the Node-API call is invoked under. 386 * @param info The callback info passed into the callback function. 387 * 388 * @return The return value from NAPI C++ to JS for the module. 389 */ 390 napi_value NAPI_CancelBackgroundRunningCommon(napi_env env, napi_callback_info info); 391 392 bool CheckAbilityType(const CBBase *cbBase); 393 394 enum ErrorCode { 395 NO_ERROR = 0, 396 INVALID_PARAMETER = -1, 397 ABILITY_NOT_FOUND = -2, 398 PERMISSION_DENY = -3, 399 }; 400 } // namespace AppExecFwk 401 } // namespace OHOS 402 #endif // OHOS_ABILITY_RUNTIME_NAPI_COMMON_ABILITY_H 403