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 "bundle_manager_ffi.h"
17
18 #include <shared_mutex>
19
20 #include "app_log_wrapper.h"
21 #include "bundle_error.h"
22 #include "bundle_info.h"
23 #include "bundle_manager.h"
24 #include "bundle_manager_convert.h"
25 #include "bundle_manager_utils.h"
26 #include "cj_common_ffi.h"
27 #include "ipc_skeleton.h"
28 #include "securec.h"
29
30 using namespace OHOS::CJSystemapi::BundleManager::Convert;
31 using namespace OHOS::CJSystemapi::BundleManager;
32
33 namespace OHOS {
34 namespace CJSystemapi {
35 namespace BundleManager {
36
CharPtrToVector(char ** charPtr,int32_t size)37 std::vector<std::string> CharPtrToVector(char** charPtr, int32_t size)
38 {
39 std::vector<std::string> result;
40 for (int32_t i = 0; i < size; i++) {
41 if (charPtr != nullptr) {
42 result.push_back(std::string(charPtr[i]));
43 }
44 }
45 return result;
46 }
47
48 extern "C" {
FfiOHOSGetCallingUid()49 int32_t FfiOHOSGetCallingUid()
50 {
51 return IPCSkeleton::GetCallingUid();
52 }
53
FfiOHOSGetBundleInfoForSelf(int32_t bundleFlags)54 RetBundleInfo FfiOHOSGetBundleInfoForSelf(int32_t bundleFlags)
55 {
56 APP_LOGI("BundleManager::FfiOHOSGetBundleInfoForSelf");
57 AppExecFwk::BundleInfo bundleInfo = BundleManagerImpl::GetBundleInfoForSelf(bundleFlags);
58 RetBundleInfo cjInfo = ConvertBundleInfo(bundleInfo, bundleFlags);
59 APP_LOGI("BundleManager::FfiOHOSGetBundleInfoForSelf success");
60 return cjInfo;
61 }
62
FfiOHOSGetBundleInfoForSelfV2(int32_t bundleFlags)63 RetBundleInfoV2 FfiOHOSGetBundleInfoForSelfV2(int32_t bundleFlags)
64 {
65 APP_LOGI("BundleManager::FfiOHOSGetBundleInfoForSelf");
66 AppExecFwk::BundleInfo bundleInfo = BundleManagerImpl::GetBundleInfoForSelf(bundleFlags);
67 RetBundleInfoV2 cjInfo = ConvertBundleInfoV2(bundleInfo, bundleFlags);
68 APP_LOGI("BundleManager::FfiOHOSGetBundleInfoForSelf success");
69 return cjInfo;
70 }
71
FfiOHOSVerifyAbc(CArrString cAbcPaths,bool deleteOriginalFiles)72 int32_t FfiOHOSVerifyAbc(CArrString cAbcPaths, bool deleteOriginalFiles)
73 {
74 APP_LOGI("BundleManager::FfiOHOSVerifyAbc");
75 std::vector<std::string> abcPaths = CharPtrToVector(cAbcPaths.head, cAbcPaths.size);
76 auto code = BundleManagerImpl::VerifyAbc(abcPaths, deleteOriginalFiles);
77 if (code != 0) {
78 APP_LOGE("FfiOHOSVerifyAbc failed, code is %{public}d", code);
79 return code;
80 }
81 APP_LOGI("BundleManager::FfiOHOSVerifyAbc success");
82 return code;
83 }
84
FfiGetProfileByExtensionAbility(char * moduleName,char * extensionAbilityName,char * metadataName)85 RetCArrString FfiGetProfileByExtensionAbility(char* moduleName, char* extensionAbilityName, char* metadataName)
86 {
87 APP_LOGI("BundleManager::FfiGetProfileByExtensionAbility");
88 RetCArrString res = { .code = -1, .value = {}};
89 auto [status, extensionAbilityInfo] = BundleManagerImpl::GetProfileByExtensionAbility(
90 std::string(moduleName), std::string(extensionAbilityName), metadataName);
91 if (status != 0) {
92 APP_LOGE("FfiGetProfileByExtensionAbility failed, code is %{public}d", status);
93 return {status, {}};
94 }
95 res.code = SUCCESS_CODE;
96 res.value = ConvertArrString(extensionAbilityInfo);
97 APP_LOGI("BundleManager::FfiGetProfileByExtensionAbility success");
98 return res;
99 }
100
FfiGetProfileByAbility(char * moduleName,char * extensionAbilityName,char * metadataName)101 RetCArrString FfiGetProfileByAbility(char* moduleName, char* extensionAbilityName, char* metadataName)
102 {
103 APP_LOGI("BundleManager::FfiGetProfileByAbility");
104 RetCArrString res = { .code = -1, .value = {}};
105 auto [status, extensionAbilityInfo] = BundleManagerImpl::GetProfileByAbility(
106 std::string(moduleName), std::string(extensionAbilityName), metadataName);
107 if (status != 0) {
108 APP_LOGE("FfiGetProfileByAbility failed, code is %{public}d", status);
109 return {status, {}};
110 }
111 res.code = SUCCESS_CODE;
112 res.value = ConvertArrString(extensionAbilityInfo);
113 APP_LOGI("BundleManager::FfiGetProfileByAbility success");
114 return res;
115 }
116
FfiBundleManagerCanOpenLink(char * link,int32_t & code)117 bool FfiBundleManagerCanOpenLink(char* link, int32_t& code)
118 {
119 std::string cLink(link);
120 return BundleManagerImpl::InnerCanOpenLink(link, code);
121 }
122
FfiOHOSGetAPITargetVersion()123 uint32_t FfiOHOSGetAPITargetVersion()
124 {
125 AppExecFwk::BundleInfo bundleInfo = BundleManagerImpl::GetBundleInfoForSelf(
126 AppExecFwk::BundleFlag::GET_BUNDLE_DEFAULT |
127 AppExecFwk::BundleFlag::GET_BUNDLE_WITH_ABILITIES |
128 AppExecFwk::BundleFlag::GET_BUNDLE_WITH_REQUESTED_PERMISSION |
129 AppExecFwk::BundleFlag::GET_BUNDLE_WITH_EXTENSION_INFO |
130 AppExecFwk::BundleFlag::GET_BUNDLE_WITH_HASH_VALUE);
131 return bundleInfo.targetVersion;
132 }
133
FfiOHOSGetBundleInfo(char * cBundleName,int32_t bundleFlags,int32_t userId,int32_t * errcode)134 FFI_EXPORT RetBundleInfoV2 FfiOHOSGetBundleInfo(char* cBundleName, int32_t bundleFlags,
135 int32_t userId, int32_t* errcode)
136 {
137 APP_LOGD("BundleManager::FfiOHOSGetBundleInfo");
138 RetBundleInfoV2 ret = {};
139 if (errcode == nullptr) {
140 APP_LOGE("BundleManager::FfiOHOSGetBundleInfo errcode is nullptr");
141 return ret;
142 }
143 if (cBundleName == nullptr) {
144 APP_LOGE("BundleManager::FfiOHOSGetBundleInfo cBundleName is nullptr");
145 *errcode = static_cast<int32_t>(ERROR_PARAM_CHECK_ERROR);
146 return ret;
147 }
148 std::string bundleName = std::string(cBundleName);
149 AppExecFwk::BundleInfo bundleInfo;
150 *errcode = BundleManagerImpl::GetBundleInfo(bundleName, bundleFlags, userId, bundleInfo);
151 if (*errcode != SUCCESS_CODE) {
152 APP_LOGE("BundleManager::FfiOHOSGetBundleInfo failed");
153 return ret;
154 }
155 return ConvertBundleInfoV2(bundleInfo, bundleFlags);
156 }
157
FfiOHOSFreeRetBundleInfoV2(RetBundleInfoV2 * bundleInfo)158 FFI_EXPORT void FfiOHOSFreeRetBundleInfoV2(RetBundleInfoV2* bundleInfo)
159 {
160 APP_LOGD("BundleManager::FfiOHOSFreeRetBundleInfoV2");
161 if (bundleInfo == nullptr) {
162 return;
163 }
164 FreeRetBundleInfoV2(*bundleInfo);
165 }
166
FfiOHOSGetBundleNameByUid(int32_t userId,int32_t * errcode)167 FFI_EXPORT char* FfiOHOSGetBundleNameByUid(int32_t userId, int32_t* errcode)
168 {
169 APP_LOGD("BundleManager::FfiOHOSGetBundleNameByUid");
170 if (errcode == nullptr) {
171 return nullptr;
172 }
173 auto bundleName = BundleManagerImpl::GetBundleNameByUid(userId, errcode);
174 if (*errcode != SUCCESS_CODE) {
175 APP_LOGE("BundleManager::FfiOHOSGetBundleNameByUid failed");
176 return nullptr;
177 }
178 APP_LOGD("BundleManager::FfiOHOSGetBundleNameByUid success");
179 return Convert::MallocCString(bundleName);
180 }
181
FfiOHOSFreeCString(char * retCString)182 FFI_EXPORT void FfiOHOSFreeCString(char* retCString)
183 {
184 APP_LOGD("BundleManager::FfiOHOSFreeCString");
185 if (retCString == nullptr) {
186 return;
187 }
188 free(retCString);
189 }
190 }
191
192 } // BundleManager
193 } // CJSystemapi
194 } // OHOS