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 #ifndef META_SRC_RESOURCE_FILE_RESOURCE_MANAGER_H 17 #define META_SRC_RESOURCE_FILE_RESOURCE_MANAGER_H 18 19 #include <mutex> 20 #include <shared_mutex> 21 #include <string> 22 23 #include <base/containers/unordered_map.h> 24 #include <core/resources/intf_resource_manager.h> 25 26 #include <meta/ext/implementation_macros.h> 27 #include <meta/ext/object.h> 28 29 META_BEGIN_NAMESPACE() 30 31 struct ResourceData : CORE_NS::ResourceInfo { 32 CORE_NS::IResource::Ptr object; 33 }; 34 35 class FileResourceManager : public IntroduceInterfaces<MetaObject, CORE_NS::IResourceManager> { 36 META_OBJECT(FileResourceManager, ClassId::FileResourceManager, IntroduceInterfaces) 37 public: 38 FileResourceManager(); 39 40 void AddListener(const IResourceListener::Ptr&) override; 41 void RemoveListener(const IResourceListener::Ptr&) override; 42 bool AddResourceType(CORE_NS::IResourceType::Ptr) override; 43 bool RemoveResourceType(const CORE_NS::ResourceType& type) override; 44 BASE_NS::vector<CORE_NS::IResourceType::Ptr> GetResourceTypes() const override; 45 Result Import(BASE_NS::string_view url) override; 46 BASE_NS::vector<CORE_NS::ResourceInfo> GetResourceInfos( 47 const BASE_NS::array_view<const CORE_NS::MatchingResourceId>& selection) const override; 48 BASE_NS::vector<BASE_NS::string> GetResourceGroups() const override; 49 CORE_NS::ResourceInfo GetResourceInfo(const CORE_NS::ResourceId&) const override; 50 CORE_NS::IResource::Ptr GetResource( 51 const CORE_NS::ResourceId&, const BASE_NS::shared_ptr<CORE_NS::IInterface>& context) override; 52 BASE_NS::vector<CORE_NS::IResource::Ptr> GetResources( 53 const BASE_NS::array_view<const CORE_NS::MatchingResourceId>& selection, 54 const BASE_NS::shared_ptr<CORE_NS::IInterface>& context) override; 55 Result Export(BASE_NS::string_view filePath, 56 const BASE_NS::array_view<const CORE_NS::MatchingResourceId>& selection) override; 57 58 bool AddResource(const CORE_NS::IResource::Ptr& resource) override; 59 bool AddResource(const CORE_NS::IResource::Ptr& resource, BASE_NS::string_view path) override; 60 bool AddResource(const CORE_NS::ResourceId&, const CORE_NS::ResourceType&, BASE_NS::string_view path, 61 const CORE_NS::IResourceOptions::ConstPtr&) override; 62 63 bool ReloadResource( 64 const CORE_NS::IResource::Ptr& resource, const BASE_NS::shared_ptr<CORE_NS::IInterface>& context) override; 65 bool RenameResource(const CORE_NS::ResourceId&, const CORE_NS::ResourceId&) override; 66 bool PurgeResource(const CORE_NS::ResourceId&) override; 67 bool RemoveResource(const CORE_NS::ResourceId&) override; 68 size_t PurgeGroup(BASE_NS::string_view group) override; 69 bool RemoveGroup(BASE_NS::string_view group) override; 70 void RemoveAllResources() override; 71 72 Result ExportResourcePayload(const CORE_NS::IResource::Ptr& resource) override; 73 74 void SetFileManager(CORE_NS::IFileManager::Ptr fileManager) override; 75 76 private: 77 CORE_NS::IResource::Ptr ConstructResource( 78 const CORE_NS::ResourceId& id, const BASE_NS::shared_ptr<CORE_NS::IInterface>& context); 79 Result SaveResourceData(const ResourceData& r); 80 CORE_NS::ResourceId ReadHeader(const std::string& line); 81 bool ReadHeaders(CORE_NS::IFile& file, BASE_NS::vector<CORE_NS::ResourceId>& result); 82 BASE_NS::shared_ptr<ResourceData> FindResource(const CORE_NS::ResourceId& id); 83 bool UpdateOptions(ResourceData& r); 84 void Notify(const BASE_NS::vector<CORE_NS::ResourceId>& id, IResourceListener::EventType type); 85 86 private: 87 mutable std::shared_mutex mutex_; 88 BASE_NS::unordered_map<BASE_NS::Uid, CORE_NS::IResourceType::Ptr> types_; 89 BASE_NS::unordered_map<BASE_NS::string, BASE_NS::unordered_map<BASE_NS::string, BASE_NS::shared_ptr<ResourceData>>> 90 resources_; 91 CORE_NS::IFileManager::Ptr fileManager_; 92 mutable std::shared_mutex listenerMutex_; 93 BASE_NS::vector<IResourceListener::WeakPtr> listeners_; 94 }; 95 96 META_END_NAMESPACE() 97 98 #endif 99