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 #include "ability_connect_callback_stub.h" 19 #include "ability_info.h" 20 #include "ability_manager_errors.h" 21 #include "application_info.h" 22 #include "feature_ability_common.h" 23 24 namespace OHOS { 25 namespace AppExecFwk { 26 const std::int32_t STR_MAX_SIZE = 128; 27 napi_status SetGlobalClassContext(napi_env env, napi_value constructor); 28 napi_value GetGlobalClassContext(napi_env env); 29 30 napi_status SaveGlobalDataAbilityHelper(napi_env env, napi_value constructor); 31 napi_value GetGlobalDataAbilityHelper(napi_env env); 32 bool& GetDataAbilityHelperStatus(); 33 34 napi_value WrapAppInfo(napi_env env, const ApplicationInfo &appInfo); 35 napi_value WrapProperties(napi_env env, const std::vector<std::string> properties, const std::string &proName, 36 napi_value &result); 37 napi_value WrapModuleInfos(napi_env env, const ApplicationInfo &appInfo, napi_value &result); 38 int32_t GetStartAbilityErrorCode(ErrCode innerErrorCode); 39 40 /** 41 * @brief Get Files Dir. 42 * 43 * @param env The environment that the Node-API call is invoked under. 44 * @param info The callback info passed into the callback function. 45 * 46 * @return The return value from NAPI C++ to JS for the module. 47 */ 48 napi_value NAPI_GetFilesDirCommon(napi_env env, napi_callback_info info, AbilityType abilityType); 49 50 /** 51 * @brief Get OrCreateDistribute Dir. 52 * 53 * @param env The environment that the Node-API call is invoked under. 54 * @param info The callback info passed into the callback function. 55 * 56 * @return The return value from NAPI C++ to JS for the module. 57 */ 58 napi_value NAPI_GetOrCreateDistributedDirCommon(napi_env env, napi_callback_info info, AbilityType abilityType); 59 60 napi_value NAPI_GetCacheDirCommon(napi_env env, napi_callback_info info, AbilityType abilityType); 61 62 napi_value NAPI_GetExternalCacheDirCommon(napi_env env, napi_callback_info info, AbilityType abilityType); 63 64 napi_value NAPI_IsUpdatingConfigurationsCommon(napi_env env, napi_callback_info info, AbilityType abilityType); 65 66 napi_value NAPI_PrintDrawnCompletedCommon(napi_env env, napi_callback_info info, AbilityType abilityType); 67 68 /** 69 * @brief Obtains the type of this application. 70 * 71 * @param env The environment that the Node-API call is invoked under. 72 * @param info The callback info passed into the callback function. 73 * 74 * @return The return value from NAPI C++ to JS for the module. 75 */ 76 napi_value NAPI_GetAppTypeCommon(napi_env env, napi_callback_info info, AbilityType abilityType); 77 78 /** 79 * @brief Get the display orientation of the main window. 80 * 81 * @param env The environment that the Node-API call is invoked under. 82 * @param info The callback info passed into the callback function. 83 * 84 * @return The return value from NAPI C++ to JS for the module. 85 */ 86 #ifdef SUPPORT_GRAPHICS 87 napi_value NAPI_GetDisplayOrientationCommon(napi_env env, napi_callback_info info, AbilityType abilityType); 88 bool UnwrapParamGetDisplayOrientationWrap(napi_env env, size_t argc, napi_value *argv, 89 AsyncJSCallbackInfo *asyncCallbackInfo); 90 void GetDisplayOrientationExecuteCallback(napi_env env, void *data); 91 #endif 92 93 /** 94 * @brief Obtains information about the current ability. 95 * 96 * @param env The environment that the Node-API call is invoked under. 97 * @param info The callback info passed into the callback function. 98 * 99 * @return The return value from NAPI C++ to JS for the module. 100 */ 101 napi_value NAPI_GetAbilityInfoCommon(napi_env env, napi_callback_info info, AbilityType abilityType); 102 103 /** 104 * @brief Obtains the HapModuleInfo object of the application. 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 */ 111 napi_value NAPI_GetHapModuleInfoCommon(napi_env env, napi_callback_info info, AbilityType abilityType); 112 113 /** 114 * @brief Obtains the AppVersionInfo object of the application. 115 * 116 * @param env The environment that the Node-API call is invoked under. 117 * @param info The callback info passed into the callback function. 118 * 119 * @return The return value from NAPI C++ to JS for the module. 120 */ 121 napi_value NAPI_GetAppVersionInfoCommon(napi_env env, napi_callback_info info, AbilityType abilityType); 122 123 /** 124 * @brief Create asynchronous data. 125 * 126 * @param env The environment that the Node-API call is invoked under. 127 * 128 * @return Return a pointer to AsyncCallbackInfo on success, nullptr on failure 129 */ 130 AsyncCallbackInfo *CreateAsyncCallbackInfo(napi_env env); 131 /** 132 * @brief Get context. 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 */ 139 napi_value NAPI_GetContextCommon(napi_env env, napi_callback_info info, AbilityType abilityType); 140 141 /** 142 * @brief Get want. 143 * 144 * @param env The environment that the Node-API call is invoked under. 145 * @param info The callback info passed into the callback function. 146 * 147 * @return The return value from NAPI C++ to JS for the module. 148 */ 149 napi_value NAPI_GetWantCommon(napi_env env, napi_callback_info info, AbilityType abilityType); 150 151 /** 152 * @brief Obtains the class name in this ability name, without the prefixed bundle name. 153 * 154 * @param env The environment that the Node-API call is invoked under. 155 * @param info The callback info passed into the callback function. 156 * 157 * @return The return value from NAPI C++ to JS for the module. 158 */ 159 napi_value NAPI_GetAbilityNameCommon(napi_env env, napi_callback_info info, AbilityType abilityType); 160 161 /** 162 * @brief startAbility. 163 * 164 * @param env The environment that the Node-API call is invoked under. 165 * @param info The callback info passed into the callback function. 166 * 167 * @return The return value from NAPI C++ to JS for the module. 168 */ 169 napi_value NAPI_StartAbilityCommon(napi_env env, napi_callback_info info, AbilityType abilityType); 170 171 /** 172 * @brief stopAbility. 173 * 174 * @param env The environment that the Node-API call is invoked under. 175 * @param info The callback info passed into the callback function. 176 * 177 * @return The return value from NAPI C++ to JS for the module. 178 */ 179 napi_value NAPI_StopAbilityCommon(napi_env env, napi_callback_info info, AbilityType abilityType); 180 181 /** 182 * @brief Obtains the continue ability Info this application. 183 * 184 * @param env The environment that the Node-API call is invoked under. 185 * @param value The value passed into the info. 186 * @param info The continue ability options info 187 * 188 * @return The return value from NAPI C++ to JS for the module. 189 */ 190 napi_value GetContinueAbilityOptionsInfoCommon( 191 const napi_env &env, const napi_value &value, ContinueAbilityOptionsInfo &info); 192 193 /** 194 * @brief Obtains the continue ability can reversible or not 195 * 196 * @param env The environment that the Node-API call is invoked under. 197 * @param value The value passed into the info. 198 * @param info The continue ability options info 199 * 200 * @return The return value from NAPI C++ to JS for the module. 201 */ 202 napi_value GetContinueAbilityOptionsReversible( 203 const napi_env &env, const napi_value &value, ContinueAbilityOptionsInfo &info); 204 205 /** 206 * @brief Obtains the continue ability Info this application. 207 * 208 * @param env The environment that the Node-API call is invoked under. 209 * @param value The value passed into the info. 210 * @param info The continue ability options info 211 * 212 * @return The return value from NAPI C++ to JS for the module. 213 */ 214 napi_value GetContinueAbilityOptionsDeviceID( 215 const napi_env &env, const napi_value &value, ContinueAbilityOptionsInfo &info); 216 217 bool UnwrapAbilityStartSetting(napi_env env, napi_value param, AAFwk::AbilityStartSetting &setting); 218 219 /** 220 * @brief terminateAbility. 221 * 222 * @param env The environment that the Node-API call is invoked under. 223 * @param info The callback info passed into the callback function. 224 * 225 * @return The return value from NAPI C++ to JS for the module. 226 */ 227 napi_value NAPI_TerminateAbilityCommon(napi_env env, napi_callback_info info); 228 229 /** 230 * @brief TerminateAbility processing function. 231 * 232 * @param env The environment that the Node-API call is invoked under. 233 * @param asyncCallbackInfo Process data asynchronously. 234 * 235 * @return Return JS data successfully, otherwise return nullptr. 236 */ 237 napi_value TerminateAbilityWrap(napi_env env, napi_callback_info info, AsyncCallbackInfo *asyncCallbackInfo); 238 napi_value TerminateAbilityAsync( 239 napi_env env, napi_value *args, const size_t argCallback, AsyncCallbackInfo *asyncCallbackInfo); 240 napi_value TerminateAbilityPromise(napi_env env, AsyncCallbackInfo *asyncCallbackInfo); 241 242 class NAPIAbilityConnection : public AAFwk::AbilityConnectionStub { 243 public: 244 void OnAbilityConnectDone( 245 const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject, int resultCode) override; 246 void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) override; 247 void SetEnv(const napi_env &env); 248 void SetConnectCBRef(const napi_ref &ref); 249 void SetDisconnectCBRef(const napi_ref &ref); 250 251 private: 252 napi_env env_; 253 napi_ref connectRef_; 254 napi_ref disconnectRef_; 255 }; 256 257 /** 258 * @brief connectAbility. 259 * 260 * @param env The environment that the Node-API call is invoked under. 261 * @param info The callback info passed into the callback function. 262 * 263 * @return The return value from NAPI C++ to JS for the module. 264 */ 265 napi_value NAPI_ConnectAbilityCommon(napi_env env, napi_callback_info info, AbilityType abilityType); 266 267 /** 268 * @brief disconnectAbility. 269 * 270 * @param env The environment that the Node-API call is invoked under. 271 * @param info The callback info passed into the callback function. 272 * 273 * @return The return value from NAPI C++ to JS for the module. 274 */ 275 napi_value NAPI_DisConnectAbilityCommon(napi_env env, napi_callback_info info, AbilityType abilityType); 276 277 /** 278 * @brief acquireDataAbilityHelper processing function. 279 * 280 * @param env The environment that the Node-API call is invoked under. 281 * @param dataAbilityHelperCB Process data asynchronously. 282 * 283 * @return Return JS data successfully, otherwise return nullptr. 284 */ 285 napi_value AcquireDataAbilityHelperWrap( 286 napi_env env, napi_callback_info info, DataAbilityHelperCB *dataAbilityHelperCB); 287 288 /** 289 * @brief AcquireDataAbilityHelper. 290 * 291 * @param env The environment that the Node-API call is invoked under. 292 * @param info The callback info passed into the callback function. 293 * 294 * @return The return value from NAPI C++ to JS for the module. 295 */ 296 napi_value NAPI_AcquireDataAbilityHelperCommon(napi_env env, napi_callback_info info, AbilityType abilityType); 297 298 napi_value ConvertAbilityInfo(napi_env env, const AbilityInfo &abilityInfo); 299 300 /** 301 * @brief start background running. 302 * 303 * @param env The environment that the Node-API call is invoked under. 304 * @param info The callback info passed into the callback function. 305 * 306 * @return The return value from NAPI C++ to JS for the module. 307 */ 308 napi_value NAPI_StartBackgroundRunningCommon(napi_env env, napi_callback_info info); 309 310 /** 311 * @brief cancel background running. 312 * 313 * @param env The environment that the Node-API call is invoked under. 314 * @param info The callback info passed into the callback function. 315 * 316 * @return The return value from NAPI C++ to JS for the module. 317 */ 318 napi_value NAPI_CancelBackgroundRunningCommon(napi_env env, napi_callback_info info); 319 320 bool CheckAbilityType(const CBBase *cbBase); 321 322 struct ConnecttionKey { 323 Want want; 324 int64_t id; 325 }; 326 struct key_compare { operatorkey_compare327 bool operator()(const ConnecttionKey &key1, const ConnecttionKey &key2) const 328 { 329 if (key1.id < key2.id) { 330 return true; 331 } 332 return false; 333 } 334 }; 335 static std::map<ConnecttionKey, sptr<NAPIAbilityConnection>, key_compare> connects_; 336 static int64_t serialNumber_ = 0; 337 enum ErrorCode { 338 NO_ERROR = 0, 339 INVALID_PARAMETER = -1, 340 ABILITY_NOT_FOUND = -2, 341 PERMISSION_DENY = -3, 342 }; 343 } // namespace AppExecFwk 344 } // namespace OHOS 345 #endif // OHOS_ABILITY_RUNTIME_NAPI_COMMON_ABILITY_H 346