• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 "resource_manager_impl.h"
17 
18 #include <cmath>
19 #include <cstdarg>
20 #include <cstdlib>
21 #include <cstring>
22 #include <fcntl.h>
23 #include <regex>
24 #include <sstream>
25 #include <sys/types.h>
26 #include <unistd.h>
27 #include <fstream>
28 
29 #if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__)
30 #include "hitrace_meter.h"
31 #endif
32 #include "hilog_wrapper.h"
33 #include "res_config.h"
34 #include "securec.h"
35 #include "utils/common.h"
36 #include "utils/string_utils.h"
37 #include "utils/utils.h"
38 
39 namespace OHOS {
40 namespace Global {
41 namespace Resource {
42 // default logLevel
43 #ifdef CONFIG_HILOG
44 LogLevel g_logLevel = LOG_INFO;
45 #endif
46 
CreateResourceManager()47 ResourceManager *CreateResourceManager()
48 {
49     ResourceManagerImpl *impl = new (std::nothrow) ResourceManagerImpl;
50     if (impl == nullptr) {
51         HILOG_ERROR("new ResourceManagerImpl failed when CreateResourceManager");
52         return nullptr;
53     }
54     if (impl->Init()) {
55         return impl;
56     } else {
57         delete (impl);
58         return nullptr;
59     }
60 }
61 
~ResourceManager()62 ResourceManager::~ResourceManager()
63 {}
64 
ResourceManagerImpl()65 ResourceManagerImpl::ResourceManagerImpl() : hapManager_(nullptr)
66 {
67     psueManager_ = new (std::nothrow) PsueManager();
68 }
69 
Init()70 bool ResourceManagerImpl::Init()
71 {
72     ResConfigImpl *resConfig = new (std::nothrow) ResConfigImpl;
73     if (resConfig == nullptr) {
74         HILOG_ERROR("new ResConfigImpl failed when ResourceManagerImpl::Init");
75         return false;
76     }
77     hapManager_ = new (std::nothrow) HapManager(resConfig);
78     if (hapManager_ == nullptr) {
79         delete (resConfig);
80         HILOG_ERROR("new HapManager failed when ResourceManagerImpl::Init");
81         return false;
82     }
83     if (Utils::isFileExist(SYSTEM_RESOURCE_OVERLAY_PATH) && Utils::isFileExist(SYSTEM_RESOURCE_PATH)) {
84         vector<string> overlayPaths;
85         overlayPaths.push_back(SYSTEM_RESOURCE_OVERLAY_PATH);
86         AddResource(SYSTEM_RESOURCE_PATH.c_str(), overlayPaths);
87         return true;
88     }
89 
90     if (Utils::isFileExist(SYSTEM_RESOURCE_OVERLAY_PATH_COMPRESSED) &&
91         Utils::isFileExist(SYSTEM_RESOURCE_PATH_COMPRESSED)) {
92         vector<string> overlayPaths;
93         overlayPaths.push_back(SYSTEM_RESOURCE_OVERLAY_PATH_COMPRESSED);
94         AddResource(SYSTEM_RESOURCE_PATH_COMPRESSED.c_str(), overlayPaths);
95         return true;
96     }
97 
98     if (Utils::isFileExist(SYSTEM_RESOURCE_PATH)) {
99         AddResource(SYSTEM_RESOURCE_PATH.c_str());
100     } else {
101         AddResource(SYSTEM_RESOURCE_PATH_COMPRESSED.c_str());
102     }
103     return true;
104 }
105 
GetStringById(uint32_t id,std::string & outValue)106 RState ResourceManagerImpl::GetStringById(uint32_t id, std::string &outValue)
107 {
108     const IdItem *idItem = hapManager_->FindResourceById(id);
109     if (idItem == nullptr) {
110         HILOG_ERROR("find resource by string id error");
111         return ERROR_CODE_RES_ID_NOT_FOUND;
112     }
113     RState state = GetString(idItem, outValue);
114     if (state != SUCCESS && state != ERROR_CODE_RES_REF_TOO_MUCH) {
115         return ERROR_CODE_RES_NOT_FOUND_BY_ID;
116     }
117     return state;
118 }
119 
GetStringByName(const char * name,std::string & outValue)120 RState ResourceManagerImpl::GetStringByName(const char *name, std::string &outValue)
121 {
122     const IdItem *idItem = hapManager_->FindResourceByName(name, ResType::STRING);
123     if (idItem == nullptr) {
124         HILOG_ERROR("find resource by string name error");
125         return ERROR_CODE_RES_NAME_NOT_FOUND;
126     }
127     RState state = GetString(idItem, outValue);
128     if (state != SUCCESS && state != ERROR_CODE_RES_REF_TOO_MUCH) {
129         return ERROR_CODE_RES_NOT_FOUND_BY_NAME;
130     }
131     return state;
132 }
133 
GetStringFormatById(std::string & outValue,uint32_t id,...)134 RState ResourceManagerImpl::GetStringFormatById(std::string &outValue, uint32_t id, ...)
135 {
136     const IdItem *idItem = hapManager_->FindResourceById(id);
137     std::string temp;
138     RState rState = GetString(idItem, temp);
139     if (rState != SUCCESS) {
140         return rState;
141     }
142     va_list args;
143     va_start(args, id);
144     outValue = FormatString(temp.c_str(), args);
145     va_end(args);
146     return SUCCESS;
147 }
148 
GetStringFormatByName(std::string & outValue,const char * name,...)149 RState ResourceManagerImpl::GetStringFormatByName(std::string &outValue, const char *name, ...)
150 {
151     const IdItem *idItem = hapManager_->FindResourceByName(name, ResType::STRING);
152     std::string temp;
153     RState rState = GetString(idItem, temp);
154     if (rState != SUCCESS) {
155         return rState;
156     }
157     va_list args;
158     va_start(args, name);
159     outValue = FormatString(temp.c_str(), args);
160     va_end(args);
161     return SUCCESS;
162 }
163 
GetString(const IdItem * idItem,std::string & outValue)164 RState ResourceManagerImpl::GetString(const IdItem *idItem, std::string &outValue)
165 {
166     // not found or type invalid
167     if (idItem == nullptr || idItem->resType_ != ResType::STRING) {
168         return NOT_FOUND;
169     }
170     RState ret = ResolveReference(idItem->value_, outValue);
171     if (isFakeLocale) {
172         ProcessPsuedoTranslate(outValue);
173     }
174     if (ret != SUCCESS) {
175         return ret;
176     }
177     return SUCCESS;
178 }
179 
GetStringArrayById(uint32_t id,std::vector<std::string> & outValue)180 RState ResourceManagerImpl::GetStringArrayById(uint32_t id, std::vector<std::string> &outValue)
181 {
182     const IdItem *idItem = hapManager_->FindResourceById(id);
183     if (idItem == nullptr) {
184         HILOG_ERROR("find resource by string array id error");
185         return ERROR_CODE_RES_ID_NOT_FOUND;
186     }
187     RState state = GetStringArray(idItem, outValue);
188     if (state != SUCCESS && state != ERROR_CODE_RES_REF_TOO_MUCH) {
189         return ERROR_CODE_RES_NOT_FOUND_BY_ID;
190     }
191     return state;
192 }
193 
GetStringArrayByName(const char * name,std::vector<std::string> & outValue)194 RState ResourceManagerImpl::GetStringArrayByName(const char *name, std::vector<std::string> &outValue)
195 {
196     const IdItem *idItem = hapManager_->FindResourceByName(name, ResType::STRINGARRAY);
197     if (idItem == nullptr) {
198         HILOG_ERROR("find resource by string array name error");
199         return ERROR_CODE_RES_NAME_NOT_FOUND;
200     }
201     RState state = GetStringArray(idItem, outValue);
202     if (state != SUCCESS && state != ERROR_CODE_RES_REF_TOO_MUCH) {
203         return ERROR_CODE_RES_NOT_FOUND_BY_NAME;
204     }
205     return state;
206 }
207 
GetStringArray(const IdItem * idItem,std::vector<std::string> & outValue)208 RState ResourceManagerImpl::GetStringArray(const IdItem *idItem, std::vector<std::string> &outValue)
209 {
210     // not found or type invalid
211     if (idItem == nullptr || idItem->resType_ != ResType::STRINGARRAY) {
212         return NOT_FOUND;
213     }
214     outValue.clear();
215 
216     for (size_t i = 0; i < idItem->values_.size(); ++i) {
217         std::string resolvedValue;
218         RState rrRet = ResolveReference(idItem->values_[i], resolvedValue);
219         if (rrRet != SUCCESS) {
220             HILOG_ERROR("ResolveReference failed, value:%s", idItem->values_[i].c_str());
221             return rrRet;
222         }
223         outValue.push_back(resolvedValue);
224     }
225     if (isFakeLocale) {
226         for (auto &iter : outValue) {
227             ProcessPsuedoTranslate(iter);
228         }
229     }
230     return SUCCESS;
231 }
232 
GetPatternById(uint32_t id,std::map<std::string,std::string> & outValue)233 RState ResourceManagerImpl::GetPatternById(uint32_t id, std::map<std::string, std::string> &outValue)
234 {
235     const IdItem *idItem = hapManager_->FindResourceById(id);
236     return GetPattern(idItem, outValue);
237 }
238 
GetPatternByName(const char * name,std::map<std::string,std::string> & outValue)239 RState ResourceManagerImpl::GetPatternByName(const char *name, std::map<std::string, std::string> &outValue)
240 {
241     const IdItem *idItem = hapManager_->FindResourceByName(name, ResType::PATTERN);
242     return GetPattern(idItem, outValue);
243 }
244 
GetPattern(const IdItem * idItem,std::map<std::string,std::string> & outValue)245 RState ResourceManagerImpl::GetPattern(const IdItem *idItem, std::map<std::string, std::string> &outValue)
246 {
247     // not found or type invalid
248     if (idItem == nullptr || idItem->resType_ != ResType::PATTERN) {
249         return NOT_FOUND;
250     }
251     return ResolveParentReference(idItem, outValue);
252 }
253 
GetPluralStringById(uint32_t id,int quantity,std::string & outValue)254 RState ResourceManagerImpl::GetPluralStringById(uint32_t id, int quantity, std::string &outValue)
255 {
256     const HapResource::ValueUnderQualifierDir *vuqd = hapManager_->FindQualifierValueById(id);
257     return GetPluralString(vuqd, quantity, outValue);
258 }
259 
GetPluralStringByName(const char * name,int quantity,std::string & outValue)260 RState ResourceManagerImpl::GetPluralStringByName(const char *name, int quantity, std::string &outValue)
261 {
262     const HapResource::ValueUnderQualifierDir *vuqd =
263         hapManager_->FindQualifierValueByName(name, ResType::PLURALS);
264     return GetPluralString(vuqd, quantity, outValue);
265 }
266 
GetPluralStringByIdFormat(std::string & outValue,uint32_t id,int quantity,...)267 RState ResourceManagerImpl::GetPluralStringByIdFormat(std::string &outValue, uint32_t id, int quantity, ...)
268 {
269     const HapResource::ValueUnderQualifierDir *vuqd = hapManager_->FindQualifierValueById(id);
270     if (vuqd == nullptr) {
271         HILOG_ERROR("find qualifier value by plural id error");
272         return ERROR_CODE_RES_ID_NOT_FOUND;
273     }
274     std::string temp;
275     RState rState = GetPluralString(vuqd, quantity, temp);
276     if (rState == ERROR_CODE_RES_REF_TOO_MUCH) {
277         return rState;
278     }
279     if (rState != SUCCESS) {
280         return ERROR_CODE_RES_NOT_FOUND_BY_ID;
281     }
282 
283     va_list args;
284     va_start(args, quantity);
285     outValue = FormatString(temp.c_str(), args);
286     va_end(args);
287 
288     return SUCCESS;
289 }
290 
GetPluralStringByNameFormat(std::string & outValue,const char * name,int quantity,...)291 RState ResourceManagerImpl::GetPluralStringByNameFormat(std::string &outValue, const char *name, int quantity, ...)
292 {
293     const HapResource::ValueUnderQualifierDir *vuqd =
294         hapManager_->FindQualifierValueByName(name, ResType::PLURALS);
295     if (vuqd == nullptr) {
296         HILOG_ERROR("find qualifier value by plural name error");
297         return ERROR_CODE_RES_NAME_NOT_FOUND;
298     }
299     std::string temp;
300     RState rState = GetPluralString(vuqd, quantity, temp);
301     if (rState == ERROR_CODE_RES_REF_TOO_MUCH) {
302         return rState;
303     }
304     if (rState != SUCCESS) {
305         return ERROR_CODE_RES_NOT_FOUND_BY_NAME;
306     }
307 
308     va_list args;
309     va_start(args, quantity);
310     outValue = FormatString(temp.c_str(), args);
311     va_end(args);
312 
313     return SUCCESS;
314 }
315 
GetPluralString(const HapResource::ValueUnderQualifierDir * vuqd,int quantity,std::string & outValue)316 RState ResourceManagerImpl::GetPluralString(const HapResource::ValueUnderQualifierDir *vuqd,
317     int quantity, std::string &outValue)
318 {
319     // not found or type invalid
320     if (vuqd == nullptr) {
321         return NOT_FOUND;
322     }
323     auto idItem = vuqd->GetIdItem();
324     if (idItem == nullptr || idItem->resType_ != ResType::PLURALS) {
325         return NOT_FOUND;
326     }
327     std::map<std::string, std::string> map;
328 
329     size_t startIdx = 0;
330     size_t loop = idItem->values_.size() / 2;
331     for (size_t i = 0; i < loop; ++i) {
332         // 2 means key and value appear in pairs
333         std::string key(idItem->values_[startIdx + i * 2]);
334         std::string value(idItem->values_[startIdx + i * 2 + 1]);
335         auto iter = map.find(key);
336         if (iter == map.end()) {
337             std::string resolvedValue;
338             RState rrRet = ResolveReference(value, resolvedValue);
339             if (rrRet != SUCCESS) {
340                 HILOG_ERROR("ResolveReference failed, value:%s", value.c_str());
341                 return rrRet;
342             }
343             map[key] = resolvedValue;
344         }
345     }
346 
347     std::string converted = hapManager_->GetPluralRulesAndSelect(quantity);
348     auto mapIter = map.find(converted);
349     if (mapIter == map.end()) {
350         mapIter = map.find("other");
351         if (mapIter == map.end()) {
352             return NOT_FOUND;
353         }
354     }
355     outValue = mapIter->second;
356     if (isFakeLocale) {
357         ProcessPsuedoTranslate(outValue);
358     }
359     return SUCCESS;
360 }
361 
ResolveReference(const std::string value,std::string & outValue)362 RState ResourceManagerImpl::ResolveReference(const std::string value, std::string &outValue)
363 {
364     int id;
365     ResType resType;
366     bool isRef = true;
367     int count = 0;
368     std::string refStr(value);
369     while (isRef) {
370         isRef = IdItem::IsRef(refStr, resType, id);
371         if (!isRef) {
372             outValue = refStr;
373             return SUCCESS;
374         }
375 
376         if (IdItem::IsArrayOfType(resType)) {
377             // can't be array
378             HILOG_ERROR("ref %s can't be array", refStr.c_str());
379             return ERROR;
380         }
381         const IdItem *idItem = hapManager_->FindResourceById(id);
382         if (idItem == nullptr) {
383             HILOG_ERROR("ref %s id not found", refStr.c_str());
384             return ERROR;
385         }
386         // unless compile bug
387         if (resType != idItem->resType_) {
388             HILOG_ERROR("impossible. ref %s type mismatch, found type: %d", refStr.c_str(), idItem->resType_);
389             return ERROR;
390         }
391 
392         refStr = idItem->value_;
393 
394         if (++count > MAX_DEPTH_REF_SEARCH) {
395             HILOG_ERROR("ref %s has re-ref too much", value.c_str());
396             return ERROR_CODE_RES_REF_TOO_MUCH;
397         }
398     }
399     return SUCCESS;
400 }
401 
ResolveParentReference(const IdItem * idItem,std::map<std::string,std::string> & outValue)402 RState ResourceManagerImpl::ResolveParentReference(const IdItem *idItem, std::map<std::string, std::string> &outValue)
403 {
404     // only pattern and theme
405     // ref always at idx 0
406     // child will cover parent
407     if (idItem == nullptr) {
408         return ERROR;
409     }
410     if (!(idItem->resType_ == THEME || idItem->resType_ == PATTERN)) {
411         HILOG_ERROR("only pattern and theme have parent: %d", idItem->resType_);
412         return ERROR;
413     }
414     outValue.clear();
415 
416     bool haveParent = false;
417     int count = 0;
418     const IdItem *currItem = idItem;
419     do {
420         haveParent = currItem->HaveParent();
421         size_t startIdx = haveParent ? 1 : 0;
422         // add currItem values into map when key is absent
423         // this make sure child covers parent
424         size_t loop = currItem->values_.size() / 2;
425         for (size_t i = 0; i < loop; ++i) {
426             // 2 means key and value appear in pairs
427             std::string key(currItem->values_[startIdx + i * 2]);
428             std::string value(currItem->values_[startIdx + i * 2 + 1]);
429             auto iter = outValue.find(key);
430             if (iter == outValue.end()) {
431                 std::string resolvedValue;
432                 RState rrRet = ResolveReference(value, resolvedValue);
433                 if (rrRet != SUCCESS) {
434                     HILOG_ERROR("ResolveReference failed, value:%s", value.c_str());
435                     return ERROR;
436                 }
437                 outValue[key] = resolvedValue;
438             }
439         }
440         if (haveParent) {
441             // get parent
442             int id;
443             ResType resType;
444             bool isRef = IdItem::IsRef(currItem->values_[0], resType, id);
445             if (!isRef) {
446                 HILOG_ERROR("something wrong, pls check HaveParent(). idItem: %s", idItem->ToString().c_str());
447                 return ERROR;
448             }
449             currItem = hapManager_->FindResourceById(id);
450             if (currItem == nullptr) {
451                 HILOG_ERROR("ref %s id not found", idItem->values_[0].c_str());
452                 return ERROR;
453             }
454         }
455 
456         if (++count > MAX_DEPTH_REF_SEARCH) {
457             HILOG_ERROR(" %u has too many parents", idItem->id_);
458             return ERROR;
459         }
460     } while (haveParent);
461 
462     return SUCCESS;
463 }
464 
GetBooleanById(uint32_t id,bool & outValue)465 RState ResourceManagerImpl::GetBooleanById(uint32_t id, bool &outValue)
466 {
467     const IdItem *idItem = hapManager_->FindResourceById(id);
468     if (idItem == nullptr) {
469         HILOG_ERROR("find resource by Boolean id error");
470         return ERROR_CODE_RES_ID_NOT_FOUND;
471     }
472     RState state = GetBoolean(idItem, outValue);
473     if (state != SUCCESS && state != ERROR_CODE_RES_REF_TOO_MUCH) {
474         return ERROR_CODE_RES_NOT_FOUND_BY_ID;
475     }
476     return state;
477 }
478 
GetBooleanByName(const char * name,bool & outValue)479 RState ResourceManagerImpl::GetBooleanByName(const char *name, bool &outValue)
480 {
481     const IdItem *idItem = hapManager_->FindResourceByName(name, ResType::BOOLEAN);
482     if (idItem == nullptr) {
483         HILOG_ERROR("find resource by Boolean name error");
484         return ERROR_CODE_RES_NAME_NOT_FOUND;
485     }
486     RState state = GetBoolean(idItem, outValue);
487     if (state != SUCCESS && state != ERROR_CODE_RES_REF_TOO_MUCH) {
488         return ERROR_CODE_RES_NOT_FOUND_BY_NAME;
489     }
490     return state;
491 }
492 
GetBoolean(const IdItem * idItem,bool & outValue)493 RState ResourceManagerImpl::GetBoolean(const IdItem *idItem, bool &outValue)
494 {
495     if (idItem == nullptr || idItem->resType_ != ResType::BOOLEAN) {
496         return NOT_FOUND;
497     }
498     std::string temp;
499     RState state = ResolveReference(idItem->value_, temp);
500     if (state == SUCCESS) {
501         if (strcmp(temp.c_str(), "true") == 0) {
502             outValue = true;
503             return SUCCESS;
504         }
505         if (strcmp(temp.c_str(), "false") == 0) {
506             outValue = false;
507             return SUCCESS;
508         }
509         return ERROR;
510     }
511     return state;
512 }
513 
GetFloatById(uint32_t id,float & outValue)514 RState ResourceManagerImpl::GetFloatById(uint32_t id, float &outValue)
515 {
516     const IdItem *idItem = hapManager_->FindResourceById(id);
517     if (idItem == nullptr) {
518         HILOG_ERROR("find resource by Float id error");
519         return ERROR_CODE_RES_ID_NOT_FOUND;
520     }
521     std::string unit;
522     RState state = GetFloat(idItem, outValue, unit);
523     if (state == SUCCESS) {
524         return RecalculateFloat(unit, outValue);
525     }
526     if (state != ERROR_CODE_RES_REF_TOO_MUCH) {
527         return ERROR_CODE_RES_NOT_FOUND_BY_ID;
528     }
529     return state;
530 }
531 
GetFloatById(uint32_t id,float & outValue,std::string & unit)532 RState ResourceManagerImpl::GetFloatById(uint32_t id, float &outValue, std::string &unit)
533 {
534     const IdItem *idItem = hapManager_->FindResourceById(id);
535     return GetFloat(idItem, outValue, unit);
536 }
537 
GetFloatByName(const char * name,float & outValue)538 RState ResourceManagerImpl::GetFloatByName(const char *name, float &outValue)
539 {
540     const IdItem *idItem = hapManager_->FindResourceByName(name, ResType::FLOAT);
541     if (idItem == nullptr) {
542         HILOG_ERROR("find resource by Float name error");
543         return ERROR_CODE_RES_NAME_NOT_FOUND;
544     }
545     std::string unit;
546     RState state = GetFloat(idItem, outValue, unit);
547     if (state == SUCCESS) {
548         return RecalculateFloat(unit, outValue);
549     }
550     if (state != ERROR_CODE_RES_REF_TOO_MUCH) {
551         return ERROR_CODE_RES_NOT_FOUND_BY_NAME;
552     }
553     return state;
554 }
555 
GetFloatByName(const char * name,float & outValue,std::string & unit)556 RState ResourceManagerImpl::GetFloatByName(const char *name, float &outValue, std::string &unit)
557 {
558     const IdItem *idItem = hapManager_->FindResourceByName(name, ResType::FLOAT);
559     return GetFloat(idItem, outValue, unit);
560 }
561 
RecalculateFloat(const std::string & unit,float & result)562 RState ResourceManagerImpl::RecalculateFloat(const std::string &unit, float &result)
563 {
564     ResConfigImpl rc;
565     GetResConfig(rc);
566     float density = rc.GetScreenDensity();
567     if (density == SCREEN_DENSITY_NOT_SET) {
568         HILOG_INFO("RecalculateFloat srcDensity SCREEN_DENSITY_NOT_SET ");
569         return SUCCESS;
570     }
571     if (unit == VIRTUAL_PIXEL) {
572         result = result * density;
573     } else if (unit == FONT_SIZE_PIXEL) {
574         float fontSizeDensity = density * ((fabs(fontRatio_) <= 1E-6) ? 1.0f : fontRatio_);
575         result = result * fontSizeDensity;
576     } else {
577         // no unit
578     }
579     return SUCCESS;
580 }
581 
ParseFloat(const std::string & strValue,float & result,std::string & unit)582 RState ResourceManagerImpl::ParseFloat(const std::string &strValue, float &result, std::string &unit)
583 {
584     std::regex reg("(\\+|-)?\\d+(\\.\\d+)? *(px|vp|fp)?");
585     std::smatch floatMatch;
586     if (!regex_search(strValue, floatMatch, reg)) {
587         HILOG_ERROR("not valid float value %{public}s", strValue.c_str());
588         return ERROR;
589     }
590     std::string matchString(floatMatch.str());
591     unit = floatMatch[floatMatch.size() - 1];
592     std::istringstream stream(matchString.substr(0, matchString.length() - unit.length()));
593     stream >> result;
594     return SUCCESS;
595 }
596 
GetFloat(const IdItem * idItem,float & outValue,std::string & unit)597 RState ResourceManagerImpl::GetFloat(const IdItem *idItem, float &outValue, std::string &unit)
598 {
599     if (idItem == nullptr || idItem->resType_ != ResType::FLOAT) {
600         return NOT_FOUND;
601     }
602     std::string temp;
603     RState state = ResolveReference(idItem->value_, temp);
604     if (state == SUCCESS) {
605         return ParseFloat(temp.c_str(), outValue, unit);
606     }
607     return state;
608 }
609 
GetIntegerById(uint32_t id,int & outValue)610 RState ResourceManagerImpl::GetIntegerById(uint32_t id, int &outValue)
611 {
612     const IdItem *idItem = hapManager_->FindResourceById(id);
613     if (idItem == nullptr) {
614         HILOG_ERROR("find resource by Integer id error");
615         return ERROR_CODE_RES_ID_NOT_FOUND;
616     }
617     RState state = GetInteger(idItem, outValue);
618     if (state != SUCCESS && state != ERROR_CODE_RES_REF_TOO_MUCH) {
619         return ERROR_CODE_RES_NOT_FOUND_BY_ID;
620     }
621     return state;
622 }
623 
GetIntegerByName(const char * name,int & outValue)624 RState ResourceManagerImpl::GetIntegerByName(const char *name, int &outValue)
625 {
626     const IdItem *idItem = hapManager_->FindResourceByName(name, ResType::INTEGER);
627     if (idItem == nullptr) {
628         HILOG_ERROR("find resource by Integer name error");
629         return ERROR_CODE_RES_NAME_NOT_FOUND;
630     }
631     RState state = GetInteger(idItem, outValue);
632     if (state != SUCCESS && state != ERROR_CODE_RES_REF_TOO_MUCH) {
633         return ERROR_CODE_RES_NOT_FOUND_BY_NAME;
634     }
635     return state;
636 }
637 
GetInteger(const IdItem * idItem,int & outValue)638 RState ResourceManagerImpl::GetInteger(const IdItem *idItem, int &outValue)
639 {
640     if (idItem == nullptr || idItem->resType_ != ResType::INTEGER) {
641         return NOT_FOUND;
642     }
643     std::string temp;
644     RState state = ResolveReference(idItem->value_, temp);
645     if (state == SUCCESS) {
646         outValue = stoi(temp);
647         return SUCCESS;
648     }
649     return state;
650 }
651 
GetColorById(uint32_t id,uint32_t & outValue)652 RState ResourceManagerImpl::GetColorById(uint32_t id, uint32_t &outValue)
653 {
654     const IdItem *idItem = hapManager_->FindResourceById(id);
655     return GetColor(idItem, outValue);
656 }
657 
GetColorByName(const char * name,uint32_t & outValue)658 RState ResourceManagerImpl::GetColorByName(const char *name, uint32_t &outValue)
659 {
660     const IdItem *idItem = hapManager_->FindResourceByName(name, ResType::COLOR);
661     return GetColor(idItem, outValue);
662 }
663 
GetColor(const IdItem * idItem,uint32_t & outValue)664 RState ResourceManagerImpl::GetColor(const IdItem *idItem, uint32_t &outValue)
665 {
666     if (idItem == nullptr || idItem->resType_ != ResType::COLOR) {
667         return NOT_FOUND;
668     }
669     std::string temp;
670     RState state = ResolveReference(idItem->value_, temp);
671     if (state == SUCCESS) {
672         return Utils::ConvertColorToUInt32(temp.c_str(), outValue);
673     }
674     return state;
675 }
676 
GetIntArrayById(uint32_t id,std::vector<int> & outValue)677 RState ResourceManagerImpl::GetIntArrayById(uint32_t id, std::vector<int> &outValue)
678 {
679     const IdItem *idItem = hapManager_->FindResourceById(id);
680     return GetIntArray(idItem, outValue);
681 }
682 
GetIntArrayByName(const char * name,std::vector<int> & outValue)683 RState ResourceManagerImpl::GetIntArrayByName(const char *name, std::vector<int> &outValue)
684 {
685     const IdItem *idItem = hapManager_->FindResourceByName(name, ResType::INTARRAY);
686     return GetIntArray(idItem, outValue);
687 }
688 
GetIntArray(const IdItem * idItem,std::vector<int> & outValue)689 RState ResourceManagerImpl::GetIntArray(const IdItem *idItem, std::vector<int> &outValue)
690 {
691     // not found or type invalid
692     if (idItem == nullptr || idItem->resType_ != ResType::INTARRAY) {
693         return NOT_FOUND;
694     }
695     outValue.clear();
696 
697     for (size_t i = 0; i < idItem->values_.size(); ++i) {
698         std::string resolvedValue;
699         RState rrRet = ResolveReference(idItem->values_[i], resolvedValue);
700         if (rrRet != SUCCESS) {
701             HILOG_ERROR("ResolveReference failed, value:%s", idItem->values_[i].c_str());
702             return ERROR;
703         }
704         outValue.push_back(stoi(resolvedValue));
705     }
706     return SUCCESS;
707 }
708 
GetThemeById(uint32_t id,std::map<std::string,std::string> & outValue)709 RState ResourceManagerImpl::GetThemeById(uint32_t id, std::map<std::string, std::string> &outValue)
710 {
711     const IdItem *idItem = hapManager_->FindResourceById(id);
712     return GetTheme(idItem, outValue);
713 }
714 
GetThemeByName(const char * name,std::map<std::string,std::string> & outValue)715 RState ResourceManagerImpl::GetThemeByName(const char *name, std::map<std::string, std::string> &outValue)
716 {
717     const IdItem *idItem = hapManager_->FindResourceByName(name, ResType::THEME);
718     return GetTheme(idItem, outValue);
719 }
720 
GetTheme(const IdItem * idItem,std::map<std::string,std::string> & outValue)721 RState ResourceManagerImpl::GetTheme(const IdItem *idItem, std::map<std::string, std::string> &outValue)
722 {
723 // not found or type invalid
724     if (idItem == nullptr || idItem->resType_ != ResType::THEME) {
725         return NOT_FOUND;
726     }
727     return ResolveParentReference(idItem, outValue);
728 }
729 
GetProfileById(uint32_t id,std::string & outValue)730 RState ResourceManagerImpl::GetProfileById(uint32_t id, std::string &outValue)
731 {
732     auto qd = hapManager_->FindQualifierValueById(id);
733     if (qd == nullptr) {
734         return NOT_FOUND;
735     }
736     return GetRawFile(qd, ResType::PROF, outValue);
737 }
738 
GetProfileByName(const char * name,std::string & outValue)739 RState ResourceManagerImpl::GetProfileByName(const char *name, std::string &outValue)
740 {
741     auto qd = hapManager_->FindQualifierValueByName(name, ResType::PROF);
742     if (qd == nullptr) {
743         return NOT_FOUND;
744     }
745     return GetRawFile(qd, ResType::PROF, outValue);
746 }
747 
GetMediaById(uint32_t id,std::string & outValue)748 RState ResourceManagerImpl::GetMediaById(uint32_t id, std::string &outValue)
749 {
750     auto qd = hapManager_->FindQualifierValueById(id);
751     if (qd == nullptr) {
752         HILOG_ERROR("find qualifier value by Media id error");
753         return ERROR_CODE_RES_ID_NOT_FOUND;
754     }
755     RState state = GetRawFile(qd, ResType::MEDIA, outValue);
756     if (state != SUCCESS) {
757         return ERROR_CODE_RES_NOT_FOUND_BY_ID;
758     }
759     return state;
760 }
761 
GetMediaById(uint32_t id,uint32_t density,std::string & outValue)762 RState ResourceManagerImpl::GetMediaById(uint32_t id, uint32_t density, std::string &outValue)
763 {
764     if (!IsDensityValid(density)) {
765         HILOG_ERROR("density invalid");
766         return ERROR_CODE_INVALID_INPUT_PARAMETER;
767     }
768     auto qualifierDir = hapManager_->FindQualifierValueById(id, density);
769     if (qualifierDir == nullptr) {
770         HILOG_ERROR("find qualifier value by media id error");
771         return ERROR_CODE_RES_ID_NOT_FOUND;
772     }
773     RState state = GetRawFile(qualifierDir, ResType::MEDIA, outValue);
774     if (state != SUCCESS) {
775         return ERROR_CODE_RES_NOT_FOUND_BY_ID;
776     }
777     return state;
778 }
779 
GetMediaByName(const char * name,std::string & outValue)780 RState ResourceManagerImpl::GetMediaByName(const char *name, std::string &outValue)
781 {
782     auto qd = hapManager_->FindQualifierValueByName(name, ResType::MEDIA);
783     if (qd == nullptr) {
784         HILOG_ERROR("find qualifier value by Media name error");
785         return ERROR_CODE_RES_NAME_NOT_FOUND;
786     }
787     RState state = GetRawFile(qd, ResType::MEDIA, outValue);
788     if (state != SUCCESS) {
789         return ERROR_CODE_RES_NOT_FOUND_BY_NAME;
790     }
791     return state;
792 }
793 
GetMediaByName(const char * name,uint32_t density,std::string & outValue)794 RState ResourceManagerImpl::GetMediaByName(const char *name, uint32_t density, std::string &outValue)
795 {
796     if (!IsDensityValid(density)) {
797         HILOG_ERROR("density invalid");
798         return ERROR_CODE_INVALID_INPUT_PARAMETER;
799     }
800     auto qualifierDir = hapManager_->FindQualifierValueByName(name, ResType::MEDIA, density);
801     if (qualifierDir == nullptr) {
802         HILOG_ERROR("find qualifier value by media name error");
803         return ERROR_CODE_RES_NOT_FOUND_BY_NAME;
804     }
805     return GetRawFile(qualifierDir, ResType::MEDIA, outValue);
806 }
807 
GetMediaBase64Data(const std::string & iconPath,std::string & base64Data)808 RState GetMediaBase64Data(const std::string& iconPath, std::string &base64Data)
809 {
810     int len = 0;
811     auto tempData = Utils::LoadResourceFile(iconPath, len);
812     if (tempData == nullptr) {
813         HILOG_ERROR("get the tempData error");
814         return NOT_FOUND;
815     }
816     auto pos = iconPath.find_last_of('.');
817     std::string imgType;
818     if (pos != std::string::npos) {
819         imgType = iconPath.substr(pos + 1);
820     }
821     Utils::EncodeBase64(tempData, len, imgType, base64Data);
822     return SUCCESS;
823 }
824 
GetMediaBase64ByNameData(const char * name,uint32_t density,std::string & base64Data)825 RState ResourceManagerImpl::GetMediaBase64ByNameData(const char *name, uint32_t density, std::string &base64Data)
826 {
827     std::string path;
828     RState state = ResourceManagerImpl::GetMediaByName(name, density, path);
829     if (state != SUCCESS) {
830         HILOG_ERROR("the resource path is not exist");
831         return NOT_FOUND;
832     }
833     return GetMediaBase64Data(path, base64Data);
834 }
835 
GetMediaBase64ByIdData(uint32_t id,uint32_t density,std::string & base64Data)836 RState ResourceManagerImpl::GetMediaBase64ByIdData(uint32_t id, uint32_t density, std::string &base64Data)
837 {
838     std::string path;
839     RState state = ResourceManagerImpl::GetMediaById(id, density, path);
840     if (state != SUCCESS) {
841         HILOG_ERROR("the resource path is not exist");
842         return NOT_FOUND;
843     }
844     return GetMediaBase64Data(path, base64Data);
845 }
846 
GetRawFile(const HapResource::ValueUnderQualifierDir * vuqd,const ResType resType,std::string & outValue)847 RState ResourceManagerImpl::GetRawFile(const HapResource::ValueUnderQualifierDir *vuqd, const ResType resType,
848     std::string &outValue)
849 {
850     // not found or type invalid
851     if (vuqd == nullptr) {
852         return NOT_FOUND;
853     }
854     const IdItem *idItem = vuqd->GetIdItem();
855     if (idItem == nullptr || idItem->resType_ != resType) {
856         return NOT_FOUND;
857     }
858     outValue = vuqd->GetHapResource()->GetResourcePath();
859 #if defined(__ARKUI_CROSS__)
860     auto index = idItem->value_.find('/');
861     if (index == std::string::npos) {
862         HILOG_ERROR("resource path format error, %s", idItem->value_.c_str());
863         return NOT_FOUND;
864     }
865     auto nameWithoutModule = idItem->value_.substr(index + 1);
866     outValue.append(nameWithoutModule);
867 #elif defined(__IDE_PREVIEW__)
868     if (IsFileExist(idItem->value_)) {
869         outValue = idItem->value_;
870         return SUCCESS;
871     }
872     auto index = idItem->value_.find('/');
873     if (index == std::string::npos) {
874         HILOG_ERROR("resource path format error, %s", idItem->value_.c_str());
875         return NOT_FOUND;
876     }
877     auto nameWithoutModule = idItem->value_.substr(index + 1);
878     outValue.append(nameWithoutModule);
879 #else
880     outValue.append(idItem->value_);
881 #endif
882     return SUCCESS;
883 }
884 
GetRawFilePathByName(const std::string & name,std::string & outValue)885 RState ResourceManagerImpl::GetRawFilePathByName(const std::string &name, std::string &outValue)
886 {
887     return hapManager_->FindRawFile(name, outValue);
888 }
889 
GetRawFileDescriptor(const std::string & name,RawFileDescriptor & descriptor)890 RState ResourceManagerImpl::GetRawFileDescriptor(const std::string &name, RawFileDescriptor &descriptor)
891 {
892     auto it = rawFileDescriptor_.find(name);
893     if (it != rawFileDescriptor_.end()) {
894         descriptor.fd = rawFileDescriptor_[name].fd;
895         descriptor.length = rawFileDescriptor_[name].length;
896         descriptor.offset = rawFileDescriptor_[name].offset;
897         return SUCCESS;
898     }
899     std::string paths = "";
900     RState rState = GetRawFilePathByName(name, paths);
901     if (rState != SUCCESS) {
902         return rState;
903     }
904     int fd = open(paths.c_str(), O_RDONLY);
905     if (fd > 0) {
906         long length = lseek(fd, 0, SEEK_END);
907         if (length == -1) {
908             close(fd);
909             return ERROR_CODE_RES_PATH_INVALID;
910         }
911         long begin = lseek(fd, 0, SEEK_SET);
912         if (begin == -1) {
913             close(fd);
914             return ERROR_CODE_RES_PATH_INVALID;
915         }
916         descriptor.fd = fd;
917         descriptor.length = length;
918         descriptor.offset = 0;
919         rawFileDescriptor_[name] = descriptor;
920         return SUCCESS;
921     }
922     return ERROR_CODE_RES_PATH_INVALID;
923 }
924 
CloseRawFileDescriptor(const std::string & name)925 RState ResourceManagerImpl::CloseRawFileDescriptor(const std::string &name)
926 {
927     auto it = rawFileDescriptor_.find(name);
928     if (it == rawFileDescriptor_.end()) {
929         return ERROR_CODE_RES_PATH_INVALID;
930     }
931     int fd = rawFileDescriptor_[name].fd;
932     if (fd > 0) {
933         int result = close(fd);
934         if (result == -1) {
935             return ERROR_CODE_RES_PATH_INVALID;
936         }
937         rawFileDescriptor_.erase(name);
938         return SUCCESS;
939     }
940     return ERROR_CODE_RES_PATH_INVALID;
941 }
942 
ProcessPsuedoTranslate(std::string & outValue)943 void ResourceManagerImpl::ProcessPsuedoTranslate(std::string &outValue)
944 {
945     int len = outValue.length() + 1;
946     char src[len];
947     if (strcpy_s(src, len, outValue.c_str()) == EOK) {
948         std::string resultMsg = psueManager_->Convert(src, outValue);
949         if (resultMsg != "") {
950             HILOG_ERROR("Psuedo translate failed, value:%s", src);
951         }
952     }
953 }
954 
~ResourceManagerImpl()955 ResourceManagerImpl::~ResourceManagerImpl()
956 {
957     if (hapManager_ != nullptr) {
958         delete hapManager_;
959         hapManager_ = nullptr;
960     }
961     if (psueManager_ != nullptr) {
962         delete (psueManager_);
963         psueManager_ = nullptr;
964     }
965 }
966 
AddResource(const char * path)967 bool ResourceManagerImpl::AddResource(const char *path)
968 {
969 #if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__)
970     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
971 #endif
972     return this->hapManager_->AddResource(path);
973 }
974 
AddResource(const std::string & path,const std::vector<std::string> & overlayPaths)975 bool ResourceManagerImpl::AddResource(const std::string &path, const std::vector<std::string> &overlayPaths)
976 {
977     return this->hapManager_->AddResource(path, overlayPaths);
978 }
979 
UpdateResConfig(ResConfig & resConfig)980 RState ResourceManagerImpl::UpdateResConfig(ResConfig &resConfig)
981 {
982 #if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__)
983     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
984 #endif
985 #ifdef SUPPORT_GRAPHICS
986     if (resConfig.GetLocaleInfo() == nullptr) {
987         return LOCALEINFO_IS_NULL;
988     }
989     if (resConfig.GetLocaleInfo()->getLanguage() == nullptr) {
990         return LOCALEINFO_IS_NULL;
991     }
992     const char* language = resConfig.GetLocaleInfo()->getLanguage();
993     const char* region = resConfig.GetLocaleInfo()->getCountry();
994     if (language != nullptr && region != nullptr) {
995         std::string languageStr = language;
996         std::string regionStr = region;
997         if (languageStr == "en" && regionStr == "XA") {
998             isFakeLocale = true;
999         } else {
1000             isFakeLocale = false;
1001         }
1002     }
1003 #endif
1004     return this->hapManager_->UpdateResConfig(resConfig);
1005 }
1006 
GetResConfig(ResConfig & resConfig)1007 void ResourceManagerImpl::GetResConfig(ResConfig &resConfig)
1008 {
1009     this->hapManager_->GetResConfig(resConfig);
1010 }
1011 
GetResourcePaths()1012 std::vector<std::string> ResourceManagerImpl::GetResourcePaths()
1013 {
1014     return this->hapManager_->GetResourcePaths();
1015 }
1016 
IsDensityValid(uint32_t density)1017 bool ResourceManagerImpl::IsDensityValid(uint32_t density)
1018 {
1019     switch (density) {
1020         case SCREEN_DENSITY_NOT_SET:
1021         case SCREEN_DENSITY_SDPI:
1022         case SCREEN_DENSITY_MDPI:
1023         case SCREEN_DENSITY_LDPI:
1024         case SCREEN_DENSITY_XLDPI:
1025         case SCREEN_DENSITY_XXLDPI:
1026         case SCREEN_DENSITY_XXXLDPI:
1027             return true;
1028         default:
1029             return false;
1030     }
1031 }
1032 
GetMediaDataById(uint32_t id,size_t & len,std::unique_ptr<uint8_t[]> & outValue)1033 RState ResourceManagerImpl::GetMediaDataById(uint32_t id, size_t &len, std::unique_ptr<uint8_t[]> &outValue)
1034 {
1035     auto qd = hapManager_->FindQualifierValueById(id);
1036     if (qd == nullptr) {
1037         HILOG_ERROR("find qualifier value by media id error");
1038         return ERROR_CODE_RES_ID_NOT_FOUND;
1039     }
1040     RState state = hapManager_->GetMediaData(qd, len, outValue);
1041     if (state != SUCCESS) {
1042         return ERROR_CODE_RES_NOT_FOUND_BY_ID;
1043     }
1044     return state;
1045 }
1046 
GetMediaDataByName(const char * name,size_t & len,std::unique_ptr<uint8_t[]> & outValue)1047 RState ResourceManagerImpl::GetMediaDataByName(const char *name, size_t &len, std::unique_ptr<uint8_t[]> &outValue)
1048 {
1049     auto qd = hapManager_->FindQualifierValueByName(name, ResType::MEDIA);
1050     if (qd == nullptr) {
1051         HILOG_ERROR("find qualifier value by media name error");
1052         return ERROR_CODE_RES_NAME_NOT_FOUND;
1053     }
1054     RState state = hapManager_->GetMediaData(qd, len, outValue);
1055     if (state != SUCCESS) {
1056         return ERROR_CODE_RES_NOT_FOUND_BY_NAME;
1057     }
1058     return state;
1059 }
1060 
GetMediaDataById(uint32_t id,uint32_t density,size_t & len,std::unique_ptr<uint8_t[]> & outValue)1061 RState ResourceManagerImpl::GetMediaDataById(uint32_t id, uint32_t density, size_t &len,
1062     std::unique_ptr<uint8_t[]> &outValue)
1063 {
1064     if (!IsDensityValid(density)) {
1065         HILOG_ERROR("density invalid");
1066         return NOT_SUPPORT_SEP;
1067     }
1068     auto qd = hapManager_->FindQualifierValueById(id, density);
1069     if (qd == nullptr) {
1070         HILOG_ERROR("find qualifier value by media id error");
1071         return NOT_FOUND;
1072     }
1073     return hapManager_->GetMediaData(qd, len, outValue);
1074 }
1075 
GetMediaDataByName(const char * name,uint32_t density,size_t & len,std::unique_ptr<uint8_t[]> & outValue)1076 RState ResourceManagerImpl::GetMediaDataByName(const char *name, uint32_t density, size_t &len,
1077     std::unique_ptr<uint8_t[]> &outValue)
1078 {
1079     if (!IsDensityValid(density)) {
1080         HILOG_ERROR("density invalid");
1081         return NOT_SUPPORT_SEP;
1082     }
1083     auto qd = hapManager_->FindQualifierValueByName(name, ResType::MEDIA, density);
1084     if (qd == nullptr) {
1085         HILOG_ERROR("find qualifier value by media name error");
1086         return NOT_FOUND;
1087     }
1088     return hapManager_->GetMediaData(qd, len, outValue);
1089 }
1090 
GetMediaBase64DataById(uint32_t id,std::string & outValue)1091 RState ResourceManagerImpl::GetMediaBase64DataById(uint32_t id, std::string &outValue)
1092 {
1093     auto qd = hapManager_->FindQualifierValueById(id);
1094     if (qd == nullptr) {
1095         HILOG_ERROR("find qualifier value by media id error");
1096         return ERROR_CODE_RES_ID_NOT_FOUND;
1097     }
1098     RState state = hapManager_->GetMediaBase64Data(qd, outValue);
1099     if (state != SUCCESS) {
1100         return ERROR_CODE_RES_NOT_FOUND_BY_ID;
1101     }
1102     return state;
1103 }
1104 
GetMediaBase64DataByName(const char * name,std::string & outValue)1105 RState ResourceManagerImpl::GetMediaBase64DataByName(const char *name,  std::string &outValue)
1106 {
1107     auto qd = hapManager_->FindQualifierValueByName(name, ResType::MEDIA);
1108     if (qd == nullptr) {
1109         HILOG_ERROR("find qualifier value by media name error");
1110         return ERROR_CODE_RES_NAME_NOT_FOUND;
1111     }
1112     RState state = hapManager_->GetMediaBase64Data(qd, outValue);
1113     if (state != SUCCESS) {
1114         return ERROR_CODE_RES_NOT_FOUND_BY_NAME;
1115     }
1116     return state;
1117 }
1118 
GetMediaBase64DataById(uint32_t id,uint32_t density,std::string & outValue)1119 RState ResourceManagerImpl::GetMediaBase64DataById(uint32_t id, uint32_t density, std::string &outValue)
1120 {
1121     if (!IsDensityValid(density)) {
1122         HILOG_ERROR("density invalid");
1123         return NOT_SUPPORT_SEP;
1124     }
1125     auto qd = hapManager_->FindQualifierValueById(id, density);
1126     if (qd == nullptr) {
1127         HILOG_ERROR("find qualifier value by media id error");
1128         return NOT_FOUND;
1129     }
1130     return hapManager_->GetMediaBase64Data(qd, outValue);
1131 }
1132 
GetMediaBase64DataByName(const char * name,uint32_t density,std::string & outValue)1133 RState ResourceManagerImpl::GetMediaBase64DataByName(const char *name, uint32_t density, std::string &outValue)
1134 {
1135     if (!IsDensityValid(density)) {
1136         HILOG_ERROR("density invalid");
1137         return NOT_SUPPORT_SEP;
1138     }
1139     auto qd = hapManager_->FindQualifierValueByName(name, ResType::MEDIA, density);
1140     if (qd == nullptr) {
1141         HILOG_ERROR("find qualifier value by media name error");
1142         return NOT_FOUND;
1143     }
1144     return hapManager_->GetMediaBase64Data(qd, outValue);
1145 }
1146 
GetProfileDataById(uint32_t id,size_t & len,std::unique_ptr<uint8_t[]> & outValue)1147 RState ResourceManagerImpl::GetProfileDataById(uint32_t id, size_t &len, std::unique_ptr<uint8_t[]> &outValue)
1148 {
1149     auto qd = hapManager_->FindQualifierValueById(id);
1150     if (qd == nullptr) {
1151         HILOG_ERROR("find qualifier value by profile id error");
1152         return ERROR_CODE_RES_NOT_FOUND_BY_ID;
1153     }
1154     return hapManager_->GetProfileData(qd, len, outValue);
1155 }
1156 
GetProfileDataByName(const char * name,size_t & len,std::unique_ptr<uint8_t[]> & outValue)1157 RState ResourceManagerImpl::GetProfileDataByName(const char *name, size_t &len, std::unique_ptr<uint8_t[]> &outValue)
1158 {
1159     auto qd = hapManager_->FindQualifierValueByName(name, ResType::PROF);
1160     if (qd == nullptr) {
1161         HILOG_ERROR("find qualifier value by profile name error");
1162         return ERROR_CODE_RES_NOT_FOUND_BY_NAME;
1163     }
1164     return hapManager_->GetProfileData(qd, len, outValue);
1165 }
1166 
GetRawFileFromHap(const std::string & rawFileName,std::unique_ptr<RawFile> & rawFile)1167 RState ResourceManagerImpl::GetRawFileFromHap(const std::string &rawFileName, std::unique_ptr<RawFile> &rawFile)
1168 {
1169     return hapManager_->FindRawFileFromHap(rawFileName, rawFile);
1170 }
1171 
GetRawFileDescriptorFromHap(const std::string & rawFileName,RawFileDescriptor & descriptor)1172 RState ResourceManagerImpl::GetRawFileDescriptorFromHap(const std::string &rawFileName, RawFileDescriptor &descriptor)
1173 {
1174     auto it = rawFileDescriptor_.find(rawFileName);
1175     if (it != rawFileDescriptor_.end()) {
1176         descriptor.fd = rawFileDescriptor_[rawFileName].fd;
1177         descriptor.length = rawFileDescriptor_[rawFileName].length;
1178         descriptor.offset = rawFileDescriptor_[rawFileName].offset;
1179         return SUCCESS;
1180     }
1181     auto rawFile = std::make_unique<RawFile>();
1182     ResourceManagerImpl::GetRawFileFromHap(rawFileName, rawFile);
1183     if (rawFile->pf == nullptr) {
1184         HILOG_ERROR("failed to get rawfile pf");
1185         return ERROR_CODE_RES_PATH_INVALID;
1186     }
1187     int fd = fileno(rawFile->pf);
1188     if (fd < 0) {
1189         HILOG_ERROR("failed to get fd in GetRawFileDescriptorFromHap");
1190         return ERROR_CODE_RES_PATH_INVALID;
1191     }
1192     descriptor.fd = fd;
1193     descriptor.length = rawFile->length;
1194     descriptor.offset = rawFile->offset;
1195     rawFileDescriptor_[rawFileName] = descriptor;
1196     return SUCCESS;
1197 }
1198 
IsLoadHap()1199 RState ResourceManagerImpl::IsLoadHap()
1200 {
1201     if (hapManager_->IsLoadHap()) {
1202         return SUCCESS;
1203     }
1204     return NOT_FOUND;
1205 }
1206 
IsFileExist(const std::string & path)1207 bool ResourceManagerImpl::IsFileExist(const std::string& path)
1208 {
1209     std::fstream inputFile;
1210     inputFile.open(path, std::ios::in);
1211     if (inputFile) {
1212         return true;
1213     }
1214     return false;
1215 }
1216 } // namespace Resource
1217 } // namespace Global
1218 } // namespace OHOS
1219