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 RESOURCE_MANAGER_IMPL_H
17 #define RESOURCE_MANAGER_IMPL_H
18 
19 #include "foundation/ability/ability_runtime/interfaces/kits/native/appkit/ability_runtime/context/context.h"
20 #include "resource_manager.h"
21 #include "ffi_remote_data.h"
22 #include "resource_manager_interface.h"
23 #include "drawable_descriptor/drawable_descriptor.h"
24 
25 #include <cstdint>
26 #include <string>
27 
28 namespace OHOS::Resource {
29 
30 class ResourceManagerImpl : public virtual IResourceManager, public OHOS::FFI::FFIData {
31 public:
GetRuntimeType()32     OHOS::FFI::RuntimeType *GetRuntimeType() override
33     {
34         return GetClassType();
35     }
36     explicit ResourceManagerImpl(OHOS::AbilityRuntime::Context* context);
37 
38     int32_t CloseRawFd(const std::string &name) override;
39 
40     int32_t GetRawFd(const std::string &rawFileName,
41         Global::Resource::ResourceManager::RawFileDescriptor &descriptor) override;
42 
43     int32_t GetRawFileContent(const std::string &name, size_t &len, std::unique_ptr<uint8_t[]> &outValue) override;
44 
45     int32_t GetRawFileList(const std::string &rawDirPath, std::vector<std::string>& rawfileList) override;
46 
47     int32_t GetPluralStringValue(uint32_t resId, int64_t num, std::string &outValue) override;
48 
49     int32_t GetPluralStringValue(const char *name, int64_t num, std::string &outValue) override;
50 
51     int32_t GetStringArrayValue(uint32_t resId, std::vector<std::string> &outValue) override;
52 
53     int32_t GetStringArrayByName(const char *name, std::vector<std::string> &outValue) override;
54 
55     int32_t GetString(uint32_t resId, std::string &outValue) override;
56 
57     int32_t GetStringByName(const char *name, std::string &outValue) override;
58 
59     int32_t AddResource(const char *path) override;
60 
61     int32_t RemoveResource(const char *path) override;
62 
63     int32_t GetColorByName(const char *name, uint32_t &outValue) override;
64 
65     int32_t GetColorById(uint32_t id, uint32_t &outValue) override;
66 
67     int32_t GetBooleanById(uint32_t id, bool &outValue) override;
68 
69     int32_t GetBooleanByName(const char *name, bool &outValue) override;
70 
71     int32_t GetIntegerById(uint32_t id, int &outValue) override;
72 
73     int32_t GetIntegerByName(const char *name, int &outValue) override;
74 
75     int32_t GetFloatById(uint32_t id, float &outValue) override;
76 
77     int32_t GetFloatByName(const char *name, float &outValue) override;
78 
79     void GetConfiguration(Configuration &configuration) override;
80 
81     void GetDeviceCapability(DeviceCapability &deviceCapability) override;
82 
83     int32_t GetMediaDataByName(const char *name, size_t &len, std::unique_ptr<uint8_t[]> &outValue,
84         uint32_t density) override;
85 
86     int32_t GetMediaDataById(uint32_t id, size_t &len, std::unique_ptr<uint8_t[]> &outValue,
87         uint32_t density) override;
88 
89     int32_t GetMediaContentBase64ById(uint32_t id, std::string &outValue, uint32_t density) override;
90 
91     int32_t GetMediaContentBase64ByName(const char *name, std::string &outValue, uint32_t density) override;
92 
93     int32_t GetDrawableDescriptor(uint32_t id, int64_t &outValue, uint32_t density) override;
94 
95     int32_t GetDrawableDescriptorByName(const char *name, int64_t &outValue, uint32_t density) override;
96 
97     void GetLocales(bool includeSystem, std::vector<std::string> &outValue) override;
98 
99     bool GetHapResourceManager(Global::Resource::ResourceManager::Resource resource,
100         std::shared_ptr<Global::Resource::ResourceManager> &resMgr, int32_t &resId);
101 private:
102     friend class OHOS::FFI::RuntimeType;
103     friend class OHOS::FFI::TypeBase;
GetClassType()104     static OHOS::FFI::RuntimeType *GetClassType()
105     {
106         static OHOS::FFI::RuntimeType runtimeType =
107             OHOS::FFI::RuntimeType::Create<OHOS::FFI::FFIData>("ResourceManagerImpl");
108         return &runtimeType;
109     }
110     std::shared_ptr<Global::Resource::ResourceManager> resMgr_;
111     bool isSystem_ = false;
112     std::string bundleName_;
113     std::shared_ptr<AbilityRuntime::Context> context_;
114     std::string GetLocale(std::unique_ptr<Global::Resource::ResConfig> &cfg);
115 };
116 
117 class DrawableDescriptorImpl : public OHOS::FFI::FFIData {
118 public:
GetRuntimeType()119     OHOS::FFI::RuntimeType *GetRuntimeType() override
120     {
121         return GetClassType();
122     }
DrawableDescriptorImpl(OHOS::Ace::Napi::DrawableDescriptor * drawableDescriptor)123     explicit DrawableDescriptorImpl(OHOS::Ace::Napi::DrawableDescriptor* drawableDescriptor)
124         :drawableDescriptor_(std::shared_ptr<OHOS::Ace::Napi::DrawableDescriptor>(drawableDescriptor)){};
125 private:
126     friend class OHOS::FFI::RuntimeType;
127     friend class OHOS::FFI::TypeBase;
GetClassType()128     static OHOS::FFI::RuntimeType *GetClassType()
129     {
130         static OHOS::FFI::RuntimeType runtimeType =
131             OHOS::FFI::RuntimeType::Create<OHOS::FFI::FFIData>("DrawableDescriptorImpl");
132         return &runtimeType;
133     }
134     std::shared_ptr<OHOS::Ace::Napi::DrawableDescriptor> drawableDescriptor_;
135 };
136 
137 OHOS::Ace::Napi::DrawableDescriptor* GetDrawableDescriptorPtr(uint32_t id,
138     std::shared_ptr<Global::Resource::ResourceManager> resMgr, uint32_t density, Global::Resource::RState &state);
139 } // namespace OHOS::Resource
140 
141 #endif // RESOURCE_MANAGER_IMPL_H
142