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