1 /*
2 * Copyright (c) 2021-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 "form_ams_helper.h"
17
18 #include "ability_manager_interface.h"
19 #include "app_mgr_client.h"
20 #include "form_resource_observer.h"
21 #include "fms_log_wrapper.h"
22 #include "form_mgr_errors.h"
23 #include "form_serial_queue.h"
24 #include "if_system_ability_manager.h"
25 #include "in_process_call_wrapper.h"
26 #include "ipc_skeleton.h"
27 #include "iservice_registry.h"
28 #include "system_ability_definition.h"
29
30 namespace OHOS {
31 namespace AppExecFwk {
FormAmsHelper()32 FormAmsHelper::FormAmsHelper()
33 {}
34
~FormAmsHelper()35 FormAmsHelper::~FormAmsHelper()
36 {}
37
38 /**
39 * @brief acquire a form ability manager, if it not existed,
40 * @return returns the ability manager ipc object, or nullptr for failed.
41 */
GetAbilityManager()42 sptr<AAFwk::IAbilityManager> FormAmsHelper::GetAbilityManager()
43 {
44 if (abilityManager_ == nullptr) {
45 sptr<ISystemAbilityManager> systemManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
46 if (systemManager == nullptr) {
47 HILOG_ERROR("get registry failed");
48 return nullptr;
49 }
50 sptr<IRemoteObject> remoteObject = systemManager->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
51 if (remoteObject == nullptr) {
52 HILOG_ERROR("connect AbilityMgrService failed");
53 return nullptr;
54 }
55 HILOG_DEBUG("connect AbilityMgrService success");
56
57 abilityManager_ = iface_cast<AAFwk::IAbilityManager>(remoteObject);
58 }
59
60 return abilityManager_;
61 }
62
63 /**
64 * @brief ConnectAbility, connect session with service ability.
65 * @param want Special want for service type's ability.
66 * @param connect Callback used to notify caller the result of connecting or disconnecting.
67 * @return Returns ERR_OK on success, others on failure.
68 */
ConnectServiceAbility(const Want & want,const sptr<AAFwk::IAbilityConnection> & connect)69 ErrCode FormAmsHelper::ConnectServiceAbility(
70 const Want &want, const sptr<AAFwk::IAbilityConnection> &connect)
71 {
72 HILOG_DEBUG("connect service ability");
73 sptr<AAFwk::IAbilityManager> ams = GetAbilityManager();
74 if (ams == nullptr) {
75 HILOG_ERROR("ability service not connect");
76 return ERR_APPEXECFWK_FORM_BIND_PROVIDER_FAILED;
77 }
78 return IN_PROCESS_CALL(ams->ConnectAbility(want, connect, nullptr));
79 }
80
81 /**
82 * @brief ConnectAbility, connect session with service ability.
83 * @param want Special want for service type's ability.
84 * @param connect Callback used to notify caller the result of connecting or disconnecting.
85 * @param userId Designation User ID.
86 * @return Returns ERR_OK on success, others on failure.
87 */
ConnectServiceAbilityWithUserId(const Want & want,const sptr<AAFwk::IAbilityConnection> & connect,int32_t userId)88 ErrCode FormAmsHelper::ConnectServiceAbilityWithUserId(
89 const Want &want, const sptr<AAFwk::IAbilityConnection> &connect, int32_t userId)
90 {
91 HILOG_DEBUG("connect service ability with userId");
92 sptr<AAFwk::IAbilityManager> abilityManagerService = GetAbilityManager();
93 if (abilityManagerService == nullptr) {
94 HILOG_ERROR("ability service not connect");
95 return ERR_APPEXECFWK_FORM_BIND_PROVIDER_FAILED;
96 }
97 return IN_PROCESS_CALL(abilityManagerService->ConnectAbility(want, connect, nullptr, userId));
98 }
99
100 /**
101 * @brief Disconnect ability, disconnect session with service ability.
102 * @param connect Callback used to notify caller the result of connecting or disconnecting.
103 * @return Returns ERR_OK on success, others on failure.
104 */
DisconnectServiceAbility(const sptr<AAFwk::IAbilityConnection> & connect)105 ErrCode FormAmsHelper::DisconnectServiceAbility(const sptr<AAFwk::IAbilityConnection> &connect)
106 {
107 HILOG_INFO("call");
108 sptr<AAFwk::IAbilityManager> ams = GetAbilityManager();
109 if (ams == nullptr) {
110 HILOG_ERROR("ability service not connect");
111 return ERR_APPEXECFWK_FORM_BIND_PROVIDER_FAILED;
112 }
113 return IN_PROCESS_CALL(ams->DisconnectAbility(connect));
114 }
115 /**
116 * @brief Disconnect ability delay, disconnect session with service ability.
117 * @param connect Callback used to notify caller the result of connecting or disconnecting.
118 * @param delayTime Specifying time to delay, default is FORM_DISCONNECT_DELAY_TIME.
119 * @return Returns ERR_OK on success, others on failure.
120 */
DisconnectServiceAbilityDelay(const sptr<AAFwk::IAbilityConnection> & connect,int delayTime)121 ErrCode FormAmsHelper::DisconnectServiceAbilityDelay(const sptr<AAFwk::IAbilityConnection> &connect, int delayTime)
122 {
123 if (serialQueue_ == nullptr) {
124 HILOG_ERROR("null serialQueue_");
125 return ERR_INVALID_OPERATION;
126 }
127 auto disConnectAbilityFunc = [connect]() {
128 FormAmsHelper::GetInstance().DisconnectAbilityTask(connect);
129 };
130 if (!serialQueue_->ScheduleTask(delayTime, disConnectAbilityFunc)) {
131 HILOG_ERROR("fail disconnect ability");
132 return ERR_APPEXECFWK_FORM_BIND_PROVIDER_FAILED;
133 }
134 return ERR_OK;
135 }
136 /**
137 * @brief StopExtensionAbility with want, send want to ability manager service.
138 * @param want The want of the ability to start.
139 * @return Returns ERR_OK on success, others on failure.
140 */
StopExtensionAbility(const Want & want)141 ErrCode FormAmsHelper::StopExtensionAbility(const Want &want)
142 {
143 HILOG_DEBUG("call");
144 sptr<AAFwk::IAbilityManager> ams = GetAbilityManager();
145 if (ams == nullptr) {
146 HILOG_ERROR("StopExtensionAbility ability service not connect");
147 return ERR_APPEXECFWK_FORM_BIND_PROVIDER_FAILED;
148 }
149 return IN_PROCESS_CALL(ams->StopExtensionAbility(want, nullptr));
150 }
151 /**
152 * @brief Add the ability manager instance for debug.
153 * @param abilityManager the ability manager ipc object.
154 */
SetAbilityManager(const sptr<AAFwk::IAbilityManager> & abilityManager)155 void FormAmsHelper::SetAbilityManager(const sptr<AAFwk::IAbilityManager> &abilityManager)
156 {
157 abilityManager_ = abilityManager;
158 }
159
160 /**
161 * @brief Disconnect ability task, disconnect session with service ability.
162 * @param want Special want for service type's ability.
163 * @param connect Callback used to notify caller the result of connecting or disconnecting.
164 */
DisconnectAbilityTask(const sptr<AAFwk::IAbilityConnection> & connect)165 void FormAmsHelper::DisconnectAbilityTask(const sptr<AAFwk::IAbilityConnection> &connect)
166 {
167 sptr<AAFwk::IAbilityManager> ams = GetAbilityManager();
168 if (ams == nullptr) {
169 HILOG_ERROR("ability service not connect");
170 return;
171 }
172 IN_PROCESS_CALL_WITHOUT_RET(ams->DisconnectAbility(connect));
173 }
174
StartAbility(const Want & want,int32_t userId)175 ErrCode FormAmsHelper::StartAbility(const Want &want, int32_t userId)
176 {
177 sptr<AAFwk::IAbilityManager> ams = GetAbilityManager();
178 if (ams == nullptr) {
179 HILOG_ERROR("ability service not connect");
180 return ERR_APPEXECFWK_FORM_BIND_PROVIDER_FAILED;
181 }
182 return IN_PROCESS_CALL(ams->StartAbility(want, userId));
183 }
184
RegisterConfigurationObserver()185 void FormAmsHelper::RegisterConfigurationObserver()
186 {
187 HILOG_INFO("begin");
188 if (configurationObserver != nullptr) {
189 HILOG_WARN("configurationObserver not null");
190 return;
191 }
192 sptr<IConfigurationObserver> configurationObserver(new (std::nothrow) FormFwkResourceObserver());
193 if (configurationObserver == nullptr) {
194 HILOG_ERROR("create configurationObserver failed");
195 return;
196 }
197 auto appMgrClient = std::make_unique<AppMgrClient>();
198 if (appMgrClient == nullptr) {
199 HILOG_ERROR("create appMgrClient failed");
200 return;
201 }
202 appMgrClient->RegisterConfigurationObserver(configurationObserver);
203 HILOG_INFO("end");
204 }
205
UnRegisterConfigurationObserver()206 void FormAmsHelper::UnRegisterConfigurationObserver()
207 {
208 HILOG_INFO("begin");
209 if (configurationObserver == nullptr) {
210 HILOG_WARN("null configurationObserver");
211 return;
212 }
213 auto appMgrClient = std::make_unique<AppMgrClient>();
214 if (appMgrClient == nullptr) {
215 HILOG_ERROR("create appMgrClient failed");
216 return;
217 }
218 appMgrClient->UnregisterConfigurationObserver(configurationObserver);
219 HILOG_INFO("end");
220 }
221
222 } // namespace AppExecFwk
223 } // namespace OHOS
224