• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 ANI_JS_INITIALIZE_H
17 #define ANI_JS_INITIALIZE_H
18 
19 #include <ani.h>
20 #include "ability.h"
21 #include "data_ability_helper.h"
22 #include "directory_ex.h"
23 #include "request_common.h"
24 
25 namespace OHOS::Request {
26 static const std::map<ExceptionErrorCode, std::string> ErrorCodeToMsg{
27     { E_OK, E_OK_INFO },
28     { E_PERMISSION, E_PERMISSION_INFO },
29     { E_PARAMETER_CHECK, E_PARAMETER_CHECK_INFO },
30     { E_UNSUPPORTED, E_UNSUPPORTED_INFO },
31     { E_FILE_IO, E_FILE_IO_INFO },
32     { E_FILE_PATH, E_FILE_PATH_INFO },
33     { E_SERVICE_ERROR, E_SERVICE_ERROR_INFO },
34     { E_OTHER, E_OTHER_INFO },
35 };
36 
37 class JsInitialize {
38 public:
39     JsInitialize() = default;
40     ~JsInitialize() = default;
41 
42     static bool GetAppBaseDir(std::string &baseDir);
43     static bool CheckBelongAppBaseDir(const std::string &filepath, std::string &baseDir);
44     static void StringSplit(const std::string &str, const char delim, std::vector<std::string> &elems);
45     static void StringTrim(std::string &str);
46     static bool CreateDirs(const std::vector<std::string> &pathDirs);
47     static bool FindDir(const std::string &pathDir);
48 
49     static std::shared_ptr<OHOS::AbilityRuntime::Context> GetContext(ani_env *env, ani_object object);
50 
51     static bool GetInternalPath(const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, const Config &config,
52         std::string &path, std::string &errInfo);
53 
54     static bool CheckUploadBodyFiles(const std::string &filePath, Config &config, ExceptionError &error);
55     static bool CheckPathIsFile(const std::string &path, ExceptionError &error);
56     static bool CheckPathOverWrite(const std::string &path, const Config &config, ExceptionError &error);
57     static bool GetFdUpload(const std::string &path, const Config &config, ExceptionError &error);
58     static bool GetFdDownload(const std::string &path, const Config &config, ExceptionError &error);
59     static bool InterceptData(const std::string &str, const std::string &in, std::string &out);
60     static bool CheckDownloadFilePath(
61         const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, Config &config, std::string &errInfo);
62     static bool StandardizePath(
63         const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, const Config &config, std::string &path);
64     static bool BaseToWhole(const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, std::string &path);
65     static bool CacheToWhole(const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, std::string &path);
66     static bool FileToWhole(
67         const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, const Config &config, std::string &path);
68     static bool WholeToNormal(std::string &path, std::vector<std::string> &out);
69     static bool PathVecToNormal(const std::vector<std::string> &in, std::vector<std::string> &out);
70     static bool IsUserFile(const std::string &filePath);
71     static void StandardizeFileSpec(FileSpec &file);
72     static bool GetSandboxPath(const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, const Config &config,
73         std::string &path, std::vector<std::string> &pathVec, std::string &errInfo);
74     static bool CheckUserFileSpec(const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, const Config &config,
75         FileSpec &file, ExceptionError &error);
76     static bool CheckUploadFileSpec(const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, Config &config,
77         FileSpec &file, ExceptionError &error);
78     static bool CheckDownloadFile(
79         const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, Config &config, ExceptionError &error);
80     static bool CheckUploadFiles(
81         const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, Config &config, ExceptionError &error);
82     static bool CheckFilePath(
83         const std::shared_ptr<OHOS::AbilityRuntime::Context> &context, Config &config, ExceptionError &error);
84 
85     static bool Convert2FileSpecs(
86         ani_env *env, ani_object aniValue, const std::string &name, std::vector<FileSpec> &files);
87     static bool Convert2FileSpec(ani_env *env, ani_object aniValue, const std::string &name, FileSpec &file);
88 };
89 }
90 #endif // ANI_JS_INITIALIZE_H