• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "app_log_wrapper.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     if (vec.size() == 0) {
48         return {nullptr, 0};
49     }
50     char** result = new char* [vec.size()];
51     if (result == nullptr) {
52         APP_LOGE("VectorToCArrString malloc failed");
53         return {nullptr, 0};
54     }
55     size_t temp = 0;
56     for (size_t i = 0; i < vec.size(); i++) {
57         result[i] = new char[vec[i].length() + 1];
58         if (result[i] == nullptr) {
59             break;
60         }
61         auto res = strcpy_s(result[i], vec[i].length() + 1, vec[i].c_str());
62         if (res != EOK) {
63             APP_LOGE("failed to strcpy_s.");
64         }
65         temp++;
66     }
67 
68     if (temp != vec.size()) {
69         for (size_t j = temp; j > 0; j--) {
70             delete result[j - 1];
71             result[j - 1] = nullptr;
72         }
73         delete[] result;
74         return {nullptr, 0};
75     }
76     return {result, vec.size()};
77 }
78 
79 extern "C" {
FfiOHOSGetCallingUid()80     int32_t FfiOHOSGetCallingUid()
81     {
82         return IPCSkeleton::GetCallingUid();
83     }
84 
FfiOHOSGetBundleInfoForSelfV2(int32_t bundleFlags)85     RetBundleInfoV2 FfiOHOSGetBundleInfoForSelfV2(int32_t bundleFlags)
86     {
87         APP_LOGI("BundleManager::FfiOHOSGetBundleInfoForSelf");
88         AppExecFwk::BundleInfo bundleInfo = BundleManagerImpl::GetBundleInfoForSelf(bundleFlags);
89         RetBundleInfoV2 cjInfo = ConvertBundleInfoV2(bundleInfo, bundleFlags);
90         APP_LOGI("BundleManager::FfiOHOSGetBundleInfoForSelf success");
91         return cjInfo;
92     }
93 
FfiOHOSGetBundleInfoForSelf(int32_t bundleFlags)94     RetBundleInfo FfiOHOSGetBundleInfoForSelf(int32_t bundleFlags)
95     {
96         APP_LOGI("BundleManager::FfiOHOSGetBundleInfoForSelf");
97         AppExecFwk::BundleInfo bundleInfo = BundleManagerImpl::GetBundleInfoForSelf(bundleFlags);
98         RetBundleInfo cjInfo = ConvertBundleInfo(bundleInfo, bundleFlags);
99         APP_LOGI("BundleManager::FfiOHOSGetBundleInfoForSelf success");
100         return cjInfo;
101     }
102 
FfiOHOSVerifyAbc(CArrString cAbcPaths,bool deleteOriginalFiles)103     int32_t FfiOHOSVerifyAbc(CArrString cAbcPaths, bool deleteOriginalFiles)
104     {
105         APP_LOGI("BundleManager::FfiOHOSVerifyAbc");
106         std::vector<std::string> abcPaths = CharPtrToVector(cAbcPaths.head, cAbcPaths.size);
107         auto code = BundleManagerImpl::VerifyAbc(abcPaths, deleteOriginalFiles);
108         if (code != 0) {
109             APP_LOGE("FfiOHOSVerifyAbc failed, code is %{public}d", code);
110             return code;
111         }
112         APP_LOGI("BundleManager::FfiOHOSVerifyAbc success");
113         return code;
114     }
115 
FfiGetProfileByExtensionAbility(char * moduleName,char * extensionAbilityName,char * metadataName)116     RetCArrString FfiGetProfileByExtensionAbility(char* moduleName, char* extensionAbilityName, char* metadataName)
117     {
118         APP_LOGI("BundleManager::FfiGetProfileByExtensionAbility");
119         RetCArrString res = { .code = -1, .value = {}};
120         auto [status, extensionAbilityInfo] = BundleManagerImpl::GetProfileByExtensionAbility(
121             std::string(moduleName), std::string(extensionAbilityName), metadataName);
122         if (status != 0) {
123             APP_LOGE("FfiGetProfileByExtensionAbility failed, code is %{public}d", status);
124             return {status, {}};
125         }
126         res.code = SUCCESS_CODE;
127         res.value = VectorToCArrString(extensionAbilityInfo);
128         APP_LOGI("BundleManager::FfiGetProfileByExtensionAbility success");
129         return res;
130     }
131 
FfiGetProfileByAbility(char * moduleName,char * extensionAbilityName,char * metadataName)132     RetCArrString FfiGetProfileByAbility(char* moduleName, char* extensionAbilityName, char* metadataName)
133     {
134         APP_LOGI("BundleManager::FfiGetProfileByAbility");
135         RetCArrString res = { .code = -1, .value = {}};
136         auto [status, extensionAbilityInfo] = BundleManagerImpl::GetProfileByAbility(
137             std::string(moduleName), std::string(extensionAbilityName), metadataName);
138         if (status != 0) {
139             APP_LOGE("FfiGetProfileByAbility failed, code is %{public}d", status);
140             return {status, {}};
141         }
142         res.code = SUCCESS_CODE;
143         res.value = VectorToCArrString(extensionAbilityInfo);
144         APP_LOGI("BundleManager::FfiGetProfileByAbility success");
145         return res;
146     }
147 
FfiBundleManagerCanOpenLink(char * link,int32_t & code)148     bool FfiBundleManagerCanOpenLink(char* link, int32_t& code)
149     {
150         std::string cLink(link);
151         return BundleManagerImpl::InnerCanOpenLink(link, code);
152     }
153 }
154 
155 } // BundleManager
156 } // CJSystemapi
157 } // OHOS