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