• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2025 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 #ifndef OHOS_RESOURCE_MANAGER_HAPRESOURCEMANAGER_H
16 #define OHOS_RESOURCE_MANAGER_HAPRESOURCEMANAGER_H
17 
18 #include <shared_mutex>
19 #include "hap_resource.h"
20 #include "res_config_impl.h"
21 #include "res_desc.h"
22 #include "resource_manager.h"
23 
24 #ifdef SUPPORT_GRAPHICS
25 #include <unicode/plurrule.h>
26 #endif
27 
28 namespace OHOS {
29 namespace Global {
30 namespace Resource {
31 class HapResourceManager {
32 public:
33     HapResourceManager(const HapResourceManager &other) = delete;
34     HapResourceManager operator=(const HapResourceManager &other) = delete;
35 
36     static HapResourceManager& GetInstance();
37 
38     /**
39      * Load a HapResource.
40      *
41      * @param path hap file path
42      * @param defaultConfig  match defaultConfig to keys of index file, only parse the matched keys.
43      *     'null' means parse all keys.
44      * @param isSystem If `isSystem` is true, the package is marked as a system package and allows some functions to
45      *     filter out this package when computing what configurations/resources are available.
46      * @param isOverlay If 'isOverlay' is true, the package is marked as an overlay package and overlay resource can
47      *     replace non-overlay resource.
48      * @return the resource if create load success, else nullptr
49      */
50     const std::shared_ptr<HapResource> Load(const char* path, std::shared_ptr<ResConfigImpl> &defaultConfig,
51         bool isSystem = false, bool isOverlay = false, uint32_t selectedTypes = SELECT_ALL);
52 
53     /**
54      * Load overlay resources
55      * @param path the resources.index file path
56      * @param overlayPath  the resource overlay path
57      * @param defaultConfig the resource config
58      * @param isSystem judge the overlay is system or not
59      * @return the map of overlay resource path and resource info if success, else null
60      */
61     const std::unordered_map<std::string, std::shared_ptr<HapResource>> LoadOverlays(
62         const std::string &path, const std::vector<std::string> &overlayPath,
63         std::shared_ptr<ResConfigImpl> &defaultConfig, bool isSystem = false);
64 
65     /**
66      * Put hapResource into the map of HapResourceManager
67      *
68      * @param path the resources.index file path
69      * @param pResource hapResource
70      * @return th resource
71      */
72     std::shared_ptr<HapResource> PutAndGetResource(const std::string &path, std::shared_ptr<HapResource> pResource);
73 
74     /**
75      * Put patch resource
76      *
77      * @param path the resources.index file path
78      * @param patchPath the patch path
79      * @return Whether patch resource is put successfully
80      */
81     bool PutPatchResource(const std::string &path, const std::string &patchPath);
82 
83 #if defined(__ARKUI_CROSS__)
84     /**
85      * remove resource from map
86      *
87      * @param path the resources.index file path
88      */
89     void RemoveHapResource(const std::string &path);
90 #endif
91 
92 private:
93     HapResourceManager() = default;
94 
95     /**
96      * get resource from map
97      * @param path hap file path
98      * @return the resource if map contain the hap, else nullptr
99      */
100     std::shared_ptr<HapResource> GetHapResource(const std::string &path);
101 
102     std::shared_mutex mutexRw_;
103     std::unordered_map<std::string, std::weak_ptr<HapResource>> hapResourceMap_;
104 };
105 } // namespace Resource
106 } // namespace Global
107 } // namespace OHOS
108 #endif