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 "form_mgr_errors.h"
20 #include "hilog_wrapper.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 }
42
~FormRenderStub()43 FormRenderStub::~FormRenderStub()
44 {
45 memberFuncMap_.clear();
46 }
47
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)48 int FormRenderStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
49 {
50 HILOG_INFO("FormRenderStub::OnReceived, code = %{public}u, flags= %{public}d.", code, option.GetFlags());
51 std::u16string descriptor = FormRenderStub::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_APPEXECFWK_FORM_INVALID_PARAM;
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
HandleRenderForm(MessageParcel & data,MessageParcel & reply)69 int FormRenderStub::HandleRenderForm(MessageParcel &data, MessageParcel &reply)
70 {
71 std::unique_ptr<FormJsInfo> formJsInfo(data.ReadParcelable<FormJsInfo>());
72 if (!formJsInfo) {
73 HILOG_ERROR("%{public}s, failed to ReadParcelable<formJsInfo>", __func__);
74 return ERR_APPEXECFWK_PARCEL_ERROR;
75 }
76 std::unique_ptr<Want> want(data.ReadParcelable<Want>());
77 if (!want) {
78 HILOG_ERROR("%{public}s, failed to ReadParcelable<Want>", __func__);
79 return ERR_APPEXECFWK_PARCEL_ERROR;
80 }
81
82 sptr<IRemoteObject> client = data.ReadRemoteObject();
83 if (client == nullptr) {
84 HILOG_ERROR("%{public}s, failed to get remote object.", __func__);
85 return ERR_APPEXECFWK_PARCEL_ERROR;
86 }
87
88 int32_t result = RenderForm(*formJsInfo, *want, client);
89 reply.WriteInt32(result);
90 return result;
91 }
92
HandleStopRenderingForm(MessageParcel & data,MessageParcel & reply)93 int FormRenderStub::HandleStopRenderingForm(MessageParcel &data, MessageParcel &reply)
94 {
95 std::unique_ptr<FormJsInfo> formJsInfo(data.ReadParcelable<FormJsInfo>());
96 if (!formJsInfo) {
97 HILOG_ERROR("%{public}s, failed to ReadParcelable<formJsInfo>", __func__);
98 return ERR_APPEXECFWK_PARCEL_ERROR;
99 }
100 std::unique_ptr<Want> want(data.ReadParcelable<Want>());
101 if (!want) {
102 HILOG_ERROR("%{public}s, failed to ReadParcelable<Want>", __func__);
103 return ERR_APPEXECFWK_PARCEL_ERROR;
104 }
105
106 sptr<IRemoteObject> client = data.ReadRemoteObject();
107 if (client == nullptr) {
108 HILOG_ERROR("%{public}s, failed to get remote object.", __func__);
109 return ERR_APPEXECFWK_PARCEL_ERROR;
110 }
111
112 int32_t result = StopRenderingForm(*formJsInfo, *want, client);
113 reply.WriteInt32(result);
114 return result;
115 }
116
HandleCleanFormHost(MessageParcel & data,MessageParcel & reply)117 int FormRenderStub::HandleCleanFormHost(MessageParcel &data, MessageParcel &reply)
118 {
119 sptr<IRemoteObject> hostToken = data.ReadRemoteObject();
120 if (hostToken == nullptr) {
121 HILOG_ERROR("hostToken is nullptr.");
122 return ERR_APPEXECFWK_PARCEL_ERROR;
123 }
124
125 int32_t result = CleanFormHost(hostToken);
126 reply.WriteInt32(result);
127 return result;
128 }
129
HandleReloadForm(MessageParcel & data,MessageParcel & reply)130 int FormRenderStub::HandleReloadForm(MessageParcel &data, MessageParcel &reply)
131 {
132 int32_t size = data.ReadInt32();
133 if (size < 0 || size >= MAX_ALLOW_SIZE) {
134 HILOG_ERROR("%{public}s, invalid size: %{public}d", __func__, size);
135 return ERR_APPEXECFWK_PARCEL_ERROR;
136 }
137 std::vector<int64_t> formIds;
138 data.ReadInt64Vector(&formIds);
139 std::unique_ptr<Want> want(data.ReadParcelable<Want>());
140 if (!want) {
141 HILOG_ERROR("%{public}s, failed to ReadParcelable<Want>", __func__);
142 return ERR_APPEXECFWK_PARCEL_ERROR;
143 }
144 int32_t result = ReloadForm(std::move(formIds), *want);
145 reply.WriteInt32(result);
146 return result;
147 }
148 } // namespace AppExecFwk
149 } // namespace OHOS
150