• 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 "bundle_mgr_service.h"
19 #include "system_ability_helper.h"
20 
21 #include "app_log_tag_wrapper.h"
22 #ifdef BUNDLE_FRAMEWORK_FREE_INSTALL
23 #include "ability_manager_client.h"
24 #include "app_mgr_interface.h"
25 #include "running_process_info.h"
26 #endif
27 
28 namespace OHOS {
29 namespace AppExecFwk {
UninstallApplicationProcesses(const std::string & bundleName,const int uid,bool isUpgradeApp,int32_t appIndex)30 bool AbilityManagerHelper::UninstallApplicationProcesses(
31     const std::string &bundleName, const int uid, bool isUpgradeApp, int32_t appIndex)
32 {
33 #ifdef ABILITY_RUNTIME_ENABLE
34     LOG_NOFUNC_I(BMS_TAG_INSTALLER, "begin UninstallApplicationProcesses -n %{public}s, -up %{public}d, -u %{public}d,"
35         "-i %{public}d", bundleName.c_str(), isUpgradeApp, uid, appIndex);
36     int ret = 0;
37     if (isUpgradeApp) {
38         ret = SystemAbilityHelper::UpgradeApp(bundleName, uid, appIndex);
39     } else {
40         ret = SystemAbilityHelper::UninstallApp(bundleName, uid, appIndex);
41     }
42     if (ret != 0) {
43         APP_LOGE("kill application process failed uid: %{public}d, appIndex: %{public}d", uid, appIndex);
44         return false;
45     }
46     LOG_NOFUNC_I(BMS_TAG_INSTALLER, "UninstallApplicationProcesses end");
47     return true;
48 #else
49     LOG_NOFUNC_I(BMS_TAG_INSTALLER, "ABILITY_RUNTIME_ENABLE is false");
50     return true;
51 #endif
52 }
53 
IsRunning(const std::string & bundleName)54 int32_t AbilityManagerHelper::IsRunning(const std::string &bundleName)
55 {
56 #ifdef BUNDLE_FRAMEWORK_FREE_INSTALL
57     LOG_NOFUNC_I(BMS_TAG_DEFAULT, "IsRunning check %{public}s", bundleName.c_str());
58     sptr<IAppMgr> appMgrProxy =
59         iface_cast<IAppMgr>(SystemAbilityHelper::GetSystemAbility(APP_MGR_SERVICE_ID));
60     if (appMgrProxy == nullptr) {
61         APP_LOGE("fail to find the app mgr service to check app is running");
62         return FAILED;
63     }
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     LOG_NOFUNC_I(BMS_TAG_DEFAULT, "size %{public}zu", runningList.size());
72 
73     for (const auto &info : runningList) {
74         auto res = std::any_of(info.bundleNames.begin(), info.bundleNames.end(),
75             [bundleName](const auto &bundleNameInRunningProcessInfo) {
76                 return bundleNameInRunningProcessInfo == bundleName;
77             });
78         if (res) {
79             return RUNNING;
80         }
81     }
82 
83     return NOT_RUNNING;
84 #else
85     APP_LOGI("BUNDLE_FRAMEWORK_FREE_INSTALL is false");
86     return FAILED;
87 #endif
88 }
89 }  // namespace AppExecFwk
90 }  // namespace OHOS
91