• 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 
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 "if_system_ability_manager.h"
24 #include "ipc_skeleton.h"
25 #include "iservice_registry.h"
26 #include "string_ex.h"
27 #include "system_ability_definition.h"
28 
29 namespace OHOS {
30 namespace AppExecFwk {
GetSystemAbility(const int32_t systemAbilityId)31 sptr<IRemoteObject> SystemAbilityHelper::GetSystemAbility(const int32_t systemAbilityId)
32 {
33     sptr<ISystemAbilityManager> systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
34     if (systemAbilityMgr == nullptr) {
35         APP_LOGE("fail to get the system ability manager to get %{public}d proxy", systemAbilityId);
36         return nullptr;
37     }
38     return systemAbilityMgr->GetSystemAbility(systemAbilityId);
39 }
40 
AddSystemAbility(const int32_t systemAbilityId,const sptr<IRemoteObject> & systemAbility)41 bool SystemAbilityHelper::AddSystemAbility(const int32_t systemAbilityId, const sptr<IRemoteObject> &systemAbility)
42 {
43     sptr<ISystemAbilityManager> systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
44     if (systemAbilityMgr && (systemAbilityMgr->AddSystemAbility(systemAbilityId, systemAbility) == 0)) {
45         return true;
46     }
47     APP_LOGE("fail to register %{public}d to system ability manager", systemAbilityId);
48     return false;
49 }
50 
RemoveSystemAbility(const int32_t systemAbilityId)51 bool SystemAbilityHelper::RemoveSystemAbility(const int32_t systemAbilityId)
52 {
53     sptr<ISystemAbilityManager> systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
54     if (systemAbilityMgr && (systemAbilityMgr->RemoveSystemAbility(systemAbilityId) == 0)) {
55         return true;
56     }
57     APP_LOGE("fail to remove %{public}d from system ability manager", systemAbilityId);
58     return false;
59 }
60 
UninstallApp(const std::string & bundleName,int32_t uid)61 int SystemAbilityHelper::UninstallApp(const std::string &bundleName, int32_t uid)
62 {
63 #ifdef ABILITY_RUNTIME_ENABLE
64     sptr<AAFwk::IAbilityManager> abilityMgrProxy =
65         iface_cast<AAFwk::IAbilityManager>(SystemAbilityHelper::GetSystemAbility(ABILITY_MGR_SERVICE_ID));
66     if (abilityMgrProxy == nullptr) {
67         APP_LOGE("fail to find the app mgr service to kill application");
68         return -1;
69     }
70     std::string identity = IPCSkeleton::ResetCallingIdentity();
71     auto ret = abilityMgrProxy->UninstallApp(bundleName, uid);
72     IPCSkeleton::SetCallingIdentity(identity);
73     return ret;
74 #else
75     return 0;
76 #endif
77 }
78 }  // namespace AppExecFwk
79 }  // namespace OHOS