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