• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 "form_ams_helper.h"
17 
18 #include "ability_manager_interface.h"
19 #include "form_mgr_errors.h"
20 #include "hilog_wrapper.h"
21 #include "if_system_ability_manager.h"
22 #include "in_process_call_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 const int FORM_DISCONNECT_DELAY_TIME = 5000; // ms
FormAmsHelper()30 FormAmsHelper::FormAmsHelper()
31 {}
32 
~FormAmsHelper()33 FormAmsHelper::~FormAmsHelper()
34 {}
35 
36 /**
37  * @brief acquire a form ability manager, if it not existed,
38  * @return returns the ability manager ipc object, or nullptr for failed.
39  */
GetAbilityManager()40 sptr<AAFwk::IAbilityManager> FormAmsHelper::GetAbilityManager()
41 {
42     if (abilityManager_ == nullptr) {
43         sptr<ISystemAbilityManager> systemManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
44         if (systemManager == nullptr) {
45             HILOG_ERROR("%{public}s:fail to get registry", __func__);
46             return nullptr;
47         }
48         sptr<IRemoteObject> remoteObject = systemManager->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
49         if (remoteObject == nullptr) {
50             HILOG_ERROR("%{public}s:fail to connect AbilityMgrService", __func__);
51             return nullptr;
52         }
53         HILOG_DEBUG("connect AbilityMgrService success");
54 
55         abilityManager_ = iface_cast<AAFwk::IAbilityManager>(remoteObject);
56     }
57 
58     return abilityManager_;
59 }
60 
61 /**
62  * @brief ConnectAbility, connect session with service ability.
63  * @param want Special want for service type's ability.
64  * @param connect Callback used to notify caller the result of connecting or disconnecting.
65  * @return Returns ERR_OK on success, others on failure.
66  */
ConnectServiceAbility(const Want & want,const sptr<AAFwk::IAbilityConnection> & connect)67 ErrCode FormAmsHelper::ConnectServiceAbility(
68     const Want &want, const sptr<AAFwk::IAbilityConnection> &connect)
69 {
70     HILOG_INFO("%{public}s called.", __func__);
71     sptr<AAFwk::IAbilityManager> ams = GetAbilityManager();
72     if (ams == nullptr) {
73         HILOG_ERROR("%{public}s:ability service not connect", __func__);
74         return ERR_APPEXECFWK_FORM_BIND_PROVIDER_FAILED;
75     }
76     return IN_PROCESS_CALL(ams->ConnectAbility(want, connect, nullptr));
77 }
78 /**
79  * @brief Disconnect ability, disconnect session with service ability.
80  * @param want Special want for service type's ability.
81  * @param connect Callback used to notify caller the result of connecting or disconnecting.
82  * @return Returns ERR_OK on success, others on failure.
83  */
DisconnectServiceAbility(const sptr<AAFwk::IAbilityConnection> & connect)84 ErrCode FormAmsHelper::DisconnectServiceAbility(const sptr<AAFwk::IAbilityConnection> &connect)
85 {
86     HILOG_INFO("%{public}s called.", __func__);
87     sptr<AAFwk::IAbilityManager> ams = GetAbilityManager();
88     if (ams == nullptr) {
89         HILOG_ERROR("%{public}s:ability service not connect", __func__);
90         return ERR_APPEXECFWK_FORM_BIND_PROVIDER_FAILED;
91     }
92     return IN_PROCESS_CALL(ams->DisconnectAbility(connect));
93 }
94 /**
95  * @brief Disconnect ability delay, disconnect session with service ability.
96  * @param want Special want for service type's ability.
97  * @param connect Callback used to notify caller the result of connecting or disconnecting.
98  * @return Returns ERR_OK on success, others on failure.
99  */
DisconnectServiceAbilityDelay(const sptr<AAFwk::IAbilityConnection> & connect)100 ErrCode FormAmsHelper::DisconnectServiceAbilityDelay(const sptr<AAFwk::IAbilityConnection> &connect)
101 {
102     if (eventHandler_ == nullptr) {
103         HILOG_ERROR("%{public}s fail, eventhandler invalidate", __func__);
104         return ERR_INVALID_OPERATION;
105     }
106     auto disConnectAbilityFunc = [connect]() {
107         FormAmsHelper::GetInstance().DisconnectAbilityTask(connect);
108     };
109     if (!eventHandler_->PostTask(disConnectAbilityFunc, FORM_DISCONNECT_DELAY_TIME)) {
110         HILOG_ERROR("%{public}s, failed to disconnect ability", __func__);
111         return ERR_APPEXECFWK_FORM_BIND_PROVIDER_FAILED;
112     }
113     return ERR_OK;
114 }
115 /**
116  * @brief Add the ability manager instance for debug.
117  * @param abilityManager the ability manager ipc object.
118  */
SetAbilityManager(const sptr<AAFwk::IAbilityManager> & abilityManager)119 void FormAmsHelper::SetAbilityManager(const sptr<AAFwk::IAbilityManager> &abilityManager)
120 {
121     abilityManager_ = abilityManager;
122 }
123 
124 /**
125  * @brief Disconnect ability task, disconnect session with service ability.
126  * @param want Special want for service type's ability.
127  * @param connect Callback used to notify caller the result of connecting or disconnecting.
128  */
DisconnectAbilityTask(const sptr<AAFwk::IAbilityConnection> & connect)129 void FormAmsHelper::DisconnectAbilityTask(const sptr<AAFwk::IAbilityConnection> &connect)
130 {
131     sptr<AAFwk::IAbilityManager> ams = GetAbilityManager();
132     if (ams == nullptr) {
133         HILOG_ERROR("%{public}s, ability service not connect", __func__);
134         return;
135     }
136     IN_PROCESS_CALL_WITHOUT_RET(ams->DisconnectAbility(connect));
137 }
138 
StartAbility(const Want & want,int32_t userId)139 ErrCode FormAmsHelper::StartAbility(const Want &want, int32_t userId)
140 {
141     sptr<AAFwk::IAbilityManager> ams = GetAbilityManager();
142     if (ams == nullptr) {
143         HILOG_ERROR("%{public}s, ability service not connect", __func__);
144         return ERR_APPEXECFWK_FORM_BIND_PROVIDER_FAILED;
145     }
146     return IN_PROCESS_CALL(ams->StartAbility(want, userId));
147 }
148 }  // namespace AppExecFwk
149 }  // namespace OHOS
150