• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
24 #include "ability_context.h"
25 #include "cj_notify_data_listener.h"
26 #include "cj_request_ffi.h"
27 #include "cj_response_listener.h"
28 #include "constant.h"
29 #include "request_common.h"
30 
31 namespace OHOS::CJSystemapi::Request {
32 using OHOS::Request::Config;
33 using OHOS::Request::ExceptionError;
34 using OHOS::Request::Filter;
35 using OHOS::Request::SubscribeType;
36 using OHOS::Request::TaskInfo;
37 
38 class CJRequestTask {
39 public:
40     CJRequestTask();
41     ~CJRequestTask();
42 
43     static ExceptionError Remove(const std::string &tid);
44     static ExceptionError Touch(const std::string &tid, TaskInfo &task, const std::string &token = "null");
45     static ExceptionError Search(const Filter &filter, std::vector<std::string> &tids);
46 
47     std::mutex listenerMutex_;
48     std::map<SubscribeType, std::shared_ptr<CJNotifyDataListener>> notifyDataListenerMap_;
49     std::shared_ptr<CJResponseListener> responseListener_;
50 
51     Config config_;
52     std::string taskId_{};
53 
54     static std::mutex taskMutex_;
55     static std::map<std::string, CJRequestTask *> taskMap_;
56     static void AddTaskMap(const std::string &key, CJRequestTask *task);
57     static CJRequestTask *FindTaskById(std::string &taskId);
58     static ExceptionError GetTask(OHOS::AbilityRuntime::Context *context, std::string &taskId, std::string &token,
59                                   Config &config);
60     static CJRequestTask *ClearTaskMap(const std::string &key);
61     static void ClearTaskTemp(const std::string &tid, bool isRmFiles, bool isRmAcls, bool isRmCertsAcls);
62 
63     static std::mutex pathMutex_;
64     static std::map<std::string, int32_t> pathMap_;
65     static void AddPathMap(const std::string &filepath, const std::string &baseDir);
66     static void RemovePathMap(const std::string &filepath);
67     static void ResetDirAccess(const std::string &filepath);
68     static void RemoveDirsPermission(const std::vector<std::string> &dirs);
69 
70     static bool register_;
71     static void RegisterForegroundResume();
72 
73     static bool SetPathPermission(const std::string &filepath);
74     static bool SetDirsPermission(std::vector<std::string> &dirs);
75 
76     std::string GetTidStr() const;
77     void SetTid();
78 
79     ExceptionError Create(OHOS::AbilityRuntime::Context *context, Config &config);
80     ExceptionError On(std::string type, std::string &taskId, void *callback);
81     ExceptionError Off(std::string event, CFunc callback);
82 
83     static void ReloadListener();
84 
85 private:
86     std::string tid_;
87 };
88 
89 } // namespace OHOS::CJSystemapi::Request
90 #endif