1 /* 2 * Copyright (c) 2021-2022 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 "js_runtime_utils.h" 21 #include "napi/native_api.h" 22 #include "napi/native_common.h" 23 #include "napi/native_node_api.h" 24 #include "nlohmann/json.hpp" 25 26 namespace OHOS { 27 namespace AbilityRuntime { 28 const int32_t ERR_COMMON = 1; 29 const int32_t ERR_PERMISSION_DENY = 2; 30 const int32_t ERR_GET_INFO_FAILED = 4; 31 const int32_t ERR_GET_BUNDLE_FAILED = 5; 32 const int32_t ERR_GET_LAYOUT_FAILED = 6; 33 const int32_t ERR_ADD_INVALID_PARAM = 7; 34 const int32_t ERR_CFG_NOT_MATCH_ID = 8; 35 const int32_t ERR_NOT_EXIST_ID = 9; 36 const int32_t ERR_BIND_PROVIDER_FAILED = 10; 37 const int32_t ERR_MAX_SYSTEM_FORMS = 11; 38 const int32_t ERR_MAX_INSTANCES_PER_FORM = 12; 39 const int32_t ERR_OPERATION_FORM_NOT_SELF = 13; 40 const int32_t ERR_PROVIDER_DEL_FAIL = 14; 41 const int32_t ERR_MAX_FORMS_PER_CLIENT = 15; 42 const int32_t ERR_MAX_SYSTEM_TEMP_FORMS = 16; 43 const int32_t ERR_FORM_NO_SUCH_MODULE = 17; 44 const int32_t ERR_FORM_NO_SUCH_ABILITY = 18; 45 const int32_t ERR_FORM_NO_SUCH_DIMENSION = 19; 46 const int32_t ERR_FORM_FA_NOT_INSTALLED = 20; 47 const int32_t ERR_SYSTEM_RESPONSES_FAILED = 30; 48 const int32_t ERR_FORM_DUPLICATE_ADDED = 31; 49 const int32_t ERR_IN_RECOVERY = 36; 50 const int32_t ERR_DISTRIBUTED_SCHEDULE_FAILED = 37; 51 52 const int32_t CALLBACK_RETURN_MSG_SIZE = 2; 53 54 struct AsyncCallbackInfoBase { 55 napi_env env = nullptr; 56 napi_async_work asyncWork = nullptr; 57 napi_deferred deferred = nullptr; 58 napi_ref callback = nullptr; 59 AsyncCallbackInfoBaseAsyncCallbackInfoBase60 explicit AsyncCallbackInfoBase(napi_env env) : env(env) {}; 61 virtual ~AsyncCallbackInfoBase() = default;; 62 }; 63 64 struct AsyncErrMsgCallbackInfo { 65 napi_env env; 66 napi_async_work asyncWork; 67 napi_deferred deferred; 68 napi_ref callback; 69 napi_value callbackValue; 70 int code; 71 int type; 72 }; 73 74 class NapiFormUtil { 75 public: 76 static bool Throw(NativeEngine &engine, int32_t errCode, const std::string &errMessage); 77 78 static bool ThrowByInternalErrorCode(NativeEngine &engine, int32_t internalErrorCode); 79 80 static NativeValue *CreateErrorByInternalErrorCode(NativeEngine &engine, int32_t internalErrorCode); 81 82 static bool ThrowParamTypeError(NativeEngine &engine, const std::string ¶mName, const std::string &type); 83 84 static bool ThrowParamNumError(NativeEngine &engine, const std::string &gotNum, const std::string &expectedNum); 85 86 static bool ThrowParamError(NativeEngine &engine, const std::string &extraMessage); 87 88 private: 89 static std::string CreateParamTypeErrorMessage(const std::string ¶mName, const std::string &type); 90 }; 91 92 std::string QueryRetMsg(int32_t errorCode); 93 int32_t QueryRetCode(int32_t innerErrorCode); 94 napi_value NapiGetResult(napi_env env, int iResult); 95 bool ConvertStringToInt64(const std::string &strInfo, int64_t &int64Value); 96 void InnerCreateCallbackRetMsg(napi_env env, int32_t code, napi_value (&result)[CALLBACK_RETURN_MSG_SIZE]); 97 void InnerCreatePromiseRetMsg(napi_env env, int32_t code, napi_value* result); 98 napi_value RetErrMsg(AsyncErrMsgCallbackInfo* asyncCallbackInfo); 99 void ParseFormInfoIntoNapi(napi_env env, const OHOS::AppExecFwk::FormInfo &formInfo, napi_value &result); 100 AsyncErrMsgCallbackInfo *InitErrMsg(napi_env env, int32_t code, int32_t type, napi_value callbackValue); 101 NativeValue* CreateFormInfos(NativeEngine &engine, const std::vector<OHOS::AppExecFwk::FormInfo> &formInfos); 102 NativeValue* CreateFormInfo(NativeEngine &engine, const OHOS::AppExecFwk::FormInfo &formInfo); 103 NativeValue *CreateFormCustomizeDatas( 104 NativeEngine &engine, const std::vector<OHOS::AppExecFwk::FormCustomizeData> &customizeDatas); 105 } // namespace AbilityRuntime 106 } // namespace OHOS 107 #endif /* OHOS_FORM_FWK_NAPI_FORM_UTIL_H */ 108