• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
31 // Helper function to release char* memory
ReleaseMemory(char * & str)32 static void ReleaseMemory(char* &str)
33 {
34     if (str != nullptr) {
35         free(str);
36         str = nullptr;
37     }
38 }
39 
40 template <typename... Args>
ReleaseStrings(Args...args)41 void ReleaseStrings(Args... args)
42 {
43     (ReleaseMemory(args), ...);
44 }
45 
OH_NativeBundle_GetCurrentApplicationInfo()46 OH_NativeBundle_ApplicationInfo OH_NativeBundle_GetCurrentApplicationInfo()
47 {
48     OH_NativeBundle_ApplicationInfo nativeApplicationInfo;
49     OHOS::AppExecFwk::BundleMgrProxyNative bundleMgrProxyNative;
50     OHOS::AppExecFwk::BundleInfo bundleInfo;
51     auto bundleInfoFlag = static_cast<int32_t>(OHOS::AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION) |
52         static_cast<int32_t>(OHOS::AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_SIGNATURE_INFO);
53 
54     if (!bundleMgrProxyNative.GetBundleInfoForSelf(bundleInfoFlag, bundleInfo)) {
55         APP_LOGE("can not get bundleInfo for self");
56         return nativeApplicationInfo;
57     };
58     size_t bundleNameLen = bundleInfo.applicationInfo.bundleName.size();
59     if ((bundleNameLen == 0) || (bundleNameLen + 1) > CHAR_MAX_LENGTH) {
60         APP_LOGE("failed due to the length of bundleName is empty or too long");
61         return nativeApplicationInfo;
62     }
63     nativeApplicationInfo.bundleName = static_cast<char*>(malloc(bundleNameLen + 1));
64     if (nativeApplicationInfo.bundleName == nullptr) {
65         APP_LOGE("failed due to malloc error");
66         return nativeApplicationInfo;
67     }
68     if (strcpy_s(nativeApplicationInfo.bundleName, bundleNameLen + 1,
69         bundleInfo.applicationInfo.bundleName.c_str()) != EOK) {
70         APP_LOGE("failed due to strcpy_s error");
71         ReleaseStrings(nativeApplicationInfo.bundleName);
72         return nativeApplicationInfo;
73     }
74     size_t fingerprintLen = bundleInfo.signatureInfo.fingerprint.size();
75     if ((fingerprintLen == 0) || (fingerprintLen + 1) > CHAR_MAX_LENGTH) {
76         APP_LOGE("failed due to the length of fingerprint is empty or too long");
77         ReleaseStrings(nativeApplicationInfo.bundleName);
78         return nativeApplicationInfo;
79     }
80     nativeApplicationInfo.fingerprint = static_cast<char*>(malloc(fingerprintLen + 1));
81     if (nativeApplicationInfo.fingerprint == nullptr) {
82         APP_LOGE("failed due to malloc error");
83         ReleaseStrings(nativeApplicationInfo.bundleName);
84         return nativeApplicationInfo;
85     }
86     if (strcpy_s(nativeApplicationInfo.fingerprint, fingerprintLen + 1,
87         bundleInfo.signatureInfo.fingerprint.c_str()) != EOK) {
88         APP_LOGE("failed due to strcpy_s error");
89         ReleaseStrings(nativeApplicationInfo.bundleName, nativeApplicationInfo.fingerprint);
90         return nativeApplicationInfo;
91     }
92     APP_LOGI("OH_NativeBundle_GetCurrentApplicationInfo success");
93     return nativeApplicationInfo;
94 }
95 
OH_NativeBundle_GetAppId()96 char* OH_NativeBundle_GetAppId()
97 {
98     OHOS::AppExecFwk::BundleMgrProxyNative bundleMgrProxyNative;
99     OHOS::AppExecFwk::BundleInfo bundleInfo;
100     auto bundleInfoFlag =
101         static_cast<int32_t>(OHOS::AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_SIGNATURE_INFO);
102 
103     if (!bundleMgrProxyNative.GetBundleInfoForSelf(bundleInfoFlag, bundleInfo)) {
104         APP_LOGE("can not get bundleInfo for self");
105         return nullptr;
106     };
107 
108     size_t appIdLen = bundleInfo.signatureInfo.appId.size();
109     if ((appIdLen == 0) || (appIdLen + 1) > CHAR_MAX_LENGTH) {
110         APP_LOGE("failed due to the length of appId is empty or too long");
111         return nullptr;
112     }
113     char *appId = static_cast<char*>(malloc(appIdLen + 1));
114     if (appId == nullptr) {
115         APP_LOGE("failed due to malloc error");
116         return nullptr;
117     }
118     if (strcpy_s(appId, appIdLen + 1, bundleInfo.signatureInfo.appId.c_str()) != EOK) {
119         APP_LOGE("failed due to strcpy_s error");
120         free(appId);
121         return nullptr;
122     }
123     APP_LOGI("OH_NativeBundle_GetAppId success");
124     return appId;
125 }
126 
OH_NativeBundle_GetAppIdentifier()127 char* OH_NativeBundle_GetAppIdentifier()
128 {
129     OHOS::AppExecFwk::BundleMgrProxyNative bundleMgrProxyNative;
130     OHOS::AppExecFwk::BundleInfo bundleInfo;
131     auto bundleInfoFlag =
132         static_cast<int32_t>(OHOS::AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_SIGNATURE_INFO);
133 
134     if (!bundleMgrProxyNative.GetBundleInfoForSelf(bundleInfoFlag, bundleInfo)) {
135         APP_LOGE("can not get bundleInfo for self");
136         return nullptr;
137     };
138 
139     size_t appIdentifierLen = bundleInfo.signatureInfo.appIdentifier.size();
140     if (appIdentifierLen + 1 > CHAR_MAX_LENGTH) {
141         APP_LOGE("failed due to the length of appIdentifier is too long");
142         return nullptr;
143     }
144     char* appIdentifier = static_cast<char*>(malloc(appIdentifierLen + 1));
145     if (appIdentifier == nullptr) {
146         APP_LOGE("failed due to malloc error");
147         return nullptr;
148     }
149     if (strcpy_s(appIdentifier, appIdentifierLen + 1,
150         bundleInfo.signatureInfo.appIdentifier.c_str()) != EOK) {
151         APP_LOGE("failed due to strcpy_s error");
152         free(appIdentifier);
153         return nullptr;
154     }
155     APP_LOGI("OH_NativeBundle_GetAppIdentifier success");
156     return appIdentifier;
157 }
158