1 /* 2 * Copyright (C) 2023 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 DOWNLOAD_MANAGER_H 17 #define DOWNLOAD_MANAGER_H 18 19 #include <atomic> 20 #include <condition_variable> 21 #include <map> 22 #include <mutex> 23 24 #include "constant.h" 25 #include "data_ability_helper.h" 26 #include "iremote_object.h" 27 #include "iservice_registry.h" 28 #include "js_common.h" 29 #include "notify_stub.h" 30 #include "refbase.h" 31 #include "request_service_interface.h" 32 #include "system_ability_status_change_stub.h" 33 #include "visibility.h" 34 35 namespace OHOS::Request { 36 class RequestSaDeathRecipient : public IRemoteObject::DeathRecipient { 37 public: 38 explicit RequestSaDeathRecipient(); 39 ~RequestSaDeathRecipient() = default; 40 void OnRemoteDied(const wptr<IRemoteObject> &object) override; 41 }; 42 43 class RequestManager : public RefBase { 44 public: 45 RequestManager(); 46 ~RequestManager(); 47 REQUEST_API static sptr<RequestManager> GetInstance(); 48 REQUEST_API int32_t Create(const Config &config, int32_t &tid, sptr<NotifyInterface> listener); 49 REQUEST_API int32_t GetTask(const std::string &tid, const std::string &token, Config &config); 50 REQUEST_API int32_t Start(const std::string &tid); 51 REQUEST_API int32_t Stop(const std::string &tid); 52 REQUEST_API int32_t Query(const std::string &tid, TaskInfo &info); 53 REQUEST_API int32_t Touch(const std::string &tid, const std::string &token, TaskInfo &info); 54 REQUEST_API int32_t Search(const Filter &filter, std::vector<std::string> &tids); 55 REQUEST_API int32_t Show(const std::string &tid, TaskInfo &info); 56 REQUEST_API int32_t Pause(const std::string &tid, Version version); 57 REQUEST_API int32_t QueryMimeType(const std::string &tid, std::string &mimeType); 58 REQUEST_API int32_t Remove(const std::string &tid, Version version); 59 REQUEST_API int32_t Resume(const std::string &tid); 60 61 REQUEST_API int32_t On( 62 const std::string &type, const std::string &tid, const sptr<NotifyInterface> &listener, Version version); 63 REQUEST_API int32_t Off(const std::string &type, const std::string &tid, Version version); 64 65 REQUEST_API void RestoreListener(void (*callback)()); 66 void OnRemoteSaDied(const wptr<IRemoteObject> &object); 67 REQUEST_API bool LoadRequestServer(); 68 69 REQUEST_API bool IsSaReady(); 70 void LoadServerSuccess(); 71 void LoadServerFail(); 72 73 private: 74 sptr<RequestServiceInterface> GetRequestServiceProxy(); 75 int32_t Retry(int32_t &taskId, const Config &config, int32_t errorCode, sptr<NotifyInterface> listener); 76 void SetRequestServiceProxy(sptr<RequestServiceInterface> proxy); 77 bool SubscribeSA(sptr<ISystemAbilityManager> systemAbilityManager); 78 79 private: 80 static std::mutex instanceLock_; 81 static sptr<RequestManager> instance_; 82 std::mutex downloadMutex_; 83 std::mutex conditionMutex_; 84 std::mutex serviceProxyMutex_; 85 86 sptr<RequestServiceInterface> requestServiceProxy_; 87 sptr<RequestSaDeathRecipient> deathRecipient_; 88 sptr<ISystemAbilityStatusChange> saChangeListener_; 89 std::condition_variable syncCon_; 90 std::atomic<bool> ready_ = false; 91 static constexpr int LOAD_SA_TIMEOUT_MS = 15000; 92 void (*callback_)() = nullptr; 93 94 private: 95 class SystemAbilityStatusChangeListener : public OHOS::SystemAbilityStatusChangeStub { 96 public: 97 SystemAbilityStatusChangeListener(); 98 ~SystemAbilityStatusChangeListener() = default; 99 virtual void OnAddSystemAbility(int32_t saId, const std::string &deviceId) override; 100 virtual void OnRemoveSystemAbility(int32_t asId, const std::string &deviceId) override; 101 }; 102 }; 103 } // namespace OHOS::Request 104 #endif // DOWNLOAD_MANAGER_H 105