• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 
16 #include "native_interface_bundle.h"
17 
18 #include <cstring>
19 #include <memory>
20 #include <mutex>
21 #include <string>
22 #include <vector>
23 
24 #include "application_info.h"
25 #include "bundle_info.h"
26 #include "app_log_wrapper.h"
27 #include "bundle_mgr_proxy_native.h"
28 #include "ipc_skeleton.h"
29 #include "securec.h"
30 namespace {
31 const size_t CHAR_MIN_LENGTH = 1;
32 const size_t CHAR_MAX_LENGTH = 10240;
33 const size_t MAX_ALLOWED_SIZE = 1024 * 1024;
34 }
35 
36 // Helper function to release char* memory
ReleaseMemory(char * & str)37 static void ReleaseMemory(char* &str)
38 {
39     if (str != nullptr) {
40         free(str);
41         str = nullptr;
42     }
43 }
44 
45 template <typename... Args>
ReleaseStrings(Args...args)46 void ReleaseStrings(Args... args)
47 {
48     (ReleaseMemory(args), ...);
49 }
50 
CopyStringToChar(char * & name,const std::string & value)51 bool CopyStringToChar(char* &name, const std::string &value)
52 {
53     size_t length = value.size();
54     if ((length == 0) || (length + 1) > CHAR_MAX_LENGTH) {
55         APP_LOGE("failed due to the length of value is empty or too long");
56         return false;
57     }
58     name = static_cast<char*>(malloc(length + 1));
59     if (name == nullptr) {
60         APP_LOGE("failed due to malloc error");
61         return false;
62     }
63     if (strcpy_s(name, length + 1, value.c_str()) != EOK) {
64         APP_LOGE("failed due to strcpy_s error");
65         ReleaseStrings(name);
66         return false;
67     }
68     return true;
69 }
70 
GetElementNameByAbilityInfo(const OHOS::AppExecFwk::AbilityInfo & abilityInfo,OH_NativeBundle_ElementName & elementName)71 bool GetElementNameByAbilityInfo(
72     const OHOS::AppExecFwk::AbilityInfo &abilityInfo, OH_NativeBundle_ElementName &elementName)
73 {
74     if (!CopyStringToChar(elementName.bundleName, abilityInfo.bundleName)) {
75         APP_LOGE("failed to obtains bundleName");
76         return false;
77     }
78 
79     if (!CopyStringToChar(elementName.moduleName, abilityInfo.moduleName)) {
80         APP_LOGE("failed to obtains moduleName");
81         ReleaseStrings(elementName.bundleName);
82         return false;
83     }
84 
85     if (!CopyStringToChar(elementName.abilityName, abilityInfo.name)) {
86         APP_LOGE("failed to obtains abilityName");
87         ReleaseStrings(elementName.bundleName, elementName.moduleName);
88         return false;
89     }
90     return true;
91 }
92 
GetElementNameByModuleInfo(const OHOS::AppExecFwk::HapModuleInfo & hapModuleInfo,OH_NativeBundle_ElementName & elementName)93 bool GetElementNameByModuleInfo(
94     const OHOS::AppExecFwk::HapModuleInfo &hapModuleInfo, OH_NativeBundle_ElementName &elementName)
95 {
96     for (const auto &abilityInfo : hapModuleInfo.abilityInfos) {
97         if (abilityInfo.name.compare(hapModuleInfo.mainElementName) == 0) {
98             return GetElementNameByAbilityInfo(abilityInfo, elementName);
99         }
100     }
101     return false;
102 }
103 
OH_NativeBundle_GetCurrentApplicationInfo()104 OH_NativeBundle_ApplicationInfo OH_NativeBundle_GetCurrentApplicationInfo()
105 {
106     OH_NativeBundle_ApplicationInfo nativeApplicationInfo;
107     OHOS::AppExecFwk::BundleMgrProxyNative bundleMgrProxyNative;
108     OHOS::AppExecFwk::BundleInfo bundleInfo;
109     auto bundleInfoFlag = static_cast<int32_t>(OHOS::AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION) |
110         static_cast<int32_t>(OHOS::AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_SIGNATURE_INFO);
111 
112     if (!bundleMgrProxyNative.GetBundleInfoForSelf(bundleInfoFlag, bundleInfo)) {
113         APP_LOGE("can not get bundleInfo for self");
114         return nativeApplicationInfo;
115     };
116     size_t bundleNameLen = bundleInfo.applicationInfo.bundleName.size();
117     if ((bundleNameLen == 0) || (bundleNameLen + 1) > CHAR_MAX_LENGTH) {
118         APP_LOGE("failed due to the length of bundleName is empty or too long");
119         return nativeApplicationInfo;
120     }
121     nativeApplicationInfo.bundleName = static_cast<char*>(malloc(bundleNameLen + 1));
122     if (nativeApplicationInfo.bundleName == nullptr) {
123         APP_LOGE("failed due to malloc error");
124         return nativeApplicationInfo;
125     }
126     if (strcpy_s(nativeApplicationInfo.bundleName, bundleNameLen + 1,
127         bundleInfo.applicationInfo.bundleName.c_str()) != EOK) {
128         APP_LOGE("failed due to strcpy_s error");
129         ReleaseStrings(nativeApplicationInfo.bundleName);
130         return nativeApplicationInfo;
131     }
132     size_t fingerprintLen = bundleInfo.signatureInfo.fingerprint.size();
133     if ((fingerprintLen == 0) || (fingerprintLen + 1) > CHAR_MAX_LENGTH) {
134         APP_LOGE("failed due to the length of fingerprint is empty or too long");
135         ReleaseStrings(nativeApplicationInfo.bundleName);
136         return nativeApplicationInfo;
137     }
138     nativeApplicationInfo.fingerprint = static_cast<char*>(malloc(fingerprintLen + 1));
139     if (nativeApplicationInfo.fingerprint == nullptr) {
140         APP_LOGE("failed due to malloc error");
141         ReleaseStrings(nativeApplicationInfo.bundleName);
142         return nativeApplicationInfo;
143     }
144     if (strcpy_s(nativeApplicationInfo.fingerprint, fingerprintLen + 1,
145         bundleInfo.signatureInfo.fingerprint.c_str()) != EOK) {
146         APP_LOGE("failed due to strcpy_s error");
147         ReleaseStrings(nativeApplicationInfo.bundleName, nativeApplicationInfo.fingerprint);
148         return nativeApplicationInfo;
149     }
150     APP_LOGD("OH_NativeBundle_GetCurrentApplicationInfo success");
151     return nativeApplicationInfo;
152 }
153 
OH_NativeBundle_GetAppId()154 char* OH_NativeBundle_GetAppId()
155 {
156     OHOS::AppExecFwk::BundleMgrProxyNative bundleMgrProxyNative;
157     OHOS::AppExecFwk::BundleInfo bundleInfo;
158     auto bundleInfoFlag =
159         static_cast<int32_t>(OHOS::AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_SIGNATURE_INFO);
160 
161     if (!bundleMgrProxyNative.GetBundleInfoForSelf(bundleInfoFlag, bundleInfo)) {
162         APP_LOGE("can not get bundleInfo for self");
163         return nullptr;
164     };
165 
166     size_t appIdLen = bundleInfo.signatureInfo.appId.size();
167     if ((appIdLen == 0) || (appIdLen + 1) > CHAR_MAX_LENGTH) {
168         APP_LOGE("failed due to the length of appId is empty or too long");
169         return nullptr;
170     }
171     char *appId = static_cast<char*>(malloc(appIdLen + 1));
172     if (appId == nullptr) {
173         APP_LOGE("failed due to malloc error");
174         return nullptr;
175     }
176     if (strcpy_s(appId, appIdLen + 1, bundleInfo.signatureInfo.appId.c_str()) != EOK) {
177         APP_LOGE("failed due to strcpy_s error");
178         free(appId);
179         return nullptr;
180     }
181     APP_LOGD("OH_NativeBundle_GetAppId success");
182     return appId;
183 }
184 
OH_NativeBundle_GetAppIdentifier()185 char* OH_NativeBundle_GetAppIdentifier()
186 {
187     OHOS::AppExecFwk::BundleMgrProxyNative bundleMgrProxyNative;
188     OHOS::AppExecFwk::BundleInfo bundleInfo;
189     auto bundleInfoFlag =
190         static_cast<int32_t>(OHOS::AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_SIGNATURE_INFO);
191 
192     if (!bundleMgrProxyNative.GetBundleInfoForSelf(bundleInfoFlag, bundleInfo)) {
193         APP_LOGE("can not get bundleInfo for self");
194         return nullptr;
195     };
196 
197     size_t appIdentifierLen = bundleInfo.signatureInfo.appIdentifier.size();
198     if (appIdentifierLen + 1 > CHAR_MAX_LENGTH) {
199         APP_LOGE("failed due to the length of appIdentifier is too long");
200         return nullptr;
201     }
202     char* appIdentifier = static_cast<char*>(malloc(appIdentifierLen + 1));
203     if (appIdentifier == nullptr) {
204         APP_LOGE("failed due to malloc error");
205         return nullptr;
206     }
207     if (strcpy_s(appIdentifier, appIdentifierLen + 1,
208         bundleInfo.signatureInfo.appIdentifier.c_str()) != EOK) {
209         APP_LOGE("failed due to strcpy_s error");
210         free(appIdentifier);
211         return nullptr;
212     }
213     APP_LOGD("Native_Identifier success");
214     return appIdentifier;
215 }
216 
OH_NativeBundle_GetMainElementName()217 OH_NativeBundle_ElementName OH_NativeBundle_GetMainElementName()
218 {
219     OH_NativeBundle_ElementName elementName;
220     OHOS::AppExecFwk::BundleMgrProxyNative bundleMgrProxyNative;
221     OHOS::AppExecFwk::BundleInfo bundleInfo;
222     auto bundleInfoFlag = static_cast<int32_t>(OHOS::AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_HAP_MODULE) |
223                           static_cast<int32_t>(OHOS::AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_ABILITY);
224 
225     if (!bundleMgrProxyNative.GetBundleInfoForSelf(bundleInfoFlag, bundleInfo)) {
226         APP_LOGE("can not get bundleInfo for self");
227         return elementName;
228     };
229 
230     for (const auto &hapModuleInfo : bundleInfo.hapModuleInfos) {
231         if (hapModuleInfo.moduleType != OHOS::AppExecFwk::ModuleType::ENTRY) {
232             continue;
233         }
234         if (GetElementNameByModuleInfo(hapModuleInfo, elementName)) {
235             return elementName;
236         }
237     }
238 
239     for (const auto &hapModuleInfo : bundleInfo.hapModuleInfos) {
240         if (hapModuleInfo.moduleType != OHOS::AppExecFwk::ModuleType::FEATURE) {
241             continue;
242         }
243         if (GetElementNameByModuleInfo(hapModuleInfo, elementName)) {
244             return elementName;
245         }
246     }
247 
248     for (const auto &hapModuleInfo : bundleInfo.hapModuleInfos) {
249         for (const auto &abilityInfo : hapModuleInfo.abilityInfos) {
250             GetElementNameByAbilityInfo(abilityInfo, elementName);
251             return elementName;
252         }
253     }
254     return elementName;
255 }
256 
OH_NativeBundle_GetCompatibleDeviceType()257 char* OH_NativeBundle_GetCompatibleDeviceType()
258 {
259     OHOS::AppExecFwk::BundleMgrProxyNative bundleMgrProxyNative;
260     std::string deviceType;
261     if (!bundleMgrProxyNative.GetCompatibleDeviceTypeNative(deviceType)) {
262         APP_LOGE("can not get compatible device type");
263         return nullptr;
264     }
265     if (deviceType.size() + 1 > CHAR_MAX_LENGTH) {
266         APP_LOGE("failed due to the length of device type is too long");
267         return nullptr;
268     }
269     char* deviceTypeC = static_cast<char*>(malloc(deviceType.size() + 1));
270     if (deviceTypeC == nullptr) {
271         APP_LOGE("failed due to malloc error");
272         return nullptr;
273     }
274     if (strcpy_s(deviceTypeC, deviceType.size() + 1, deviceType.c_str()) != EOK) {
275         APP_LOGE("failed due to strcpy_s error");
276         free(deviceTypeC);
277         return nullptr;
278     }
279     APP_LOGD("OH_NativeBundle_GetCompatibleDeviceType success");
280     return deviceTypeC;
281 }
282 
OH_NativeBundle_IsDebugMode(bool * isDebugMode)283 bool OH_NativeBundle_IsDebugMode(bool* isDebugMode)
284 {
285     if (isDebugMode == nullptr) {
286         APP_LOGE("invalid param isDebugMode");
287         return false;
288     }
289     OHOS::AppExecFwk::BundleMgrProxyNative bundleMgrProxyNative;
290     OHOS::AppExecFwk::BundleInfo bundleInfo;
291 
292     auto bundleInfoFlag =
293         static_cast<int32_t>(OHOS::AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION);
294 
295     if (!bundleMgrProxyNative.GetBundleInfoForSelf(bundleInfoFlag, bundleInfo)) {
296         APP_LOGE("can not get bundleInfo for self");
297         return false;
298     }
299 
300     *isDebugMode = bundleInfo.applicationInfo.debug;
301     APP_LOGD("OH_NativeBundle_IsDebugMode success");
302     return true;
303 }
304 
FreeMetadataArray(OH_NativeBundle_Metadata * & metadata,size_t metadataSize)305 void FreeMetadataArray(OH_NativeBundle_Metadata *&metadata, size_t metadataSize)
306 {
307     if (metadata == nullptr || metadataSize == 0) {
308         APP_LOGE("invalid param");
309         return;
310     }
311     for (size_t i = 0; i < metadataSize; ++i) {
312         free(metadata[i].name);
313         free(metadata[i].value);
314         free(metadata[i].resource);
315     }
316     free(metadata);
317 }
318 
FreeModuleMetadataArray(OH_NativeBundle_ModuleMetadata * & moduleMetadata,size_t moduleMetadataSize)319 void FreeModuleMetadataArray(OH_NativeBundle_ModuleMetadata *&moduleMetadata, size_t moduleMetadataSize)
320 {
321     if (moduleMetadata == nullptr || moduleMetadataSize == 0) {
322         APP_LOGE("invalid param");
323         return;
324     }
325 
326     for (size_t i = 0; i < moduleMetadataSize; ++i) {
327         free(moduleMetadata[i].moduleName);
328         FreeMetadataArray(moduleMetadata[i].metadataArray, moduleMetadata[i].metadataArraySize);
329     }
330     free(moduleMetadata);
331 }
332 
ResetMetadataArray(OH_NativeBundle_Metadata * & metadata,size_t metadataSize)333 void ResetMetadataArray(OH_NativeBundle_Metadata *&metadata, size_t metadataSize)
334 {
335     if (metadata == nullptr || metadataSize == 0) {
336         APP_LOGE("invalid param");
337         return;
338     }
339     for (size_t i = 0; i < metadataSize; ++i) {
340         metadata[i].name = nullptr;
341         metadata[i].value = nullptr;
342         metadata[i].resource = nullptr;
343     }
344 }
345 
ResetModuleMetadataArray(OH_NativeBundle_ModuleMetadata * & moduleMetadata,size_t moduleMetadataSize)346 void ResetModuleMetadataArray(OH_NativeBundle_ModuleMetadata *&moduleMetadata, size_t moduleMetadataSize)
347 {
348     if (moduleMetadata == nullptr || moduleMetadataSize == 0) {
349         APP_LOGE("invalid param");
350         return;
351     }
352 
353     for (size_t i = 0; i < moduleMetadataSize; ++i) {
354         moduleMetadata[i].moduleName = nullptr;
355         moduleMetadata[i].metadataArray = nullptr;
356         moduleMetadata[i].metadataArraySize = 0;
357     }
358 }
359 
CopyMetadataStringToChar(char * & name,const std::string & value)360 bool CopyMetadataStringToChar(char *&name, const std::string &value)
361 {
362     size_t length = value.size();
363     if ((length == 0) || (length + 1) > CHAR_MAX_LENGTH) {
364         APP_LOGW("failed due to the length of value is empty or too long");
365         name = static_cast<char *>(malloc(CHAR_MIN_LENGTH));
366         if (name == nullptr) {
367             APP_LOGE("failed due to malloc error");
368             return false;
369         }
370         name[0] = '\0';
371         return true;
372     }
373     name = static_cast<char *>(malloc(length + 1));
374     if (name == nullptr) {
375         APP_LOGE("failed due to malloc error");
376         return false;
377     }
378     for (size_t i = 0; i < length; i++) {
379         name[i] = 0;
380     }
381     name[length] = '\0';
382     if (strcpy_s(name, length + 1, value.c_str()) != EOK) {
383         APP_LOGE("failed due to strcpy_s error");
384         return false;
385     }
386     return true;
387 }
388 
AllocateModuleMetadata(size_t moduleMetadataSize)389 OH_NativeBundle_ModuleMetadata* AllocateModuleMetadata(size_t moduleMetadataSize)
390 {
391     if (moduleMetadataSize == 0 || moduleMetadataSize > MAX_ALLOWED_SIZE) {
392         APP_LOGE("failed due to the length of value is empty or too long");
393         return nullptr;
394     }
395     OH_NativeBundle_ModuleMetadata *moduleMetadata = static_cast<OH_NativeBundle_ModuleMetadata *>(
396         malloc(moduleMetadataSize * sizeof(OH_NativeBundle_ModuleMetadata)));
397     if (moduleMetadata == nullptr) {
398         APP_LOGE("failed to allocate memory for OH_NativeBundle_ModuleMetadata");
399     }
400     ResetModuleMetadataArray(moduleMetadata, moduleMetadataSize);
401     return moduleMetadata;
402 }
403 
FillBundleModuleNames(const std::vector<std::string> & moduleNames)404 OH_NativeBundle_ModuleMetadata* FillBundleModuleNames(const std::vector<std::string> &moduleNames)
405 {
406     if (moduleNames.empty() || moduleNames.size() > MAX_ALLOWED_SIZE) {
407         APP_LOGE("moduleNames is empty or too long");
408         return nullptr;
409     }
410     OH_NativeBundle_ModuleMetadata *moduleMetadata = static_cast<OH_NativeBundle_ModuleMetadata *>(
411         malloc(moduleNames.size() * sizeof(OH_NativeBundle_ModuleMetadata)));
412     if (moduleMetadata == nullptr) {
413         APP_LOGE("failed to allocate memory for OH_NativeBundle_ModuleMetadata");
414         return nullptr;
415     }
416     ResetModuleMetadataArray(moduleMetadata, moduleNames.size());
417     for (size_t i = 0; i < moduleNames.size(); ++i) {
418         if (!CopyMetadataStringToChar(moduleMetadata[i].moduleName, moduleNames[i])) {
419             APP_LOGE("failed to allocate memory for OH_NativeBundle_ModuleMetadata moduleName");
420             FreeModuleMetadataArray(moduleMetadata, i + 1);
421             return nullptr;
422         }
423         moduleMetadata[i].metadataArray = nullptr;
424         moduleMetadata[i].metadataArraySize = 0;
425     }
426     return moduleMetadata;
427 }
428 
FillModuleMetadata(OH_NativeBundle_ModuleMetadata & moduleMetadata,const std::string & moduleName,const std::vector<OHOS::AppExecFwk::Metadata> & metadataArray)429 bool FillModuleMetadata(OH_NativeBundle_ModuleMetadata &moduleMetadata, const std::string &moduleName,
430     const std::vector<OHOS::AppExecFwk::Metadata> &metadataArray)
431 {
432     if (!CopyMetadataStringToChar(moduleMetadata.moduleName, moduleName.c_str())) {
433         APP_LOGE("failed to allocate memory for OH_NativeBundle_ModuleMetadata moduleName");
434         return false;
435     }
436 
437     auto metadataArraySize = metadataArray.size();
438     if (metadataArraySize == 0) {
439         moduleMetadata.metadataArray = nullptr;
440         moduleMetadata.metadataArraySize = metadataArraySize;
441         APP_LOGI("metadataArray is empty");
442         return true;
443     }
444 
445     if (metadataArraySize > MAX_ALLOWED_SIZE) {
446         APP_LOGE("metadataArraySize is too long");
447         return false;
448     }
449 
450     moduleMetadata.metadataArray =
451         static_cast<OH_NativeBundle_Metadata *>(malloc(metadataArraySize * sizeof(OH_NativeBundle_Metadata)));
452     if (moduleMetadata.metadataArray == nullptr) {
453         APP_LOGE("failed to allocate memory for metadataArray");
454         return false;
455     }
456     ResetMetadataArray(moduleMetadata.metadataArray, metadataArraySize);
457     moduleMetadata.metadataArraySize = metadataArraySize;
458 
459     for (size_t j = 0; j < metadataArraySize; ++j) {
460         if (!CopyMetadataStringToChar(moduleMetadata.metadataArray[j].name, metadataArray[j].name.c_str())) {
461             APP_LOGE("failed to allocate memory for OH_NativeBundle_ModuleMetadata name");
462             return false;
463         }
464         if (!CopyMetadataStringToChar(moduleMetadata.metadataArray[j].value, metadataArray[j].value.c_str())) {
465             APP_LOGE("failed to allocate memory for OH_NativeBundle_ModuleMetadata value");
466             return false;
467         }
468         if (!CopyMetadataStringToChar(moduleMetadata.metadataArray[j].resource, metadataArray[j].resource.c_str())) {
469             APP_LOGE("failed to allocate memory for OH_NativeBundle_ModuleMetadata resource");
470             return false;
471         }
472     }
473     return true;
474 }
475 
CreateModuleMetadata(const std::map<std::string,std::vector<OHOS::AppExecFwk::Metadata>> & metadata)476 OH_NativeBundle_ModuleMetadata* CreateModuleMetadata(const std::map<std::string,
477     std::vector<OHOS::AppExecFwk::Metadata>> &metadata)
478 {
479     auto moduleMetadataSize = metadata.size();
480     OH_NativeBundle_ModuleMetadata *moduleMetadata = AllocateModuleMetadata(moduleMetadataSize);
481     if (moduleMetadata == nullptr) {
482         APP_LOGE("allocate module metadata failed");
483         return nullptr;
484     }
485 
486     size_t i = 0;
487     for (const auto &[moduleName, metadataArray] : metadata) {
488         if (!FillModuleMetadata(moduleMetadata[i], moduleName, metadataArray)) {
489             FreeModuleMetadataArray(moduleMetadata, i + 1);
490             APP_LOGE("fill module metadata failed");
491             return nullptr;
492         }
493         ++i;
494     }
495 
496     return moduleMetadata;
497 }
498 
OH_NativeBundle_GetModuleMetadata(size_t * size)499 OH_NativeBundle_ModuleMetadata* OH_NativeBundle_GetModuleMetadata(size_t* size)
500 {
501     if (size == nullptr) {
502         APP_LOGE("invalid param size");
503         return nullptr;
504     }
505     *size = 0;
506     OHOS::AppExecFwk::BundleMgrProxyNative bundleMgrProxyNative;
507     OHOS::AppExecFwk::BundleInfo bundleInfo;
508     auto bundleInfoFlag =
509         static_cast<uint32_t>(OHOS::AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_METADATA) |
510         static_cast<uint32_t>(OHOS::AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION);
511 
512     if (!bundleMgrProxyNative.GetBundleInfoForSelf(bundleInfoFlag, bundleInfo)) {
513         APP_LOGE("failed to get bundleInfo for self, flags: %{public}d", bundleInfoFlag);
514         return nullptr;
515     }
516 
517     OH_NativeBundle_ModuleMetadata *moduleMetadata = nullptr;
518 
519     if (bundleInfo.applicationInfo.metadata.empty()) {
520         if (bundleInfo.moduleNames.empty()) {
521             APP_LOGE("bundleInfo applicationInfo metadata and bundleInfo moduleNames is empty");
522             return nullptr;
523         }
524 
525         moduleMetadata = FillBundleModuleNames(bundleInfo.moduleNames);
526         if (moduleMetadata == nullptr) {
527             APP_LOGE("failed to fill bundle module names");
528             return nullptr;
529         }
530 
531         *size = bundleInfo.moduleNames.size();
532         APP_LOGI("bundleInfo applicationInfo metadata is empty");
533         return moduleMetadata;
534     }
535     moduleMetadata = CreateModuleMetadata(bundleInfo.applicationInfo.metadata);
536     if (moduleMetadata == nullptr) {
537         APP_LOGE("failed to create moduleMetadata");
538         return nullptr;
539     }
540     *size = bundleInfo.applicationInfo.metadata.size();
541     APP_LOGD("OH_NativeBundle_GetModuleMetadata success");
542     return moduleMetadata;
543 }