• 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(RefPtr<ResourceObject> & resourceObject)33 RefPtr<ResourceAdapter> ResourceManager::GetOrCreateResourceAdapter(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 
DumpResLoadError()58 void ResourceManager::DumpResLoadError()
59 {
60     std::unique_lock<std::shared_mutex> lock(errorMutex_);
61     auto resLoadErrorSize = resourceErrorList_.size();
62     DumpLog::GetInstance().Print("----------ResourceLoadWrrorInfo----------");
63     if (resLoadErrorSize == 0) {
64         DumpLog::GetInstance().Print("No resource load error have occurred.");
65         return;
66     }
67 
68     DumpLog::GetInstance().Print("ResourceLoadErrorTimes: " + std::to_string(resLoadErrorSize));
69     for (const auto& nodeError : resourceErrorList_) {
70         DumpLog::GetInstance().Print(1, "Node: " + std::to_string(nodeError.nodeId) +
71             ", nodeTag: " + nodeError.nodeTag + ", sourceKey: " + nodeError.sourceKey +
72             ", sourceTag: " + nodeError.sourceTag +
73             ", errorCode: " + std::to_string(nodeError.state) + ", errorTime: " +
74             ConvertTimestampToStr(nodeError.errorTime));
75     }
76 }
77 } // namespace OHOS::Ace
78