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