1 /*
2  * Copyright (c) 2023 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 #if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__)
16 #include "resource_manager_ext_mgr.h"
17 
18 #include <dlfcn.h>
19 #include "hilog_wrapper.h"
20 
21 namespace OHOS {
22 namespace Global {
23 namespace Resource {
ResourceManagerExtMgr()24 ResourceManagerExtMgr::ResourceManagerExtMgr()
25 {
26 }
27 
~ResourceManagerExtMgr()28 ResourceManagerExtMgr::~ResourceManagerExtMgr()
29 {
30     for (auto entry : resMgrExtMap_) {
31         entry.second.reset();
32         entry.second = nullptr;
33     }
34     resMgrExtMap_.clear();
35     if (handle_ != nullptr) {
36         dlclose(handle_);
37     }
38     handle_ = nullptr;
39 }
40 
Init(std::shared_ptr<ResourceManager> & resMgrExt,const std::string & bundleName,const int32_t appType)41 bool ResourceManagerExtMgr::Init(std::shared_ptr<ResourceManager> &resMgrExt, const std::string &bundleName,
42     const int32_t appType)
43 {
44     if (handle_ == nullptr) {
45         std::string pluginPath = "system/lib64/libglobal_resmgr_broker.z.so";
46         handle_ = dlopen(pluginPath.c_str(), RTLD_LAZY);
47         if (handle_ == nullptr) {
48             RESMGR_HILOGE(RESMGR_TAG, "open so fail");
49             return false;
50         }
51     }
52     std::string resMgrExtKey(bundleName);
53     auto iter = resMgrExtMap_.find(resMgrExtKey);
54     if (iter != resMgrExtMap_.end()) {
55         resMgrExt = resMgrExtMap_[resMgrExtKey];
56         return true;
57     }
58     IResMgrExt iResMgrExt = (IResMgrExt)dlsym(handle_, "CreateResMgrExt");
59     int ret = (*iResMgrExt)(resMgrExt, bundleName, appType);
60     if (ret) {
61         RESMGR_HILOGE(RESMGR_TAG, "CreateResMgrExt fail.");
62         return false;
63     }
64     resMgrExtMap_[bundleName] = resMgrExt;
65     return true;
66 }
67 } // namespace Resource
68 } // namespace Global
69 } // namespace OHOS
70 #endif