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 "js_common.h" 31 #include "log.h" 32 #include "refbase.h" 33 #include "request.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 constexpr int REMOTE_DIED_ERROR = 29189; 43 44 class RequestManagerImpl : public IResponseMessageHandler { 45 public: 46 static const std::unique_ptr<RequestManagerImpl> &GetInstance(); 47 int32_t Create(const Config &config, int32_t seq, std::string &tid); 48 int32_t GetTask(const std::string &tid, const std::string &token, Config &config); 49 int32_t Start(const std::string &tid); 50 int32_t Stop(const std::string &tid); 51 int32_t Query(const std::string &tid, TaskInfo &info); 52 int32_t Touch(const std::string &tid, const std::string &token, TaskInfo &info); 53 int32_t Search(const Filter &filter, std::vector<std::string> &tids); 54 int32_t Show(const std::string &tid, TaskInfo &info); 55 int32_t Pause(const std::string &tid, Version version); 56 int32_t QueryMimeType(const std::string &tid, std::string &mimeType); 57 int32_t Remove(const std::string &tid, Version version); 58 int32_t Resume(const std::string &tid); 59 60 int32_t Subscribe(const std::string &taskId); 61 int32_t Unsubscribe(const std::string &taskId); 62 63 int32_t AddListener( 64 const std::string &taskId, const SubscribeType &type, const std::shared_ptr<IResponseListener> &listener); 65 int32_t RemoveListener( 66 const std::string &taskId, const SubscribeType &type, const std::shared_ptr<IResponseListener> &listener); 67 int32_t AddListener( 68 const std::string &taskId, const SubscribeType &type, const std::shared_ptr<INotifyDataListener> &listener); 69 int32_t RemoveListener( 70 const std::string &taskId, const SubscribeType &type, const std::shared_ptr<INotifyDataListener> &listener); 71 void RemoveAllListeners(const std::string &taskId); 72 73 int32_t SubRunCount(const sptr<NotifyInterface> &listener); 74 int32_t UnsubRunCount(); 75 76 void RestoreListener(void (*callback)()); 77 void RestoreSubRunCount(); 78 void LoadRequestServer(); 79 bool IsSaReady(); 80 void ReopenChannel(); 81 int32_t GetNextSeq(); 82 bool SubscribeSA(); 83 bool UnsubscribeSA(); 84 int32_t CreateGroup( 85 std::string &gid, const bool gauge, std::optional<std::string> title, std::optional<std::string> text); 86 int32_t AttachGroup(const std::string &gid, const std::vector<std::string> &tids); 87 int32_t DeleteGroup(const std::string &gid); 88 89 private: 90 RequestManagerImpl() = default; 91 RequestManagerImpl(const RequestManagerImpl &) = delete; 92 RequestManagerImpl(RequestManagerImpl &&) = delete; 93 RequestManagerImpl &operator=(const RequestManagerImpl &) = delete; 94 sptr<RequestServiceInterface> GetRequestServiceProxy(bool load); 95 int32_t EnsureChannelOpen(); 96 std::shared_ptr<Request> GetTask(const std::string &taskId); 97 void OnChannelBroken() override; 98 void OnResponseReceive(const std::shared_ptr<Response> &response) override; 99 void OnNotifyDataReceive(const std::shared_ptr<NotifyData> ¬ifyData) override; 100 101 private: 102 std::mutex serviceProxyMutex_; 103 std::mutex saChangeListenerMutex_; 104 105 sptr<RequestServiceInterface> requestServiceProxy_; 106 sptr<ISystemAbilityStatusChange> saChangeListener_; 107 static constexpr int LOAD_SA_TIMEOUT_MS = 15000; 108 void (*callback_)() = nullptr; 109 std::mutex tasksMutex_; 110 std::map<std::string, std::shared_ptr<Request>> tasks_; 111 std::recursive_mutex msgReceiverMutex_; 112 std::shared_ptr<ResponseMessageReceiver> msgReceiver_; 113 114 class SystemAbilityStatusChangeListener : public OHOS::SystemAbilityStatusChangeStub { 115 public: 116 SystemAbilityStatusChangeListener(); 117 ~SystemAbilityStatusChangeListener() = default; 118 virtual void OnAddSystemAbility(int32_t saId, const std::string &deviceId) override; 119 virtual void OnRemoveSystemAbility(int32_t asId, const std::string &deviceId) override; 120 }; 121 CallProxyMethod(ProxyMethod method,Args &&...args)122 template<typename ProxyMethod, typename... Args> int32_t CallProxyMethod(ProxyMethod method, Args &&...args) 123 { 124 int32_t ret = E_SERVICE_ERROR; 125 for (int i = 0; i < RETRY_TIMES; i++) { 126 auto proxy = this->GetRequestServiceProxy(true); 127 if (proxy == nullptr) { 128 REQUEST_HILOGE("Get service proxy failed"); 129 continue; 130 } 131 ret = (proxy->*method)(args...); 132 if (ret == E_SERVICE_ERROR) { 133 REQUEST_HILOGE("Remote died, retry times: %{public}d", i); 134 { 135 std::lock_guard<std::mutex> lock(serviceProxyMutex_); 136 requestServiceProxy_ = nullptr; 137 } 138 continue; 139 } 140 break; 141 } 142 return ret; 143 } 144 }; 145 146 } // namespace OHOS::Request 147 #endif // OHOS_REQUEST_DOWNLOAD_MANAGER_IMPL_H 148