• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "hilog_wrapper.h"
19 #include "form_constants.h"
20 #include "form_host_client.h"
21 
22 namespace OHOS {
23 namespace AppExecFwk {
24 sptr<FormHostClient> FormHostClient::instance_ = nullptr;
25 std::mutex FormHostClient::instanceMutex_;
26 
FormHostClient()27 FormHostClient::FormHostClient()
28 {
29 }
30 
~FormHostClient()31 FormHostClient::~FormHostClient()
32 {
33 }
34 
35 /**
36  * @brief Get FormHostClient instance.
37  *
38  * @return FormHostClient instance.
39  */
GetInstance()40 sptr<FormHostClient> FormHostClient::GetInstance()
41 {
42     if (instance_ == nullptr) {
43         std::lock_guard<std::mutex> lock_l(instanceMutex_);
44         if (instance_ == nullptr) {
45             instance_ = new FormHostClient();
46         }
47     }
48     return instance_;
49 }
50 
51 /**
52  * @brief Add form callback.
53  *
54  * @param formCallback the host's form callback.
55  * @param formId The Id of the form.
56  * @return none.
57  */
AddForm(std::shared_ptr<FormCallbackInterface> formCallback,const int64_t formId)58 void FormHostClient::AddForm(std::shared_ptr<FormCallbackInterface> formCallback, const int64_t formId)
59 {
60     HILOG_INFO("%{public}s called.", __func__);
61     if (formId <= 0 || formCallback == nullptr) {
62         HILOG_ERROR("%{public}s error, invalid formId or formCallback.", __func__);
63         return;
64     }
65     std::lock_guard<std::mutex> lock(callbackMutex_);
66     auto iter = formCallbackMap_.find(formId);
67     if (iter == formCallbackMap_.end()) {
68         std::set<std::shared_ptr<FormCallbackInterface>> callbacks;
69         callbacks.emplace(formCallback);
70         formCallbackMap_.emplace(formId, callbacks);
71     } else {
72         iter->second.emplace(formCallback);
73     }
74 }
75 
76 /**
77  * @brief Remove form callback.
78  *
79  * @param formCallback the host's form callback.
80  * @param formId The Id of the form.
81  * @return none.
82  */
RemoveForm(std::shared_ptr<FormCallbackInterface> formCallback,const int64_t formId)83 void FormHostClient::RemoveForm(std::shared_ptr<FormCallbackInterface> formCallback, const int64_t formId)
84 {
85     HILOG_INFO("%{public}s called.", __func__);
86     if (formId <= 0 || formCallback == nullptr) {
87         HILOG_ERROR("%{public}s, invalid formId or formCallback.", __func__);
88         return;
89     }
90     std::lock_guard<std::mutex> lock(callbackMutex_);
91     auto iter = formCallbackMap_.find(formId);
92     if (iter == formCallbackMap_.end()) {
93         HILOG_ERROR("%{public}s, not find formId:%{public}s.", __func__, std::to_string(formId).c_str());
94         return;
95     }
96     iter->second.erase(formCallback);
97     if (iter->second.empty()) {
98         formCallbackMap_.erase(iter);
99     }
100     HILOG_INFO("%{public}s end.", __func__);
101 }
102 
103 /**
104  * @brief Check whether the form exist in the formhosts.
105  *
106  * @param formId The Id of the form.
107  * @return Returns true if contains form; returns false otherwise.
108  */
ContainsForm(int64_t formId)109 bool FormHostClient::ContainsForm(int64_t formId)
110 {
111     HILOG_INFO("%{public}s called.", __func__);
112     std::lock_guard<std::mutex> lock(callbackMutex_);
113     return formCallbackMap_.find(formId) != formCallbackMap_.end();
114 }
115 
116 /**
117  * @brief Add form state.
118  *
119  * @param formStateCallback the host's form state callback.
120  * @param want the want of acquiring form state.
121  * @return Returns true if contains form; returns false otherwise.
122  */
AddFormState(std::shared_ptr<FormStateCallbackInterface> & formStateCallback,const AAFwk::Want & want)123 bool FormHostClient::AddFormState(std::shared_ptr<FormStateCallbackInterface> &formStateCallback,
124                                   const AAFwk::Want &want)
125 {
126     HILOG_INFO("%{public}s called.", __func__);
127     std::string bundleName = want.GetElement().GetBundleName();
128     std::string abilityName = want.GetElement().GetAbilityName();
129     const std::string doubleColon = "::";
130     std::string key;
131     key.append(bundleName).append(doubleColon).append(abilityName).append(doubleColon)
132         .append(want.GetStringParam(AppExecFwk::Constants::PARAM_MODULE_NAME_KEY)).append(doubleColon)
133         .append(want.GetStringParam(AppExecFwk::Constants::PARAM_FORM_NAME_KEY)).append(doubleColon)
134         .append(std::to_string(want.GetIntParam(AppExecFwk::Constants::PARAM_FORM_DIMENSION_KEY, 1)));
135     std::lock_guard<std::mutex> lock(formStateCallbackMutex_);
136     auto iter = formStateCallbackMap_.find(key);
137     if (iter == formStateCallbackMap_.end()) {
138         std::set<std::shared_ptr<FormStateCallbackInterface>> callbacks;
139         callbacks.emplace(formStateCallback);
140         formStateCallbackMap_.emplace(key, callbacks);
141     } else {
142         iter->second.insert(formStateCallback);
143     }
144     HILOG_INFO("%{public}s done.", __func__);
145     return true;
146 }
147 
RegisterUninstallCallback(UninstallCallback callback)148 bool FormHostClient::RegisterUninstallCallback(UninstallCallback callback)
149 {
150     std::lock_guard<std::mutex> lock(uninstallCallbackMutex_);
151     uninstallCallback_ = callback;
152     return true;
153 }
154 
155 /**
156  * @brief Request to give back a form.
157  *
158  * @param formJsInfo Form js info.
159  * @return none.
160  */
OnAcquired(const FormJsInfo & formJsInfo)161 void FormHostClient::OnAcquired(const FormJsInfo &formJsInfo)
162 {
163     HILOG_INFO("%{public}s called.", __func__);
164     HILOG_INFO("Imamge number is %{public}zu.", formJsInfo.imageDataMap.size());
165     int64_t formId = formJsInfo.formId;
166     if (formId < 0) {
167         HILOG_ERROR("%{public}s error, the passed form id can't be negative.", __func__);
168         return;
169     }
170     std::lock_guard<std::mutex> lock(callbackMutex_);
171     auto iter = formCallbackMap_.find(formId);
172     if (iter == formCallbackMap_.end()) {
173         HILOG_ERROR("%{public}s error, not find formId:%{public}s.", __func__, std::to_string(formId).c_str());
174         return;
175     }
176     for (const auto& callback : iter->second) {
177         HILOG_INFO("%{public}s, formId: %{public}" PRId64 ", jspath: %{public}s, data: %{public}s",
178             __func__, formId, formJsInfo.jsFormCodePath.c_str(), formJsInfo.formData.c_str());
179         callback->ProcessFormUpdate(formJsInfo);
180     }
181 }
182 
183 /**
184  * @brief Update form.
185  *
186  * @param formJsInfo Form js info.
187  * @return none.
188  */
OnUpdate(const FormJsInfo & formJsInfo)189 void FormHostClient::OnUpdate(const FormJsInfo &formJsInfo)
190 {
191     HILOG_INFO("%{public}s called.", __func__);
192     HILOG_INFO("Imamge number is %{public}zu.", formJsInfo.imageDataMap.size());
193     int64_t formId = formJsInfo.formId;
194     if (formId < 0) {
195         HILOG_ERROR("%{public}s error, the passed form id can't be negative.", __func__);
196         return;
197     }
198     std::lock_guard<std::mutex> lock(callbackMutex_);
199     auto iter = formCallbackMap_.find(formId);
200     if (iter == formCallbackMap_.end()) {
201         HILOG_ERROR("%{public}s error, not find formId:%{public}s.", __func__, std::to_string(formId).c_str());
202         return;
203     }
204     for (const auto& callback : iter->second) {
205         HILOG_INFO("%{public}s, formId: %{public}" PRId64 ", jspath: %{public}s, data: %{public}s",
206             __func__, formId, formJsInfo.jsFormCodePath.c_str(), formJsInfo.formData.c_str());
207         callback->ProcessFormUpdate(formJsInfo);
208     }
209 }
210 
211 /**
212  * @brief UnInstall the forms.
213  *
214  * @param formIds The Id of the forms.
215  * @return none.
216  */
OnUninstall(const std::vector<int64_t> & formIds)217 void FormHostClient::OnUninstall(const std::vector<int64_t> &formIds)
218 {
219     HILOG_INFO("%{public}s called.", __func__);
220     if (formIds.empty()) {
221         HILOG_ERROR("%{public}s error, formIds is empty.", __func__);
222         return;
223     }
224     {
225         std::lock_guard<std::mutex> lock(uninstallCallbackMutex_);
226         if (uninstallCallback_ != nullptr) {
227             uninstallCallback_(formIds);
228         }
229     }
230     for (auto &formId : formIds) {
231         if (formId < 0) {
232             HILOG_ERROR("%{public}s error, the passed form id can't be negative.", __func__);
233             continue;
234         }
235         std::lock_guard<std::mutex> lock(callbackMutex_);
236         auto iter = formCallbackMap_.find(formId);
237         if (iter == formCallbackMap_.end()) {
238             HILOG_ERROR("%{public}s error, not find formId:%{public}s.", __func__, std::to_string(formId).c_str());
239             continue;
240         }
241         for (const auto& callback : iter->second) {
242             HILOG_ERROR("%{public}s uninstall formId:%{public}s.", __func__, std::to_string(formId).c_str());
243             callback->ProcessFormUninstall(formId);
244         }
245     }
246 }
247 
248 /**
249  * @brief Form provider is acquire state
250  * @param state The form state.
251  * @param want The form want.
252  */
OnAcquireState(FormState state,const AAFwk::Want & want)253 void FormHostClient::OnAcquireState(FormState state, const AAFwk::Want &want)
254 {
255     HILOG_INFO("%{public}s state:%{public}d.", __func__, state);
256     std::string bundleName = want.GetElement().GetBundleName();
257     std::string abilityName = want.GetElement().GetAbilityName();
258     const std::string doubleColon = "::";
259     std::string key;
260     key.append(bundleName).append(doubleColon).append(abilityName).append(doubleColon)
261         .append(want.GetStringParam(AppExecFwk::Constants::PARAM_MODULE_NAME_KEY)).append(doubleColon)
262         .append(want.GetStringParam(AppExecFwk::Constants::PARAM_FORM_NAME_KEY)).append(doubleColon)
263         .append(std::to_string(want.GetIntParam(AppExecFwk::Constants::PARAM_FORM_DIMENSION_KEY, 1)));
264 
265     std::lock_guard<std::mutex> lock(formStateCallbackMutex_);
266     auto iter = formStateCallbackMap_.find(key);
267     if (iter == formStateCallbackMap_.end()) {
268         HILOG_INFO("form state callback not found");
269     } else {
270         std::set<std::shared_ptr<FormStateCallbackInterface>> &callbackSet = iter->second;
271         for (auto &callback: callbackSet) {
272             callback->ProcessAcquireState(state);
273         }
274         formStateCallbackMap_.erase(iter);
275     }
276     HILOG_INFO("%{public}s done", __func__);
277 }
278 }  // namespace AppExecFwk
279 }  // namespace OHOS
280