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