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_stub.h"
16
17 #include "form_renderer_hilog.h"
18
19 namespace OHOS {
20 namespace Ace {
21 static std::mutex g_surfaceNodeMutex_;
22 static std::map<uint64_t, std::shared_ptr<Rosen::RSSurfaceNode>> g_surfaceNodeMap_;
23
FormRendererDelegateStub()24 FormRendererDelegateStub::FormRendererDelegateStub()
25 {
26 memberFuncMap_[static_cast<uint32_t>(IFormRendererDelegate::Message::ON_SURFACE_CREATE)] =
27 &FormRendererDelegateStub::HandleOnSurfaceCreate;
28 memberFuncMap_[static_cast<uint32_t>(IFormRendererDelegate::Message::ON_SURFACE_REUSE)] =
29 &FormRendererDelegateStub::HandleOnSurfaceReuse;
30 memberFuncMap_[static_cast<uint32_t>(IFormRendererDelegate::Message::ON_SURFACE_RELEASE)] =
31 &FormRendererDelegateStub::HandleOnSurfaceRelease;
32 memberFuncMap_[static_cast<uint32_t>(IFormRendererDelegate::Message::ON_ACTION_CREATE)] =
33 &FormRendererDelegateStub::HandleOnActionEvent;
34 memberFuncMap_[static_cast<uint32_t>(IFormRendererDelegate::Message::ON_ERROR)] =
35 &FormRendererDelegateStub::HandleOnError;
36 memberFuncMap_[static_cast<uint32_t>(IFormRendererDelegate::Message::ON_SURFACE_CHANGE)] =
37 &FormRendererDelegateStub::HandleOnSurfaceChange;
38 memberFuncMap_[static_cast<uint32_t>(IFormRendererDelegate::Message::ON_FORM_LINK_INFO_UPDATE)] =
39 &FormRendererDelegateStub::HandleOnFormLinkInfoUpdate;
40 memberFuncMap_[static_cast<uint32_t>(IFormRendererDelegate::Message::ON_FORMSURFACE_DETACH)] =
41 &FormRendererDelegateStub::HandleOnSurfaceDetach;
42 memberFuncMap_[static_cast<uint32_t>(IFormRendererDelegate::Message::ON_GET_RECT_RELATIVE_TO_WINDOW)] =
43 &FormRendererDelegateStub::HandleOnGetRectRelativeToWindow;
44 memberFuncMap_[static_cast<uint32_t>(IFormRendererDelegate::Message::ON_CHECK_MANAGER_DELEGATE)] =
45 &FormRendererDelegateStub::HandleOnCheckManagerDelegate;
46 }
47
~FormRendererDelegateStub()48 FormRendererDelegateStub::~FormRendererDelegateStub()
49 {
50 memberFuncMap_.clear();
51 }
52
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)53 int FormRendererDelegateStub::OnRemoteRequest(
54 uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)
55 {
56 HILOG_DEBUG("FormRendererDelegateStub::OnReceived, code = %{public}u, flags= %{public}d.", code, option.GetFlags());
57 std::u16string descriptor = FormRendererDelegateStub::GetDescriptor();
58 std::u16string remoteDescriptor = data.ReadInterfaceToken();
59 if (descriptor != remoteDescriptor) {
60 HILOG_ERROR("%{public}s failed, local descriptor is not equal to remote", __func__);
61 return ERR_INVALID_VALUE;
62 }
63
64 auto itFunc = memberFuncMap_.find(code);
65 if (itFunc != memberFuncMap_.end()) {
66 auto memberFunc = itFunc->second;
67 if (memberFunc != nullptr) {
68 return (this->*memberFunc)(data, reply);
69 }
70 }
71
72 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
73 }
74
HandleOnSurfaceCreate(MessageParcel & data,MessageParcel & reply)75 int FormRendererDelegateStub::HandleOnSurfaceCreate(MessageParcel& data, MessageParcel& reply)
76 {
77 auto surfaceNode = Rosen::RSSurfaceNode::Unmarshalling(data);
78 if (surfaceNode == nullptr) {
79 HILOG_ERROR("surfaceNode is nullptr");
80 return ERR_INVALID_VALUE;
81 }
82 {
83 std::lock_guard<std::mutex> lock(g_surfaceNodeMutex_);
84 g_surfaceNodeMap_[surfaceNode->GetId()] = surfaceNode;
85 }
86 HILOG_INFO("Stub create surfaceNode:%{public}s", std::to_string(surfaceNode->GetId()).c_str());
87 std::unique_ptr<AppExecFwk::FormJsInfo> formJsInfo(data.ReadParcelable<AppExecFwk::FormJsInfo>());
88 if (formJsInfo == nullptr) {
89 HILOG_ERROR("formJsInfo is nullptr");
90 return ERR_INVALID_VALUE;
91 }
92
93 std::shared_ptr<AAFwk::Want> want(data.ReadParcelable<AAFwk::Want>());
94 if (want == nullptr) {
95 HILOG_ERROR("want is nullptr");
96 return ERR_INVALID_VALUE;
97 }
98
99 int32_t errCode = OnSurfaceCreate(surfaceNode, *formJsInfo, *want);
100 reply.WriteInt32(errCode);
101 return ERR_OK;
102 }
103
HandleOnSurfaceReuse(MessageParcel & data,MessageParcel & reply)104 int32_t FormRendererDelegateStub::HandleOnSurfaceReuse(MessageParcel& data, MessageParcel& reply)
105 {
106 uint64_t id = UINT64_MAX;
107 data.ReadUint64(id);
108 std::shared_ptr<Rosen::RSSurfaceNode> surfaceNode;
109 {
110 std::lock_guard<std::mutex> lock(g_surfaceNodeMutex_);
111 auto iter = g_surfaceNodeMap_.find(id);
112 if (iter != g_surfaceNodeMap_.end()) {
113 surfaceNode = iter->second;
114 }
115 }
116 if (surfaceNode == nullptr) {
117 HILOG_ERROR("surfaceNode:%{public}s is nullptr", std::to_string(id).c_str());
118 return ERR_INVALID_VALUE;
119 }
120
121 HILOG_INFO("Stub reuse surfaceNode:%{public}s", std::to_string(id).c_str());
122 std::unique_ptr<AppExecFwk::FormJsInfo> formJsInfo(data.ReadParcelable<AppExecFwk::FormJsInfo>());
123 if (formJsInfo == nullptr) {
124 HILOG_ERROR("formJsInfo is nullptr");
125 return ERR_INVALID_VALUE;
126 }
127
128 std::shared_ptr<AAFwk::Want> want(data.ReadParcelable<AAFwk::Want>());
129 if (want == nullptr) {
130 HILOG_ERROR("want is nullptr");
131 return ERR_INVALID_VALUE;
132 }
133
134 int32_t errCode = OnSurfaceCreate(surfaceNode, *formJsInfo, *want);
135 reply.WriteInt32(errCode);
136 return ERR_OK;
137 }
138
HandleOnSurfaceDetach(MessageParcel & data,MessageParcel & reply)139 int32_t FormRendererDelegateStub::HandleOnSurfaceDetach(MessageParcel& data, MessageParcel& reply)
140 {
141 uint64_t id = UINT64_MAX;
142 data.ReadUint64(id);
143
144 HILOG_INFO("Stub detach surfaceNode:%{public}s", std::to_string(id).c_str());
145
146 int32_t errCode = OnSurfaceDetach(id);
147 reply.WriteInt32(errCode);
148 return ERR_OK;
149 }
150
HandleOnSurfaceRelease(MessageParcel & data,MessageParcel & reply)151 int32_t FormRendererDelegateStub::HandleOnSurfaceRelease(MessageParcel& data, MessageParcel& reply)
152 {
153 uint64_t id = UINT64_MAX;
154 data.ReadUint64(id);
155 HILOG_INFO("Stub release surfaceNode:%{public}s start", std::to_string(id).c_str());
156 {
157 std::lock_guard<std::mutex> lock(g_surfaceNodeMutex_);
158 auto iter = g_surfaceNodeMap_.find(id);
159 if (iter != g_surfaceNodeMap_.end()) {
160 HILOG_INFO("Stub release surfaceNode:%{public}s finish", std::to_string(id).c_str());
161 g_surfaceNodeMap_.erase(iter);
162 }
163 }
164 reply.WriteInt32(ERR_OK);
165 return ERR_OK;
166 }
167
HandleOnActionEvent(MessageParcel & data,MessageParcel & reply)168 int FormRendererDelegateStub::HandleOnActionEvent(MessageParcel& data, MessageParcel& reply)
169 {
170 std::string action = data.ReadString();
171 int32_t errCode = OnActionEvent(action);
172 reply.WriteInt32(errCode);
173 return ERR_OK;
174 }
175
HandleOnError(MessageParcel & data,MessageParcel & reply)176 int32_t FormRendererDelegateStub::HandleOnError(MessageParcel& data, MessageParcel& reply)
177 {
178 std::string code = data.ReadString();
179 std::string msg = data.ReadString();
180 int32_t errCode = OnError(code, msg);
181 reply.WriteInt32(errCode);
182 return ERR_OK;
183 }
184
HandleOnSurfaceChange(MessageParcel & data,MessageParcel & reply)185 int32_t FormRendererDelegateStub::HandleOnSurfaceChange(MessageParcel& data, MessageParcel& reply)
186 {
187 float width = data.ReadFloat();
188 float height = data.ReadFloat();
189 float borderWidth = data.ReadFloat();
190
191 OnSurfaceChange(width, height, borderWidth);
192 reply.WriteInt32(ERR_OK);
193 return ERR_OK;
194 }
195
HandleOnFormLinkInfoUpdate(MessageParcel & data,MessageParcel & reply)196 int32_t FormRendererDelegateStub::HandleOnFormLinkInfoUpdate(MessageParcel& data, MessageParcel& reply)
197 {
198 std::vector<std::string> formLinkInfos;
199 data.ReadStringVector(&formLinkInfos);
200 int32_t errCode = OnFormLinkInfoUpdate(formLinkInfos);
201 reply.WriteInt32(errCode);
202 return ERR_OK;
203 }
204
HandleOnGetRectRelativeToWindow(MessageParcel & data,MessageParcel & reply)205 int32_t FormRendererDelegateStub::HandleOnGetRectRelativeToWindow(MessageParcel& data, MessageParcel& reply)
206 {
207 int32_t top = 0;
208 int32_t left = 0;
209 int32_t errCode = OnGetRectRelativeToWindow(top, left);
210 reply.WriteInt32(errCode);
211 reply.WriteInt32(top);
212 reply.WriteInt32(left);
213 return ERR_OK;
214 }
215
HandleOnCheckManagerDelegate(MessageParcel & data,MessageParcel & reply)216 int32_t FormRendererDelegateStub::HandleOnCheckManagerDelegate(MessageParcel& data, MessageParcel& reply)
217 {
218 bool checkFlag = true;
219 int32_t errCode = OnCheckManagerDelegate(checkFlag);
220 reply.WriteInt32(errCode);
221 reply.WriteBool(checkFlag);
222 return ERR_OK;
223 }
224 } // namespace Ace
225 } // namespace OHOS
226