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_render_stub.h"
16 #include "appexecfwk_errors.h"
17 #include "app_scheduler_interface.h"
18 #include "errors.h"
19 #include "fms_log_wrapper.h"
20 #include "form_mgr_errors.h"
21 #include "ipc_skeleton.h"
22 #include "ipc_types.h"
23 #include "iremote_object.h"
24
25 namespace OHOS {
26 namespace AppExecFwk {
27 namespace {
28 constexpr int32_t MAX_ALLOW_SIZE = 8 * 1024;
29 }
30
FormRenderStub()31 FormRenderStub::FormRenderStub()
32 {
33 memberFuncMap_[static_cast<uint32_t>(IFormRender::Message::FORM_RENDER_RENDER_FORM)] =
34 &FormRenderStub::HandleRenderForm;
35 memberFuncMap_[static_cast<uint32_t>(IFormRender::Message::FORM_RENDER_STOP_RENDERING_FORM)] =
36 &FormRenderStub::HandleStopRenderingForm;
37 memberFuncMap_[static_cast<uint32_t>(IFormRender::Message::FORM_RENDER_FORM_HOST_DIED)] =
38 &FormRenderStub::HandleCleanFormHost;
39 memberFuncMap_[static_cast<uint32_t>(IFormRender::Message::FORM_RENDER_RELOAD_FORM)] =
40 &FormRenderStub::HandleReloadForm;
41 memberFuncMap_[static_cast<uint32_t>(IFormRender::Message::FORM_RENDER_RELEASE_RENDERER)] =
42 &FormRenderStub::HandleReleaseRenderer;
43 memberFuncMap_[static_cast<uint32_t>(IFormRender::Message::FORM_RENDER_UNLOCKED)] =
44 &FormRenderStub::HandleOnUnlock;
45 }
46
~FormRenderStub()47 FormRenderStub::~FormRenderStub()
48 {
49 memberFuncMap_.clear();
50 }
51
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)52 int FormRenderStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
53 {
54 HILOG_INFO("FormRenderStub::OnReceived, code = %{public}u, flags= %{public}d.", code, option.GetFlags());
55 std::u16string descriptor = FormRenderStub::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_APPEXECFWK_FORM_INVALID_PARAM;
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
HandleRenderForm(MessageParcel & data,MessageParcel & reply)73 int FormRenderStub::HandleRenderForm(MessageParcel &data, MessageParcel &reply)
74 {
75 std::unique_ptr<FormJsInfo> formJsInfo(data.ReadParcelable<FormJsInfo>());
76 if (!formJsInfo) {
77 HILOG_ERROR("%{public}s, failed to ReadParcelable<formJsInfo>", __func__);
78 return ERR_APPEXECFWK_PARCEL_ERROR;
79 }
80 std::unique_ptr<Want> want(data.ReadParcelable<Want>());
81 if (!want) {
82 HILOG_ERROR("%{public}s, failed to ReadParcelable<Want>", __func__);
83 return ERR_APPEXECFWK_PARCEL_ERROR;
84 }
85
86 sptr<IRemoteObject> client = data.ReadRemoteObject();
87 if (client == nullptr) {
88 HILOG_ERROR("%{public}s, failed to get remote object.", __func__);
89 return ERR_APPEXECFWK_PARCEL_ERROR;
90 }
91
92 int32_t result = RenderForm(*formJsInfo, *want, client);
93 reply.WriteInt32(result);
94 return result;
95 }
96
HandleStopRenderingForm(MessageParcel & data,MessageParcel & reply)97 int FormRenderStub::HandleStopRenderingForm(MessageParcel &data, MessageParcel &reply)
98 {
99 std::unique_ptr<FormJsInfo> formJsInfo(data.ReadParcelable<FormJsInfo>());
100 if (!formJsInfo) {
101 HILOG_ERROR("%{public}s, failed to ReadParcelable<formJsInfo>", __func__);
102 return ERR_APPEXECFWK_PARCEL_ERROR;
103 }
104 std::unique_ptr<Want> want(data.ReadParcelable<Want>());
105 if (!want) {
106 HILOG_ERROR("%{public}s, failed to ReadParcelable<Want>", __func__);
107 return ERR_APPEXECFWK_PARCEL_ERROR;
108 }
109
110 sptr<IRemoteObject> client = data.ReadRemoteObject();
111 if (client == nullptr) {
112 HILOG_ERROR("%{public}s, failed to get remote object.", __func__);
113 return ERR_APPEXECFWK_PARCEL_ERROR;
114 }
115
116 int32_t result = StopRenderingForm(*formJsInfo, *want, client);
117 reply.WriteInt32(result);
118 return result;
119 }
120
HandleCleanFormHost(MessageParcel & data,MessageParcel & reply)121 int FormRenderStub::HandleCleanFormHost(MessageParcel &data, MessageParcel &reply)
122 {
123 sptr<IRemoteObject> hostToken = data.ReadRemoteObject();
124 if (hostToken == nullptr) {
125 HILOG_ERROR("hostToken is nullptr.");
126 return ERR_APPEXECFWK_PARCEL_ERROR;
127 }
128
129 int32_t result = CleanFormHost(hostToken);
130 reply.WriteInt32(result);
131 return result;
132 }
133
HandleReleaseRenderer(MessageParcel & data,MessageParcel & reply)134 int32_t FormRenderStub::HandleReleaseRenderer(MessageParcel &data, MessageParcel &reply)
135 {
136 int64_t formId = data.ReadInt64();
137 std::string compId = data.ReadString();
138 std::string uid = data.ReadString();
139 int32_t result = ReleaseRenderer(formId, compId, uid);
140 reply.WriteInt32(result);
141 return result;
142 }
143
HandleReloadForm(MessageParcel & data,MessageParcel & reply)144 int FormRenderStub::HandleReloadForm(MessageParcel &data, MessageParcel &reply)
145 {
146 std::vector<FormJsInfo> formJsInfos;
147 int32_t result = GetParcelableInfos(data, formJsInfos);
148 if (result != ERR_OK) {
149 HILOG_ERROR("%{public}s, failed to GetParcelableInfos<FormJsInfo>", __func__);
150 return result;
151 }
152
153 std::unique_ptr<Want> want(data.ReadParcelable<Want>());
154 if (!want) {
155 HILOG_ERROR("%{public}s, failed to ReadParcelable<Want>", __func__);
156 return ERR_APPEXECFWK_PARCEL_ERROR;
157 }
158
159 result = ReloadForm(std::move(formJsInfos), *want);
160 reply.WriteInt32(result);
161 return result;
162 }
163
HandleOnUnlock(MessageParcel & data,MessageParcel & reply)164 int32_t FormRenderStub::HandleOnUnlock(MessageParcel &data, MessageParcel &reply)
165 {
166 int32_t result = OnUnlock();
167 reply.WriteInt32(result);
168 return result;
169 }
170
171 template<typename T>
GetParcelableInfos(MessageParcel & reply,std::vector<T> & parcelableInfos)172 int32_t FormRenderStub::GetParcelableInfos(MessageParcel &reply, std::vector<T> &parcelableInfos)
173 {
174 int32_t infoSize = reply.ReadInt32();
175 if (infoSize < 0 || infoSize > MAX_ALLOW_SIZE) {
176 HILOG_ERROR("%{public}s invalid size: %{public}d", __func__, infoSize);
177 return ERR_APPEXECFWK_PARCEL_ERROR;
178 }
179
180 for (int32_t i = 0; i < infoSize; i++) {
181 std::unique_ptr<T> info(reply.ReadParcelable<T>());
182 if (!info) {
183 HILOG_ERROR("%{public}s, failed to Read Parcelable infos", __func__);
184 return ERR_APPEXECFWK_PARCEL_ERROR;
185 }
186 parcelableInfos.emplace_back(*info);
187 }
188 return ERR_OK;
189 }
190 } // namespace AppExecFwk
191 } // namespace OHOS
192