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_TASK_H 17 #define ANI_TASK_H 18 19 #include <ani.h> 20 #include "i_response_listener.h" 21 #include "i_notify_data_listener.h" 22 #include "listener_list.h" 23 24 namespace OHOS::Request { 25 26 class ResponseListener : 27 public IResponseListener, 28 public std::enable_shared_from_this<ResponseListener>, 29 public ListenerList { 30 public: 31 virtual ~ResponseListener() = default; ResponseListener(ani_vm * vm,std::string tid,SubscribeType type)32 ResponseListener(ani_vm* vm, std::string tid, SubscribeType type) : vm_(vm), tid_(tid), type_(type) 33 { 34 } 35 36 virtual void OnResponseReceive(const std::shared_ptr<Response> &response); 37 void AddListener(ani_ref &callback); 38 39 private: 40 ani_vm *vm_ = nullptr; 41 std::string tid_ = ""; 42 SubscribeType type_ = SubscribeType::FAILED; 43 }; 44 45 class NotifyDataListener : 46 public INotifyDataListener, 47 public std::enable_shared_from_this<NotifyDataListener>, 48 public ListenerList { 49 public: 50 virtual ~NotifyDataListener() = default; NotifyDataListener(ani_vm * vm,std::string tid,SubscribeType type)51 NotifyDataListener(ani_vm *vm, std::string tid, SubscribeType type) : vm_(vm), tid_(tid), type_(type) 52 { 53 } 54 55 virtual void OnNotifyDataReceive(const std::shared_ptr<NotifyData> ¬ifyData); OnFaultsReceive(const std::shared_ptr<int32_t> & tid,const std::shared_ptr<SubscribeType> & type,const std::shared_ptr<Reason> & reason)56 virtual void OnFaultsReceive(const std::shared_ptr<int32_t> &tid, const std::shared_ptr<SubscribeType> &type, 57 const std::shared_ptr<Reason> &reason) 58 { 59 } OnWaitReceive(std::int32_t taskId,WaitingReason reason)60 virtual void OnWaitReceive(std::int32_t taskId, WaitingReason reason) 61 { 62 } 63 void AddListener(ani_ref &callback); 64 65 private: 66 ani_vm *vm_ = nullptr; 67 std::string tid_ = ""; 68 SubscribeType type_ = SubscribeType::FAILED; 69 70 std::shared_ptr<Response> response_ = nullptr; 71 }; 72 73 class AniTask { 74 public: AniTask(const std::string & tid)75 AniTask(const std::string &tid) : tid_(tid) { 76 } 77 78 ~AniTask(); 79 80 static AniTask* Create([[maybe_unused]] ani_env* env, Config config); 81 82 void Start(ani_env *env); 83 void On([[maybe_unused]] ani_env *env, std::string, ani_ref callback); 84 GetTid()85 std::string GetTid() 86 { 87 return tid_; 88 } 89 SetTid(std::string & tid)90 void SetTid(std::string &tid) 91 { 92 tid_ = tid; 93 } 94 95 static bool SetDirsPermission(std::vector<std::string> &dirs); 96 static bool SetPathPermission(const std::string &filepath); 97 static void RemoveDirsPermission(const std::vector<std::string> &dirs); 98 static void AddPathMap(const std::string &filepath, const std::string &baseDir); 99 static void ResetDirAccess(const std::string &filepath); 100 static void RemovePathMap(const std::string &filepath); 101 static void AddTaskMap(const std::string &key, AniTask *task); 102 static void ClearTaskMap(const std::string &key); 103 104 Config config_ = {.action = Action::DOWNLOAD, .version = Version::API8}; 105 bool isGetPermission = false; 106 107 static std::mutex pathMutex_; 108 static std::mutex taskMutex_; 109 static std::map<std::string, AniTask *> taskMap_; 110 static std::map<std::string, int32_t> pathMap_; 111 static std::map<std::string, int32_t> fileMap_; 112 113 private: 114 std::string tid_ = ""; 115 SubscribeType type_ = SubscribeType::FAILED; 116 static std::map<std::string, SubscribeType> supportEventsAni_; 117 118 std::mutex listenerMutex_; 119 std::shared_ptr<ResponseListener> responseListener_; 120 std::map<SubscribeType, std::shared_ptr<NotifyDataListener>> notifyDataListenerMap_; 121 }; 122 123 } // namespace OHOS::Request 124 125 #endif // ANI_TASK_H 126