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 
16 #include "system_ability_helper.h"
17 
18 #ifdef ABILITY_RUNTIME_ENABLE
19 #include "ability_manager_interface.h"
20 #endif
21 
22 #include "app_log_wrapper.h"
23 #include "ipc_skeleton.h"
24 #include "iservice_registry.h"
25 #include "system_ability_definition.h"
26 
27 namespace OHOS {
28 namespace AppExecFwk {
29 namespace {
30 const std::string KILL_REASON = "Kill Reason: UpgradeApp";
31 }
GetSystemAbility(const int32_t systemAbilityId)32 sptr<IRemoteObject> SystemAbilityHelper::GetSystemAbility(const int32_t systemAbilityId)
33 {
34     sptr<ISystemAbilityManager> systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
35     if (systemAbilityMgr == nullptr) {
36         APP_LOGE("fail get system ability manager to get %{public}d proxy", systemAbilityId);
37         return nullptr;
38     }
39     return systemAbilityMgr->GetSystemAbility(systemAbilityId);
40 }
41 
AddSystemAbility(const int32_t systemAbilityId,const sptr<IRemoteObject> & systemAbility)42 bool SystemAbilityHelper::AddSystemAbility(const int32_t systemAbilityId, const sptr<IRemoteObject> &systemAbility)
43 {
44     sptr<ISystemAbilityManager> systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
45     if (systemAbilityMgr && (systemAbilityMgr->AddSystemAbility(systemAbilityId, systemAbility) == 0)) {
46         return true;
47     }
48     APP_LOGE("fail register %{public}d to system ability manager", systemAbilityId);
49     return false;
50 }
51 
RemoveSystemAbility(const int32_t systemAbilityId)52 bool SystemAbilityHelper::RemoveSystemAbility(const int32_t systemAbilityId)
53 {
54     sptr<ISystemAbilityManager> systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
55     if (systemAbilityMgr && (systemAbilityMgr->RemoveSystemAbility(systemAbilityId) == 0)) {
56         return true;
57     }
58     APP_LOGE("fail remove %{public}d from system ability manager", systemAbilityId);
59     return false;
60 }
61 
UninstallApp(const std::string & bundleName,int32_t uid,int32_t appIndex)62 int SystemAbilityHelper::UninstallApp(const std::string &bundleName, int32_t uid, int32_t appIndex)
63 {
64 #ifdef ABILITY_RUNTIME_ENABLE
65     sptr<AAFwk::IAbilityManager> abilityMgrProxy =
66         iface_cast<AAFwk::IAbilityManager>(SystemAbilityHelper::GetSystemAbility(ABILITY_MGR_SERVICE_ID));
67     if (abilityMgrProxy == nullptr) {
68         APP_LOGE("fail to find the app mgr service to kill application");
69         return -1;
70     }
71     std::string identity = IPCSkeleton::ResetCallingIdentity();
72     auto ret = abilityMgrProxy->UninstallApp(bundleName, uid, appIndex);
73     IPCSkeleton::SetCallingIdentity(identity);
74     return ret;
75 #else
76     return 0;
77 #endif
78 }
79 
UpgradeApp(const std::string & bundleName,int32_t uid,int32_t appIndex)80 int SystemAbilityHelper::UpgradeApp(const std::string &bundleName, int32_t uid, int32_t appIndex)
81 {
82 #ifdef ABILITY_RUNTIME_ENABLE
83     sptr<AAFwk::IAbilityManager> abilityMgrProxy =
84         iface_cast<AAFwk::IAbilityManager>(SystemAbilityHelper::GetSystemAbility(ABILITY_MGR_SERVICE_ID));
85     if (abilityMgrProxy == nullptr) {
86         APP_LOGE("fail to find the app mgr service to kill application");
87         return -1;
88     }
89     std::string identity = IPCSkeleton::ResetCallingIdentity();
90     auto ret = abilityMgrProxy->UpgradeApp(bundleName, uid, KILL_REASON, appIndex);
91     IPCSkeleton::SetCallingIdentity(identity);
92     return ret;
93 #else
94     return 0;
95 #endif
96 }
97 
UnloadSystemAbility(const int32_t systemAbilityId)98 bool SystemAbilityHelper::UnloadSystemAbility(const int32_t systemAbilityId)
99 {
100     sptr<ISystemAbilityManager> systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
101     if (systemAbilityMgr != nullptr && (systemAbilityMgr->UnloadSystemAbility(systemAbilityId) == 0)) {
102         return true;
103     }
104     APP_LOGE("fail unload %{public}d from system ability manager", systemAbilityId);
105     return false;
106 }
107 }  // namespace AppExecFwk
108 }  // namespace OHOS