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 OHOS_REQUEST_DOWNLOAD_MANAGER_IMPL_H 17 #define OHOS_REQUEST_DOWNLOAD_MANAGER_IMPL_H 18 19 #include <atomic> 20 #include <condition_variable> 21 #include <map> 22 #include <mutex> 23 #include <vector> 24 25 #include "constant.h" 26 #include "i_notify_data_listener.h" 27 #include "i_response_message_handler.h" 28 #include "iremote_object.h" 29 #include "iservice_registry.h" 30 #include "log.h" 31 #include "refbase.h" 32 #include "request.h" 33 #include "request_common.h" 34 #include "request_service_interface.h" 35 #include "response_message_receiver.h" 36 #include "system_ability_status_change_stub.h" 37 #include "visibility.h" 38 39 namespace OHOS::Request { 40 41 constexpr int RETRY_TIMES = 5; 42 43 class RequestManagerImpl : public IResponseMessageHandler { 44 public: 45 static const std::unique_ptr<RequestManagerImpl> &GetInstance(); 46 ExceptionErrorCode CreateTasks(const std::vector<Config> &configs, std::vector<TaskRet> &rets); 47 ExceptionErrorCode StartTasks(const std::vector<std::string> &tids, std::vector<ExceptionErrorCode> &rets); 48 ExceptionErrorCode StopTasks(const std::vector<std::string> &tids, std::vector<ExceptionErrorCode> &rets); 49 ExceptionErrorCode ResumeTasks(const std::vector<std::string> &tids, std::vector<ExceptionErrorCode> &rets); 50 ExceptionErrorCode RemoveTasks( 51 const std::vector<std::string> &tids, const Version version, std::vector<ExceptionErrorCode> &rets); 52 ExceptionErrorCode PauseTasks( 53 const std::vector<std::string> &tids, const Version version, std::vector<ExceptionErrorCode> &rets); 54 ExceptionErrorCode QueryTasks(const std::vector<std::string> &tids, std::vector<TaskInfoRet> &rets); 55 ExceptionErrorCode ShowTasks(const std::vector<std::string> &tids, std::vector<TaskInfoRet> &rets); 56 ExceptionErrorCode TouchTasks(const std::vector<TaskIdAndToken> &tids, std::vector<TaskInfoRet> &rets); 57 ExceptionErrorCode SetMaxSpeeds(const std::vector<SpeedConfig> &speedConfig, std::vector<ExceptionErrorCode> &rets); 58 ExceptionErrorCode SetMode(const std::string &tid, const Mode mode); 59 ExceptionErrorCode DisableTaskNotification( 60 const std::vector<std::string> &tids, std::vector<ExceptionErrorCode> &rets); 61 62 int32_t Create(const Config &config, int32_t seq, std::string &tid); 63 int32_t GetTask(const std::string &tid, const std::string &token, Config &config); 64 int32_t Start(const std::string &tid); 65 int32_t Stop(const std::string &tid); 66 int32_t Query(const std::string &tid, TaskInfo &info); 67 int32_t Touch(const std::string &tid, const std::string &token, TaskInfo &info); 68 int32_t Search(const Filter &filter, std::vector<std::string> &tids); 69 int32_t Show(const std::string &tid, TaskInfo &info); 70 int32_t Pause(const std::string &tid, const Version version); 71 int32_t QueryMimeType(const std::string &tid, std::string &mimeType); 72 int32_t Remove(const std::string &tid, const Version version); 73 int32_t Resume(const std::string &tid); 74 int32_t SetMaxSpeed(const std::string &tid, const int64_t maxSpeed); 75 76 int32_t Subscribe(const std::string &taskId); 77 int32_t Unsubscribe(const std::string &taskId); 78 79 int32_t AddListener( 80 const std::string &taskId, const SubscribeType &type, const std::shared_ptr<IResponseListener> &listener); 81 int32_t RemoveListener( 82 const std::string &taskId, const SubscribeType &type, const std::shared_ptr<IResponseListener> &listener); 83 int32_t AddListener( 84 const std::string &taskId, const SubscribeType &type, const std::shared_ptr<INotifyDataListener> &listener); 85 int32_t RemoveListener( 86 const std::string &taskId, const SubscribeType &type, const std::shared_ptr<INotifyDataListener> &listener); 87 void RemoveAllListeners(const std::string &taskId); 88 89 int32_t SubRunCount(const sptr<NotifyInterface> &listener); 90 int32_t UnsubRunCount(); 91 92 void RestoreListener(void (*callback)()); 93 void RestoreSubRunCount(); 94 void LoadRequestServer(); 95 bool IsSaReady(); 96 void ReopenChannel(); 97 int32_t GetNextSeq(); 98 bool SubscribeSA(); 99 bool UnsubscribeSA(); 100 int32_t CreateGroup( 101 std::string &gid, const bool gauge, Notification ¬ification); 102 int32_t AttachGroup(const std::string &gid, const std::vector<std::string> &tids); 103 int32_t DeleteGroup(const std::string &gid); 104 105 private: 106 RequestManagerImpl() = default; 107 RequestManagerImpl(const RequestManagerImpl &) = delete; 108 RequestManagerImpl(RequestManagerImpl &&) = delete; 109 RequestManagerImpl &operator=(const RequestManagerImpl &) = delete; 110 sptr<RequestServiceInterface> GetRequestServiceProxy(bool load); 111 int32_t EnsureChannelOpen(); 112 std::shared_ptr<Request> GetTask(const std::string &taskId); 113 void OnChannelBroken() override; 114 void OnResponseReceive(const std::shared_ptr<Response> &response) override; 115 void OnNotifyDataReceive(const std::shared_ptr<NotifyData> ¬ifyData) override; 116 void OnFaultsReceive(const std::shared_ptr<int32_t> &tid, const std::shared_ptr<SubscribeType> &type, 117 const std::shared_ptr<Reason> &reason) override; 118 void OnWaitReceive(std::int32_t taskId, WaitingReason reason) override; 119 120 private: 121 std::mutex serviceProxyMutex_; 122 std::mutex saChangeListenerMutex_; 123 124 sptr<RequestServiceInterface> requestServiceProxy_; 125 sptr<ISystemAbilityStatusChange> saChangeListener_; 126 static constexpr int LOAD_SA_TIMEOUT_MS = 15000; 127 void (*callback_)() = nullptr; 128 std::mutex tasksMutex_; 129 std::map<std::string, std::shared_ptr<Request>> tasks_; 130 std::recursive_mutex msgReceiverMutex_; 131 std::shared_ptr<ResponseMessageReceiver> msgReceiver_; 132 133 class SystemAbilityStatusChangeListener : public OHOS::SystemAbilityStatusChangeStub { 134 public: 135 SystemAbilityStatusChangeListener(); 136 ~SystemAbilityStatusChangeListener() = default; 137 virtual void OnAddSystemAbility(int32_t saId, const std::string &deviceId) override; 138 virtual void OnRemoveSystemAbility(int32_t asId, const std::string &deviceId) override; 139 }; 140 CallProxyMethod(ProxyMethod method,Args &&...args)141 template<typename ProxyMethod, typename... Args> int32_t CallProxyMethod(ProxyMethod method, Args &&...args) 142 { 143 int32_t ret = E_SERVICE_ERROR; 144 for (int i = 0; i < RETRY_TIMES; i++) { 145 auto proxy = this->GetRequestServiceProxy(true); 146 if (proxy == nullptr) { 147 REQUEST_HILOGE("Get service proxy failed"); 148 continue; 149 } 150 ret = (proxy->*method)(args...); 151 if (ret == E_SERVICE_ERROR) { 152 REQUEST_HILOGE("Remote died, retry times: %{public}d", i); 153 { 154 std::lock_guard<std::mutex> lock(serviceProxyMutex_); 155 requestServiceProxy_ = nullptr; 156 } 157 continue; 158 } 159 break; 160 } 161 return ret; 162 } 163 }; 164 165 } // namespace OHOS::Request 166 #endif // OHOS_REQUEST_DOWNLOAD_MANAGER_IMPL_H 167