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 "securec.h"
17 #include "bundle_manager_log.h"
18 #include "cj_common_ffi.h"
19 #include "bundle_manager_utils.h"
20 #include "bundle_manager.h"
21 #include "bundle_info.h"
22 #include "ipc_skeleton.h"
23 #include "bundle_manager_convert.h"
24 #include "bundle_manager_ffi.h"
25 #include <shared_mutex>
26 
27 using namespace OHOS::CJSystemapi::BundleManager::Convert;
28 using namespace OHOS::CJSystemapi::BundleManager;
29 
30 namespace OHOS {
31 namespace CJSystemapi {
32 namespace BundleManager {
33 
CharPtrToVector(char ** charPtr,int32_t size)34 std::vector<std::string> CharPtrToVector(char** charPtr, int32_t size)
35 {
36     std::vector<std::string> result;
37     for (int32_t i = 0; i < size; i++) {
38         if (charPtr != nullptr) {
39             result.push_back(std::string(charPtr[i]));
40         }
41     }
42     return result;
43 }
44 
VectorToCArrString(std::vector<std::string> & vec)45 CArrString VectorToCArrString(std::vector<std::string> &vec)
46 {
47     char** result = new char* [vec.size()];
48     if (result == nullptr) {
49         LOGE("VectorToCArrString malloc failed");
50         return {nullptr, 0};
51     }
52     size_t temp = 0;
53     for (size_t i = 0; i < vec.size(); i++) {
54         result[i] = new char[vec[i].length() + 1];
55         if (result[i] == nullptr) {
56             break;
57         }
58         auto res = strcpy_s(result[i], vec[i].length() + 1, vec[i].c_str());
59         if (res != EOK) {
60             LOGE("failed to strcpy_s.")
61         }
62         temp++;
63     }
64 
65     if (temp != vec.size()) {
66         for (size_t j = temp; j > 0; j--) {
67             delete result[j - 1];
68             result[j - 1] = nullptr;
69         }
70         delete[] result;
71         return {nullptr, 0};
72     }
73     return {result, vec.size()};
74 }
75 
76 extern "C" {
FfiOHOSGetCallingUid()77     int32_t FfiOHOSGetCallingUid()
78     {
79         return IPCSkeleton::GetCallingUid();
80     }
81 
FfiOHOSGetBundleInfoForSelf(int32_t bundleFlags)82     RetBundleInfo FfiOHOSGetBundleInfoForSelf(int32_t bundleFlags)
83     {
84         LOGI("BundleManager::FfiOHOSGetBundleInfoForSelf");
85         AppExecFwk::BundleInfo bundleInfo = BundleManagerImpl::GetBundleInfoForSelf(bundleFlags);
86         RetBundleInfo cjInfo = ConvertBundleInfo(bundleInfo, bundleFlags);
87         LOGI("BundleManager::FfiOHOSGetBundleInfoForSelf success");
88         return cjInfo;
89     }
90 
FfiOHOSVerifyAbc(CArrString cAbcPaths,bool deleteOriginalFiles)91     int32_t FfiOHOSVerifyAbc(CArrString cAbcPaths, bool deleteOriginalFiles)
92     {
93         LOGI("BundleManager::FfiOHOSVerifyAbc");
94         std::vector<std::string> abcPaths = CharPtrToVector(cAbcPaths.head, cAbcPaths.size);
95         auto code = BundleManagerImpl::VerifyAbc(abcPaths, deleteOriginalFiles);
96         if (code != 0) {
97             LOGE("FfiOHOSVerifyAbc failed, code is %{public}d", code);
98             return code;
99         }
100         LOGI("BundleManager::FfiOHOSVerifyAbc success");
101         return code;
102     }
103 
FfiGetProfileByExtensionAbility(char * moduleName,char * extensionAbilityName,char * metadataName)104     RetCArrString FfiGetProfileByExtensionAbility(char* moduleName, char* extensionAbilityName, char* metadataName)
105     {
106         LOGI("BundleManager::FfiGetProfileByExtensionAbility");
107         RetCArrString res = { .code = -1, .value = {}};
108         auto [status, extensionAbilityInfo] = BundleManagerImpl::GetProfileByExtensionAbility(
109             std::string(moduleName), std::string(extensionAbilityName), metadataName);
110         if (status != 0) {
111             LOGE("FfiGetProfileByExtensionAbility failed, code is %{public}d", status);
112             return {status, {}};
113         }
114         res.code = SUCCESS_CODE;
115         res.value = VectorToCArrString(extensionAbilityInfo);
116         LOGI("BundleManager::FfiGetProfileByExtensionAbility success");
117         return res;
118     }
119 
FfiGetProfileByAbility(char * moduleName,char * extensionAbilityName,char * metadataName)120     RetCArrString FfiGetProfileByAbility(char* moduleName, char* extensionAbilityName, char* metadataName)
121     {
122         LOGI("BundleManager::FfiGetProfileByAbility");
123         RetCArrString res = { .code = -1, .value = {}};
124         auto [status, extensionAbilityInfo] = BundleManagerImpl::GetProfileByAbility(
125             std::string(moduleName), std::string(extensionAbilityName), metadataName);
126         if (status != 0) {
127             LOGE("FfiGetProfileByAbility failed, code is %{public}d", status);
128             return {status, {}};
129         }
130         res.code = SUCCESS_CODE;
131         res.value = VectorToCArrString(extensionAbilityInfo);
132         LOGI("BundleManager::FfiGetProfileByAbility success");
133         return res;
134     }
135 }
136 
137 } // BundleManager
138 } // CJSystemapi
139 } // OHOS