/* * Copyright (C) 2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef OHOS_REQUEST_DOWNLOAD_MANAGER_IMPL_H #define OHOS_REQUEST_DOWNLOAD_MANAGER_IMPL_H #include #include #include #include #include #include "constant.h" #include "i_notify_data_listener.h" #include "i_response_message_handler.h" #include "iremote_object.h" #include "iservice_registry.h" #include "js_common.h" #include "log.h" #include "refbase.h" #include "request.h" #include "request_service_interface.h" #include "response_message_receiver.h" #include "system_ability_status_change_stub.h" #include "visibility.h" namespace OHOS::Request { constexpr int RETRY_TIMES = 5; constexpr int REMOTE_DIED_ERROR = 29189; class RequestManagerImpl : public IResponseMessageHandler { public: static const std::unique_ptr &GetInstance(); int32_t Create(const Config &config, int32_t seq, std::string &tid); int32_t GetTask(const std::string &tid, const std::string &token, Config &config); int32_t Start(const std::string &tid); int32_t Stop(const std::string &tid); int32_t Query(const std::string &tid, TaskInfo &info); int32_t Touch(const std::string &tid, const std::string &token, TaskInfo &info); int32_t Search(const Filter &filter, std::vector &tids); int32_t Show(const std::string &tid, TaskInfo &info); int32_t Pause(const std::string &tid, Version version); int32_t QueryMimeType(const std::string &tid, std::string &mimeType); int32_t Remove(const std::string &tid, Version version); int32_t Resume(const std::string &tid); int32_t Subscribe(const std::string &taskId); int32_t Unsubscribe(const std::string &taskId); int32_t AddListener( const std::string &taskId, const SubscribeType &type, const std::shared_ptr &listener); int32_t RemoveListener( const std::string &taskId, const SubscribeType &type, const std::shared_ptr &listener); int32_t AddListener( const std::string &taskId, const SubscribeType &type, const std::shared_ptr &listener); int32_t RemoveListener( const std::string &taskId, const SubscribeType &type, const std::shared_ptr &listener); void RemoveAllListeners(const std::string &taskId); int32_t SubRunCount(const sptr &listener); int32_t UnsubRunCount(); void RestoreListener(void (*callback)()); void RestoreSubRunCount(); void LoadRequestServer(); bool IsSaReady(); void ReopenChannel(); int32_t GetNextSeq(); bool SubscribeSA(); bool UnsubscribeSA(); int32_t CreateGroup( std::string &gid, const bool gauge, std::optional title, std::optional text); int32_t AttachGroup(const std::string &gid, const std::vector &tids); int32_t DeleteGroup(const std::string &gid); private: RequestManagerImpl() = default; RequestManagerImpl(const RequestManagerImpl &) = delete; RequestManagerImpl(RequestManagerImpl &&) = delete; RequestManagerImpl &operator=(const RequestManagerImpl &) = delete; sptr GetRequestServiceProxy(bool load); int32_t EnsureChannelOpen(); std::shared_ptr GetTask(const std::string &taskId); void OnChannelBroken() override; void OnResponseReceive(const std::shared_ptr &response) override; void OnNotifyDataReceive(const std::shared_ptr ¬ifyData) override; private: std::mutex serviceProxyMutex_; std::mutex saChangeListenerMutex_; sptr requestServiceProxy_; sptr saChangeListener_; static constexpr int LOAD_SA_TIMEOUT_MS = 15000; void (*callback_)() = nullptr; std::mutex tasksMutex_; std::map> tasks_; std::recursive_mutex msgReceiverMutex_; std::shared_ptr msgReceiver_; class SystemAbilityStatusChangeListener : public OHOS::SystemAbilityStatusChangeStub { public: SystemAbilityStatusChangeListener(); ~SystemAbilityStatusChangeListener() = default; virtual void OnAddSystemAbility(int32_t saId, const std::string &deviceId) override; virtual void OnRemoveSystemAbility(int32_t asId, const std::string &deviceId) override; }; template int32_t CallProxyMethod(ProxyMethod method, Args &&...args) { int32_t ret = E_SERVICE_ERROR; for (int i = 0; i < RETRY_TIMES; i++) { auto proxy = this->GetRequestServiceProxy(true); if (proxy == nullptr) { REQUEST_HILOGE("Get service proxy failed"); continue; } ret = (proxy->*method)(args...); if (ret == E_SERVICE_ERROR) { REQUEST_HILOGE("Remote died, retry times: %{public}d", i); { std::lock_guard lock(serviceProxyMutex_); requestServiceProxy_ = nullptr; } continue; } break; } return ret; } }; } // namespace OHOS::Request #endif // OHOS_REQUEST_DOWNLOAD_MANAGER_IMPL_H