1 /*
2  * Copyright (c) 2022-2023 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 "ability_manager_helper.h"
17 
18 #include "bundle_mgr_service.h"
19 #include "system_ability_helper.h"
20 
21 #ifdef BUNDLE_FRAMEWORK_FREE_INSTALL
22 #include "ability_manager_client.h"
23 #include "app_mgr_interface.h"
24 #include "running_process_info.h"
25 #endif
26 
27 namespace OHOS {
28 namespace AppExecFwk {
UninstallApplicationProcesses(const std::string & bundleName,const int uid,bool isUpgradeApp,int32_t appIndex)29 bool AbilityManagerHelper::UninstallApplicationProcesses(
30     const std::string &bundleName, const int uid, bool isUpgradeApp, int32_t appIndex)
31 {
32 #ifdef ABILITY_RUNTIME_ENABLE
33     APP_LOGI("uninstall kill running processes, app name is %{public}s, isUpgradeApp : %{public}d",
34         bundleName.c_str(), isUpgradeApp);
35     int ret = 0;
36     if (isUpgradeApp) {
37         ret = SystemAbilityHelper::UpgradeApp(bundleName, uid, appIndex);
38     } else {
39         ret = SystemAbilityHelper::UninstallApp(bundleName, uid, appIndex);
40     }
41     if (ret != 0) {
42         APP_LOGE("kill application process failed uid: %{public}d, appIndex: %{public}d", uid, appIndex);
43         return false;
44     }
45     return true;
46 #else
47     APP_LOGI("ABILITY_RUNTIME_ENABLE is false");
48     return true;
49 #endif
50 }
51 
IsRunning(const std::string & bundleName,const int bundleUid)52 int32_t AbilityManagerHelper::IsRunning(const std::string &bundleName, const int bundleUid)
53 {
54 #ifdef BUNDLE_FRAMEWORK_FREE_INSTALL
55     APP_LOGI("check app is running, app name is %{public}s", bundleName.c_str());
56     if (bundleUid < 0) {
57         APP_LOGE("bundleUid is error");
58         return FAILED;
59     }
60     sptr<IAppMgr> appMgrProxy = iface_cast<IAppMgr>(SystemAbilityHelper::GetSystemAbility(APP_MGR_SERVICE_ID));
61     if (appMgrProxy == nullptr) {
62         APP_LOGE("fail to find the app mgr service to check app is running");
63         return FAILED;
64     }
65     std::vector<RunningProcessInfo> runningList;
66     int result = appMgrProxy->GetAllRunningProcesses(runningList);
67     if (result != ERR_OK) {
68         APP_LOGE("GetAllRunningProcesses failed");
69         return FAILED;
70     }
71     for (const RunningProcessInfo &info : runningList) {
72         if (info.uid_ == bundleUid) {
73             auto res = std::any_of(info.bundleNames.begin(), info.bundleNames.end(),
74                 [bundleName](const auto &bundleNameInRunningProcessInfo) {
75                     return bundleNameInRunningProcessInfo == bundleName;
76                 });
77             if (res) {
78                 return RUNNING;
79             }
80         }
81     }
82     APP_LOGI("nothing app running");
83     return NOT_RUNNING;
84 #else
85     APP_LOGI("BUNDLE_FRAMEWORK_FREE_INSTALL is false");
86     return FAILED;
87 #endif
88 }
89 
IsRunning(const std::string & bundleName)90 int32_t AbilityManagerHelper::IsRunning(const std::string &bundleName)
91 {
92 #ifdef BUNDLE_FRAMEWORK_FREE_INSTALL
93     APP_LOGD("check app is running, app name is %{public}s", bundleName.c_str());
94     sptr<IAppMgr> appMgrProxy =
95         iface_cast<IAppMgr>(SystemAbilityHelper::GetSystemAbility(APP_MGR_SERVICE_ID));
96     if (appMgrProxy == nullptr) {
97         APP_LOGE("fail to find the app mgr service to check app is running");
98         return FAILED;
99     }
100 
101     std::vector<RunningProcessInfo> runningList;
102     int result = appMgrProxy->GetAllRunningProcesses(runningList);
103     if (result != ERR_OK) {
104         APP_LOGE("GetAllRunningProcesses failed");
105         return FAILED;
106     }
107 
108     for (const auto &info : runningList) {
109         auto res = std::any_of(info.bundleNames.begin(), info.bundleNames.end(),
110             [bundleName](const auto &bundleNameInRunningProcessInfo) {
111                 return bundleNameInRunningProcessInfo == bundleName;
112             });
113         if (res) {
114             return RUNNING;
115         }
116     }
117 
118     return NOT_RUNNING;
119 #else
120     APP_LOGI("BUNDLE_FRAMEWORK_FREE_INSTALL is false");
121     return FAILED;
122 #endif
123 }
124 }  // namespace AppExecFwk
125 }  // namespace OHOS
126