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 58 struct AsyncCallbackInfoBase { 59 napi_env env = nullptr; 60 napi_async_work asyncWork = nullptr; 61 napi_deferred deferred = nullptr; 62 napi_ref callback = nullptr; 63 AsyncCallbackInfoBaseAsyncCallbackInfoBase64 explicit AsyncCallbackInfoBase(napi_env env) : env(env) {}; 65 virtual ~AsyncCallbackInfoBase() = default;; 66 }; 67 68 struct AsyncErrMsgCallbackInfo { 69 napi_env env; 70 napi_async_work asyncWork; 71 napi_deferred deferred; 72 napi_ref callback; 73 napi_value callbackValue; 74 int code; 75 int type; 76 }; 77 78 struct NapiParamPackage { 79 napi_env env; 80 size_t argc; 81 napi_value* argv; 82 NapiParamPackageNapiParamPackage83 NapiParamPackage(napi_env env, size_t argc, napi_value* argv) 84 { 85 this->env = env; 86 this->argc = argc; 87 this->argv = argv; 88 } 89 }; 90 91 class NapiFormUtil { 92 public: 93 static bool Throw(napi_env env, int32_t errCode, const std::string &errMessage); 94 95 static bool ThrowByInternalErrorCode(napi_env env, int32_t internalErrorCode); 96 97 static bool ThrowByExternalErrorCode(napi_env env, int32_t externalErrorCode); 98 99 static napi_value CreateErrorByInternalErrorCode(napi_env env, int32_t internalErrorCode); 100 101 static napi_value CreateErrorByExternalErrorCode(napi_env env, int32_t externalErrorCode); 102 103 static bool ThrowParamTypeError(napi_env env, const std::string ¶mName, const std::string &type); 104 105 static bool ThrowParamNumError(napi_env env, const std::string &gotNum, const std::string &expectedNum); 106 107 static bool ThrowParamError(napi_env env, const std::string &extraMessage); 108 109 private: 110 static std::string CreateParamTypeErrorMessage(const std::string ¶mName, const std::string &type); 111 }; 112 113 std::string QueryRetMsg(int32_t errorCode); 114 int32_t QueryRetCode(int32_t innerErrorCode); 115 napi_value NapiGetResult(napi_env env, int iResult); 116 bool ConvertStringToInt64(const std::string &strInfo, int64_t &int64Value); 117 void InnerCreateCallbackRetMsg(napi_env env, int32_t code, napi_value (&result)[CALLBACK_RETURN_MSG_SIZE]); 118 void InnerCreatePromiseRetMsg(napi_env env, int32_t code, napi_value* result); 119 napi_value RetErrMsg(AsyncErrMsgCallbackInfo* asyncCallbackInfo); 120 napi_value RetErrMsgForCallback(AsyncErrMsgCallbackInfo* asyncCallbackInfo); 121 napi_value RetErrMsgForPromise(AsyncErrMsgCallbackInfo* asyncCallbackInfo); 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 bool ConvertFormInfoFilter(napi_env env, napi_value value, AppExecFwk::FormInfoFilter &formInfoFilter); 139 } // namespace AbilityRuntime 140 } // namespace OHOS 141 #endif /* OHOS_FORM_FWK_NAPI_FORM_UTIL_H */ 142