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 #include <dlfcn.h> 17 18 #include "base/log/log_wrapper.h" 19 #include "base/network/download_manager.h" 20 21 namespace OHOS::Ace { 22 using CreateDownloadManagerFunc = DownloadManager* (*)(); 23 std::unique_ptr<DownloadManager> DownloadManager::instance_ = nullptr; 24 constexpr char CREATE_DOWNLOAD_MANAGER_FUNC[] = "OHOS_ACE_CreateDownloadManager"; 25 constexpr char ACE_NET_WORK_NAME[] = "libace_network.z.so"; 26 CreateDownloadManager()27DownloadManager* CreateDownloadManager() 28 { 29 void* handle = dlopen(ACE_NET_WORK_NAME, RTLD_LAZY | RTLD_LOCAL); 30 if (handle == nullptr) { 31 TAG_LOGW(AceLogTag::ACE_DOWNLOAD_MANAGER, "load libace_network failed"); 32 return nullptr; 33 } 34 35 auto entry = reinterpret_cast<CreateDownloadManagerFunc>(dlsym(handle, CREATE_DOWNLOAD_MANAGER_FUNC)); 36 if (entry == nullptr) { 37 dlclose(handle); 38 return nullptr; 39 } 40 41 auto* manager = entry(); 42 return manager; 43 } 44 GetInstance()45DownloadManager* DownloadManager::GetInstance() 46 { 47 TAG_LOGI(AceLogTag::ACE_DOWNLOAD_MANAGER, "DownloadManager GetInstance"); 48 struct DownloadManagerHolder { 49 DownloadManager* mgr_; 50 DownloadManagerHolder() 51 { 52 mgr_ = CreateDownloadManager(); 53 } 54 }; 55 static DownloadManagerHolder mgrHolder; 56 return mgrHolder.mgr_; 57 } 58 } // namespace OHOS::Ace