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 stopAbility. 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_StopAbilityCommon(napi_env env, napi_callback_info info, AbilityType abilityType); 175 176 /** 177 * @brief Obtains the continue ability Info this application. 178 * 179 * @param env The environment that the Node-API call is invoked under. 180 * @param value The value passed into the info. 181 * @param info The continue ability options info 182 * 183 * @return The return value from NAPI C++ to JS for the module. 184 */ 185 napi_value GetContinueAbilityOptionsInfoCommon( 186 const napi_env &env, const napi_value &value, ContinueAbilityOptionsInfo &info); 187 188 /** 189 * @brief Obtains the continue ability can reversible or not 190 * 191 * @param env The environment that the Node-API call is invoked under. 192 * @param value The value passed into the info. 193 * @param info The continue ability options info 194 * 195 * @return The return value from NAPI C++ to JS for the module. 196 */ 197 napi_value GetContinueAbilityOptionsReversible( 198 const napi_env &env, const napi_value &value, ContinueAbilityOptionsInfo &info); 199 200 /** 201 * @brief Obtains the continue ability Info this application. 202 * 203 * @param env The environment that the Node-API call is invoked under. 204 * @param value The value passed into the info. 205 * @param info The continue ability options info 206 * 207 * @return The return value from NAPI C++ to JS for the module. 208 */ 209 napi_value GetContinueAbilityOptionsDeviceID( 210 const napi_env &env, const napi_value &value, ContinueAbilityOptionsInfo &info); 211 212 bool UnwrapAbilityStartSetting(napi_env env, napi_value param, AAFwk::AbilityStartSetting &setting); 213 214 enum { 215 CONNECTION_STATE_DISCONNECTED = -1, 216 217 CONNECTION_STATE_CONNECTED = 0, 218 219 CONNECTION_STATE_CONNECTING = 1 220 }; 221 222 class JsNapiCommon; 223 using ConnectRemoveKeyType = JsNapiCommon*; 224 struct ConnectionCallback { ConnectionCallbackConnectionCallback225 ConnectionCallback(napi_env env, napi_value cbInfo, ConnectRemoveKeyType key) 226 { 227 this->env = env; 228 napi_value jsMethod = nullptr; 229 napi_get_named_property(env, cbInfo, "onConnect", &jsMethod); 230 napi_create_reference(env, jsMethod, 1, &connectCallbackRef); 231 napi_get_named_property(env, cbInfo, "onDisconnect", &jsMethod); 232 napi_create_reference(env, jsMethod, 1, &disconnectCallbackRef); 233 napi_get_named_property(env, cbInfo, "onFailed", &jsMethod); 234 napi_create_reference(env, jsMethod, 1, &failedCallbackRef); 235 removeKey = key; 236 } 237 ConnectionCallback(ConnectionCallback &) = delete; ConnectionCallbackConnectionCallback238 ConnectionCallback(ConnectionCallback &&other) 239 : env(other.env), connectCallbackRef(other.connectCallbackRef), 240 disconnectCallbackRef(other.disconnectCallbackRef), failedCallbackRef(other.failedCallbackRef), 241 removeKey(other.removeKey) 242 { 243 other.env = nullptr; 244 other.connectCallbackRef = nullptr; 245 other.disconnectCallbackRef = nullptr; 246 other.failedCallbackRef = nullptr; 247 other.removeKey = nullptr; 248 } 249 const ConnectionCallback &operator=(ConnectionCallback &) = delete; 250 const ConnectionCallback &operator=(ConnectionCallback &&other) 251 { 252 Reset(); 253 env = other.env; 254 connectCallbackRef = other.connectCallbackRef; 255 disconnectCallbackRef = other.disconnectCallbackRef; 256 failedCallbackRef = other.failedCallbackRef; 257 other.env = nullptr; 258 other.connectCallbackRef = nullptr; 259 other.disconnectCallbackRef = nullptr; 260 other.failedCallbackRef = nullptr; 261 other.removeKey = nullptr; 262 return *this; 263 } ~ConnectionCallbackConnectionCallback264 ~ConnectionCallback() 265 { 266 Reset(); 267 } ResetConnectionCallback268 void Reset() 269 { 270 if (env) { 271 if (connectCallbackRef) { 272 napi_delete_reference(env, connectCallbackRef); 273 connectCallbackRef = nullptr; 274 } 275 if (disconnectCallbackRef) { 276 napi_delete_reference(env, disconnectCallbackRef); 277 disconnectCallbackRef = nullptr; 278 } 279 if (failedCallbackRef) { 280 napi_delete_reference(env, failedCallbackRef); 281 failedCallbackRef = nullptr; 282 } 283 env = nullptr; 284 } 285 removeKey = nullptr; 286 } 287 288 napi_env env = nullptr; 289 napi_ref connectCallbackRef = nullptr; 290 napi_ref disconnectCallbackRef = nullptr; 291 napi_ref failedCallbackRef = nullptr; 292 ConnectRemoveKeyType removeKey = nullptr; 293 }; 294 295 class NAPIAbilityConnection : public AAFwk::AbilityConnectionStub { 296 public: 297 void OnAbilityConnectDone( 298 const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject, int resultCode) override; 299 void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) override; 300 void AddConnectionCallback(std::shared_ptr<ConnectionCallback> callback); 301 void HandleOnAbilityConnectDone(ConnectionCallback &callback, int resultCode); 302 void HandleOnAbilityDisconnectDone(ConnectionCallback &callback, int resultCode); 303 int GetConnectionState() const; 304 void SetConnectionState(int connectionState); 305 size_t GetCallbackSize(); 306 size_t RemoveAllCallbacks(ConnectRemoveKeyType key); 307 308 private: 309 std::list<std::shared_ptr<ConnectionCallback>> callbacks_; 310 AppExecFwk::ElementName element_; 311 sptr<IRemoteObject> serviceRemoteObject_ = nullptr; 312 int connectionState_ = CONNECTION_STATE_DISCONNECTED; 313 mutable std::mutex lock_; 314 }; 315 316 /** 317 * @brief acquireDataAbilityHelper processing function. 318 * 319 * @param env The environment that the Node-API call is invoked under. 320 * @param dataAbilityHelperCB Process data asynchronously. 321 * 322 * @return Return JS data successfully, otherwise return nullptr. 323 */ 324 napi_value AcquireDataAbilityHelperWrap( 325 napi_env env, napi_callback_info info, DataAbilityHelperCB *dataAbilityHelperCB); 326 327 /** 328 * @brief AcquireDataAbilityHelper. 329 * 330 * @param env The environment that the Node-API call is invoked under. 331 * @param info The callback info passed into the callback function. 332 * 333 * @return The return value from NAPI C++ to JS for the module. 334 */ 335 napi_value NAPI_AcquireDataAbilityHelperCommon(napi_env env, napi_callback_info info, AbilityType abilityType); 336 337 napi_value ConvertAbilityInfo(napi_env env, const AbilityInfo &abilityInfo); 338 339 /** 340 * @brief start background running. 341 * 342 * @param env The environment that the Node-API call is invoked under. 343 * @param info The callback info passed into the callback function. 344 * 345 * @return The return value from NAPI C++ to JS for the module. 346 */ 347 napi_value NAPI_StartBackgroundRunningCommon(napi_env env, napi_callback_info info); 348 349 /** 350 * @brief cancel background running. 351 * 352 * @param env The environment that the Node-API call is invoked under. 353 * @param info The callback info passed into the callback function. 354 * 355 * @return The return value from NAPI C++ to JS for the module. 356 */ 357 napi_value NAPI_CancelBackgroundRunningCommon(napi_env env, napi_callback_info info); 358 359 bool CheckAbilityType(const CBBase *cbBase); 360 361 struct ConnectionKey { 362 Want want; 363 int64_t id; 364 }; 365 struct key_compare { operatorkey_compare366 bool operator()(const ConnectionKey &key1, const ConnectionKey &key2) const 367 { 368 if (key1.id < key2.id) { 369 return true; 370 } 371 return false; 372 } 373 }; 374 static std::map<ConnectionKey, sptr<NAPIAbilityConnection>, key_compare> connects_; 375 static std::mutex g_connectionsLock_; 376 static int64_t serialNumber_ = 0; 377 enum ErrorCode { 378 NO_ERROR = 0, 379 INVALID_PARAMETER = -1, 380 ABILITY_NOT_FOUND = -2, 381 PERMISSION_DENY = -3, 382 }; 383 } // namespace AppExecFwk 384 } // namespace OHOS 385 #endif // OHOS_ABILITY_RUNTIME_NAPI_COMMON_ABILITY_H 386