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 #include "form_render_stub.h"
16 #include "appexecfwk_errors.h"
17 #include "app_scheduler_interface.h"
18 #include "errors.h"
19 #include "fms_log_wrapper.h"
20 #include "form_mgr_errors.h"
21 #include "ipc_skeleton.h"
22 #include "ipc_types.h"
23 #include "iremote_object.h"
24
25 namespace OHOS {
26 namespace AppExecFwk {
27 namespace {
28 constexpr int32_t MAX_ALLOW_SIZE = 8 * 1024;
29 }
30
FormRenderStub()31 FormRenderStub::FormRenderStub()
32 {}
33
~FormRenderStub()34 FormRenderStub::~FormRenderStub()
35 {}
36
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)37 int FormRenderStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
38 {
39 HILOG_DEBUG("FormRenderStub::OnReceived,code = %{public}u,flags= %{public}d", code, option.GetFlags());
40 std::u16string descriptor = FormRenderStub::GetDescriptor();
41 std::u16string remoteDescriptor = data.ReadInterfaceToken();
42 if (descriptor != remoteDescriptor) {
43 HILOG_ERROR("localDescriptor not equal to remote");
44 return ERR_APPEXECFWK_FORM_INVALID_PARAM;
45 }
46
47 switch (code) {
48 case static_cast<uint32_t>(IFormRender::Message::FORM_RENDER_RENDER_FORM):
49 return HandleRenderForm(data, reply);
50 case static_cast<uint32_t>(IFormRender::Message::FORM_RENDER_STOP_RENDERING_FORM):
51 return HandleStopRenderingForm(data, reply);
52 case static_cast<uint32_t>(IFormRender::Message::FORM_RENDER_FORM_HOST_DIED):
53 return HandleCleanFormHost(data, reply);
54 case static_cast<uint32_t>(IFormRender::Message::FORM_RENDER_RELOAD_FORM):
55 return HandleReloadForm(data, reply);
56 case static_cast<uint32_t>(IFormRender::Message::FORM_RENDER_RELEASE_RENDERER):
57 return HandleReleaseRenderer(data, reply);
58 case static_cast<uint32_t>(IFormRender::Message::FORM_RENDER_UNLOCKED):
59 return HandleOnUnlock(data, reply);
60 case static_cast<uint32_t>(IFormRender::Message::FORM_RECYCLE_FORM):
61 return HandleRecycleForm(data, reply);
62 case static_cast<uint32_t>(IFormRender::Message::FORM_RECOVER_FORM):
63 return HandleRecoverForm(data, reply);
64 case static_cast<uint32_t>(IFormRender::Message::FORM_RUN_CACHED_CONFIG):
65 RunCachedConfigurationUpdated();
66 return ERR_OK;
67 case static_cast<uint32_t>(IFormRender::Message::FORM_SET_VISIBLE_CHANGE):
68 return HandleSetVisibleChange(data, reply);
69 default:
70 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
71 }
72 }
73
HandleRenderForm(MessageParcel & data,MessageParcel & reply)74 int FormRenderStub::HandleRenderForm(MessageParcel &data, MessageParcel &reply)
75 {
76 std::unique_ptr<FormJsInfo> formJsInfo(data.ReadParcelable<FormJsInfo>());
77 if (!formJsInfo) {
78 HILOG_ERROR("error to ReadParcelable<formJsInfo>");
79 return ERR_APPEXECFWK_PARCEL_ERROR;
80 }
81 std::unique_ptr<Want> want(data.ReadParcelable<Want>());
82 if (!want) {
83 HILOG_ERROR("error to ReadParcelable<Want>");
84 return ERR_APPEXECFWK_PARCEL_ERROR;
85 }
86
87 sptr<IRemoteObject> client = data.ReadRemoteObject();
88 if (client == nullptr) {
89 HILOG_ERROR("error to get remote object");
90 return ERR_APPEXECFWK_PARCEL_ERROR;
91 }
92
93 int32_t result = RenderForm(*formJsInfo, *want, client);
94 reply.WriteInt32(result);
95 return result;
96 }
97
HandleStopRenderingForm(MessageParcel & data,MessageParcel & reply)98 int FormRenderStub::HandleStopRenderingForm(MessageParcel &data, MessageParcel &reply)
99 {
100 std::unique_ptr<FormJsInfo> formJsInfo(data.ReadParcelable<FormJsInfo>());
101 if (!formJsInfo) {
102 HILOG_ERROR("ReadParcelable<formJsInfo> fail");
103 return ERR_APPEXECFWK_PARCEL_ERROR;
104 }
105 std::unique_ptr<Want> want(data.ReadParcelable<Want>());
106 if (!want) {
107 HILOG_ERROR("ReadParcelable<Want> fail");
108 return ERR_APPEXECFWK_PARCEL_ERROR;
109 }
110
111 sptr<IRemoteObject> client = data.ReadRemoteObject();
112 if (client == nullptr) {
113 HILOG_ERROR("get remote object failed");
114 return ERR_APPEXECFWK_PARCEL_ERROR;
115 }
116
117 int32_t result = StopRenderingForm(*formJsInfo, *want, client);
118 reply.WriteInt32(result);
119 return result;
120 }
121
HandleCleanFormHost(MessageParcel & data,MessageParcel & reply)122 int FormRenderStub::HandleCleanFormHost(MessageParcel &data, MessageParcel &reply)
123 {
124 sptr<IRemoteObject> hostToken = data.ReadRemoteObject();
125 if (hostToken == nullptr) {
126 HILOG_ERROR("null hostToken");
127 return ERR_APPEXECFWK_PARCEL_ERROR;
128 }
129
130 int32_t result = CleanFormHost(hostToken);
131 reply.WriteInt32(result);
132 return result;
133 }
134
HandleReleaseRenderer(MessageParcel & data,MessageParcel & reply)135 int32_t FormRenderStub::HandleReleaseRenderer(MessageParcel &data, MessageParcel &reply)
136 {
137 int64_t formId = data.ReadInt64();
138 std::string compId = data.ReadString();
139 std::string uid = data.ReadString();
140 int32_t result = ReleaseRenderer(formId, compId, uid);
141 reply.WriteInt32(result);
142 return result;
143 }
144
HandleReloadForm(MessageParcel & data,MessageParcel & reply)145 int FormRenderStub::HandleReloadForm(MessageParcel &data, MessageParcel &reply)
146 {
147 std::vector<FormJsInfo> formJsInfos;
148 int32_t result = GetParcelableInfos(data, formJsInfos);
149 if (result != ERR_OK) {
150 HILOG_ERROR("fail GetParcelableInfos<FormJsInfo>");
151 return result;
152 }
153
154 std::unique_ptr<Want> want(data.ReadParcelable<Want>());
155 if (!want) {
156 HILOG_ERROR("ReadParcelable<Want> failed");
157 return ERR_APPEXECFWK_PARCEL_ERROR;
158 }
159
160 result = ReloadForm(std::move(formJsInfos), *want);
161 reply.WriteInt32(result);
162 return result;
163 }
164
HandleOnUnlock(MessageParcel & data,MessageParcel & reply)165 int32_t FormRenderStub::HandleOnUnlock(MessageParcel &data, MessageParcel &reply)
166 {
167 int32_t result = OnUnlock();
168 reply.WriteInt32(result);
169 return result;
170 }
171
HandleSetVisibleChange(MessageParcel & data,MessageParcel & reply)172 int32_t FormRenderStub::HandleSetVisibleChange(MessageParcel &data, MessageParcel &reply)
173 {
174 HILOG_ERROR("begin");
175 int64_t formId = data.ReadInt64();
176 bool isVisible = data.ReadBool();
177 std::unique_ptr<Want> want(data.ReadParcelable<Want>());
178 if (!want) {
179 HILOG_ERROR("error to ReadParcelable<Want>");
180 return ERR_APPEXECFWK_PARCEL_ERROR;
181 }
182 int32_t result = SetVisibleChange(formId, isVisible, *want);
183 reply.WriteInt32(result);
184 HILOG_ERROR("end");
185 return result;
186 }
187
188 template<typename T>
GetParcelableInfos(MessageParcel & reply,std::vector<T> & parcelableInfos)189 int32_t FormRenderStub::GetParcelableInfos(MessageParcel &reply, std::vector<T> &parcelableInfos)
190 {
191 int32_t infoSize = reply.ReadInt32();
192 if (infoSize < 0 || infoSize > MAX_ALLOW_SIZE) {
193 HILOG_ERROR("invalid size:%{public}d", infoSize);
194 return ERR_APPEXECFWK_PARCEL_ERROR;
195 }
196
197 for (int32_t i = 0; i < infoSize; i++) {
198 std::unique_ptr<T> info(reply.ReadParcelable<T>());
199 if (!info) {
200 HILOG_ERROR("Read Parcelable infos error");
201 return ERR_APPEXECFWK_PARCEL_ERROR;
202 }
203 parcelableInfos.emplace_back(*info);
204 }
205 return ERR_OK;
206 }
207
HandleRecycleForm(MessageParcel & data,MessageParcel & reply)208 int32_t FormRenderStub::HandleRecycleForm(MessageParcel &data, MessageParcel &reply)
209 {
210 int64_t formId = data.ReadInt64();
211 std::unique_ptr<Want> want(data.ReadParcelable<Want>());
212 if (!want) {
213 HILOG_ERROR("error to ReadParcelable<Want>");
214 return ERR_APPEXECFWK_PARCEL_ERROR;
215 }
216 int32_t result = RecycleForm(formId, *want);
217 reply.WriteInt32(result);
218 return result;
219 }
220
HandleRecoverForm(MessageParcel & data,MessageParcel & reply)221 int32_t FormRenderStub::HandleRecoverForm(MessageParcel &data, MessageParcel &reply)
222 {
223 std::unique_ptr<FormJsInfo> formJsInfo(data.ReadParcelable<FormJsInfo>());
224 if (!formJsInfo) {
225 HILOG_ERROR("read FormJsInfo error");
226 return ERR_APPEXECFWK_PARCEL_ERROR;
227 }
228 std::unique_ptr<Want> want(data.ReadParcelable<Want>());
229 if (!want) {
230 HILOG_ERROR("read want error");
231 return ERR_APPEXECFWK_PARCEL_ERROR;
232 }
233 int32_t result = RecoverForm(*formJsInfo, *want);
234 reply.WriteInt32(result);
235 return result;
236 }
237 } // namespace AppExecFwk
238 } // namespace OHOS
239