/* * 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. */ #include #include "base/log/log.h" #include "base/log/log_wrapper.h" #include "base/network/download_manager.h" namespace OHOS::Ace { using CreateDownloadManagerFunc = DownloadManager* (*)(); std::unique_ptr DownloadManager::instance_ = nullptr; constexpr char CREATE_DOWNLOAD_MANAGER_FUNC[] = "OHOS_ACE_CreateDownloadManager"; constexpr char ACE_NET_WORK_NAME[] = "libace_network.z.so"; DownloadManager* CreateDownloadManager() { void* handle = dlopen(ACE_NET_WORK_NAME, RTLD_LAZY | RTLD_LOCAL); if (handle == nullptr) { TAG_LOGW(AceLogTag::ACE_DOWNLOAD_MANAGER, "load libace_network failed"); return nullptr; } auto entry = reinterpret_cast(dlsym(handle, CREATE_DOWNLOAD_MANAGER_FUNC)); if (entry == nullptr) { dlclose(handle); return nullptr; } auto* manager = entry(); return manager; } DownloadManager* DownloadManager::GetInstance() { TAG_LOGI(AceLogTag::ACE_DOWNLOAD_MANAGER, "DownloadManager GetInstance"); static std::once_flag onceFlag; std::call_once(onceFlag, []() { instance_.reset(CreateDownloadManager()); }); return instance_.get(); } } // namespace OHOS::Ace