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 "form_ams_helper.h"
22 #include "form_bms_helper.h"
23 #include "form_data_mgr.h"
24 #include "form_db_cache.h"
25 #include "form_info.h"
26 #include "form_info_mgr.h"
27 #include "form_supply_callback.h"
28 #include "form_timer_mgr.h"
29 #include "hilog_wrapper.h"
30 #include "want.h"
31
32 namespace OHOS {
33 namespace AppExecFwk {
34 /**
35 * @brief OnAbilityConnectDone, AbilityMs notify caller ability the result of connect.
36 * @param element service ability's ElementName.
37 * @param remoteObject the session proxy of service ability.
38 * @param resultCode ERR_OK on success, others on failure.
39 */
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int resultCode)40 void FormAbilityConnection::OnAbilityConnectDone(
41 const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject, int resultCode)
42 {
43 if (resultCode != ERR_OK) {
44 HILOG_ERROR("%{public}s, formId:%{public}" PRId64 ", resultCode:%{public}d",
45 __func__, formId_, resultCode);
46 return;
47 }
48
49 HILOG_INFO("%{public}s, free install is %{public}d.", __func__, isFreeInstall_);
50 if (!isFreeInstall_) {
51 return;
52 }
53
54 std::vector<FormInfo> targetForms;
55 if (FormInfoMgr::GetInstance().GetFormsInfoByBundle(bundleName_, targetForms) != ERR_OK) {
56 HILOG_ERROR("%{public}s error, failed to get forms info for %{public}s.", __func__, bundleName_.c_str());
57 return;
58 }
59
60 FormRecord formRecord;
61 if (!FormDataMgr::GetInstance().GetFormRecord(formId_, formRecord)) {
62 HILOG_ERROR("%{public}s error, not exist such form:%{public}" PRId64 ".", __func__, formId_);
63 return;
64 }
65
66 FormDataMgr::GetInstance().SetRecordNeedFreeInstall(formId_, false);
67 FormInfo updatedForm;
68 if (FormDataMgr::GetInstance().GetUpdatedForm(formRecord, targetForms, updatedForm)) {
69 HILOG_INFO("%{public}s, refresh form", __func__);
70 FormDataMgr::GetInstance().SetNeedRefresh(formId_, true);
71 FormBmsHelper::GetInstance().NotifyModuleNotRemovable(formRecord.bundleName, formRecord.moduleName);
72 return;
73 }
74
75 // delete form
76 if (formRecord.formTempFlag) {
77 FormDataMgr::GetInstance().DeleteTempForm(formId_);
78 } else {
79 FormDbCache::GetInstance().DeleteFormInfo(formId_);
80 }
81 FormDataMgr::GetInstance().DeleteFormRecord(formId_);
82 const std::vector<int64_t> removedForms {formId_};
83 FormDataMgr::GetInstance().CleanHostRemovedForms(removedForms);
84 FormTimerMgr::GetInstance().RemoveFormTimer(formId_);
85 }
86
87 /**
88 * @brief OnAbilityDisconnectDone, AbilityMs notify caller ability the result of disconnect.
89 * @param element service ability's ElementName.
90 * @param resultCode ERR_OK on success, others on failure.
91 */
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int resultCode)92 void FormAbilityConnection::OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode)
93 {
94 HILOG_DEBUG("%{public}s, element:%{public}s, resultCode:%{public}d",
95 __func__, element.GetURI().c_str(), resultCode);
96 if (connectId_ != 0) {
97 FormSupplyCallback::GetInstance()->RemoveConnection(connectId_);
98 connectId_ = 0;
99 } else {
100 HILOG_ERROR("%{public}s fail, invalid connectId_: %{public}d", __func__, connectId_);
101 }
102 }
103
104 /**
105 * @brief Remote object died event.
106 * @param remoteObject the remote object of service ability.
107 */
OnConnectDied(const wptr<IRemoteObject> & remoteObject)108 void FormAbilityConnection::OnConnectDied(const wptr<IRemoteObject> &remoteObject)
109 {
110 if (connectId_ != 0) {
111 FormSupplyCallback::GetInstance()->RemoveConnection(connectId_);
112 connectId_ = 0;
113 } else {
114 HILOG_ERROR("%{public}s fail, connectId_ invalidate. connectId_: %{public}d", __func__, connectId_);
115 }
116 }
117
118 /**
119 * @brief Set connectId.
120 * @param connectId The ability connection id.
121 */
SetConnectId(int32_t connectId)122 void FormAbilityConnection::SetConnectId(int32_t connectId)
123 {
124 HILOG_INFO("%{public}s, connectId_: %{public}d", __func__, connectId);
125 connectId_ = connectId;
126 }
127
128 /**
129 * @brief Get connectId.
130 * @return The ability connection id.
131 */
GetConnectId() const132 int32_t FormAbilityConnection::GetConnectId() const
133 {
134 return connectId_;
135 }
136
137 /**
138 * @brief Get the provider Key
139 *
140 * @return The provider Key
141 */
GetProviderKey() const142 std::string FormAbilityConnection::GetProviderKey() const
143 {
144 if (bundleName_.empty() || abilityName_.empty()) {
145 return "";
146 }
147 return bundleName_ + "::" + abilityName_;
148 }
149
150 /**
151 * @brief Set the Provider Key
152 *
153 * @param bundleName bundleName
154 * @param abilityName abilityName
155 */
SetProviderKey(const std::string & bundleName,const std::string & abilityName)156 void FormAbilityConnection::SetProviderKey(const std::string &bundleName, const std::string &abilityName)
157 {
158 bundleName_ = bundleName;
159 abilityName_ = abilityName;
160 }
161
SetFreeInstall(bool isFreeInstall)162 void FormAbilityConnection::SetFreeInstall(bool isFreeInstall)
163 {
164 isFreeInstall_ = isFreeInstall;
165 }
166
SetFormId(int64_t formId)167 void FormAbilityConnection::SetFormId(int64_t formId)
168 {
169 formId_ = formId;
170 }
171
GetFormId() const172 int64_t FormAbilityConnection::GetFormId() const
173 {
174 return formId_;
175 }
176
SetHostToken(const sptr<IRemoteObject> hostToken)177 void FormAbilityConnection::SetHostToken(const sptr<IRemoteObject> hostToken)
178 {
179 hostToken_ = hostToken;
180 }
181
GetHostToken() const182 sptr<IRemoteObject> FormAbilityConnection::GetHostToken() const
183 {
184 return hostToken_;
185 }
186
SetProviderToken(const sptr<IRemoteObject> providerToken)187 void FormAbilityConnection::SetProviderToken(const sptr<IRemoteObject> providerToken)
188 {
189 providerToken_ = providerToken;
190 }
191
GetProviderToken() const192 sptr<IRemoteObject> FormAbilityConnection::GetProviderToken() const
193 {
194 return providerToken_;
195 }
196 } // namespace AppExecFwk
197 } // namespace OHOS