• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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", formId_, resultCode);
51         return;
52     }
53 
54     HILOG_INFO("free install is %{public}d", isFreeInstall_);
55     if (!isFreeInstall_) {
56         return;
57     }
58 
59     std::vector<FormInfo> targetForms;
60     if (FormInfoMgr::GetInstance().GetFormsInfoByBundle(bundleName_, targetForms) != ERR_OK) {
61         HILOG_ERROR("fail get forms info for %{public}s", bundleName_.c_str());
62         return;
63     }
64 
65     FormRecord formRecord;
66     if (!FormDataMgr::GetInstance().GetFormRecord(formId_, formRecord)) {
67         HILOG_ERROR("not exist such form:%{public}" PRId64 ".", formId_);
68         return;
69     }
70 
71     FormDataMgr::GetInstance().SetRecordNeedFreeInstall(formId_, false);
72     FormInfo updatedForm;
73     if (FormDataMgr::GetInstance().GetUpdatedForm(formRecord, targetForms, updatedForm)) {
74         HILOG_INFO("refresh form");
75         FormDataMgr::GetInstance().SetNeedRefresh(formId_, true);
76         FormBmsHelper::GetInstance().NotifyModuleNotRemovable(formRecord.bundleName, formRecord.moduleName);
77         return;
78     }
79 
80     // delete form
81     if (formRecord.formTempFlag) {
82         FormDataMgr::GetInstance().DeleteTempForm(formId_);
83     } else {
84         FormDbCache::GetInstance().DeleteFormInfo(formId_);
85     }
86     FormDataMgr::GetInstance().DeleteFormRecord(formId_);
87     const std::vector<int64_t> removedForms {formId_};
88     FormDataMgr::GetInstance().CleanHostRemovedForms(removedForms);
89     FormTimerMgr::GetInstance().RemoveFormTimer(formId_);
90 }
91 
92 /**
93  * @brief OnAbilityDisconnectDone, AbilityMs notify caller ability the result of disconnect.
94  * @param element service ability's ElementName.
95  * @param resultCode ERR_OK on success, others on failure.
96  */
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int resultCode)97 void FormAbilityConnection::OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode)
98 {
99     HILOG_INFO("element:%{public}s, resultCode:%{public}d", element.GetURI().c_str(), resultCode);
100     if (connectId_ != 0) {
101         FormSupplyCallback::GetInstance()->RemoveConnection(connectId_);
102         connectId_ = 0;
103     } else {
104         HILOG_ERROR("invalid connectId_:%{public}d", connectId_);
105     }
106     ReportFormAppUnbindEvent();
107 }
108 
109 /**
110  * @brief Remote object died event.
111  * @param remoteObject the remote object of service ability.
112  */
OnConnectDied(const wptr<IRemoteObject> & remoteObject)113 void FormAbilityConnection::OnConnectDied(const wptr<IRemoteObject> &remoteObject)
114 {
115     if (connectId_ != 0) {
116         FormSupplyCallback::GetInstance()->RemoveConnection(connectId_);
117         connectId_ = 0;
118     } else {
119         HILOG_ERROR("connectId_ invalidate. connectId_:%{public}d", connectId_);
120     }
121 }
122 
GetAppMgr()123 sptr<OHOS::AppExecFwk::IAppMgr> FormAbilityConnection::GetAppMgr()
124 {
125     sptr<ISystemAbilityManager> systemMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
126     if (systemMgr == nullptr) {
127         HILOG_ERROR("connect systemAbilityManager failed");
128         return nullptr;
129     }
130     sptr<IRemoteObject> remoteObject = systemMgr->GetSystemAbility(APP_MGR_SERVICE_ID);
131     if (remoteObject == nullptr) {
132         HILOG_ERROR("connect appMgrService failed");
133         return nullptr;
134     }
135     return iface_cast<OHOS::AppExecFwk::IAppMgr>(remoteObject);
136 }
137 
onFormAppConnect()138 bool FormAbilityConnection::onFormAppConnect()
139 {
140     std::string bundleName = GetBundleName();
141     if (bundleName.empty()) {
142         HILOG_ERROR("Empty bundle name");
143         return false;
144     }
145     auto appMgr = GetAppMgr();
146     if (!appMgr) {
147         HILOG_ERROR("Get app mgr failed");
148         return false;
149     }
150 
151     std::vector<AppExecFwk::RunningProcessInfo> infos;
152     int32_t userId = FormUtil::GetCurrentAccountId();
153     int32_t ret = appMgr->GetRunningProcessInformation(bundleName, userId, infos);
154     if (ret != ERR_OK) {
155         HILOG_ERROR("Get running process info failed");
156         return false;
157     }
158     std::string targetProcessName = bundleName + ":form";
159     for (std::vector<AppExecFwk::RunningProcessInfo>::iterator iter = infos.begin(); iter != infos.end(); iter++) {
160         if ((*iter).processName_ != targetProcessName) {
161             continue;
162         }
163         appFormPid_ = (*iter).pid_;
164         return true;
165     }
166     return false;
167 }
168 
ReportFormAppUnbindEvent()169 void FormAbilityConnection::ReportFormAppUnbindEvent()
170 {
171     FormEventInfo eventInfo;
172     eventInfo.timeStamp = FormUtil::GetNowMillisecond();
173     eventInfo.formId = GetFormId();
174     eventInfo.bundleName = GetBundleName() + ":form";
175     eventInfo.formAppPid = GetAppFormPid();
176     HILOG_DEBUG("bundleName:%{public}s, formId:%{public}" PRId64 ",pid:%{public}" PRId32 ",timstamp:%{public}" PRId64,
177         eventInfo.bundleName.c_str(), eventInfo.formId, eventInfo.formAppPid, eventInfo.timeStamp);
178     FormReport::GetInstance().RemoveFormId(eventInfo.formId);
179     FormEventReport::SendThirdFormEvent(FormEventName::UNBIND_FORM_APP, HiSysEventType::BEHAVIOR, eventInfo);
180 }
181 
182 /**
183  * @brief Set connectId.
184  * @param connectId The ability connection id.
185  */
SetConnectId(int32_t connectId)186 void FormAbilityConnection::SetConnectId(int32_t connectId)
187 {
188     HILOG_INFO("connectId_:%{public}d", connectId);
189     connectId_ = connectId;
190 }
191 
192 /**
193  * @brief Get connectId.
194  * @return The ability connection id.
195  */
GetConnectId() const196 int32_t FormAbilityConnection::GetConnectId() const
197 {
198     return connectId_;
199 }
200 
201 /**
202  * @brief Get the provider Key
203  *
204  * @return The provider Key
205  */
GetProviderKey() const206 std::string FormAbilityConnection::GetProviderKey() const
207 {
208     if (bundleName_.empty() || abilityName_.empty()) {
209         return "";
210     }
211     return bundleName_ + "::" + abilityName_;
212 }
213 
214 /**
215  * @brief Set the Provider Key
216  *
217  * @param bundleName bundleName
218  * @param abilityName abilityName
219  */
SetProviderKey(const std::string & bundleName,const std::string & abilityName)220 void FormAbilityConnection::SetProviderKey(const std::string &bundleName, const std::string &abilityName)
221 {
222     bundleName_ = bundleName;
223     abilityName_ = abilityName;
224 }
225 
SetFreeInstall(bool isFreeInstall)226 void FormAbilityConnection::SetFreeInstall(bool isFreeInstall)
227 {
228     isFreeInstall_ = isFreeInstall;
229 }
230 
SetFormId(int64_t formId)231 void FormAbilityConnection::SetFormId(int64_t formId)
232 {
233     formId_ = formId;
234 }
235 
GetFormId() const236 int64_t FormAbilityConnection::GetFormId() const
237 {
238     return formId_;
239 }
240 
SetHostToken(const sptr<IRemoteObject> hostToken)241 void FormAbilityConnection::SetHostToken(const sptr<IRemoteObject> hostToken)
242 {
243     hostToken_ = hostToken;
244 }
245 
GetHostToken() const246 sptr<IRemoteObject> FormAbilityConnection::GetHostToken() const
247 {
248     return hostToken_;
249 }
250 
SetProviderToken(const sptr<IRemoteObject> providerToken)251 void FormAbilityConnection::SetProviderToken(const sptr<IRemoteObject> providerToken)
252 {
253     providerToken_ = providerToken;
254 }
255 
GetProviderToken() const256 sptr<IRemoteObject> FormAbilityConnection::GetProviderToken() const
257 {
258     return providerToken_;
259 }
260 
GetBundleName()261 std::string FormAbilityConnection::GetBundleName()
262 {
263     return bundleName_;
264 }
265 
GetAppFormPid()266 int32_t FormAbilityConnection::GetAppFormPid()
267 {
268     return appFormPid_;
269 }
270 
271 } // namespace AppExecFwk
272 } // namespace OHOS