18e9cee63Sopenharmony_ci/* 28e9cee63Sopenharmony_ci * Copyright (C) 2023 Huawei Device Co., Ltd. 38e9cee63Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 48e9cee63Sopenharmony_ci * you may not use this file except in compliance with the License. 58e9cee63Sopenharmony_ci * You may obtain a copy of the License at 68e9cee63Sopenharmony_ci * 78e9cee63Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 88e9cee63Sopenharmony_ci * 98e9cee63Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 108e9cee63Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 118e9cee63Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 128e9cee63Sopenharmony_ci * See the License for the specific language governing permissions and 138e9cee63Sopenharmony_ci * limitations under the License. 148e9cee63Sopenharmony_ci */ 158e9cee63Sopenharmony_ci 168e9cee63Sopenharmony_ci#ifndef JS_INITIALIZE_H 178e9cee63Sopenharmony_ci#define JS_INITIALIZE_H 188e9cee63Sopenharmony_ci 198e9cee63Sopenharmony_ci#include "ability.h" 208e9cee63Sopenharmony_ci#include "data_ability_helper.h" 218e9cee63Sopenharmony_ci#include "directory_ex.h" 228e9cee63Sopenharmony_ci#include "js_task.h" 238e9cee63Sopenharmony_ci#include "napi_base_context.h" 248e9cee63Sopenharmony_ci 258e9cee63Sopenharmony_cinamespace OHOS::Request { 268e9cee63Sopenharmony_cistatic constexpr uint32_t TOKEN_MAX_BYTES = 2048; 278e9cee63Sopenharmony_cistatic constexpr uint32_t TOKEN_MIN_BYTES = 8; 288e9cee63Sopenharmony_cistatic constexpr int ACL_SUCC = 0; 298e9cee63Sopenharmony_cistatic const std::string SA_PERMISSION_RWX = "g:3815:rwx"; 308e9cee63Sopenharmony_cistatic const std::string SA_PERMISSION_X = "g:3815:x"; 318e9cee63Sopenharmony_cistatic const std::string SA_PERMISSION_CLEAN = "g:3815:---"; 328e9cee63Sopenharmony_cistatic const std::string AREA1 = "/data/storage/el1/base"; 338e9cee63Sopenharmony_cistatic const std::string AREA2 = "/data/storage/el2/base"; 348e9cee63Sopenharmony_cistatic const std::string AREA5 = "/data/storage/el5/base"; 358e9cee63Sopenharmony_ci 368e9cee63Sopenharmony_ciclass JsInitialize { 378e9cee63Sopenharmony_cipublic: 388e9cee63Sopenharmony_ci JsInitialize() = default; 398e9cee63Sopenharmony_ci ~JsInitialize() = default; 408e9cee63Sopenharmony_ci 418e9cee63Sopenharmony_ci static napi_value Initialize(napi_env env, napi_callback_info info, Version version, bool firstInit = true); 428e9cee63Sopenharmony_ci static void CreatProperties(napi_env env, napi_value &self, napi_value config, JsTask *task); 438e9cee63Sopenharmony_ci static napi_status GetContext( 448e9cee63Sopenharmony_ci napi_env env, napi_value value, std::shared_ptr<OHOS::AbilityRuntime::Context> &context); 458e9cee63Sopenharmony_ci static bool GetAppBaseDir(std::string &baseDir); 468e9cee63Sopenharmony_ci static bool CheckBelongAppBaseDir(const std::string &filepath, std::string &baseDir); 478e9cee63Sopenharmony_ci static void StringSplit(const std::string &str, const char delim, std::vector<std::string> &elems); 488e9cee63Sopenharmony_ci static void StringTrim(std::string &str); 498e9cee63Sopenharmony_ci static bool CreateDirs(const std::vector<std::string> &pathDirs); 508e9cee63Sopenharmony_ci static bool FindDir(const std::string &pathDir); 518e9cee63Sopenharmony_ci 528e9cee63Sopenharmony_ciprivate: 538e9cee63Sopenharmony_ci static ExceptionError InitParam( 548e9cee63Sopenharmony_ci napi_env env, napi_value *argv, std::shared_ptr<OHOS::AbilityRuntime::Context> &context, Config &config); 558e9cee63Sopenharmony_ci static bool ParseConfig(napi_env env, napi_value jsConfig, Config &config, std::string &errInfo); 568e9cee63Sopenharmony_ci static bool ParseConfigV9(napi_env env, napi_value jsConfig, Config &config, std::string &errInfo); 578e9cee63Sopenharmony_ci static void SetParseConfig(napi_env env, napi_value jsConfig, Config &config); 588e9cee63Sopenharmony_ci static bool ParseUploadConfig(napi_env env, napi_value jsConfig, Config &config, std::string &errInfo); 598e9cee63Sopenharmony_ci static bool ParseDownloadConfig(napi_env env, napi_value jsConfig, Config &config, std::string &errInfo); 608e9cee63Sopenharmony_ci static bool ParseAction(napi_env env, napi_value jsConfig, Action &action, std::string &errInfo); 618e9cee63Sopenharmony_ci static bool ParseUrl(napi_env env, napi_value jsConfig, std::string &url, std::string &errInfo); 628e9cee63Sopenharmony_ci static bool ParseProxy(napi_env env, napi_value jsConfig, std::string &proxy, std::string &errInfo); 638e9cee63Sopenharmony_ci static bool ParseCertsPath( 648e9cee63Sopenharmony_ci napi_env env, napi_value jsConfig, std::vector<std::string> &certsPath, std::string &errInfo); 658e9cee63Sopenharmony_ci static bool ParseData(napi_env env, napi_value jsConfig, Config &config, std::string &errInfo); 668e9cee63Sopenharmony_ci static bool ParseIndex(napi_env env, napi_value jsConfig, Config &config, std::string &errInfo); 678e9cee63Sopenharmony_ci static bool ParseName(napi_env env, napi_value jsVal, std::string &name); 688e9cee63Sopenharmony_ci static bool ParseTitle(napi_env env, napi_value jsConfig, Config &config, std::string &errInfo); 698e9cee63Sopenharmony_ci static void ParseNetwork(napi_env env, napi_value jsConfig, Network &network); 708e9cee63Sopenharmony_ci static void ParseCertificatePins(napi_env env, std::string &url, std::string &certificatePins); 718e9cee63Sopenharmony_ci static void ParseMethod(napi_env env, napi_value jsConfig, Config &config); 728e9cee63Sopenharmony_ci static void ParseRedirect(napi_env env, napi_value jsConfig, bool &redirect); 738e9cee63Sopenharmony_ci static void ParseRoaming(napi_env env, napi_value jsConfig, Config &config); 748e9cee63Sopenharmony_ci static void ParseRetry(napi_env env, napi_value jsConfig, bool &retry); 758e9cee63Sopenharmony_ci static bool ParseSaveas(napi_env env, napi_value jsConfig, Config &config, std::string &errInfo); 768e9cee63Sopenharmony_ci static bool ParseToken(napi_env env, napi_value jsConfig, Config &config, std::string &errInfo); 778e9cee63Sopenharmony_ci static bool ParseDescription(napi_env env, napi_value jsConfig, std::string &description, std::string &errInfo); 788e9cee63Sopenharmony_ci static int64_t ParseEnds(napi_env env, napi_value jsConfig); 798e9cee63Sopenharmony_ci static int64_t ParseBegins(napi_env env, napi_value jsConfig); 808e9cee63Sopenharmony_ci static uint32_t ParsePriority(napi_env env, napi_value jsConfig); 818e9cee63Sopenharmony_ci static std::map<std::string, std::string> ParseMap( 828e9cee63Sopenharmony_ci napi_env env, napi_value jsConfig, const std::string &propertyName); 838e9cee63Sopenharmony_ci 848e9cee63Sopenharmony_ci static bool GetFormItems( 858e9cee63Sopenharmony_ci napi_env env, napi_value jsVal, std::vector<FormItem> &forms, std::vector<FileSpec> &files); 868e9cee63Sopenharmony_ci static bool Convert2FormItems(napi_env env, napi_value jsValue, std::vector<FormItem> &forms, 878e9cee63Sopenharmony_ci std::vector<FileSpec> &files, std::string &errInfo); 888e9cee63Sopenharmony_ci static bool Convert2FileSpecs( 898e9cee63Sopenharmony_ci napi_env env, napi_value jsValue, const std::string &name, std::vector<FileSpec> &files); 908e9cee63Sopenharmony_ci static bool Convert2FileSpec(napi_env env, napi_value jsValue, const std::string &name, FileSpec &file); 918e9cee63Sopenharmony_ci static bool GetInternalPath(const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, const Config &config, 928e9cee63Sopenharmony_ci std::string &path, std::string &errInfo); 938e9cee63Sopenharmony_ci 948e9cee63Sopenharmony_ci static bool CheckUploadBodyFiles(const std::string &filePath, Config &config, ExceptionError &error); 958e9cee63Sopenharmony_ci static bool CheckPathIsFile(const std::string &path, ExceptionError &error); 968e9cee63Sopenharmony_ci static bool CheckPathOverWrite(const std::string &path, const Config &config, ExceptionError &error); 978e9cee63Sopenharmony_ci static bool GetFdInner(const std::string &path, const Config &config, int32_t &fd, ExceptionError &error); 988e9cee63Sopenharmony_ci static bool GetFdUpload(const std::string &path, const Config &config, int32_t &fd, ExceptionError &error); 998e9cee63Sopenharmony_ci static bool GetFdDownload(const std::string &path, const Config &config, int32_t &fd, ExceptionError &error); 1008e9cee63Sopenharmony_ci static bool InterceptData(const std::string &str, const std::string &in, std::string &out); 1018e9cee63Sopenharmony_ci static bool IsStageMode(napi_env env, napi_value value); 1028e9cee63Sopenharmony_ci static bool CheckDownloadFilePath( 1038e9cee63Sopenharmony_ci const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, Config &config, std::string &errInfo); 1048e9cee63Sopenharmony_ci static bool StandardizePath( 1058e9cee63Sopenharmony_ci const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, const Config &config, std::string &path); 1068e9cee63Sopenharmony_ci static bool BaseToWhole(const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, std::string &path); 1078e9cee63Sopenharmony_ci static bool CacheToWhole(const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, std::string &path); 1088e9cee63Sopenharmony_ci static bool FileToWhole( 1098e9cee63Sopenharmony_ci const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, const Config &config, std::string &path); 1108e9cee63Sopenharmony_ci static bool WholeToNormal(std::string &path, std::vector<std::string> &out); 1118e9cee63Sopenharmony_ci static bool PathVecToNormal(const std::vector<std::string> &in, std::vector<std::string> &out); 1128e9cee63Sopenharmony_ci static bool IsUserFile(const std::string &filePath); 1138e9cee63Sopenharmony_ci static void StandardizeFileSpec(FileSpec &file); 1148e9cee63Sopenharmony_ci static bool GetSandboxPath(const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, const Config &config, 1158e9cee63Sopenharmony_ci std::string &path, std::vector<std::string> &pathVec, std::string &errInfo); 1168e9cee63Sopenharmony_ci static bool CheckUserFileSpec(const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, const Config &config, 1178e9cee63Sopenharmony_ci FileSpec &file, ExceptionError &error); 1188e9cee63Sopenharmony_ci static bool CheckUploadFileSpec(const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, Config &config, 1198e9cee63Sopenharmony_ci FileSpec &file, ExceptionError &error); 1208e9cee63Sopenharmony_ci static bool CheckDownloadFile( 1218e9cee63Sopenharmony_ci const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, Config &config, ExceptionError &error); 1228e9cee63Sopenharmony_ci static bool CheckUploadFiles( 1238e9cee63Sopenharmony_ci const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, Config &config, ExceptionError &error); 1248e9cee63Sopenharmony_ci static bool CheckFilePath( 1258e9cee63Sopenharmony_ci const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, Config &config, ExceptionError &error); 1268e9cee63Sopenharmony_ci}; 1278e9cee63Sopenharmony_ci} // namespace OHOS::Request 1288e9cee63Sopenharmony_ci#endif // JS_INITIALIZE_H 129