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