1
2 /*
3 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "form_ability_connection.h"
18
19 #include <cinttypes>
20
21 #include "fms_log_wrapper.h"
22 #include "form_ams_helper.h"
23 #include "form_bms_helper.h"
24 #include "form_data_mgr.h"
25 #include "form_db_cache.h"
26 #include "form_event_report.h"
27 #include "form_info.h"
28 #include "form_info_mgr.h"
29 #include "form_supply_callback.h"
30 #include "form_timer_mgr.h"
31 #include "form_util.h"
32 #include "iremote_proxy.h"
33 #include "iservice_registry.h"
34 #include "system_ability_definition.h"
35 #include "want.h"
36 #include "form_report.h"
37
38 namespace OHOS {
39 namespace AppExecFwk {
40 /**
41 * @brief OnAbilityConnectDone, AbilityMs notify caller ability the result of connect.
42 * @param element service ability's ElementName.
43 * @param remoteObject the session proxy of service ability.
44 * @param resultCode ERR_OK on success, others on failure.
45 */
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int resultCode)46 void FormAbilityConnection::OnAbilityConnectDone(
47 const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject, int resultCode)
48 {
49 if (resultCode != ERR_OK) {
50 HILOG_ERROR("formId:%{public}" PRId64 ", resultCode:%{public}d",
51 formId_, resultCode);
52 return;
53 }
54
55 HILOG_INFO("free install is %{public}d", isFreeInstall_);
56 if (!isFreeInstall_) {
57 return;
58 }
59
60 std::vector<FormInfo> targetForms;
61 if (FormInfoMgr::GetInstance().GetFormsInfoByBundle(bundleName_, targetForms) != ERR_OK) {
62 HILOG_ERROR("fail get forms info for %{public}s", bundleName_.c_str());
63 return;
64 }
65
66 FormRecord formRecord;
67 if (!FormDataMgr::GetInstance().GetFormRecord(formId_, formRecord)) {
68 HILOG_ERROR("not exist such form:%{public}" PRId64 ".", formId_);
69 return;
70 }
71
72 FormDataMgr::GetInstance().SetRecordNeedFreeInstall(formId_, false);
73 FormInfo updatedForm;
74 if (FormDataMgr::GetInstance().GetUpdatedForm(formRecord, targetForms, updatedForm)) {
75 HILOG_INFO("refresh form");
76 FormDataMgr::GetInstance().SetNeedRefresh(formId_, true);
77 FormBmsHelper::GetInstance().NotifyModuleNotRemovable(formRecord.bundleName, formRecord.moduleName);
78 return;
79 }
80
81 // delete form
82 if (formRecord.formTempFlag) {
83 FormDataMgr::GetInstance().DeleteTempForm(formId_);
84 } else {
85 FormDbCache::GetInstance().DeleteFormInfo(formId_);
86 }
87 FormDataMgr::GetInstance().DeleteFormRecord(formId_);
88 const std::vector<int64_t> removedForms {formId_};
89 FormDataMgr::GetInstance().CleanHostRemovedForms(removedForms);
90 FormTimerMgr::GetInstance().RemoveFormTimer(formId_);
91 }
92
93 /**
94 * @brief OnAbilityDisconnectDone, AbilityMs notify caller ability the result of disconnect.
95 * @param element service ability's ElementName.
96 * @param resultCode ERR_OK on success, others on failure.
97 */
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int resultCode)98 void FormAbilityConnection::OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode)
99 {
100 HILOG_INFO("element:%{public}s, resultCode:%{public}d", element.GetURI().c_str(), resultCode);
101 if (connectId_ != 0) {
102 FormSupplyCallback::GetInstance()->RemoveConnection(connectId_);
103 connectId_ = 0;
104 } else {
105 HILOG_ERROR("invalid connectId_:%{public}d", connectId_);
106 }
107 ReportFormAppUnbindEvent();
108 }
109
110 /**
111 * @brief Remote object died event.
112 * @param remoteObject the remote object of service ability.
113 */
OnConnectDied(const wptr<IRemoteObject> & remoteObject)114 void FormAbilityConnection::OnConnectDied(const wptr<IRemoteObject> &remoteObject)
115 {
116 if (connectId_ != 0) {
117 FormSupplyCallback::GetInstance()->RemoveConnection(connectId_);
118 connectId_ = 0;
119 } else {
120 HILOG_ERROR("connectId_ invalidate. connectId_:%{public}d", connectId_);
121 }
122 }
123
GetAppMgr()124 sptr<OHOS::AppExecFwk::IAppMgr> FormAbilityConnection::GetAppMgr()
125 {
126 sptr<ISystemAbilityManager> systemMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
127 if (systemMgr == nullptr) {
128 HILOG_ERROR("connect systemAbilityManager failed");
129 return nullptr;
130 }
131 sptr<IRemoteObject> remoteObject = systemMgr->GetSystemAbility(APP_MGR_SERVICE_ID);
132 if (remoteObject == nullptr) {
133 HILOG_ERROR("connect appMgrService failed");
134 return nullptr;
135 }
136 return iface_cast<OHOS::AppExecFwk::IAppMgr>(remoteObject);
137 }
138
onFormAppConnect()139 bool FormAbilityConnection::onFormAppConnect()
140 {
141 std::string bundleName = GetBundleName();
142 if (bundleName.empty()) {
143 HILOG_ERROR("Empty bundle name");
144 return false;
145 }
146 auto appMgr = GetAppMgr();
147 if (!appMgr) {
148 HILOG_ERROR("Get app mgr failed");
149 return false;
150 }
151
152 std::vector<AppExecFwk::RunningProcessInfo> infos;
153 int32_t userId = FormUtil::GetCurrentAccountId();
154 int32_t ret = appMgr->GetRunningProcessInformation(bundleName, userId, infos);
155 if (ret != ERR_OK) {
156 HILOG_ERROR("Get running process info failed");
157 return false;
158 }
159 std::string targetProcessName = bundleName + ":form";
160 for (std::vector<AppExecFwk::RunningProcessInfo>::iterator iter = infos.begin(); iter != infos.end(); iter++) {
161 if ((*iter).processName_ != targetProcessName) {
162 continue;
163 }
164 appFormPid_ = (*iter).pid_;
165 return true;
166 }
167 return false;
168 }
169
ReportFormAppUnbindEvent()170 void FormAbilityConnection::ReportFormAppUnbindEvent()
171 {
172 FormEventInfo eventInfo;
173 eventInfo.timeStamp = FormUtil::GetNowMillisecond();
174 eventInfo.formId = GetFormId();
175 eventInfo.bundleName = GetBundleName() + ":form";
176 eventInfo.formAppPid = GetAppFormPid();
177 HILOG_DEBUG("bundleName:%{public}s, formId:%{public}" PRId64 ",pid:%{public}" PRId32 ",timstamp:%{public}" PRId64,
178 eventInfo.bundleName.c_str(), eventInfo.formId, eventInfo.formAppPid, eventInfo.timeStamp);
179 FormReport::GetInstance().RemoveFormId(eventInfo.formId);
180 FormEventReport::SendThirdFormEvent(FormEventName::UNBIND_FORM_APP, HiSysEventType::BEHAVIOR, eventInfo);
181 }
182
183 /**
184 * @brief Set connectId.
185 * @param connectId The ability connection id.
186 */
SetConnectId(int32_t connectId)187 void FormAbilityConnection::SetConnectId(int32_t connectId)
188 {
189 HILOG_INFO("connectId_:%{public}d", connectId);
190 connectId_ = connectId;
191 }
192
193 /**
194 * @brief Get connectId.
195 * @return The ability connection id.
196 */
GetConnectId() const197 int32_t FormAbilityConnection::GetConnectId() const
198 {
199 return connectId_;
200 }
201
202 /**
203 * @brief Get the provider Key
204 *
205 * @return The provider Key
206 */
GetProviderKey() const207 std::string FormAbilityConnection::GetProviderKey() const
208 {
209 if (bundleName_.empty() || abilityName_.empty()) {
210 return "";
211 }
212 return bundleName_ + "::" + abilityName_;
213 }
214
215 /**
216 * @brief Set the Provider Key
217 *
218 * @param bundleName bundleName
219 * @param abilityName abilityName
220 */
SetProviderKey(const std::string & bundleName,const std::string & abilityName)221 void FormAbilityConnection::SetProviderKey(const std::string &bundleName, const std::string &abilityName)
222 {
223 bundleName_ = bundleName;
224 abilityName_ = abilityName;
225 }
226
SetFreeInstall(bool isFreeInstall)227 void FormAbilityConnection::SetFreeInstall(bool isFreeInstall)
228 {
229 isFreeInstall_ = isFreeInstall;
230 }
231
SetFormId(int64_t formId)232 void FormAbilityConnection::SetFormId(int64_t formId)
233 {
234 formId_ = formId;
235 }
236
GetFormId() const237 int64_t FormAbilityConnection::GetFormId() const
238 {
239 return formId_;
240 }
241
SetHostToken(const sptr<IRemoteObject> hostToken)242 void FormAbilityConnection::SetHostToken(const sptr<IRemoteObject> hostToken)
243 {
244 hostToken_ = hostToken;
245 }
246
GetHostToken() const247 sptr<IRemoteObject> FormAbilityConnection::GetHostToken() const
248 {
249 return hostToken_;
250 }
251
SetProviderToken(const sptr<IRemoteObject> providerToken)252 void FormAbilityConnection::SetProviderToken(const sptr<IRemoteObject> providerToken)
253 {
254 providerToken_ = providerToken;
255 }
256
GetProviderToken() const257 sptr<IRemoteObject> FormAbilityConnection::GetProviderToken() const
258 {
259 return providerToken_;
260 }
261
GetBundleName()262 std::string FormAbilityConnection::GetBundleName()
263 {
264 return bundleName_;
265 }
266
GetAppFormPid()267 int32_t FormAbilityConnection::GetAppFormPid()
268 {
269 return appFormPid_;
270 }
271
272 } // namespace AppExecFwk
273 } // namespace OHOS