• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "core/common/resource/resource_manager.h"
17 
18 #include "base/log/dump_log.h"
19 #include "base/utils/time_util.h"
20 
21 namespace OHOS::Ace {
22 namespace {
23 const std::string DEFAULT_BUNDLE_NAME = "";
24 const std::string DEFAULT_MODULE_NAME = "";
25 } // namespace
26 
GetInstance()27 ResourceManager& ResourceManager::GetInstance()
28 {
29     static ResourceManager instance;
30     return instance;
31 }
32 
GetOrCreateResourceAdapter(const RefPtr<ResourceObject> & resourceObject)33 RefPtr<ResourceAdapter> ResourceManager::GetOrCreateResourceAdapter(const RefPtr<ResourceObject>& resourceObject)
34 {
35     int32_t instanceId = resourceObject->GetInstanceId();
36     std::string bundleName = resourceObject->GetBundleName();
37     std::string moduleName = resourceObject->GetModuleName();
38 
39     auto resourceAdapter = GetResourceAdapter(bundleName, moduleName, instanceId);
40     if (resourceAdapter == nullptr) {
41         resourceAdapter = ResourceAdapter::CreateNewResourceAdapter(bundleName, moduleName);
42         if (!resourceAdapter) {
43             return GetResourceAdapter(DEFAULT_BUNDLE_NAME, DEFAULT_MODULE_NAME, instanceId);
44         }
45         AddResourceAdapter(bundleName, moduleName, instanceId, resourceAdapter);
46     }
47     return resourceAdapter;
48 }
49 
RegisterMainResourceAdapter(const std::string & bundleName,const std::string & moduleName,int32_t instanceId,const RefPtr<ResourceAdapter> & resAdapter)50 void ResourceManager::RegisterMainResourceAdapter(const std::string& bundleName, const std::string& moduleName,
51     int32_t instanceId, const RefPtr<ResourceAdapter>& resAdapter)
52 {
53     std::unique_lock<std::shared_mutex> lock(mutex_);
54     auto key = MakeCacheKey(bundleName, moduleName, instanceId);
55     resourceAdapters_.emplace(key, resAdapter);
56 }
57 
UpdateResourceConfig(const std::string &,const std::string &,int32_t instanceId,const ResourceConfiguration & config,bool themeFlag)58 void ResourceManager::UpdateResourceConfig(const std::string& /*bundleName*/, const std::string& /*moduleName*/,
59     int32_t instanceId, const ResourceConfiguration& config, bool themeFlag)
60 {
61     std::unique_lock<std::shared_mutex> lock(mutex_);
62     std::string compareId = std::to_string(instanceId);
63     for (auto iter = resourceAdapters_.begin(); iter != resourceAdapters_.end(); ++iter) {
64         if (GetCacheKeyInstanceId(iter->first) == compareId) {
65             iter->second->UpdateConfig(config, themeFlag);
66         }
67     }
68     for (auto iter = cacheList_.begin(); iter != cacheList_.end(); ++iter) {
69         if (GetCacheKeyInstanceId(iter->cacheKey) == compareId) {
70             iter->cacheObj->UpdateConfig(config, themeFlag);
71         }
72     }
73 }
74 
UpdateColorMode(const std::string &,const std::string &,int32_t instanceId,ColorMode colorMode)75 void ResourceManager::UpdateColorMode(
76     const std::string& /*bundleName*/, const std::string& /*moduleName*/, int32_t instanceId, ColorMode colorMode)
77 {
78     std::unique_lock<std::shared_mutex> lock(mutex_);
79     std::string compareId = std::to_string(instanceId);
80     for (auto iter = resourceAdapters_.begin(); iter != resourceAdapters_.end(); ++iter) {
81         if (GetCacheKeyInstanceId(iter->first) == compareId) {
82             iter->second->UpdateColorMode(colorMode);
83         }
84     }
85     for (auto iter = cacheList_.begin(); iter != cacheList_.end(); ++iter) {
86         if (GetCacheKeyInstanceId(iter->cacheKey) == compareId) {
87             iter->cacheObj->UpdateColorMode(colorMode);
88         }
89     }
90 }
91 
DumpResLoadError()92 void ResourceManager::DumpResLoadError()
93 {
94     std::unique_lock<std::shared_mutex> lock(errorMutex_);
95     auto resLoadErrorSize = resourceErrorList_.size();
96     DumpLog::GetInstance().Print("----------ResourceLoadWrrorInfo----------");
97     if (resLoadErrorSize == 0) {
98         DumpLog::GetInstance().Print("No resource load error have occurred.");
99         return;
100     }
101 
102     DumpLog::GetInstance().Print("ResourceLoadErrorTimes: " + std::to_string(resLoadErrorSize));
103     for (const auto& nodeError : resourceErrorList_) {
104         DumpLog::GetInstance().Print(1, "Node: " + std::to_string(nodeError.nodeId) +
105             ", nodeTag: " + nodeError.nodeTag + ", sourceKey: " + nodeError.sourceKey +
106             ", sourceTag: " + nodeError.sourceTag +
107             ", errorCode: " + std::to_string(nodeError.state) + ", errorTime: " +
108             ConvertTimestampToStr(nodeError.errorTime));
109     }
110 }
111 } // namespace OHOS::Ace
112