• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 #include "st_ability_util.h"
16 #include "status_receiver_host.h"
17 #include "iservice_registry.h"
18 namespace OHOS {
19 namespace STABUtil {
20 using namespace OHOS;
21 using namespace OHOS::AAFwk;
22 using namespace OHOS::AppExecFwk;
23 using namespace OHOS::MTUtil;
24 using namespace OHOS::STtools;
25 using namespace OHOS::EventFwk;
26 
27 std::mutex STAbilityUtil::mutex_;
28 std::shared_ptr<STAbilityUtil> STAbilityUtil::instance_ = nullptr;
29 
PublishEvent(const std::string & eventName,const int & code,const std::string & data)30 bool STAbilityUtil::PublishEvent(const std::string &eventName, const int &code, const std::string &data)
31 {
32     Want want;
33     want.SetAction(eventName);
34     CommonEventData commonData;
35     commonData.SetWant(want);
36     commonData.SetCode(code);
37     commonData.SetData(data);
38     return CommonEventManager::PublishCommonEvent(commonData);
39 }
40 
InstallHaps(vector_str & hapNames)41 void STAbilityUtil::InstallHaps(vector_str &hapNames)
42 {
43     for (auto hapName : hapNames) {
44         Install(hapName);
45     }
46 }
47 
UninstallBundle(vector_str & bundleNames)48 void STAbilityUtil::UninstallBundle(vector_str &bundleNames)
49 {
50     for (auto bundleName : bundleNames) {
51         Uninstall(bundleName);
52     }
53 }
54 
KillService(const std::string & serviceName)55 void STAbilityUtil::KillService(const std::string &serviceName)
56 {
57     system(("kill -9 $(pidof " + serviceName + ") > /dev/null 2>&1").c_str());
58 }
59 
StartService(const std::string & serviceName,const time_t & delay)60 void STAbilityUtil::StartService(const std::string &serviceName, const time_t &delay)
61 {
62     system(("/system/bin/" + serviceName + "& > /dev/null 2>&1").c_str());
63     std::this_thread::sleep_for(std::chrono::milliseconds(delay));
64 }
65 
KillBundleProcess(vector_str & bundleNames)66 void STAbilityUtil::KillBundleProcess(vector_str &bundleNames)
67 {
68     for (std::string bundleName : bundleNames) {
69         KillService(bundleName);
70     }
71 }
72 
GetAbilityManagerService()73 sptr<IAbilityManager> STAbilityUtil::GetAbilityManagerService()
74 {
75     sptr<IRemoteObject> abilityMsObj =
76         OHOS::DelayedSingleton<SaMgrClient>::GetInstance()->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
77     if (abilityMsObj == nullptr) {
78         HILOG_ERROR("failed to get ability manager service");
79         return nullptr;
80     }
81     return iface_cast<IAbilityManager>(abilityMsObj);
82 }
83 
GetAppMgrService()84 sptr<IAppMgr> STAbilityUtil::GetAppMgrService()
85 {
86     sptr<IRemoteObject> appMsObj =
87         OHOS::DelayedSingleton<SaMgrClient>::GetInstance()->GetSystemAbility(APP_MGR_SERVICE_ID);
88     if (appMsObj == nullptr) {
89         HILOG_ERROR("failed to get app manager service");
90         return nullptr;
91     }
92     return iface_cast<IAppMgr>(appMsObj);
93 }
94 
StartAbility(const Want & want,sptr<IAbilityManager> & abilityMs,const time_t & delay)95 ErrCode STAbilityUtil::StartAbility(const Want &want, sptr<IAbilityManager> &abilityMs, const time_t &delay)
96 {
97     ErrCode result = OHOS::ERR_OK;
98     abilityMs = GetAbilityManagerService();
99     if (abilityMs == nullptr) {
100         result = OHOS::ERR_INVALID_VALUE;
101         HILOG_ERROR("failed to get ability manager service");
102         return result;
103     }
104 
105     result = abilityMs->StartAbility(want);
106     std::this_thread::sleep_for(std::chrono::milliseconds(delay));
107     if (result == OHOS::ERR_OK) {
108         HILOG_INFO("start ability successfully.");
109     } else {
110         HILOG_INFO("failed to start ability.");
111     }
112 
113     return result;
114 }
115 
StopAbility(const std::string & eventName,const int & code,const std::string & data)116 bool STAbilityUtil::StopAbility(const std::string &eventName, const int &code, const std::string &data)
117 {
118     return PublishEvent(eventName, code, data);
119 }
120 
StopServiceAbility(const Want & want,unsigned int usec)121 ErrCode STAbilityUtil::StopServiceAbility(const Want &want, unsigned int usec)
122 {
123     ErrCode result = OHOS::ERR_OK;
124 
125     sptr<IAbilityManager> abilityMs = GetAbilityManagerService();
126     if (abilityMs == nullptr) {
127         result = OHOS::ERR_INVALID_VALUE;
128         HILOG_ERROR("failed to get ability manager service");
129         return result;
130     }
131 
132     result = abilityMs->StopServiceAbility(want);
133     if (result == OHOS::ERR_OK) {
134         HILOG_INFO("stop service ability successfully.");
135     } else {
136         HILOG_INFO("failed to stop service ability.");
137     }
138 
139     return result;
140 }
141 
ConnectAbility(const Want & want,const sptr<IAbilityConnection> & connect,const sptr<IRemoteObject> & callerToken,unsigned int usec)142 ErrCode STAbilityUtil::ConnectAbility(const Want &want, const sptr<IAbilityConnection> &connect,
143     const sptr<IRemoteObject> &callerToken, unsigned int usec)
144 {
145     ErrCode result = OHOS::ERR_OK;
146 
147     sptr<IAbilityManager> abilityMs = GetAbilityManagerService();
148     if (abilityMs == nullptr) {
149         result = OHOS::ERR_INVALID_VALUE;
150         HILOG_ERROR("failed to get ability manager service");
151         return result;
152     }
153 
154     result = abilityMs->ConnectAbility(want, connect, callerToken);
155     if (result == OHOS::ERR_OK) {
156         HILOG_INFO("connect ability successfully.");
157     } else {
158         HILOG_INFO("failed to connect ability.");
159     }
160 
161     return result;
162 }
163 
DisconnectAbility(const sptr<IAbilityConnection> & connect,unsigned int usec)164 ErrCode STAbilityUtil::DisconnectAbility(const sptr<IAbilityConnection> &connect, unsigned int usec)
165 {
166     ErrCode result = OHOS::ERR_OK;
167 
168     sptr<IAbilityManager> abilityMs = GetAbilityManagerService();
169     if (abilityMs == nullptr) {
170         result = OHOS::ERR_INVALID_VALUE;
171         HILOG_ERROR("failed to get ability manager service");
172         return result;
173     }
174 
175     result = abilityMs->DisconnectAbility(connect);
176     if (result == OHOS::ERR_OK) {
177         HILOG_INFO("StopServiceAbility successfully.");
178     } else {
179         HILOG_INFO("failed to StopServiceAbility.");
180     }
181 
182     return result;
183 }
184 
MakeWant(std::string deviceId,std::string abilityName,std::string bundleName,MAP_STR_STR params)185 Want STAbilityUtil::MakeWant(std::string deviceId, std::string abilityName, std::string bundleName, MAP_STR_STR params)
186 {
187     ElementName element(deviceId, bundleName, abilityName);
188     Want want;
189     want.SetElement(element);
190     for (const auto &param : params) {
191         want.SetParam(param.first, param.second);
192     }
193     return want;
194 }
195 
MakeWant(std::string deviceId,std::string abilityName,std::string bundleName,vector_str params)196 Want STAbilityUtil::MakeWant(std::string deviceId, std::string abilityName, std::string bundleName, vector_str params)
197 {
198     ElementName element(deviceId, bundleName, abilityName);
199     Want want;
200     want.SetElement(element);
201     want.SetParam("operator", params);
202     return want;
203 }
204 
GetTopAbilityRecordId(int64_t & id,sptr<IAbilityManager> & abilityMs)205 ErrCode STAbilityUtil::GetTopAbilityRecordId(int64_t &id, sptr<IAbilityManager> &abilityMs)
206 {
207     ErrCode result = OHOS::ERR_OK;
208     id = -1;
209     abilityMs = GetAbilityManagerService();
210     if (abilityMs == nullptr) {
211         result = OHOS::ERR_INVALID_VALUE;
212         HILOG_ERROR("failed to get ability manager service");
213         return result;
214     }
215     StackInfo stackInfo;
216     abilityMs->GetAllStackInfo(stackInfo);
217     MissionStackInfo defaultMissionStack;
218     for (const auto &stackInfo : stackInfo.missionStackInfos) {
219         if (stackInfo.id == 1) {  // DEFAULT_MISSION_STACK_ID = 1
220             defaultMissionStack = stackInfo;
221             break;
222         }
223     }
224     if (!defaultMissionStack.missionRecords.empty() &&
225         !defaultMissionStack.missionRecords.begin()->abilityRecordInfos.empty()) {
226         id = defaultMissionStack.missionRecords.begin()->abilityRecordInfos.begin()->id;
227     }
228     return result;
229 }
230 
GetRunningProcessInfo(std::vector<RunningProcessInfo> & runningProcessInfo,sptr<IAppMgr> & appMs,const time_t & delay)231 ErrCode STAbilityUtil::GetRunningProcessInfo(
232     std::vector<RunningProcessInfo> &runningProcessInfo, sptr<IAppMgr> &appMs, const time_t &delay)
233 {
234     ErrCode result = ERR_OK;
235     appMs = GetAppMgrService();
236     if (appMs == nullptr) {
237         result = OHOS::ERR_INVALID_VALUE;
238         HILOG_ERROR("failed to get app manager service");
239         return result;
240     }
241     std::this_thread::sleep_for(std::chrono::milliseconds(delay));
242     result = appMs->GetAllRunningProcesses(runningProcessInfo);
243     if (result == ERR_OK) {
244         HILOG_INFO("get running process info successfully.");
245     } else {
246         HILOG_INFO("failed to get running process info.");
247     }
248     return result;
249 }
250 
KillApplication(const std::string & appName,sptr<IAppMgr> & appMs,const time_t & delay)251 ErrCode STAbilityUtil::KillApplication(const std::string &appName, sptr<IAppMgr> &appMs, const time_t &delay)
252 {
253     ErrCode result = ERR_OK;
254     appMs = GetAppMgrService();
255     if (appMs == nullptr) {
256         result = OHOS::ERR_INVALID_VALUE;
257         HILOG_ERROR("failed to get app manager service");
258         return result;
259     }
260     result = appMs->GetAmsMgr()->KillApplication(appName);
261     std::this_thread::sleep_for(std::chrono::milliseconds(delay));
262     if (result == ERR_OK) {
263         HILOG_INFO("kill application:%{public}s successfully.", appName.c_str());
264     } else {
265         HILOG_INFO("failed to kill application:%{public}s.", appName.c_str());
266     }
267     return result;
268 }
269 
GetAppProcessInfoByName(const std::string & processName,sptr<IAppMgr> & appMs,const time_t & delay)270 RunningProcessInfo STAbilityUtil::GetAppProcessInfoByName(
271     const std::string &processName, sptr<IAppMgr> &appMs, const time_t &delay)
272 {
273     RunningProcessInfo appProcessInfo;
274     appProcessInfo.pid_ = 0;
275     std::vector<RunningProcessInfo> runningProcessInfo;
276     if (ERR_OK == GetRunningProcessInfo(runningProcessInfo, appMs, delay)) {
277         for (const auto &info : runningProcessInfo) {
278             if (processName == info.processName_) {
279                 appProcessInfo = info;
280             }
281         }
282     }
283     return appProcessInfo;
284 }
285 
WaitCompleted(Event & event,const std::string & eventName,const int code,const int timeout)286 int STAbilityUtil::WaitCompleted(Event &event, const std::string &eventName, const int code, const int timeout)
287 {
288     HILOG_INFO("WaitCompleted");
289     return event.WaitingMessage(std::to_string(code) + eventName, timeout, false);
290 }
291 
Completed(Event & event,const std::string & eventName,const int code)292 void STAbilityUtil::Completed(Event &event, const std::string &eventName, const int code)
293 {
294     HILOG_INFO("Completed");
295     return event.CompleteMessage(std::to_string(code) + eventName);
296 }
297 
Completed(Event & event,const std::string & eventName,const int code,const std::string & data)298 void STAbilityUtil::Completed(Event &event, const std::string &eventName, const int code, const std::string &data)
299 {
300     HILOG_INFO("STAbilityUtil::Completed");
301     return event.CompleteMessage(std::to_string(code) + eventName, data);
302 }
303 
GetData(Event & event,const std::string & eventName,const int code)304 std::string STAbilityUtil::GetData(Event &event, const std::string &eventName, const int code)
305 {
306     HILOG_INFO("STAbilityUtil::GetData");
307     return event.GetData(std::to_string(code) + eventName);
308 }
309 
CleanMsg(Event & event)310 void STAbilityUtil::CleanMsg(Event &event)
311 {
312     HILOG_INFO("CleanMsg");
313     return event.Clean();
314 }
315 
SerializationStOperatorToVector(StOperator & ParentOperator)316 std::vector<std::string> STAbilityUtil::SerializationStOperatorToVector(StOperator &ParentOperator)
317 {
318     std::vector<std::string> vectorOperator;
319     PushOperatorInVector(vectorOperator, ParentOperator);
320     return vectorOperator;
321 }
322 
DeserializationStOperatorFromVector(StOperator & ParentOperator,std::vector<std::string> & vectorOperator)323 void STAbilityUtil::DeserializationStOperatorFromVector(
324     StOperator &ParentOperator, std::vector<std::string> &vectorOperator)
325 {
326     PullOperatorFromVector(ParentOperator, vectorOperator);
327 }
328 
PushOperatorInVector(std::vector<std::string> & vectorOperator,StOperator & ParentOperator)329 void STAbilityUtil::PushOperatorInVector(std::vector<std::string> &vectorOperator, StOperator &ParentOperator)
330 {
331     vectorOperator.emplace_back(std::to_string(ParentOperator.GetChildOperator().size()));
332     vectorOperator.emplace_back(ParentOperator.GetAbilityType());
333     vectorOperator.emplace_back(ParentOperator.GetBundleName());
334     vectorOperator.emplace_back(ParentOperator.GetAbilityName());
335     vectorOperator.emplace_back(ParentOperator.GetOperatorName());
336     vectorOperator.emplace_back(ParentOperator.GetMessage());
337     for (auto child : ParentOperator.GetChildOperator()) {
338         PushOperatorInVector(vectorOperator, *child);
339     }
340 }
341 
PullOperatorFromVector(StOperator & ParentOperator,std::vector<std::string> & vectorOperator)342 void STAbilityUtil::PullOperatorFromVector(StOperator &ParentOperator, std::vector<std::string> &vectorOperator)
343 {
344     int childnum = std::stoi(vectorOperator.front());
345     vectorOperator.erase(vectorOperator.begin());
346     std::string abilityType = vectorOperator.front();
347     vectorOperator.erase(vectorOperator.begin());
348     std::string bundleName = vectorOperator.front();
349     vectorOperator.erase(vectorOperator.begin());
350     std::string abilityName = vectorOperator.front();
351     vectorOperator.erase(vectorOperator.begin());
352     std::string operatorName = vectorOperator.front();
353     vectorOperator.erase(vectorOperator.begin());
354     std::string message = vectorOperator.front();
355     vectorOperator.erase(vectorOperator.begin());
356     ParentOperator.SetAbilityType(abilityType)
357         .SetBundleName(bundleName)
358         .SetAbilityName(abilityName)
359         .SetOperatorName(operatorName)
360         .SetMessage(message);
361     for (int i = 0; i < childnum; i++) {
362         auto child = std::make_shared<StOperator>();
363         if (child == nullptr) {
364             return;
365         }
366         ParentOperator.AddChildOperator(child);
367         PullOperatorFromVector(*(child.get()), vectorOperator);
368     }
369 }
370 
InstallToolStatusReceiver()371 InstallToolStatusReceiver::InstallToolStatusReceiver()
372 {
373     std::cout << "create status receiver instance" << std::endl;
374 }
375 
~InstallToolStatusReceiver()376 InstallToolStatusReceiver::~InstallToolStatusReceiver()
377 {
378     std::cout << "destroy status receiver instance" << std::endl;
379 }
380 
OnStatusNotify(const int progress)381 void InstallToolStatusReceiver::OnStatusNotify(const int progress)
382 {
383     iProgress_ = progress;
384     std::cout << "destroy status receiver instance" << progress << std::endl;
385 }
386 
OnFinished(const int32_t resultCode,const std::string & resultMsg)387 void InstallToolStatusReceiver::OnFinished(const int32_t resultCode, const std::string &resultMsg)
388 {
389     std::cout << "on finished result is " << resultCode << " " << resultMsg << std::endl;
390     TestCompleted(event_, resultMsg, resultCode);
391 }
392 
TestWaitCompleted(Event & event,const std::string eventName,const int code,const int timeout)393 int InstallToolStatusReceiver::TestWaitCompleted(
394     Event &event, const std::string eventName, const int code, const int timeout)
395 {
396     std::cout << "TestWaitCompleted " << eventName << std::endl;
397     return STAbilityUtil::WaitCompleted(event, eventName, code, timeout);
398 }
TestCompleted(Event & event,const std::string & eventName,const int code)399 void InstallToolStatusReceiver::TestCompleted(Event &event, const std::string &eventName, const int code)
400 {
401     std::cout << "TestCompleted " << eventName << std::endl;
402     STAbilityUtil::Completed(event, eventName, code);
403     return;
404 }
405 
406 const std::string MSG_SUCCESS = "[SUCCESS]";
Install(const std::string & bundleFilePath,const InstallFlag installFlag)407 void STAbilityUtil::Install(const std::string &bundleFilePath, const InstallFlag installFlag)
408 {
409     std::string bundlePath = "/system/vendor/" + bundleFilePath + ".hap";
410     std::string installMsg = "";
411     sptr<IBundleInstaller> installerProxy = GetInstallerProxy();
412     if (!installerProxy) {
413         std::cout << "get bundle installer Failure." << std::endl;
414         installMsg = "Failure";
415         return;
416     }
417 
418     InstallParam installParam;
419     installParam.installFlag = installFlag;
420     sptr<InstallToolStatusReceiver> statusReceiver(new (std::nothrow) InstallToolStatusReceiver());
421     if (statusReceiver == nullptr) {
422         std::cout << "get statusReceiver Failure." << std::endl;
423         return;
424     }
425     bool installResult = installerProxy->Install(bundlePath, installParam, statusReceiver);
426     if (!installResult) {
427         installMsg = "Failure";
428         return;
429     }
430     if (InstallToolStatusReceiver::TestWaitCompleted(statusReceiver->event_, MSG_SUCCESS, 0) == 0) {
431         installMsg = "Success";
432     } else {
433         installMsg = "Failure";
434     }
435 }
436 
Uninstall(const std::string & bundleName)437 void STAbilityUtil::Uninstall(const std::string &bundleName)
438 {
439     std::string uninstallMsg = "";
440     sptr<IBundleInstaller> installerProxy = GetInstallerProxy();
441     if (!installerProxy) {
442         std::cout << "get bundle installer Failure." << std::endl;
443         uninstallMsg = "Failure";
444         return;
445     }
446     if (bundleName.empty()) {
447         std::cout << "bundelname is null." << std::endl;
448         uninstallMsg = "Failure";
449     } else {
450         sptr<InstallToolStatusReceiver> statusReceiver(new (std::nothrow) InstallToolStatusReceiver());
451         if (statusReceiver == nullptr) {
452             std::cout << "get statusReceiver Failure." << std::endl;
453             uninstallMsg = "Failure";
454             return;
455         }
456         InstallParam installParam;
457         bool uninstallResult = installerProxy->Uninstall(bundleName, installParam, statusReceiver);
458         if (!uninstallResult) {
459             std::cout << "Uninstall Failure." << std::endl;
460             uninstallMsg = "Failure";
461             return;
462         }
463         if (InstallToolStatusReceiver::TestWaitCompleted(statusReceiver->event_, MSG_SUCCESS, 0) == 0) {
464             uninstallMsg = "Success";
465         } else {
466             uninstallMsg = "Failure";
467         }
468     }
469 }
470 
GetInstallerProxy()471 sptr<IBundleInstaller> STAbilityUtil::GetInstallerProxy()
472 {
473     sptr<IBundleMgr> bundleMgrProxy = GetBundleMgrProxy();
474     if (!bundleMgrProxy) {
475         std::cout << "bundle mgr proxy is nullptr." << std::endl;
476         return nullptr;
477     }
478 
479     sptr<IBundleInstaller> installerProxy = bundleMgrProxy->GetBundleInstaller();
480     if (!installerProxy) {
481         std::cout << "fail to get bundle installer proxy" << std::endl;
482         return nullptr;
483     }
484     std::cout << "get bundle installer proxy success." << std::endl;
485     return installerProxy;
486 }
487 
GetBundleMgrProxy()488 sptr<IBundleMgr> STAbilityUtil::GetBundleMgrProxy()
489 {
490     sptr<ISystemAbilityManager> systemAbilityManager =
491         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
492     if (!systemAbilityManager) {
493         std::cout << "fail to get system ability mgr." << std::endl;
494         return nullptr;
495     }
496 
497     sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
498     if (!remoteObject) {
499         std::cout << "fail to get bundle manager proxy." << std::endl;
500         return nullptr;
501     }
502 
503     std::cout << "get bundle manager proxy success." << std::endl;
504     return iface_cast<IBundleMgr>(remoteObject);
505 }
506 }  // namespace STABUtil
507 }  // namespace OHOS