• 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_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()27 DownloadManager* 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()45 DownloadManager* DownloadManager::GetInstance()
46 {
47     TAG_LOGI(AceLogTag::ACE_DOWNLOAD_MANAGER, "DownloadManager GetInstance");
48     static std::once_flag onceFlag;
49     std::call_once(onceFlag, []() {
50         instance_.reset(CreateDownloadManager());
51     });
52     return instance_.get();
53 }
54 } // namespace OHOS::Ace