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_impl.h"
16 
17 #include "errors.h"
18 #include "form_renderer_hilog.h"
19 
20 namespace OHOS {
21 namespace Ace {
OnSurfaceCreate(const std::shared_ptr<Rosen::RSSurfaceNode> & surfaceNode,const OHOS::AppExecFwk::FormJsInfo & formJsInfo,const AAFwk::Want & want)22 int32_t FormRendererDelegateImpl::OnSurfaceCreate(const std::shared_ptr<Rosen::RSSurfaceNode>& surfaceNode,
23     const OHOS::AppExecFwk::FormJsInfo& formJsInfo, const AAFwk::Want& want)
24 {
25     HILOG_DEBUG("%{public}s called.", __func__);
26     if (!surfaceNode) {
27         HILOG_ERROR("surface is invalid");
28         return ERR_NULL_OBJECT;
29     }
30     int64_t formId = formJsInfo.formId;
31     if (formId < 0) {
32         HILOG_ERROR("%{public}s error, the passed form id can't be negative.", __func__);
33         return ERR_INVALID_DATA;
34     }
35 
36     if (!surfaceCreateEventHandler_) {
37         HILOG_ERROR("surfaceCreateEventHandler_ is null");
38         return ERR_INVALID_DATA;
39     }
40     surfaceCreateEventHandler_(surfaceNode, formJsInfo, want);
41     return ERR_OK;
42 }
43 
OnActionEvent(const std::string & action)44 int32_t FormRendererDelegateImpl::OnActionEvent(const std::string& action)
45 {
46     HILOG_INFO("OnActionEvent %{public}zu", action.length());
47     if (!actionEventHandler_) {
48         HILOG_ERROR("actionEventHandler_ is null,  %{public}zu", action.length());
49         return ERR_INVALID_DATA;
50     }
51 
52     actionEventHandler_(action);
53     return ERR_OK;
54 }
55 
OnError(const std::string & code,const std::string & msg)56 int32_t FormRendererDelegateImpl::OnError(const std::string& code, const std::string& msg)
57 {
58     HILOG_INFO("OnError code: %{public}s, msg: %{public}s", code.c_str(), msg.c_str());
59     if (!errorEventHandler_) {
60         HILOG_ERROR("errorEventHandler_ is null");
61         return ERR_INVALID_DATA;
62     }
63 
64     errorEventHandler_(code, msg);
65     return ERR_OK;
66 }
67 
OnSurfaceChange(float width,float height,float borderWidth)68 int32_t FormRendererDelegateImpl::OnSurfaceChange(float width, float height, float borderWidth)
69 {
70     HILOG_DEBUG("%{public}s called.", __func__);
71     if (!surfaceChangeEventHandler_) {
72         HILOG_ERROR("surfaceChangeEventHandler_ is null");
73         return ERR_INVALID_DATA;
74     }
75     surfaceChangeEventHandler_(width, height, borderWidth);
76     return ERR_OK;
77 }
78 
OnSurfaceDetach(uint64_t surfaceId)79 int32_t FormRendererDelegateImpl::OnSurfaceDetach(uint64_t surfaceId)
80 {
81     HILOG_DEBUG("%{public}s called.", __func__);
82     if (!surfaceDetachEventHandler_) {
83         HILOG_ERROR("surfaceDetachEventHandler_ is null");
84         return ERR_INVALID_DATA;
85     }
86     surfaceDetachEventHandler_();
87     return ERR_OK;
88 }
89 
OnFormLinkInfoUpdate(const std::vector<std::string> & formLinkInfos)90 int32_t FormRendererDelegateImpl::OnFormLinkInfoUpdate(const std::vector<std::string>& formLinkInfos)
91 {
92     HILOG_DEBUG("%{public}s called.", __func__);
93     if (!formLinkInfoUpdateHandler_) {
94         HILOG_ERROR("formLinkInfoUpdateHandler_ is null");
95         return ERR_INVALID_DATA;
96     }
97     formLinkInfoUpdateHandler_(formLinkInfos);
98     return ERR_OK;
99 }
100 
OnGetRectRelativeToWindow(int32_t & top,int32_t & left)101 int32_t FormRendererDelegateImpl::OnGetRectRelativeToWindow(int32_t &top, int32_t &left)
102 {
103     HILOG_DEBUG("%{public}s called.", __func__);
104     if (!getRectRelativeToWindowHandler_) {
105         HILOG_ERROR("getRectRelativeToWindowHandler_ is null");
106         return ERR_INVALID_DATA;
107     }
108     getRectRelativeToWindowHandler_(top, left);
109     return ERR_OK;
110 }
111 
SetSurfaceCreateEventHandler(std::function<void (const std::shared_ptr<Rosen::RSSurfaceNode> &,const OHOS::AppExecFwk::FormJsInfo &,const AAFwk::Want &)> && listener)112 void FormRendererDelegateImpl::SetSurfaceCreateEventHandler(
113     std::function<void(const std::shared_ptr<Rosen::RSSurfaceNode>&, const OHOS::AppExecFwk::FormJsInfo&,
114         const AAFwk::Want&)>&& listener)
115 {
116     surfaceCreateEventHandler_ = std::move(listener);
117 }
118 
SetActionEventHandler(std::function<void (const std::string &)> && listener)119 void FormRendererDelegateImpl::SetActionEventHandler(std::function<void(const std::string&)>&& listener)
120 {
121     HILOG_INFO("EventHandle - SetActionEventHandler");
122     actionEventHandler_ = std::move(listener);
123 }
124 
SetErrorEventHandler(std::function<void (const std::string &,const std::string &)> && listener)125 void FormRendererDelegateImpl::SetErrorEventHandler(
126     std::function<void(const std::string&, const std::string&)>&& listener)
127 {
128     errorEventHandler_ = std::move(listener);
129 }
130 
SetSurfaceChangeEventHandler(std::function<void (float width,float height,float borderWidth)> && listener)131 void FormRendererDelegateImpl::SetSurfaceChangeEventHandler(
132     std::function<void(float width, float height, float borderWidth)>&& listener)
133 {
134     surfaceChangeEventHandler_ = std::move(listener);
135 }
136 
SetSurfaceDetachEventHandler(std::function<void ()> && listener)137 void FormRendererDelegateImpl::SetSurfaceDetachEventHandler(std::function<void()>&& listener)
138 {
139     surfaceDetachEventHandler_ = std::move(listener);
140 }
141 
SetFormLinkInfoUpdateHandler(std::function<void (const std::vector<std::string> &)> && listener)142 void FormRendererDelegateImpl::SetFormLinkInfoUpdateHandler(
143     std::function<void(const std::vector<std::string>&)>&& listener)
144 {
145     formLinkInfoUpdateHandler_ = std::move(listener);
146 }
147 
SetGetRectRelativeToWindowHandler(std::function<void (int32_t &,int32_t &)> && listener)148 void FormRendererDelegateImpl::SetGetRectRelativeToWindowHandler(std::function<void(int32_t&, int32_t&)>&& listener)
149 {
150     getRectRelativeToWindowHandler_ = std::move(listener);
151 }
152 } // namespace Ace
153 } // namespace OHOS
154