123b3eb3cSopenharmony_ci/*
223b3eb3cSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
323b3eb3cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
423b3eb3cSopenharmony_ci * you may not use this file except in compliance with the License.
523b3eb3cSopenharmony_ci * You may obtain a copy of the License at
623b3eb3cSopenharmony_ci *
723b3eb3cSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
823b3eb3cSopenharmony_ci *
923b3eb3cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1023b3eb3cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1123b3eb3cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1223b3eb3cSopenharmony_ci * See the License for the specific language governing permissions and
1323b3eb3cSopenharmony_ci * limitations under the License.
1423b3eb3cSopenharmony_ci */
1523b3eb3cSopenharmony_ci
1623b3eb3cSopenharmony_ci#include <dlfcn.h>
1723b3eb3cSopenharmony_ci
1823b3eb3cSopenharmony_ci#include "base/log/log_wrapper.h"
1923b3eb3cSopenharmony_ci#include "base/network/download_manager.h"
2023b3eb3cSopenharmony_ci
2123b3eb3cSopenharmony_cinamespace OHOS::Ace {
2223b3eb3cSopenharmony_ciusing CreateDownloadManagerFunc = DownloadManager* (*)();
2323b3eb3cSopenharmony_cistd::unique_ptr<DownloadManager> DownloadManager::instance_ = nullptr;
2423b3eb3cSopenharmony_cistd::mutex DownloadManager::mutex_;
2523b3eb3cSopenharmony_ciconstexpr char CREATE_DOWNLOAD_MANAGER_FUNC[] = "OHOS_ACE_CreateDownloadManager";
2623b3eb3cSopenharmony_ciconstexpr char ACE_NET_WORK_NAME[] = "libace_network.z.so";
2723b3eb3cSopenharmony_ci
2823b3eb3cSopenharmony_ciDownloadManager* CreateDownloadManager()
2923b3eb3cSopenharmony_ci{
3023b3eb3cSopenharmony_ci    void* handle = dlopen(ACE_NET_WORK_NAME, RTLD_LAZY | RTLD_LOCAL);
3123b3eb3cSopenharmony_ci    if (handle == nullptr) {
3223b3eb3cSopenharmony_ci        TAG_LOGW(AceLogTag::ACE_DOWNLOAD_MANAGER, "load libace_network failed");
3323b3eb3cSopenharmony_ci        return nullptr;
3423b3eb3cSopenharmony_ci    }
3523b3eb3cSopenharmony_ci
3623b3eb3cSopenharmony_ci    auto entry = reinterpret_cast<CreateDownloadManagerFunc>(dlsym(handle, CREATE_DOWNLOAD_MANAGER_FUNC));
3723b3eb3cSopenharmony_ci    if (entry == nullptr) {
3823b3eb3cSopenharmony_ci        dlclose(handle);
3923b3eb3cSopenharmony_ci        return nullptr;
4023b3eb3cSopenharmony_ci    }
4123b3eb3cSopenharmony_ci
4223b3eb3cSopenharmony_ci    auto* manager = entry();
4323b3eb3cSopenharmony_ci    return manager;
4423b3eb3cSopenharmony_ci}
4523b3eb3cSopenharmony_ci
4623b3eb3cSopenharmony_ciDownloadManager* DownloadManager::GetInstance()
4723b3eb3cSopenharmony_ci{
4823b3eb3cSopenharmony_ci    TAG_LOGI(AceLogTag::ACE_DOWNLOAD_MANAGER, "DownloadManager GetInstance");
4923b3eb3cSopenharmony_ci    if (!instance_) {
5023b3eb3cSopenharmony_ci        std::lock_guard<std::mutex> lock(mutex_);
5123b3eb3cSopenharmony_ci        if (!instance_) {
5223b3eb3cSopenharmony_ci            auto* manager = CreateDownloadManager();
5323b3eb3cSopenharmony_ci            instance_.reset(manager);
5423b3eb3cSopenharmony_ci        }
5523b3eb3cSopenharmony_ci    }
5623b3eb3cSopenharmony_ci    return instance_.get();
5723b3eb3cSopenharmony_ci}
5823b3eb3cSopenharmony_ci} // namespace OHOS::Ace