1 /*
2 * Copyright (c) 2021 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 <cinttypes>
17
18 #include "appexecfwk_errors.h"
19 #include "form_ams_helper.h"
20 #include "form_constants.h"
21 #include "form_provider_mgr.h"
22 #include "form_supply_callback.h"
23 #include "form_util.h"
24 #include "hilog_wrapper.h"
25 #include "string_ex.h"
26
27 namespace OHOS {
28 namespace AppExecFwk {
29 sptr<FormSupplyCallback> FormSupplyCallback::instance_ = nullptr;
30 std::mutex FormSupplyCallback::mutex_;
31
GetInstance()32 sptr<FormSupplyCallback> FormSupplyCallback::GetInstance()
33 {
34 if (instance_ == nullptr) {
35 std::lock_guard<std::mutex> lock_l(mutex_);
36 if (instance_ == nullptr) {
37 instance_ = new FormSupplyCallback();
38 }
39 }
40 return instance_;
41 }
42
43 /**
44 * @brief Accept form binding data from form provider.
45 * @param providerFormInfo Form binding data.
46 * @param want input data.
47 * @return Returns ERR_OK on success, others on failure.
48 */
OnAcquire(const FormProviderInfo & formProviderInfo,const Want & want)49 int FormSupplyCallback::OnAcquire(const FormProviderInfo &formProviderInfo, const Want &want)
50 {
51 HILOG_INFO("%{public}s called.", __func__);
52 long connectId = want.GetLongParam(Constants::FORM_CONNECT_ID, 0);
53 int errCode = want.GetIntParam(Constants::PROVIDER_FLAG, ERR_OK);
54 if (errCode != ERR_OK) {
55 RemoveConnection(connectId);
56 HILOG_ERROR("%{public}s error, errCode: %{public}d", __func__, errCode);
57 return errCode;
58 }
59
60 std::string strFormId = want.GetStringParam(Constants::PARAM_FORM_IDENTITY_KEY);
61 if (strFormId.empty()) {
62 HILOG_ERROR("%{public}s error, formId is empty.", __func__);
63 return ERR_APPEXECFWK_FORM_INVALID_PARAM;
64 }
65 int64_t formId = std::stoll(strFormId);
66 int type = want.GetIntParam(Constants::ACQUIRE_TYPE, 0);
67 HILOG_DEBUG("%{public}s come: %{public}" PRId64 ", %{public}ld, %{public}d", __func__,
68 formId, connectId, type);
69 RemoveConnection(connectId);
70
71 switch (type) {
72 case Constants::ACQUIRE_TYPE_CREATE_FORM:
73 return FormProviderMgr::GetInstance().AcquireForm(formId, formProviderInfo);
74 case Constants::ACQUIRE_TYPE_RECREATE_FORM:
75 return FormProviderMgr::GetInstance().UpdateForm(formId, formProviderInfo);
76 default:
77 HILOG_WARN("%{public}s warning, onAcquired type: %{public}d", __func__, type);
78 }
79 HILOG_INFO("%{public}s end.", __func__);
80 return ERR_APPEXECFWK_FORM_INVALID_PARAM;
81 }
82
83 /**
84 * @brief Accept other event.
85 * @param want input data.
86 * @return Returns ERR_OK on success, others on failure.
87 */
OnEventHandle(const Want & want)88 int FormSupplyCallback::OnEventHandle(const Want &want)
89 {
90 HILOG_INFO("%{public}s called.", __func__);
91 long connectId = want.GetLongParam(Constants::FORM_CONNECT_ID, 0);
92 std::string supplyInfo = want.GetStringParam(Constants::FORM_SUPPLY_INFO);
93 HILOG_DEBUG("%{public}s come: %{public}ld, %{public}s", __func__, connectId, supplyInfo.c_str());
94 RemoveConnection(connectId);
95 HILOG_INFO("%{public}s end.", __func__);
96 return ERR_OK;
97 }
98
99 /**
100 * @brief Accept form state from form provider.
101 * @param state Form state.
102 * @param provider provider info.
103 * @param wantArg The want of onAcquireFormState.
104 * @param want input data.
105 * @return Returns ERR_OK on success, others on failure.
106 */
OnAcquireStateResult(FormState state,const std::string & provider,const Want & wantArg,const Want & want)107 int FormSupplyCallback::OnAcquireStateResult(FormState state, const std::string &provider, const Want &wantArg,
108 const Want &want)
109 {
110 HILOG_INFO("%{public}s called.", __func__);
111 long connectId = want.GetLongParam(Constants::FORM_CONNECT_ID, 0);
112 RemoveConnection(connectId);
113
114 ErrCode errCode = FormProviderMgr::GetInstance().AcquireFormStateBack(state, provider, wantArg);
115 HILOG_INFO("%{public}s end.", __func__);
116 return errCode;
117 }
118
119 /**
120 * @brief Save ability Connection for the callback.
121 * @param connection ability connection.
122 */
AddConnection(sptr<FormAbilityConnection> connection)123 void FormSupplyCallback::AddConnection(sptr<FormAbilityConnection> connection)
124 {
125 HILOG_INFO("%{public}s called.", __func__);
126 std::lock_guard<std::mutex> lock_l(conMutex_);
127 long connectKey = FormUtil::GetCurrentMillisecond();
128 while (connections_.find(connectKey) != connections_.end()) {
129 connectKey++;
130 }
131 connection->SetConnectId(connectKey);
132 connections_.emplace(connectKey, connection);
133 HILOG_INFO("%{public}s end.", __func__);
134 }
135
136 /**
137 * @brief Delete ability connection after the callback come.
138 * @param connectId The ability connection id generated when save.
139 */
RemoveConnection(long connectId)140 void FormSupplyCallback::RemoveConnection(long connectId)
141 {
142 HILOG_INFO("%{public}s called.", __func__);
143 std::lock_guard<std::mutex> lock_l(conMutex_);
144 auto conIterator = connections_.find(connectId);
145 if (conIterator != connections_.end()) {
146 sptr<FormAbilityConnection> connection = conIterator->second;
147 if (connection != nullptr) {
148 if (CanDisConnect(connection)) {
149 FormAmsHelper::GetInstance().DisConnectServiceAbility(connection);
150 HILOG_INFO("%{public}s end, disconnect service ability", __func__);
151 } else {
152 FormAmsHelper::GetInstance().DisConnectServiceAbilityDelay(connection);
153 HILOG_INFO("%{public}s end, disconnect service ability delay", __func__);
154 }
155 }
156 connections_.erase(connectId);
157 }
158 HILOG_INFO("%{public}s end.", __func__);
159 }
160 /**
161 * @brief check if disconnect ability or not.
162 * @param connection The ability connection.
163 */
CanDisConnect(sptr<FormAbilityConnection> & connection)164 bool FormSupplyCallback::CanDisConnect(sptr<FormAbilityConnection> &connection)
165 {
166 HILOG_INFO("%{public}s called.", __func__);
167 int count = 0;
168 for(auto &conn : connections_) {
169 if (connection->GetProviderKey() == conn.second->GetProviderKey()) {
170 HILOG_INFO("%{public}s, key: %{public}s", __func__, conn.second->GetProviderKey().c_str());
171 count++;
172 if (count > 1) {
173 HILOG_INFO("%{public}s end, true.", __func__);
174 return true;
175 }
176 }
177 }
178 HILOG_INFO("%{public}s end, false.", __func__);
179 return false;
180 }
181 } // namespace AppExecFwk
182 } // namespace OHOS
183