136a3a8d0Sopenharmony_ci/*
236a3a8d0Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
336a3a8d0Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
436a3a8d0Sopenharmony_ci * you may not use this file except in compliance with the License.
536a3a8d0Sopenharmony_ci * You may obtain a copy of the License at
636a3a8d0Sopenharmony_ci *
736a3a8d0Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
836a3a8d0Sopenharmony_ci *
936a3a8d0Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1036a3a8d0Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1136a3a8d0Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1236a3a8d0Sopenharmony_ci * See the License for the specific language governing permissions and
1336a3a8d0Sopenharmony_ci * limitations under the License.
1436a3a8d0Sopenharmony_ci */
1536a3a8d0Sopenharmony_ci
1636a3a8d0Sopenharmony_ci#include "resource_manager.h"
1736a3a8d0Sopenharmony_ci
1836a3a8d0Sopenharmony_ci#include "hilog_wrapper.h"
1936a3a8d0Sopenharmony_ci#include "resource_manager_impl.h"
2036a3a8d0Sopenharmony_ci#include "system_resource_manager.h"
2136a3a8d0Sopenharmony_ci#if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__)
2236a3a8d0Sopenharmony_ci#include "resource_manager_ext_mgr.h"
2336a3a8d0Sopenharmony_ci#endif
2436a3a8d0Sopenharmony_ci#include "theme_pack_manager.h"
2536a3a8d0Sopenharmony_cinamespace OHOS {
2636a3a8d0Sopenharmony_cinamespace Global {
2736a3a8d0Sopenharmony_cinamespace Resource {
2836a3a8d0Sopenharmony_cistatic std::map<std::string, std::shared_ptr<ResourceManager>> resMgrMap;
2936a3a8d0Sopenharmony_ci#if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__)
3036a3a8d0Sopenharmony_cistatic std::mutex resMgrExtLock;
3136a3a8d0Sopenharmony_cistatic std::shared_ptr<ResourceManagerExtMgr> resMgrExtMgr = std::make_shared<ResourceManagerExtMgr>();
3236a3a8d0Sopenharmony_ci#endif
3336a3a8d0Sopenharmony_ci
3436a3a8d0Sopenharmony_ciResourceManager *CreateResourceManager()
3536a3a8d0Sopenharmony_ci{
3636a3a8d0Sopenharmony_ci    ResourceManagerImpl *impl = new (std::nothrow) ResourceManagerImpl;
3736a3a8d0Sopenharmony_ci    if (impl == nullptr) {
3836a3a8d0Sopenharmony_ci        RESMGR_HILOGE(RESMGR_TAG, "new ResourceManagerImpl failed when CreateResourceManager");
3936a3a8d0Sopenharmony_ci        return nullptr;
4036a3a8d0Sopenharmony_ci    }
4136a3a8d0Sopenharmony_ci    if (!impl->Init()) {
4236a3a8d0Sopenharmony_ci        delete (impl);
4336a3a8d0Sopenharmony_ci        return nullptr;
4436a3a8d0Sopenharmony_ci    }
4536a3a8d0Sopenharmony_ci    ResourceManagerImpl *systemResourceManager = SystemResourceManager::GetSystemResourceManager();
4636a3a8d0Sopenharmony_ci    if (systemResourceManager != nullptr) {
4736a3a8d0Sopenharmony_ci        impl->AddSystemResource(systemResourceManager);
4836a3a8d0Sopenharmony_ci    }
4936a3a8d0Sopenharmony_ci    return impl;
5036a3a8d0Sopenharmony_ci}
5136a3a8d0Sopenharmony_ci
5236a3a8d0Sopenharmony_cistd::shared_ptr<ResourceManager> CreateResourceManagerDef(
5336a3a8d0Sopenharmony_ci    const std::string &bundleName, const std::string &moduleName,
5436a3a8d0Sopenharmony_ci    const std::string &hapPath, const std::vector<std::string> &overlayPath,
5536a3a8d0Sopenharmony_ci    ResConfig &resConfig, int32_t userId)
5636a3a8d0Sopenharmony_ci{
5736a3a8d0Sopenharmony_ci    if (bundleName.empty()) {
5836a3a8d0Sopenharmony_ci        RESMGR_HILOGE(RESMGR_TAG, "bundleName or hapPath is empty when CreateResourceManagerDef");
5936a3a8d0Sopenharmony_ci        return nullptr;
6036a3a8d0Sopenharmony_ci    }
6136a3a8d0Sopenharmony_ci    std::shared_ptr<ResourceManager> resourceManagerImpl(CreateResourceManager());
6236a3a8d0Sopenharmony_ci    if (resourceManagerImpl == nullptr) {
6336a3a8d0Sopenharmony_ci        RESMGR_HILOGE(RESMGR_TAG, "CreateResourceManagerDef failed bundleName = %{public}s moduleName = %{public}s",
6436a3a8d0Sopenharmony_ci            bundleName.c_str(), moduleName.c_str());
6536a3a8d0Sopenharmony_ci        return nullptr;
6636a3a8d0Sopenharmony_ci    }
6736a3a8d0Sopenharmony_ci    resourceManagerImpl->bundleInfo.first = bundleName;
6836a3a8d0Sopenharmony_ci    resourceManagerImpl->bundleInfo.second = moduleName;
6936a3a8d0Sopenharmony_ci    resourceManagerImpl->userId = userId;
7036a3a8d0Sopenharmony_ci    uint32_t currentId = resConfig.GetThemeId();
7136a3a8d0Sopenharmony_ci    auto themePackManager = ThemePackManager::GetThemePackManager();
7236a3a8d0Sopenharmony_ci    if (themePackManager->IsFirstLoadResource() || themePackManager->UpdateThemeId(currentId)
7336a3a8d0Sopenharmony_ci        || themePackManager->IsUpdateByUserId(userId)) {
7436a3a8d0Sopenharmony_ci        RESMGR_HILOGI(RESMGR_TAG, "CreateResourceManagerDef LoadThemeRes");
7536a3a8d0Sopenharmony_ci        themePackManager->LoadThemeRes(bundleName, moduleName, userId);
7636a3a8d0Sopenharmony_ci    }
7736a3a8d0Sopenharmony_ci    return resourceManagerImpl;
7836a3a8d0Sopenharmony_ci}
7936a3a8d0Sopenharmony_ci
8036a3a8d0Sopenharmony_ci#if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__)
8136a3a8d0Sopenharmony_cistd::shared_ptr<ResourceManager> CreateResourceManagerExt(const std::string &bundleName, const int32_t appType)
8236a3a8d0Sopenharmony_ci{
8336a3a8d0Sopenharmony_ci    if (bundleName.empty()) {
8436a3a8d0Sopenharmony_ci        RESMGR_HILOGE(RESMGR_TAG, "bundleName is empty when CreateResourceManagerExt");
8536a3a8d0Sopenharmony_ci        return nullptr;
8636a3a8d0Sopenharmony_ci    }
8736a3a8d0Sopenharmony_ci    std::lock_guard<std::mutex> lock(resMgrExtLock);
8836a3a8d0Sopenharmony_ci    std::shared_ptr<ResourceManager> resMgrExt;
8936a3a8d0Sopenharmony_ci    if (!resMgrExtMgr->Init(resMgrExt, bundleName, appType) || resMgrExt == nullptr) {
9036a3a8d0Sopenharmony_ci        RESMGR_HILOGE(RESMGR_TAG, "ResourceManagerExt init fail");
9136a3a8d0Sopenharmony_ci        return nullptr;
9236a3a8d0Sopenharmony_ci    }
9336a3a8d0Sopenharmony_ci    return resMgrExt;
9436a3a8d0Sopenharmony_ci}
9536a3a8d0Sopenharmony_ci#endif
9636a3a8d0Sopenharmony_ci
9736a3a8d0Sopenharmony_cistd::shared_ptr<ResourceManager> CreateResourceManager(const std::string &bundleName, const std::string &moduleName,
9836a3a8d0Sopenharmony_ci    const std::string &hapPath, const std::vector<std::string> &overlayPath,
9936a3a8d0Sopenharmony_ci    ResConfig &resConfig, int32_t appType, int32_t userId)
10036a3a8d0Sopenharmony_ci{
10136a3a8d0Sopenharmony_ci    if (appType == 0) {
10236a3a8d0Sopenharmony_ci        return CreateResourceManagerDef(bundleName, moduleName, hapPath, overlayPath, resConfig, userId);
10336a3a8d0Sopenharmony_ci    } else {
10436a3a8d0Sopenharmony_ci    #if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__)
10536a3a8d0Sopenharmony_ci        return CreateResourceManagerExt(bundleName, appType);
10636a3a8d0Sopenharmony_ci    #else
10736a3a8d0Sopenharmony_ci        return nullptr;
10836a3a8d0Sopenharmony_ci    #endif
10936a3a8d0Sopenharmony_ci    }
11036a3a8d0Sopenharmony_ci}
11136a3a8d0Sopenharmony_ci
11236a3a8d0Sopenharmony_ciResourceManager *GetSystemResourceManager()
11336a3a8d0Sopenharmony_ci{
11436a3a8d0Sopenharmony_ci    return SystemResourceManager::GetSystemResourceManager();
11536a3a8d0Sopenharmony_ci}
11636a3a8d0Sopenharmony_ci
11736a3a8d0Sopenharmony_ciResourceManager *GetSystemResourceManagerNoSandBox()
11836a3a8d0Sopenharmony_ci{
11936a3a8d0Sopenharmony_ci    return SystemResourceManager::GetSystemResourceManagerNoSandBox();
12036a3a8d0Sopenharmony_ci}
12136a3a8d0Sopenharmony_ci
12236a3a8d0Sopenharmony_ciResourceManager::~ResourceManager()
12336a3a8d0Sopenharmony_ci{}
12436a3a8d0Sopenharmony_ci} // namespace Resource
12536a3a8d0Sopenharmony_ci} // namespace Global
12636a3a8d0Sopenharmony_ci} // namespace OHOS
127