1 /*
2 * Copyright (c) 2020 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 "bundlems_slite_client.h"
17
18 #include "adapter.h"
19 #include "appexecfwk_errors.h"
20 #include "bundle_mgr_service.h"
21 #include "bundlems_log.h"
22 #include "cmsis_os2.h"
23 #include "ohos_mem_pool.h"
24 #include "samgr_lite.h"
25 #include "unistd.h"
26 #include "utils.h"
27 #include "want_utils.h"
28
29 namespace OHOS {
30 const unsigned int BMS_INSTALL_MSG = 100;
31 const unsigned int ERROR_SLEEP_TIMES = 300;
32 const unsigned int RETRY_TIMES = 10;
33 Bmsbuff *g_bmsbuff = nullptr;
34
Initialize() const35 bool BundleMsClient::Initialize() const
36 {
37 if (bmsProxy_ != nullptr) {
38 return true;
39 }
40 int retry = RETRY_TIMES;
41 while (retry--) {
42 IUnknown *iUnknown = SAMGR_GetInstance()->GetFeatureApi(BMS_SERVICE, BMS_SLITE_FEATURE);
43 if (iUnknown == nullptr) {
44 osDelay(ERROR_SLEEP_TIMES); // sleep 300ms
45 continue;
46 }
47
48 (void)iUnknown->QueryInterface(iUnknown, DEFAULT_VERSION, (void **)&bmsProxy_);
49 if (bmsProxy_ == nullptr) {
50 osDelay(ERROR_SLEEP_TIMES); // sleep 300ms
51 continue;
52 }
53
54 return true;
55 }
56
57 return false;
58 }
59
Install(const char * hapPath,const InstallParam * installParam,InstallerCallback installerCallback) const60 bool BundleMsClient::Install(const char *hapPath, const InstallParam *installParam,
61 InstallerCallback installerCallback) const
62 {
63 if (g_bmsbuff == nullptr) {
64 g_bmsbuff = reinterpret_cast<Bmsbuff *>(OhosMalloc(MEM_TYPE_APPFMK_LSRAM, sizeof(Bmsbuff)));
65 }
66 BundleMgrService *service = BundleMgrService::GetInstance();
67 if (service == nullptr) {
68 return false;
69 }
70 if (hapPath == nullptr) {
71 return false;
72 }
73
74 if (memset_s(g_bmsbuff->bundleParameter, MAX_PATH_LEN, 0, MAX_PATH_LEN) != 0) {
75 return false;
76 }
77 int len = strlen(hapPath);
78 if (len >= MAX_PATH_LEN) {
79 return false;
80 }
81 if (memcpy_s(g_bmsbuff->bundleParameter, MAX_PATH_LEN, hapPath, len + 1) != 0) {
82 return false;
83 }
84 g_bmsbuff->bundleInstallerCallback = installerCallback;
85 Request request = {
86 .msgId = BMS_INSTALL_MSG,
87 .len = 0,
88 .data = nullptr,
89 .msgValue = 0,
90 };
91
92 int32_t ret = SAMGR_SendRequest(service->GetIdentity(), &request, nullptr);
93 return ret == ERR_OK;
94 }
95
Uninstall(const char * bundleName,const InstallParam * installParam,InstallerCallback installerCallback) const96 bool BundleMsClient::Uninstall (const char *bundleName, const InstallParam *installParam,
97 InstallerCallback installerCallback) const
98 {
99 BundleMgrService *service = BundleMgrService::GetInstance();
100 if (service == nullptr) {
101 return false;
102 }
103 if (bundleName == nullptr) {
104 return false;
105 }
106 if (g_bmsbuff == nullptr) {
107 g_bmsbuff = reinterpret_cast<Bmsbuff *>(OhosMalloc(MEM_TYPE_APPFMK_LSRAM, sizeof(Bmsbuff)));
108 }
109 if (memset_s(g_bmsbuff->bundleParameter, MAX_PATH_LEN, 0, MAX_PATH_LEN) != 0) {
110 return false;
111 }
112 int len = strlen(bundleName);
113 if (len >= MAX_PATH_LEN) {
114 return false;
115 }
116 if (memcpy_s(g_bmsbuff->bundleParameter, MAX_PATH_LEN, bundleName, len + 1) != 0) {
117 return false;
118 }
119 g_bmsbuff->bundleInstallerCallback = installerCallback;
120 Request request = {
121 .msgId = BMS_UNINSTALL_MSG,
122 .len = 0,
123 .data = nullptr,
124 .msgValue = 0,
125 };
126 int32_t ret = SAMGR_SendRequest(service->GetIdentity(), &request, nullptr);
127 return ret == ERR_OK;
128 }
129
RegisterInstallerCallback(InstallerCallback installerCallback) const130 bool BundleMsClient::RegisterInstallerCallback (InstallerCallback installerCallback) const
131 {
132 BundleMgrService *service = BundleMgrService::GetInstance();
133 if (service == nullptr) {
134 return false;
135 }
136 if (installerCallback == nullptr) {
137 return false;
138 }
139 if (g_bmsbuff == nullptr) {
140 g_bmsbuff = reinterpret_cast<Bmsbuff *>(OhosMalloc(MEM_TYPE_APPFMK_LSRAM, sizeof(Bmsbuff)));
141 }
142 g_bmsbuff->bundleInstallerCallback = installerCallback;
143 Request request = {
144 .msgId = BMS_REGISTER_CALLBACK_MSG,
145 .len = 0,
146 .data = nullptr,
147 .msgValue = 0,
148 };
149 int32_t ret = SAMGR_SendRequest(service->GetIdentity(), &request, nullptr);
150 return ret == ERR_OK;
151 }
152
QueryAbilityInfo(const Want * want,AbilityInfo * abilityInfo) const153 uint8_t BundleMsClient::QueryAbilityInfo (const Want *want, AbilityInfo *abilityInfo) const
154 {
155 if ((want == nullptr) || (abilityInfo == nullptr)) {
156 return ERR_APPEXECFWK_QUERY_PARAMETER_ERROR;
157 }
158 if (!Initialize()) {
159 return ERR_APPEXECFWK_INSTALL_FAILED_INTERNAL_ERROR;
160 }
161 return bmsProxy_->QueryAbilityInfo(want, abilityInfo);
162 }
163
GetBundleInfo(const char * bundleName,int32_t flags,BundleInfo * bundleInfo) const164 uint8_t BundleMsClient::GetBundleInfo (const char *bundleName, int32_t flags, BundleInfo *bundleInfo) const
165 {
166 if ((bundleName == nullptr) || (bundleInfo == nullptr)) {
167 return ERR_APPEXECFWK_QUERY_PARAMETER_ERROR;
168 }
169 if (!Initialize()) {
170 HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] Initialize is failed");
171 return ERR_APPEXECFWK_INSTALL_FAILED_INTERNAL_ERROR;
172 }
173 return bmsProxy_->GetBundleInfo(bundleName, flags, bundleInfo);
174 }
175
GetBundleInfos(int32_t flags,BundleInfo ** bundleInfos,int32_t * len) const176 uint8_t BundleMsClient::GetBundleInfos (int32_t flags, BundleInfo **bundleInfos, int32_t *len) const
177 {
178 if (bundleInfos == nullptr) {
179 return ERR_APPEXECFWK_QUERY_PARAMETER_ERROR;
180 }
181 if (len == nullptr) {
182 return ERR_APPEXECFWK_QUERY_NO_INFOS;
183 }
184 if (!Initialize()) {
185 HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] Initialize is failed");
186 return ERR_APPEXECFWK_INSTALL_FAILED_INTERNAL_ERROR;
187 }
188 return bmsProxy_->GetBundleInfos(flags, bundleInfos, len);
189 }
190
GetInstallState(const char * bundleName,InstallState * installState,uint8_t * installProcess) const191 bool BundleMsClient::GetInstallState (const char *bundleName, InstallState *installState, uint8_t *installProcess) const
192 {
193 if ((bundleName == nullptr) || (installState == nullptr) || (installProcess == nullptr)) {
194 return ERR_APPEXECFWK_QUERY_PARAMETER_ERROR;
195 }
196 if (!Initialize()) {
197 return false;
198 }
199 return bmsProxy_->GetInstallState(bundleName, installState, installProcess);
200 }
201
GetBundleSize(const char * bundleName) const202 uint32_t BundleMsClient::GetBundleSize (const char *bundleName) const
203 {
204 if (bundleName == nullptr) {
205 return ERR_APPEXECFWK_QUERY_PARAMETER_ERROR;
206 }
207 if (!Initialize()) {
208 return ERR_APPEXECFWK_INSTALL_FAILED_INTERNAL_ERROR;
209 }
210 return bmsProxy_->GetBundleSize(bundleName);
211 }
212
UpdateBundleInfoList() const213 void BundleMsClient::UpdateBundleInfoList () const
214 {
215 if (!Initialize()) {
216 return;
217 }
218 bmsProxy_->UpdateBundleInfoList();
219 }
220
GetBundleInfosNoReplication(const int flags,BundleInfo ** bundleInfos,int32_t * len) const221 uint8_t BundleMsClient::GetBundleInfosNoReplication (const int flags, BundleInfo **bundleInfos, int32_t *len) const
222 {
223 if ((bundleInfos == nullptr) || (len == nullptr)) {
224 return ERR_APPEXECFWK_QUERY_PARAMETER_ERROR;
225 }
226 if (!Initialize()) {
227 return ERR_APPEXECFWK_INSTALL_FAILED_INTERNAL_ERROR;
228 }
229 return bmsProxy_->GetBundleInfosNoReplication(flags, bundleInfos, len);
230 }
231
InitPreAppInfo() const232 PreAppList *BundleMsClient::InitPreAppInfo () const
233 {
234 if (!Initialize()) {
235 return nullptr;
236 }
237 return bmsProxy_->InitPreAppInfo();
238 }
239
InsertPreAppInfo(const char * filePath,PreAppList * list) const240 void BundleMsClient::InsertPreAppInfo (const char *filePath, PreAppList *list) const
241 {
242 if ((filePath == nullptr) || (list == nullptr)) {
243 return;
244 }
245 if (!Initialize()) {
246 return;
247 }
248 bmsProxy_->InsertPreAppInfo(filePath, list);
249 }
250
SetPreAppInfo(PreAppList * list) const251 void BundleMsClient::SetPreAppInfo(PreAppList *list) const
252 {
253 if (list == nullptr) {
254 return;
255 }
256 if (!Initialize()) {
257 return;
258 }
259 bmsProxy_->SetPreAppInfo(list);
260 }
261 } // namespace OHOS
262