1 /* 2 * Copyright (C) 2024 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 OH_CJ_INITIALIZE_H 17 #define OH_CJ_INITIALIZE_H 18 19 #include <vector> 20 #include "ability.h" 21 #include "cj_request_ffi.h" 22 #include "constant.h" 23 #include "directory_ex.h" 24 #include "js_common.h" 25 #include "napi_base_context.h" 26 27 namespace OHOS::CJSystemapi::Request { 28 using OHOS::AbilityRuntime::Context; 29 using OHOS::Request::Config; 30 using OHOS::Request::ExceptionError; 31 using OHOS::Request::FileSpec; 32 using OHOS::Request::FormItem; 33 using OHOS::Request::Mode; 34 using OHOS::Request::Network; 35 36 static constexpr uint32_t TOKEN_MAX_BYTES = 2048; 37 static constexpr uint32_t TOKEN_MIN_BYTES = 8; 38 39 class CJInitialize { 40 public: 41 CJInitialize() = default; 42 ~CJInitialize() = default; 43 44 static void StringSplit(const std::string &str, const char delim, std::vector<std::string> &elems); 45 static bool GetBaseDir(std::string &baseDir); 46 47 static ExceptionError ParseConfig(OHOS::AbilityRuntime::Context *context, const CConfig *ffiConfig, Config &config); 48 static ExceptionError ParseBundleName(const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, 49 std::string &config); 50 static bool ParseUrl(std::string &url); 51 static bool ParseCertsPath(std::string &url, std::vector<std::string> &certsPath); 52 static bool ParseFormItems(const CFormItemArr *cForms, std::vector<FormItem> &forms, std::vector<FileSpec> &files); 53 static bool ParseData(const CConfig *config, Config &out); 54 static bool Convert2FileSpec(const CFileSpec *cFile, const char *name, FileSpec &file); 55 static bool Convert2FileSpecs(const CFileSpecArr *cFiles, const char *name, std::vector<FileSpec> &files); 56 static bool ParseIndex(Config &config); 57 static int64_t ParseBegins(int64_t &begins); 58 static bool ParseTitle(Config &config); 59 static bool ParseToken(Config &config, std::string &errInfo); 60 static bool ParseDescription(std::string &description); 61 static bool ParseSaveas(Config &config); 62 static void ParseMethod(Config &config); 63 static void ParseNetwork(Network &network); 64 static void ParseBackGround(Mode mode, bool &background); 65 66 static ExceptionError CheckFileSpec(const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, Config &config); 67 static ExceptionError CheckFilePath(const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, Config &config); 68 static bool CheckPathBaseDir(const std::string &filepath, std::string &baseDir); 69 static bool CreateDirs(const std::vector<std::string> &pathDirs); 70 static bool InterceptData(const std::string &str, const std::string &in, std::string &out); 71 static bool GetInternalPath(const std::string &fileUri, 72 const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, Config &config, 73 std::string &filePath); 74 static ExceptionError GetFD(const std::string &path, const Config &config, int32_t &fd); 75 static bool FindDir(const std::string &pathDir); 76 77 private: 78 static bool CheckDownloadFilePath(const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, Config &config, 79 std::string &errInfo); 80 static bool StandardizePath(const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, const Config &config, 81 std::string &path); 82 static bool CacheToWhole(const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, std::string &path); 83 static bool FileToWhole(const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, const Config &config, 84 std::string &path); 85 static bool PathVecToNormal(const std::vector<std::string> &in, std::vector<std::string> &out); 86 static bool WholeToNormal(const std::string &wholePath, std::string &normalPath, std::vector<std::string> &out); 87 static ExceptionError CheckUploadBodyFiles(Config &config, const std::string &filePath); 88 static ExceptionError UploadBodyFileProc(std::string &fileName, Config &config); 89 }; 90 } // namespace OHOS::CJSystemapi::Request 91 #endif // CJ_INITIALIZE_H 92