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