• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "ohresmgr.h"
17 
18 #include <securec.h>
19 #include "drawable_descriptor.h"
20 #include "hilog/log_cpp.h"
21 #include "resource_manager_impl.h"
22 #include "rstate.h"
23 
24 using namespace OHOS::Global::Resource;
25 using namespace OHOS::HiviewDFX;
26 using namespace OHOS::Ace::Napi;
27 
28 namespace {
29     constexpr HiLogLabel LABEL = {LOG_CORE, 0xD001E00, "NativeResourceManager"};
30 }
31 
32 struct NativeResourceManager {
33     std::shared_ptr<ResourceManager> resManager = nullptr;
34 };
35 
OH_ResourceManager_GetMediaBase64(const NativeResourceManager * mgr,uint32_t resId,char ** resultValue,uint64_t * resultLen,uint32_t density)36 ResourceManager_ErrorCode OH_ResourceManager_GetMediaBase64(const NativeResourceManager *mgr, uint32_t resId,
37     char **resultValue, uint64_t *resultLen, uint32_t density)
38 {
39     if (mgr == nullptr || resultValue == nullptr || resultLen == nullptr) {
40         return ResourceManager_ErrorCode::ERROR_CODE_INVALID_INPUT_PARAMETER;
41     }
42     std::string tempResultValue;
43     RState state = mgr->resManager->GetMediaBase64DataById(resId, tempResultValue, density);
44     ResourceManager_ErrorCode errorCode = static_cast<ResourceManager_ErrorCode>(state);
45     if (errorCode != ResourceManager_ErrorCode::SUCCESS) {
46         HiLog::Error(LABEL, "failed get media base64 id = %{public}d, errorCode = %{public}d", resId, errorCode);
47         return errorCode;
48     }
49     *resultValue = (char*)malloc(tempResultValue.size() + 1);
50     if (*resultValue == nullptr) {
51         HiLog::Error(LABEL, "GetMediaBase64 malloc error");
52         return ResourceManager_ErrorCode::ERROR_CODE_OUT_OF_MEMORY;
53     }
54     if (strncpy_s(*resultValue, tempResultValue.size() + 1, tempResultValue.c_str(), tempResultValue.size()) != 0) {
55         HiLog::Error(LABEL, "GetMediaBase64 strncpy_s error");
56         free(*resultValue);
57         *resultValue = nullptr;
58         return ResourceManager_ErrorCode::ERROR_CODE_OUT_OF_MEMORY;
59     }
60     *resultLen = static_cast<uint64_t>(tempResultValue.size());
61     return errorCode;
62 }
63 
OH_ResourceManager_GetMediaBase64ByName(const NativeResourceManager * mgr,const char * resName,char ** resultValue,uint64_t * resultLen,uint32_t density)64 ResourceManager_ErrorCode OH_ResourceManager_GetMediaBase64ByName(const NativeResourceManager *mgr,
65     const char *resName, char **resultValue, uint64_t *resultLen, uint32_t density)
66 {
67     if (mgr == nullptr || resultValue == nullptr || resultLen == nullptr) {
68         return ResourceManager_ErrorCode::ERROR_CODE_INVALID_INPUT_PARAMETER;
69     }
70 
71     std::string tempResultValue;
72     RState state = mgr->resManager->GetMediaBase64DataByName(resName, tempResultValue, density);
73     ResourceManager_ErrorCode errorCode = static_cast<ResourceManager_ErrorCode>(state);
74     if (errorCode != ResourceManager_ErrorCode::SUCCESS) {
75         HiLog::Error(LABEL, "failed get media base64 name = %{public}s, errorCode = %{public}d", resName, errorCode);
76         return errorCode;
77     }
78     *resultValue = (char*)malloc(tempResultValue.size() + 1);
79     if (*resultValue == nullptr) {
80         HiLog::Error(LABEL, "GetMediaBase64Byname malloc error");
81         return ResourceManager_ErrorCode::ERROR_CODE_OUT_OF_MEMORY;
82     }
83     if (strncpy_s(*resultValue, tempResultValue.size() + 1, tempResultValue.c_str(), tempResultValue.size()) != 0) {
84         HiLog::Error(LABEL, "GetMediaBase64Byname strncpy_s error");
85         free(*resultValue);
86         *resultValue = nullptr;
87         return ResourceManager_ErrorCode::ERROR_CODE_OUT_OF_MEMORY;
88     }
89     *resultLen = static_cast<uint64_t>(tempResultValue.size());
90     return errorCode;
91 }
92 
OH_ResourceManager_GetMedia(const NativeResourceManager * mgr,uint32_t resId,uint8_t ** resultValue,uint64_t * resultLen,uint32_t density)93 ResourceManager_ErrorCode OH_ResourceManager_GetMedia(const NativeResourceManager *mgr, uint32_t resId,
94     uint8_t **resultValue, uint64_t *resultLen, uint32_t density)
95 {
96     if (mgr == nullptr || resultValue == nullptr || resultLen == nullptr) {
97         return ResourceManager_ErrorCode::ERROR_CODE_INVALID_INPUT_PARAMETER;
98     }
99 
100     std::unique_ptr<uint8_t[]> tempResultValue;
101     size_t len;
102     RState state = mgr->resManager->GetMediaDataById(resId, len, tempResultValue, density);
103     ResourceManager_ErrorCode errorCode = static_cast<ResourceManager_ErrorCode>(state);
104     if (errorCode != ResourceManager_ErrorCode::SUCCESS) {
105         HiLog::Error(LABEL, "failed get media resource id = %{public}d, errorCode = %{public}d", resId, errorCode);
106         return errorCode;
107     }
108     uint8_t *temPtr = tempResultValue.get();
109     *resultValue = static_cast<uint8_t*>(malloc(len));
110     if (*resultValue == nullptr) {
111         HiLog::Error(LABEL, "GetMedia malloc error");
112         return ResourceManager_ErrorCode::ERROR_CODE_OUT_OF_MEMORY;
113     }
114     std::copy(temPtr, temPtr + len, *resultValue);
115     *resultLen = static_cast<uint64_t>(len);
116     return errorCode;
117 }
118 
OH_ResourceManager_GetMediaByName(const NativeResourceManager * mgr,const char * resName,uint8_t ** resultValue,uint64_t * resultLen,uint32_t density)119 ResourceManager_ErrorCode OH_ResourceManager_GetMediaByName(const NativeResourceManager *mgr, const char *resName,
120     uint8_t **resultValue, uint64_t *resultLen, uint32_t density)
121 {
122     if (mgr == nullptr || resultValue == nullptr || resultLen == nullptr) {
123         return ResourceManager_ErrorCode::ERROR_CODE_INVALID_INPUT_PARAMETER;
124     }
125 
126     std::unique_ptr<uint8_t[]> tempResultValue;
127     size_t len;
128     RState state = mgr->resManager->GetMediaDataByName(resName, len, tempResultValue, density);
129     ResourceManager_ErrorCode errorCode = static_cast<ResourceManager_ErrorCode>(state);
130     if (errorCode != ResourceManager_ErrorCode::SUCCESS) {
131         HiLog::Error(LABEL, "failed get media resource name = %{public}s, errorCode = %{public}d", resName, errorCode);
132         return errorCode;
133     }
134     uint8_t *temPtr = tempResultValue.get();
135     *resultValue = static_cast<uint8_t*>(malloc(len));
136     if (*resultValue == nullptr) {
137         HiLog::Error(LABEL, "GetMediaByName malloc error");
138         return ResourceManager_ErrorCode::ERROR_CODE_OUT_OF_MEMORY;
139     }
140     std::copy(temPtr, temPtr + len, *resultValue);
141     *resultLen = static_cast<uint64_t>(len);
142     return errorCode;
143 }
144 
OH_ResourceManager_GetDrawableDescriptor(const NativeResourceManager * mgr,uint32_t resId,ArkUI_DrawableDescriptor ** drawableDescriptor,uint32_t density,uint32_t type)145 ResourceManager_ErrorCode OH_ResourceManager_GetDrawableDescriptor(const NativeResourceManager *mgr,
146     uint32_t resId, ArkUI_DrawableDescriptor **drawableDescriptor, uint32_t density, uint32_t type)
147 {
148     if (mgr == nullptr || drawableDescriptor == nullptr) {
149         return ResourceManager_ErrorCode::ERROR_CODE_INVALID_INPUT_PARAMETER;
150     }
151     RState state = RState::SUCCESS;
152     DrawableDescriptor::DrawableType drawableType;
153     if (type == 1) {
154         std::string themeMask = mgr->resManager->GetThemeMask();
155         std::pair<std::unique_ptr<uint8_t[]>, size_t> foregroundInfo;
156         std::pair<std::unique_ptr<uint8_t[]>, size_t> backgroundInfo;
157         state = mgr->resManager->GetThemeIcons(resId, foregroundInfo, backgroundInfo, density);
158         if (state == RState::SUCCESS) {
159             auto descriptor = DrawableDescriptorFactory::Create(foregroundInfo, backgroundInfo,
160             themeMask, drawableType, mgr->resManager);
161             *drawableDescriptor = OH_ArkUI_CreateFromNapiDrawable(descriptor.get());
162             return (*drawableDescriptor != nullptr) ? ResourceManager_ErrorCode::SUCCESS :
163                 ResourceManager_ErrorCode::ERROR_CODE_RES_NOT_FOUND_BY_ID;
164         }
165     }
166     auto descriptor = DrawableDescriptorFactory::Create(resId, mgr->resManager, state, drawableType, density);
167     if (state != RState::SUCCESS) {
168         HiLog::Error(LABEL, "Failed to Create drawableDescriptor");
169         return static_cast<ResourceManager_ErrorCode>(state);
170     }
171     *drawableDescriptor = OH_ArkUI_CreateFromNapiDrawable(descriptor.get());
172     return (*drawableDescriptor != nullptr) ? ResourceManager_ErrorCode::SUCCESS :
173         ResourceManager_ErrorCode::ERROR_CODE_RES_NOT_FOUND_BY_ID;
174 }
175 
OH_ResourceManager_GetDrawableDescriptorByName(const NativeResourceManager * mgr,const char * resName,ArkUI_DrawableDescriptor ** drawableDescriptor,uint32_t density,uint32_t type)176 ResourceManager_ErrorCode OH_ResourceManager_GetDrawableDescriptorByName(const NativeResourceManager *mgr,
177     const char *resName, ArkUI_DrawableDescriptor **drawableDescriptor, uint32_t density, uint32_t type)
178 {
179     if (mgr == nullptr || drawableDescriptor == nullptr) {
180         return ResourceManager_ErrorCode::ERROR_CODE_INVALID_INPUT_PARAMETER;
181     }
182     RState state = RState::SUCCESS;
183     DrawableDescriptor::DrawableType drawableType;
184     if (type == 1) {
185         std::string themeMask = mgr->resManager->GetThemeMask();
186         std::pair<std::unique_ptr<uint8_t[]>, size_t> foregroundInfo;
187         std::pair<std::unique_ptr<uint8_t[]>, size_t> backgroundInfo;
188         state = mgr->resManager->GetThemeIcons(0, foregroundInfo, backgroundInfo, density);
189         if (state == RState::SUCCESS) {
190             auto descriptor = DrawableDescriptorFactory::Create(foregroundInfo, backgroundInfo,
191             themeMask, drawableType, mgr->resManager);
192             *drawableDescriptor = OH_ArkUI_CreateFromNapiDrawable(descriptor.get());
193             return (*drawableDescriptor != nullptr) ? ResourceManager_ErrorCode::SUCCESS :
194                 ResourceManager_ErrorCode::ERROR_CODE_RES_NOT_FOUND_BY_ID;
195         }
196     }
197     if (type == 2) { // 2 means get the dynamic icon from theme
198         std::pair<std::unique_ptr<uint8_t[]>, size_t> iconInfo;
199         state = mgr->resManager->GetDynamicIcon(resName, iconInfo, density);
200         if (state == RState::SUCCESS) {
201             auto descriptor = std::make_unique<DrawableDescriptor>(std::move(iconInfo.first), iconInfo.second);
202             *drawableDescriptor = OH_ArkUI_CreateFromNapiDrawable(descriptor.get());
203             return (*drawableDescriptor != nullptr) ? ResourceManager_ErrorCode::SUCCESS :
204                 ResourceManager_ErrorCode::ERROR_CODE_RES_NOT_FOUND_BY_ID;
205         }
206     }
207 
208     auto descriptor = DrawableDescriptorFactory::Create(resName, mgr->resManager, state, drawableType, density);
209     if (state != RState::SUCCESS) {
210         HiLog::Error(LABEL, "Failed to Create drawableDescriptor");
211         return static_cast<ResourceManager_ErrorCode>(state);
212     }
213     *drawableDescriptor = OH_ArkUI_CreateFromNapiDrawable(descriptor.get());
214     return (*drawableDescriptor != nullptr) ? ResourceManager_ErrorCode::SUCCESS :
215         ResourceManager_ErrorCode::ERROR_CODE_RES_NOT_FOUND_BY_ID;
216 }