1 /* 2 * Copyright (c) 2021-2023 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_FORM_FWK_NAPI_FORM_UTIL_H 17 #define OHOS_FORM_FWK_NAPI_FORM_UTIL_H 18 19 #include "ability.h" 20 #include "form_instance.h" 21 #include "form_instances_filter.h" 22 #include "js_runtime_utils.h" 23 #include "napi/native_api.h" 24 #include "napi/native_common.h" 25 #include "napi/native_node_api.h" 26 #include "nlohmann/json.hpp" 27 #include "running_form_info.h" 28 29 namespace OHOS { 30 namespace AbilityRuntime { 31 const int32_t ERR_COMMON = 1; 32 const int32_t ERR_PERMISSION_DENY = 2; 33 const int32_t ERR_GET_INFO_FAILED = 4; 34 const int32_t ERR_GET_BUNDLE_FAILED = 5; 35 const int32_t ERR_GET_LAYOUT_FAILED = 6; 36 const int32_t ERR_ADD_INVALID_PARAM = 7; 37 const int32_t ERR_CFG_NOT_MATCH_ID = 8; 38 const int32_t ERR_NOT_EXIST_ID = 9; 39 const int32_t ERR_BIND_PROVIDER_FAILED = 10; 40 const int32_t ERR_MAX_SYSTEM_FORMS = 11; 41 const int32_t ERR_MAX_INSTANCES_PER_FORM = 12; 42 const int32_t ERR_OPERATION_FORM_NOT_SELF = 13; 43 const int32_t ERR_PROVIDER_DEL_FAIL = 14; 44 const int32_t ERR_MAX_FORMS_PER_CLIENT = 15; 45 const int32_t ERR_MAX_SYSTEM_TEMP_FORMS = 16; 46 const int32_t ERR_FORM_NO_SUCH_MODULE = 17; 47 const int32_t ERR_FORM_NO_SUCH_ABILITY = 18; 48 const int32_t ERR_FORM_NO_SUCH_DIMENSION = 19; 49 const int32_t ERR_FORM_FA_NOT_INSTALLED = 20; 50 const int32_t ERR_SYSTEM_RESPONSES_FAILED = 30; 51 const int32_t ERR_FORM_DUPLICATE_ADDED = 31; 52 const int32_t ERR_IN_RECOVERY = 36; 53 const int32_t ERR_DISTRIBUTED_SCHEDULE_FAILED = 37; 54 55 const int32_t CALLBACK_RETURN_MSG_SIZE = 2; 56 57 struct AsyncCallbackInfoBase { 58 napi_env env = nullptr; 59 napi_async_work asyncWork = nullptr; 60 napi_deferred deferred = nullptr; 61 napi_ref callback = nullptr; 62 AsyncCallbackInfoBaseAsyncCallbackInfoBase63 explicit AsyncCallbackInfoBase(napi_env env) : env(env) {}; 64 virtual ~AsyncCallbackInfoBase() = default;; 65 }; 66 67 struct AsyncErrMsgCallbackInfo { 68 napi_env env; 69 napi_async_work asyncWork; 70 napi_deferred deferred; 71 napi_ref callback; 72 napi_value callbackValue; 73 int code; 74 int type; 75 }; 76 77 struct NapiParamPackage { 78 napi_env env; 79 size_t argc; 80 napi_value* argv; 81 NapiParamPackageNapiParamPackage82 NapiParamPackage(napi_env env, size_t argc, napi_value* argv) 83 { 84 this->env = env; 85 this->argc = argc; 86 this->argv = argv; 87 } 88 }; 89 90 class NapiFormUtil { 91 public: 92 static bool Throw(napi_env env, int32_t errCode, const std::string &errMessage); 93 94 static bool ThrowByInternalErrorCode(napi_env env, int32_t internalErrorCode); 95 96 static bool ThrowByExternalErrorCode(napi_env env, int32_t externalErrorCode); 97 98 static napi_value CreateErrorByInternalErrorCode(napi_env env, int32_t internalErrorCode); 99 100 static napi_value CreateErrorByExternalErrorCode(napi_env env, int32_t externalErrorCode); 101 102 static bool ThrowParamTypeError(napi_env env, const std::string ¶mName, const std::string &type); 103 104 static bool ThrowParamNumError(napi_env env, const std::string &gotNum, const std::string &expectedNum); 105 106 static bool ThrowParamError(napi_env env, const std::string &extraMessage); 107 108 private: 109 static std::string CreateParamTypeErrorMessage(const std::string ¶mName, const std::string &type); 110 }; 111 112 std::string QueryRetMsg(int32_t errorCode); 113 int32_t QueryRetCode(int32_t innerErrorCode); 114 napi_value NapiGetResult(napi_env env, int iResult); 115 bool ConvertStringToInt64(const std::string &strInfo, int64_t &int64Value); 116 void InnerCreateCallbackRetMsg(napi_env env, int32_t code, napi_value (&result)[CALLBACK_RETURN_MSG_SIZE]); 117 void InnerCreatePromiseRetMsg(napi_env env, int32_t code, napi_value* result); 118 napi_value RetErrMsg(AsyncErrMsgCallbackInfo* asyncCallbackInfo); 119 napi_value RetErrMsgForCallback(AsyncErrMsgCallbackInfo* asyncCallbackInfo); 120 napi_value RetErrMsgForPromise(AsyncErrMsgCallbackInfo* asyncCallbackInfo); 121 void ParseFormInfoIntoNapi(napi_env env, const OHOS::AppExecFwk::FormInfo &formInfo, napi_value &result); 122 void ParseRunningFormInfoIntoNapi(napi_env env, const AppExecFwk::RunningFormInfo &runningFormInfo, napi_value &result); 123 AsyncErrMsgCallbackInfo *InitErrMsg(napi_env env, int32_t code, int32_t type, napi_value callbackValue); 124 napi_value CreateFormInfos(napi_env env, const std::vector<OHOS::AppExecFwk::FormInfo> &formInfos); 125 napi_value CreateRunningFormInfos(napi_env env, 126 const std::vector<AppExecFwk::RunningFormInfo> &runningFormInfos); 127 napi_value CreateRunningFormInfo(napi_env env, const AppExecFwk::RunningFormInfo &runningFormInfo); 128 napi_value CreateFormInfo(napi_env env, const OHOS::AppExecFwk::FormInfo &formInfo); 129 napi_value CreateRunningFormInfos(napi_env env, 130 const std::vector<AppExecFwk::RunningFormInfo> &runningFormInfos); 131 napi_value CreateRunningFormInfo(napi_env env, const AppExecFwk::RunningFormInfo &runningFormInfo); 132 napi_value CreateFormCustomizeDatas( 133 napi_env env, const std::vector<OHOS::AppExecFwk::FormCustomizeData> &customizeDatas); 134 bool ParseParam(napi_env env, napi_value args, AppExecFwk::FormInstancesFilter &filter); 135 std::string GetStringFromNapi(napi_env env, napi_value value); 136 napi_value CreateFormInstances(napi_env env, const std::vector<AppExecFwk::FormInstance> &formInstances); 137 napi_value CreateFormInstance(napi_env env, const AppExecFwk::FormInstance &formInstance); 138 } // namespace AbilityRuntime 139 } // namespace OHOS 140 #endif /* OHOS_FORM_FWK_NAPI_FORM_UTIL_H */ 141