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}s", action.c_str());
47 if (!actionEventHandler_) {
48 HILOG_ERROR("actionEventHandler_ is null, %{public}s", action.c_str());
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)68 int32_t FormRendererDelegateImpl::OnSurfaceChange(float width, float height)
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);
76 return ERR_OK;
77 }
78
OnFormLinkInfoUpdate(const std::vector<std::string> & formLinkInfos)79 int32_t FormRendererDelegateImpl::OnFormLinkInfoUpdate(const std::vector<std::string>& formLinkInfos)
80 {
81 HILOG_DEBUG("%{public}s called.", __func__);
82 if (!formLinkInfoUpdateHandler_) {
83 HILOG_ERROR("formLinkInfoUpdateHandler_ is null");
84 return ERR_INVALID_DATA;
85 }
86 formLinkInfoUpdateHandler_(formLinkInfos);
87 return ERR_OK;
88 }
89
SetSurfaceCreateEventHandler(std::function<void (const std::shared_ptr<Rosen::RSSurfaceNode> &,const OHOS::AppExecFwk::FormJsInfo &,const AAFwk::Want &)> && listener)90 void FormRendererDelegateImpl::SetSurfaceCreateEventHandler(
91 std::function<void(const std::shared_ptr<Rosen::RSSurfaceNode>&, const OHOS::AppExecFwk::FormJsInfo&,
92 const AAFwk::Want&)>&& listener)
93 {
94 surfaceCreateEventHandler_ = std::move(listener);
95 }
96
SetActionEventHandler(std::function<void (const std::string &)> && listener)97 void FormRendererDelegateImpl::SetActionEventHandler(std::function<void(const std::string&)>&& listener)
98 {
99 actionEventHandler_ = std::move(listener);
100 }
101
SetErrorEventHandler(std::function<void (const std::string &,const std::string &)> && listener)102 void FormRendererDelegateImpl::SetErrorEventHandler(
103 std::function<void(const std::string&, const std::string&)>&& listener)
104 {
105 errorEventHandler_ = std::move(listener);
106 }
107
SetSurfaceChangeEventHandler(std::function<void (float width,float height)> && listener)108 void FormRendererDelegateImpl::SetSurfaceChangeEventHandler(std::function<void(float width, float height)>&& listener)
109 {
110 surfaceChangeEventHandler_ = std::move(listener);
111 }
112
SetFormLinkInfoUpdateHandler(std::function<void (const std::vector<std::string> &)> && listener)113 void FormRendererDelegateImpl::SetFormLinkInfoUpdateHandler(
114 std::function<void(const std::vector<std::string>&)>&& listener)
115 {
116 formLinkInfoUpdateHandler_ = std::move(listener);
117 }
118 } // namespace Ace
119 } // namespace OHOS
120