1 /*
2  * Copyright (c) 2021 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 FOUNDATION_ACE_ADAPTER_OHOS_OSAL_RESOURCE_ADAPTER_IMPL_H
17 #define FOUNDATION_ACE_ADAPTER_OHOS_OSAL_RESOURCE_ADAPTER_IMPL_H
18 
19 #include <mutex>
20 #include <shared_mutex>
21 
22 #include "resource_manager.h"
23 
24 #include "base/image/pixel_map.h"
25 #include "core/components/theme/resource_adapter.h"
26 
27 namespace OHOS::Ace {
28 
29 class ResourceAdapterImpl : public ResourceAdapter {
30     DECLARE_ACE_TYPE(ResourceAdapterImpl, ResourceAdapter);
31 
32 public:
33     ResourceAdapterImpl() = default;
34     ~ResourceAdapterImpl() override = default;
35 
36     void Init(const ResourceInfo& resourceInfo) override;
37     void UpdateConfig(const ResourceConfiguration& config, bool themeFlag = false) override;
38 
39     RefPtr<ThemeStyle> GetTheme(int32_t themeId) override;
40 
41     Color GetColor(uint32_t resId) override;
42     Color GetColorByName(const std::string& resName) override;
43     Dimension GetDimension(uint32_t resId) override;
44     Dimension GetDimensionByName(const std::string& resName) override;
45     std::string GetString(uint32_t resId) override;
46     std::string GetStringByName(const std::string& resName) override;
47     std::string GetPluralString(uint32_t resId, int quantity) override;
48     std::string GetPluralStringByName(const std::string& resName, int quantity) override;
49     std::vector<std::string> GetStringArray(uint32_t resId) const override;
50     std::vector<std::string> GetStringArrayByName(const std::string& resName) const override;
51     double GetDouble(uint32_t resId) override;
52     double GetDoubleByName(const std::string& resName) override;
53     int32_t GetInt(uint32_t resId) override;
54     int32_t GetIntByName(const std::string& resName) override;
55     std::vector<uint32_t> GetIntArray(uint32_t resId) const override;
56     std::vector<uint32_t> GetIntArrayByName(const std::string& resName) const override;
57     bool GetBoolean(uint32_t resId) const override;
58     bool GetBooleanByName(const std::string& resName) const override;
59     std::shared_ptr<Media::PixelMap> GetPixelMap(uint32_t resId) override;
60     std::string GetMediaPath(uint32_t resId) override;
61     std::string GetMediaPathByName(const std::string& resName) override;
62     std::string GetRawfile(const std::string& fileName) override;
63     bool GetRawFileData(const std::string& rawFile, size_t& len, std::unique_ptr<uint8_t[]>& dest) override;
64     bool GetRawFileData(const std::string& rawFile, size_t& len, std::unique_ptr<uint8_t[]>& dest,
65         const std::string& bundleName, const std::string& moduleName) override;
66     bool GetMediaData(uint32_t resId, size_t& len, std::unique_ptr<uint8_t[]>& dest) override;
67     bool GetMediaData(uint32_t resId, size_t& len, std::unique_ptr<uint8_t[]>& dest, const std::string& bundleName,
68         const std::string& moduleName) override;
69     bool GetMediaData(const std::string& resName, size_t& len, std::unique_ptr<uint8_t[]>& dest) override;
70     bool GetMediaData(const std::string& resName, size_t& len, std::unique_ptr<uint8_t[]>& dest,
71         const std::string& bundleName, const std::string& moduleName) override;
72     void UpdateResourceManager(const std::string& bundleName, const std::string& moduleName) override;
73     bool GetRawFileDescription(const std::string& rawfileName, RawfileDescription& rawfileDescription) const override;
74     bool GetMediaById(const int32_t& resId, std::string& mediaPath) const override;
75     uint32_t GetResourceLimitKeys() const override;
76     uint32_t GetSymbolById(uint32_t resId) const override;
77 
78 private:
79     std::string GetActualResourceName(const std::string& resName) const;
80 
GetResourceManager()81     inline std::shared_ptr<Global::Resource::ResourceManager> GetResourceManager() const
82     {
83         std::shared_lock<std::shared_mutex> lock(resourceMutex_);
84         return resourceManager_;
85     }
86 
87     std::shared_ptr<Global::Resource::ResourceManager> resourceManager_;
88     std::shared_ptr<Global::Resource::ResourceManager> sysResourceManager_;
89     std::map<std::pair<std::string, std::string>, std::shared_ptr<Global::Resource::ResourceManager>> resourceManagers_;
90     std::string packagePathStr_;
91     // protect resourceManager_
92     mutable std::shared_mutex resourceMutex_;
93     std::shared_ptr<Global::Resource::ResConfig> resConfig_;
94     ACE_DISALLOW_COPY_AND_MOVE(ResourceAdapterImpl);
95 };
96 } // namespace OHOS::Ace
97 
98 #endif // FOUNDATION_ACE_ADAPTER_OHOS_OSAL_RESOURCE_ADAPTER_IMPL_H
99