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_REQUEST_TASK_H
17 #define OH_CJ_REQUEST_TASK_H
18 
19 #include <cstdint>
20 #include <map>
21 #include <mutex>
22 #include <vector>
23 #include "ability_context.h"
24 #include "js_common.h"
25 #include "cj_notify_data_listener.h"
26 #include "cj_request_ffi.h"
27 
28 namespace OHOS::CJSystemapi::Request {
29 using OHOS::Request::Config;
30 using OHOS::Request::Network;
31 using OHOS::Request::TaskInfo;
32 using OHOS::Request::NotifyData;
33 using OHOS::Request::Reason;
34 using OHOS::Request::DownloadErrorCode;
35 using OHOS::Request::ExceptionError;
36 using OHOS::Request::SubscribeType;
37 using OHOS::AbilityRuntime::Context;
38 
39 class CJTask {
40 public:
41     CJTask();
42     ~CJTask();
43 
44     static ExceptionError Remove(const std::string &tid);
45 
46     std::mutex listenerMutex_;
47     std::map<SubscribeType, std::shared_ptr<CJNotifyDataListener>> notifyDataListenerMap_;
48 
49     Config config_;
50     std::string taskId_{ };
51 
52     static std::mutex taskMutex_;
53     static std::map<std::string, CJTask *> taskMap_;
54     static void AddTaskMap(const std::string &key, CJTask *task);
55     static CJTask* FindTaskById(std::string &taskId);
56     static CJTask* ClearTaskMap(const std::string &key);
57     static void ClearTaskTemp(const std::string &tid, bool isRmFiles, bool isRmAcls, bool isRmCertsAcls);
58 
59     static std::mutex pathMutex_;
60     static std::map<std::string, int32_t> pathMap_;
61     static void AddPathMap(const std::string &filepath, const std::string &baseDir);
62     static void RemovePathMap(const std::string &filepath);
63     static void ResetDirAccess(const std::string &filepath);
64     static void RemoveDirsPermission(const std::vector<std::string> &dirs);
65 
66     static bool register_;
67     static void RegisterForegroundResume();
68 
69     static bool SetPathPermission(const std::string &filepath);
70     static bool SetDirsPermission(std::vector<std::string> &dirs);
71 
72     std::string GetTidStr() const;
73     void SetTid();
74 
75     ExceptionError Create(OHOS::AbilityRuntime::Context* context, Config &config);
76     ExceptionError On(std::string type, std::string &taskId, void (*callback)(CProgress progress));
77     ExceptionError Off(std::string event, CFunc callback);
78 
79     static void ReloadListener();
80 
81 private:
82     std::string tid_;
83 };
84 
85 }
86 #endif