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 24 #include "constant.h" 25 #include "i_notify_data_listener.h" 26 #include "i_response_message_handler.h" 27 #include "iremote_object.h" 28 #include "iservice_registry.h" 29 #include "js_common.h" 30 #include "refbase.h" 31 #include "request.h" 32 #include "request_service_interface.h" 33 #include "response_message_receiver.h" 34 #include "system_ability_status_change_stub.h" 35 #include "visibility.h" 36 37 namespace OHOS::Request { 38 class RequestSaDeathRecipient : public IRemoteObject::DeathRecipient { 39 public: 40 explicit RequestSaDeathRecipient(); 41 ~RequestSaDeathRecipient() = default; 42 void OnRemoteDied(const wptr<IRemoteObject> &object) override; 43 }; 44 45 class RequestManagerImpl : public IResponseMessageHandler { 46 public: 47 static const std::unique_ptr<RequestManagerImpl> &GetInstance(); 48 int32_t Create(const Config &config, int32_t seq, std::string &tid); 49 int32_t GetTask(const std::string &tid, const std::string &token, Config &config); 50 int32_t Start(const std::string &tid); 51 int32_t Stop(const std::string &tid); 52 int32_t Query(const std::string &tid, TaskInfo &info); 53 int32_t Touch(const std::string &tid, const std::string &token, TaskInfo &info); 54 int32_t Search(const Filter &filter, std::vector<std::string> &tids); 55 int32_t Show(const std::string &tid, TaskInfo &info); 56 int32_t Pause(const std::string &tid, Version version); 57 int32_t QueryMimeType(const std::string &tid, std::string &mimeType); 58 int32_t Remove(const std::string &tid, Version version); 59 int32_t Resume(const std::string &tid); 60 61 int32_t Subscribe(const std::string &taskId); 62 int32_t Unsubscribe(const std::string &taskId); 63 64 int32_t AddListener( 65 const std::string &taskId, const SubscribeType &type, const std::shared_ptr<IResponseListener> &listener); 66 int32_t RemoveListener( 67 const std::string &taskId, const SubscribeType &type, const std::shared_ptr<IResponseListener> &listener); 68 int32_t AddListener( 69 const std::string &taskId, const SubscribeType &type, const std::shared_ptr<INotifyDataListener> &listener); 70 int32_t RemoveListener( 71 const std::string &taskId, const SubscribeType &type, const std::shared_ptr<INotifyDataListener> &listener); 72 void RemoveAllListeners(const std::string &taskId); 73 74 int32_t SubRunCount(const sptr<NotifyInterface> &listener); 75 int32_t UnsubRunCount(); 76 77 void RestoreListener(void (*callback)()); 78 void RestoreSubRunCount(); 79 bool LoadRequestServer(); 80 bool IsSaReady(); 81 void OnRemoteSaDied(const wptr<IRemoteObject> &object); 82 void LoadServerSuccess(); 83 void LoadServerFail(); 84 void ReopenChannel(); 85 int32_t GetNextSeq(); 86 bool SubscribeSA(); 87 bool UnsubscribeSA(); 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(); 95 int32_t Retry(std::string &taskId, const Config &config, int32_t errorCode); 96 void SetRequestServiceProxy(sptr<RequestServiceInterface> proxy); 97 int32_t EnsureChannelOpen(); 98 std::shared_ptr<Request> GetTask(const std::string &taskId); 99 void OnChannelBroken() override; 100 void OnResponseReceive(const std::shared_ptr<Response> &response) override; 101 void OnNotifyDataReceive(const std::shared_ptr<NotifyData> ¬ifyData) override; 102 103 private: 104 static std::mutex instanceLock_; 105 static sptr<RequestManagerImpl> instance_; 106 std::mutex downloadMutex_; 107 std::mutex conditionMutex_; 108 std::mutex serviceProxyMutex_; 109 std::mutex saChangeListenerMutex_; 110 111 sptr<RequestServiceInterface> requestServiceProxy_; 112 sptr<RequestSaDeathRecipient> deathRecipient_; 113 sptr<ISystemAbilityStatusChange> saChangeListener_; 114 std::condition_variable syncCon_; 115 std::atomic<bool> ready_ = false; 116 static constexpr int LOAD_SA_TIMEOUT_MS = 15000; 117 void (*callback_)() = nullptr; 118 std::mutex tasksMutex_; 119 std::map<std::string, std::shared_ptr<Request>> tasks_; 120 std::recursive_mutex msgReceiverMutex_; 121 std::shared_ptr<ResponseMessageReceiver> msgReceiver_; 122 123 private: 124 class SystemAbilityStatusChangeListener : public OHOS::SystemAbilityStatusChangeStub { 125 public: 126 SystemAbilityStatusChangeListener(); 127 ~SystemAbilityStatusChangeListener() = default; 128 virtual void OnAddSystemAbility(int32_t saId, const std::string &deviceId) override; 129 virtual void OnRemoveSystemAbility(int32_t asId, const std::string &deviceId) override; 130 }; 131 }; 132 133 } // namespace OHOS::Request 134 #endif // OHOS_REQUEST_DOWNLOAD_MANAGER_IMPL_H 135