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