1 /*
2 * Copyright (c) 2023 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 "form_renderer_group.h"
17
18 #include "configuration.h"
19 #include "form_renderer.h"
20 #include "form_renderer_hilog.h"
21
22 namespace OHOS {
23 namespace Ace {
24 namespace {
25 constexpr char FORM_RENDERER_COMP_ID[] = "ohos.extra.param.key.form_comp_id";
26 constexpr char FORM_RENDER_STATE[] = "ohos.extra.param.key.form_render_state";
27 }
Create(const std::shared_ptr<OHOS::AbilityRuntime::Context> context,const std::shared_ptr<OHOS::AbilityRuntime::Runtime> runtime)28 std::shared_ptr<FormRendererGroup> FormRendererGroup::Create(
29 const std::shared_ptr<OHOS::AbilityRuntime::Context> context,
30 const std::shared_ptr<OHOS::AbilityRuntime::Runtime> runtime)
31 {
32 return std::make_shared<FormRendererGroup>(context, runtime);
33 }
34
FormRendererGroup(const std::shared_ptr<OHOS::AbilityRuntime::Context> context,const std::shared_ptr<OHOS::AbilityRuntime::Runtime> runtime)35 FormRendererGroup::FormRendererGroup(
36 const std::shared_ptr<OHOS::AbilityRuntime::Context> context,
37 const std::shared_ptr<OHOS::AbilityRuntime::Runtime> runtime)
38 : context_(context), runtime_(runtime) {}
39
~FormRendererGroup()40 FormRendererGroup::~FormRendererGroup()
41 {
42 DeleteForm();
43 }
44
AddForm(const OHOS::AAFwk::Want & want,const OHOS::AppExecFwk::FormJsInfo & formJsInfo)45 void FormRendererGroup::AddForm(const OHOS::AAFwk::Want& want, const OHOS::AppExecFwk::FormJsInfo& formJsInfo)
46 {
47 auto compId = want.GetStringParam(FORM_RENDERER_COMP_ID);
48 currentCompId_ = compId;
49 FormRequest formRequest;
50 formRequest.compId = compId;
51 formRequest.want = want;
52 formRequest.formJsInfo = formJsInfo;
53 auto info = std::find_if(
54 formRequests_.begin(), formRequests_.end(), formRequest);
55 if (info != formRequests_.end()) {
56 *info = formRequest;
57 } else {
58 formRequests_.emplace_back(formRequest);
59 }
60 bool isVerified = want.GetBoolParam(FORM_RENDER_STATE, false);
61 if (isVerified) {
62 HILOG_DEBUG("The user is verified, start rendering form.");
63 InnerAddForm(formRequest);
64 return;
65 }
66 HILOG_INFO("The user is not verified at this time, can not render the form now.");
67 }
68
OnUnlock()69 void FormRendererGroup::OnUnlock()
70 {
71 HILOG_INFO("The user is verified, OnUnlock called.");
72 FormRequest currentFormRequest;
73 for (auto& formRequest : formRequests_) {
74 if (currentCompId_ == formRequest.compId) {
75 currentFormRequest = formRequest;
76 formRequest.want.SetParam(FORM_RENDER_STATE, true);
77 }
78 }
79 HILOG_DEBUG("start rendering form.");
80 InnerAddForm(currentFormRequest);
81 }
82
InnerAddForm(const FormRequest & formRequest)83 void FormRendererGroup::InnerAddForm(const FormRequest& formRequest)
84 {
85 HILOG_DEBUG("InnerAddForm called");
86 auto compId = formRequest.compId;
87 OHOS::AAFwk::Want want = formRequest.want;
88 AppExecFwk::FormJsInfo formJsInfo = formRequest.formJsInfo;
89 if (formRenderer_ == nullptr) {
90 formRenderer_ = std::make_shared<FormRenderer>(context_, runtime_);
91 if (!formRenderer_) {
92 HILOG_ERROR("InnerAddForm create form render failed");
93 return;
94 }
95 HILOG_INFO("InnerAddForm compId is %{public}s. formId is %{public}s", compId.c_str(),
96 std::to_string(formJsInfo.formId).c_str());
97 formRenderer_->AddForm(want, formJsInfo);
98 } else {
99 HILOG_INFO("AttachForm compId is %{public}s formRequests size is :%{public}s.",
100 compId.c_str(), std::to_string(formRequests_.size()).c_str());
101 formRenderer_->AttachForm(want, formJsInfo);
102 }
103 }
104
ReloadForm(const AppExecFwk::FormJsInfo & formJsInfo)105 void FormRendererGroup::ReloadForm(const AppExecFwk::FormJsInfo& formJsInfo)
106 {
107 if (formRenderer_ == nullptr) {
108 HILOG_ERROR("ReloadForm failed, formRenderer is null");
109 return;
110 }
111
112 formRenderer_->ReloadForm(formJsInfo.formSrc);
113 for (auto &formRequest : formRequests_) {
114 bool allDynamic = formJsInfo.isDynamic && formRequest.isDynamic;
115 if (!allDynamic && currentCompId_ == formRequest.compId) {
116 HILOG_INFO("SurfaceReuse due to change to static card when curCompId is %{public}s.",
117 formRequest.compId.c_str());
118 formRenderer_->OnSurfaceReuse(formJsInfo);
119 }
120 formRequest.formJsInfo = formJsInfo;
121 formRequest.isDynamic = formJsInfo.isDynamic;
122 }
123 }
124
UpdateForm(const OHOS::AppExecFwk::FormJsInfo & formJsInfo)125 void FormRendererGroup::UpdateForm(const OHOS::AppExecFwk::FormJsInfo& formJsInfo)
126 {
127 HILOG_INFO("UpdateForm formId %{public}s.", std::to_string(formJsInfo.formId).c_str());
128 if (formRenderer_ == nullptr) {
129 HILOG_ERROR("UpdateForm failed, formRenderer is null");
130 return;
131 }
132 formRenderer_->UpdateForm(formJsInfo);
133 }
134
DeleteForm(const std::string & compId)135 void FormRendererGroup::DeleteForm(const std::string& compId)
136 {
137 HILOG_INFO("DeleteForm compId is %{public}s, currentCompId is %{public}s, formRequests size is %{public}s.",
138 compId.c_str(), currentCompId_.c_str(), std::to_string(formRequests_.size()).c_str());
139
140 for (auto iter = formRequests_.begin(); iter != formRequests_.end(); ++iter) {
141 if (iter->compId == compId) {
142 formRequests_.erase(iter);
143 break;
144 }
145 }
146
147 if (compId != currentCompId_) {
148 return;
149 }
150
151 if (formRequests_.empty()) {
152 HILOG_INFO("Release renderer obj due to formRequests is empty.");
153 DeleteForm();
154 return;
155 }
156
157 FormRequest request = formRequests_.back();
158 currentCompId_ = request.compId;
159 HILOG_INFO("RestoreForm compId is %{public}s.", currentCompId_.c_str());
160 formRenderer_->AttachForm(request.want, request.formJsInfo);
161 }
162
IsFormRequestsEmpty()163 bool FormRendererGroup::IsFormRequestsEmpty()
164 {
165 return formRequests_.empty();
166 }
167
GetAllRendererFormRequests() const168 const std::vector<FormRequest>& FormRendererGroup::GetAllRendererFormRequests() const
169 {
170 return formRequests_;
171 }
172
DeleteForm()173 void FormRendererGroup::DeleteForm()
174 {
175 if (formRenderer_ == nullptr) {
176 HILOG_INFO("FormRenderer has destory");
177 return;
178 }
179 formRenderer_->Destroy();
180 formRenderer_ = nullptr;
181 formRequests_.clear();
182 }
183
UpdateConfiguration(const std::shared_ptr<OHOS::AppExecFwk::Configuration> & config)184 void FormRendererGroup::UpdateConfiguration(
185 const std::shared_ptr<OHOS::AppExecFwk::Configuration>& config)
186 {
187 if (!config) {
188 HILOG_ERROR("UpdateConfiguration config is null");
189 return;
190 }
191 if (formRenderer_ == nullptr) {
192 HILOG_ERROR("UpdateConfiguration failed, formRenderer is null");
193 return;
194 }
195 formRenderer_->UpdateConfiguration(config);
196 }
197 } // namespace Ace
198 } // namespace OHOS
199