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_renderer_delegate_proxy.h"
16 
17 #include "form_renderer_delegate_interface.h"
18 #include "form_renderer_hilog.h"
19 
20 namespace OHOS {
21 namespace Ace {
FormRendererDelegateProxy(const sptr<IRemoteObject> & impl)22 FormRendererDelegateProxy::FormRendererDelegateProxy(const sptr<IRemoteObject>& impl)
23     : IRemoteProxy<IFormRendererDelegate>(impl)
24 {}
25 
OnSurfaceCreate(const std::shared_ptr<Rosen::RSSurfaceNode> & surfaceNode,const OHOS::AppExecFwk::FormJsInfo & formJsInfo,const AAFwk::Want & want)26 int32_t FormRendererDelegateProxy::OnSurfaceCreate(const std::shared_ptr<Rosen::RSSurfaceNode>& surfaceNode,
27     const OHOS::AppExecFwk::FormJsInfo& formJsInfo, const AAFwk::Want& want)
28 {
29     MessageParcel data;
30     if (!WriteInterfaceToken(data)) {
31         HILOG_ERROR("%{public}s, failed to write interface token", __func__);
32         return ERR_INVALID_VALUE;
33     }
34     if (surfaceNode == nullptr) {
35         HILOG_ERROR("%{public}s fail, surfaceNode is nullptr", __func__);
36         return ERR_INVALID_VALUE;
37     }
38     if (!surfaceNode->Marshalling(data)) {
39         HILOG_ERROR("%{public}s fail, write surfaceNode error", __func__);
40         return ERR_INVALID_VALUE;
41     }
42     if (!data.WriteParcelable(&formJsInfo)) {
43         HILOG_ERROR("%{public}s fail, write formJsInfo error", __func__);
44         return ERR_INVALID_VALUE;
45     }
46     if (!data.WriteParcelable(&want)) {
47         HILOG_ERROR("%{public}s fail, write want error", __func__);
48         return ERR_INVALID_VALUE;
49     }
50     HILOG_INFO("Proxy create surfaceNode:%{public}s", std::to_string(surfaceNode->GetId()).c_str());
51 
52     MessageParcel reply;
53     MessageOption option;
54     auto remoteProxy = Remote();
55     if (!remoteProxy) {
56         HILOG_ERROR("Send surfaceNode failed, ipc remoteObj is null");
57         return IPC_PROXY_ERR;
58     }
59     int32_t error = remoteProxy->SendRequest(
60         static_cast<uint32_t>(IFormRendererDelegate::Message::ON_SURFACE_CREATE), data, reply, option);
61     if (error != NO_ERROR) {
62         HILOG_ERROR("%{public}s, failed to SendRequest: %{public}d", __func__, error);
63         return error;
64     }
65 
66     return reply.ReadInt32();
67 }
68 
OnSurfaceReuse(uint64_t surfaceId,const OHOS::AppExecFwk::FormJsInfo & formJsInfo,const AAFwk::Want & want)69 int32_t FormRendererDelegateProxy::OnSurfaceReuse(
70     uint64_t surfaceId, const OHOS::AppExecFwk::FormJsInfo& formJsInfo, const AAFwk::Want& want)
71 {
72     MessageParcel data;
73     if (!WriteInterfaceToken(data)) {
74         HILOG_ERROR("%{public}s, failed to write interface token", __func__);
75         return ERR_INVALID_VALUE;
76     }
77     data.WriteUint64(surfaceId);
78     if (!data.WriteParcelable(&formJsInfo)) {
79         HILOG_ERROR("%{public}s fail, write formJsInfo error", __func__);
80         return ERR_INVALID_VALUE;
81     }
82     if (!data.WriteParcelable(&want)) {
83         HILOG_ERROR("%{public}s fail, write want error", __func__);
84         return ERR_INVALID_VALUE;
85     }
86     HILOG_INFO("Proxy reuse surfaceNode:%{public}s", std::to_string(surfaceId).c_str());
87 
88     MessageParcel reply;
89     MessageOption option;
90     auto remoteProxy = Remote();
91     if (!remoteProxy) {
92         HILOG_ERROR("Send surfaceNode failed, ipc remoteObj is null");
93         return IPC_PROXY_ERR;
94     }
95     int32_t error = remoteProxy->SendRequest(
96         static_cast<uint32_t>(IFormRendererDelegate::Message::ON_SURFACE_REUSE), data, reply, option);
97     if (error != NO_ERROR) {
98         HILOG_ERROR("%{public}s, failed to SendRequest: %{public}d", __func__, error);
99         return error;
100     }
101 
102     return reply.ReadInt32();
103 }
104 
OnSurfaceDetach(uint64_t surfaceId)105 int32_t FormRendererDelegateProxy::OnSurfaceDetach(uint64_t surfaceId)
106 {
107     MessageParcel data;
108     if (!WriteInterfaceToken(data)) {
109         HILOG_ERROR("%{public}s, failed to write interface token", __func__);
110         return ERR_INVALID_VALUE;
111     }
112     data.WriteUint64(surfaceId);
113     HILOG_INFO("Proxy detach surfaceNode:%{public}s", std::to_string(surfaceId).c_str());
114 
115     MessageParcel reply;
116     MessageOption option;
117     auto remoteProxy = Remote();
118     if (!remoteProxy) {
119         HILOG_ERROR("Send surfaceNode failed, ipc remoteObj is null");
120         return IPC_PROXY_ERR;
121     }
122     int32_t error = remoteProxy->SendRequest(
123         static_cast<uint32_t>(IFormRendererDelegate::Message::ON_FORMSURFACE_DETACH), data, reply, option);
124     if (error != NO_ERROR) {
125         HILOG_ERROR("%{public}s, failed to SendRequest: %{public}d", __func__, error);
126         return error;
127     }
128 
129     return reply.ReadInt32();
130 }
131 
OnSurfaceRelease(uint64_t surfaceId)132 int32_t FormRendererDelegateProxy::OnSurfaceRelease(uint64_t surfaceId)
133 {
134     MessageParcel data;
135     if (!WriteInterfaceToken(data)) {
136         HILOG_ERROR("%{public}s, failed to write interface token", __func__);
137         return ERR_INVALID_VALUE;
138     }
139     data.WriteUint64(surfaceId);
140     HILOG_INFO("Proxy release surfaceNode:%{public}s", std::to_string(surfaceId).c_str());
141 
142     MessageParcel reply;
143     MessageOption option;
144     auto remoteProxy = Remote();
145     if (!remoteProxy) {
146         HILOG_ERROR("Send surfaceNode failed, ipc remoteObj is null");
147         return IPC_PROXY_ERR;
148     }
149     int32_t error = remoteProxy->SendRequest(
150         static_cast<uint32_t>(IFormRendererDelegate::Message::ON_SURFACE_RELEASE), data, reply, option);
151     if (error != NO_ERROR) {
152         HILOG_ERROR("%{public}s, failed to SendRequest: %{public}d", __func__, error);
153         return error;
154     }
155 
156     return reply.ReadInt32();
157 }
158 
OnActionEvent(const std::string & action)159 int32_t FormRendererDelegateProxy::OnActionEvent(const std::string& action)
160 {
161     MessageParcel data;
162     if (!WriteInterfaceToken(data)) {
163         HILOG_ERROR("%{public}s, failed to write interface token", __func__);
164         return ERR_INVALID_VALUE;
165     }
166 
167     if (!data.WriteString(action)) {
168         HILOG_ERROR("%{public}s, write action error", __func__);
169         return ERR_INVALID_VALUE;
170     }
171 
172     MessageParcel reply;
173     MessageOption option;
174     int32_t error = Remote()->SendRequest(
175         static_cast<uint32_t>(IFormRendererDelegate::Message::ON_ACTION_CREATE), data, reply, option);
176     if (error != NO_ERROR) {
177         HILOG_ERROR("%{public}s, failed to SendRequest: %{public}d", __func__, error);
178         return error;
179     }
180 
181     return reply.ReadInt32();
182 }
183 
OnError(const std::string & code,const std::string & msg)184 int32_t FormRendererDelegateProxy::OnError(const std::string& code, const std::string& msg)
185 {
186     MessageParcel data;
187     if (!WriteInterfaceToken(data)) {
188         HILOG_ERROR("%{public}s, failed to write interface token", __func__);
189         return ERR_INVALID_VALUE;
190     }
191 
192     if (!data.WriteString(code)) {
193         HILOG_ERROR("%{public}s, write code error", __func__);
194         return ERR_INVALID_VALUE;
195     }
196 
197     if (!data.WriteString(msg)) {
198         HILOG_ERROR("%{public}s, write msg error", __func__);
199         return ERR_INVALID_VALUE;
200     }
201 
202     MessageParcel reply;
203     MessageOption option;
204     int32_t error =
205         Remote()->SendRequest(static_cast<uint32_t>(IFormRendererDelegate::Message::ON_ERROR), data, reply, option);
206     if (error != NO_ERROR) {
207         HILOG_ERROR("%{public}s, failed to SendRequest: %{public}d", __func__, error);
208         return error;
209     }
210 
211     return reply.ReadInt32();
212 }
213 
OnSurfaceChange(float width,float height,float borderWidth)214 int32_t FormRendererDelegateProxy::OnSurfaceChange(float width, float height, float borderWidth)
215 {
216     MessageParcel data;
217     if (!WriteInterfaceToken(data)) {
218         HILOG_ERROR("%{public}s, failed to write interface token", __func__);
219         return ERR_INVALID_VALUE;
220     }
221 
222     if (!data.WriteFloat(width)) {
223         HILOG_ERROR("write width fail, action error");
224         return ERR_INVALID_VALUE;
225     }
226 
227     if (!data.WriteFloat(height)) {
228         HILOG_ERROR("write height fail, action error");
229         return ERR_INVALID_VALUE;
230     }
231 
232     if (!data.WriteFloat(borderWidth)) {
233         HILOG_ERROR("write borderWidth fail, action error");
234         return ERR_INVALID_VALUE;
235     }
236 
237     MessageParcel reply;
238     MessageOption option;
239     int error = Remote()->SendRequest(
240         static_cast<uint32_t>(IFormRendererDelegate::Message::ON_SURFACE_CHANGE), data, reply, option);
241     if (error != NO_ERROR) {
242         HILOG_ERROR("%{public}s, failed to SendRequest: %{public}d", __func__, error);
243     }
244     return reply.ReadInt32();
245 }
246 
OnFormLinkInfoUpdate(const std::vector<std::string> & formLinkInfos)247 int32_t FormRendererDelegateProxy::OnFormLinkInfoUpdate(const std::vector<std::string>& formLinkInfos)
248 {
249     MessageParcel data;
250     if (!WriteInterfaceToken(data)) {
251         HILOG_ERROR("%{public}s, failed to write interface token", __func__);
252         return ERR_INVALID_VALUE;
253     }
254 
255     if (!data.WriteStringVector(formLinkInfos)) {
256         HILOG_ERROR("%{public}s, write formLinkInfos error", __func__);
257         return ERR_INVALID_VALUE;
258     }
259 
260     MessageParcel reply;
261     MessageOption option;
262     int32_t error = Remote()->SendRequest(
263         static_cast<uint32_t>(IFormRendererDelegate::Message::ON_FORM_LINK_INFO_UPDATE), data, reply, option);
264     if (error != NO_ERROR) {
265         HILOG_ERROR("%{public}s, failed to SendRequest: %{public}d", __func__, error);
266         return error;
267     }
268 
269     return reply.ReadInt32();
270 }
271 
OnGetRectRelativeToWindow(int32_t & top,int32_t & left)272 int32_t FormRendererDelegateProxy::OnGetRectRelativeToWindow(int32_t &top, int32_t &left)
273 {
274     MessageParcel data;
275     if (!WriteInterfaceToken(data)) {
276         HILOG_ERROR("failed to write interface token");
277         return ERR_INVALID_VALUE;
278     }
279 
280     MessageParcel reply;
281     MessageOption option;
282     int error = Remote()->SendRequest(
283         static_cast<uint32_t>(IFormRendererDelegate::Message::ON_GET_RECT_RELATIVE_TO_WINDOW), data, reply, option);
284     if (error != NO_ERROR) {
285         HILOG_ERROR("failed to SendRequest: %{public}d", error);
286         return error;
287     }
288 
289     int32_t errCode = reply.ReadInt32();
290     if (errCode != ERR_OK) {
291         HILOG_ERROR("return errCode: %{public}d", errCode);
292         return errCode;
293     }
294     reply.ReadInt32(top);
295     reply.ReadInt32(left);
296     return ERR_OK;
297 }
298 
WriteInterfaceToken(MessageParcel & data)299 bool FormRendererDelegateProxy::WriteInterfaceToken(MessageParcel& data)
300 {
301     if (!data.WriteInterfaceToken(FormRendererDelegateProxy::GetDescriptor())) {
302         HILOG_ERROR("%{public}s, failed to write interface token", __func__);
303         return false;
304     }
305     return true;
306 }
307 } // namespace Ace
308 } // namespace OHOS
309