1 /*
2 * Copyright (c) 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 "native_interface_bundle.h"
17
18 #include <mutex>
19 #include <string>
20
21 #include "application_info.h"
22 #include "bundle_info.h"
23 #include "app_log_wrapper.h"
24 #include "bundle_mgr_proxy_native.h"
25 #include "ipc_skeleton.h"
26 #include "securec.h"
27 namespace {
28 const size_t CHAR_MAX_LENGTH = 10240;
29 }
30
OH_NativeBundle_GetCurrentApplicationInfo()31 OH_NativeBundle_ApplicationInfo OH_NativeBundle_GetCurrentApplicationInfo()
32 {
33 OH_NativeBundle_ApplicationInfo nativeApplicationInfo;
34 OHOS::AppExecFwk::BundleMgrProxyNative bundleMgrProxyNative;
35 OHOS::AppExecFwk::BundleInfo bundleInfo;
36 auto bundleInfoFlag = static_cast<int32_t>(OHOS::AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION);
37
38 if (!bundleMgrProxyNative.GetBundleInfoForSelf(bundleInfoFlag, bundleInfo)) {
39 APP_LOGE("can not get bundleInfo for self");
40 return nativeApplicationInfo;
41 };
42 OHOS::AppExecFwk::ApplicationInfo applicationInfo = bundleInfo.applicationInfo;
43
44 size_t bundleNameLen = applicationInfo.bundleName.size();
45 if ((bundleNameLen == 0) || (bundleNameLen + 1) > CHAR_MAX_LENGTH) {
46 APP_LOGE("failed due to the length of bundleName is 0 or to long");
47 return nativeApplicationInfo;
48 }
49 nativeApplicationInfo.bundleName = static_cast<char*>(malloc(bundleNameLen + 1));
50 if (nativeApplicationInfo.bundleName == nullptr) {
51 APP_LOGE("failed due to malloc error");
52 return nativeApplicationInfo;
53 }
54
55 if (strcpy_s(nativeApplicationInfo.bundleName, bundleNameLen + 1, applicationInfo.bundleName.c_str()) != EOK) {
56 APP_LOGE("failed due to strcpy_s error");
57 free(nativeApplicationInfo.bundleName);
58 nativeApplicationInfo.bundleName = nullptr;
59 return nativeApplicationInfo;
60 }
61
62 size_t fingerprintLen = applicationInfo.fingerprint.size();
63 if ((fingerprintLen == 0) || (fingerprintLen + 1) > CHAR_MAX_LENGTH) {
64 APP_LOGE("failed due to the length of fingerprint is 0 or to long");
65 free(nativeApplicationInfo.bundleName);
66 nativeApplicationInfo.bundleName = nullptr;
67 return nativeApplicationInfo;
68 }
69 nativeApplicationInfo.fingerprint = static_cast<char*>(malloc(fingerprintLen + 1));
70 if (nativeApplicationInfo.fingerprint == nullptr) {
71 APP_LOGE("failed due to malloc error");
72 free(nativeApplicationInfo.bundleName);
73 nativeApplicationInfo.bundleName = nullptr;
74 return nativeApplicationInfo;
75 }
76
77 if (strcpy_s(nativeApplicationInfo.fingerprint, fingerprintLen + 1, applicationInfo.fingerprint.c_str()) != EOK) {
78 APP_LOGE("failed due to strcpy_s error");
79 free(nativeApplicationInfo.bundleName);
80 nativeApplicationInfo.bundleName = nullptr;
81 free(nativeApplicationInfo.fingerprint);
82 nativeApplicationInfo.fingerprint = nullptr;
83 return nativeApplicationInfo;
84 }
85 APP_LOGI("OH_NativeBundle_GetCurrentApplicationInfo success");
86 return nativeApplicationInfo;
87 }
88