1 /* 2 * Copyright (C) 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 JS_INITIALIZE_H 17 #define JS_INITIALIZE_H 18 19 #include "ability.h" 20 #include "js_task.h" 21 #include "napi_base_context.h" 22 23 namespace OHOS::Request { 24 static constexpr uint32_t TOKEN_MAX_BYTES = 2048; 25 static constexpr uint32_t TOKEN_MIN_BYTES = 8; 26 static constexpr int ACL_SUCC = 0; 27 static const std::string SA_PERMISSION_RWX = "g:3815:rwx"; 28 static const std::string SA_PERMISSION_X = "g:3815:x"; 29 static const std::string SA_PERMISSION_CLEAN = "g:3815:---"; 30 static const std::string AREA1 = "el1"; 31 static const std::string AREA2 = "el2"; 32 class JsInitialize { 33 public: 34 JsInitialize() = default; 35 ~JsInitialize() = default; 36 37 static napi_value Initialize(napi_env env, napi_callback_info info, Version version, bool firstInit = true); 38 static void CreatProperties(napi_env env, napi_value &self, napi_value config, JsTask *task); 39 static napi_status GetContext( 40 napi_env env, napi_value value, std::shared_ptr<OHOS::AbilityRuntime::Context> &context); 41 static bool GetBaseDir(std::string &baseDir); 42 static void StringSplit(const std::string &str, const char delim, std::vector<std::string> &elems); 43 static bool CreateDirs(const std::vector<std::string> &pathDirs); 44 45 private: 46 static ExceptionError InitParam( 47 napi_env env, napi_value *argv, std::shared_ptr<OHOS::AbilityRuntime::Context> &context, Config &config); 48 static bool ParseConfig(napi_env env, napi_value jsConfig, Config &config, std::string &errInfo); 49 static bool ParseConfigV9(napi_env env, napi_value jsConfig, Config &config, std::string &errInfo); 50 static void SetParseConfig(napi_env env, napi_value jsConfig, Config &config); 51 static bool ParseUploadConfig(napi_env env, napi_value jsConfig, Config &config, std::string &errInfo); 52 static bool ParseDownloadConfig(napi_env env, napi_value jsConfig, Config &config, std::string &errInfo); 53 static bool ParseAction(napi_env env, napi_value jsConfig, Action &action); 54 static bool ParseUrl(napi_env env, napi_value jsConfig, std::string &url); 55 static bool ParseCertsPath(napi_env env, napi_value jsConfig, std::vector<std::string> &certsPath); 56 static bool ParseData(napi_env env, napi_value jsConfig, Config &config); 57 static bool ParseIndex(napi_env env, napi_value jsConfig, Config &config); 58 static bool ParseName(napi_env env, napi_value jsVal, std::string &name); 59 static bool ParseTitle(napi_env env, napi_value jsConfig, Config &config); 60 static void ParseNetwork(napi_env env, napi_value jsConfig, Network &network); 61 static void ParseMethod(napi_env env, napi_value jsConfig, Config &config); 62 static void ParseRedirect(napi_env env, napi_value jsConfig, bool &redirect); 63 static void ParseRoaming(napi_env env, napi_value jsConfig, Config &config); 64 static void ParseRetry(napi_env env, napi_value jsConfig, bool &retry); 65 static void ParseSaveas(napi_env env, napi_value jsConfig, Config &config); 66 static bool ParseToken(napi_env env, napi_value jsConfig, Config &config); 67 static bool ParseDescription(napi_env env, napi_value jsConfig, std::string &description); 68 static int64_t ParseEnds(napi_env env, napi_value jsConfig); 69 static int64_t ParseBegins(napi_env env, napi_value jsConfig); 70 static uint32_t ParsePriority(napi_env env, napi_value jsConfig); 71 static std::map<std::string, std::string> ParseMap( 72 napi_env env, napi_value jsConfig, const std::string &propertyName); 73 74 static bool GetFormItems( 75 napi_env env, napi_value jsVal, std::vector<FormItem> &forms, std::vector<FileSpec> &files); 76 static bool Convert2FormItems( 77 napi_env env, napi_value jsValue, std::vector<FormItem> &forms, std::vector<FileSpec> &files); 78 static bool Convert2FileSpecs( 79 napi_env env, napi_value jsValue, const std::string &name, std::vector<FileSpec> &files); 80 static bool Convert2FileSpec(napi_env env, napi_value jsValue, const std::string &name, FileSpec &file); 81 static bool GetInternalPath(const std::string &fileUri, 82 const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, Config &config, std::string &filePath); 83 84 static ExceptionError CheckFilePath(const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, Config &config); 85 static ExceptionError CheckUploadBodyFiles(Config &config, const std::string &filePath); 86 static ExceptionError GetFD(const std::string &path, const Config &config, int32_t &fd); 87 static void InterceptData(const std::string &str, const std::string &in, std::string &out); 88 static bool IsStageMode(napi_env env, napi_value value); 89 }; 90 } // namespace OHOS::Request 91 #endif // JS_INITIALIZE_H 92