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 
16 #include "js_form_state_observer_stub.h"
17 
18 #include "appexecfwk_errors.h"
19 #include "fms_log_wrapper.h"
20 #include "form_mgr_errors.h"
21 #include "ipc_types.h"
22 #include "iremote_object.h"
23 
24 namespace {
25 static constexpr int32_t MAX_ALLOW_SIZE = 8 * 1024;
26 }
27 namespace OHOS {
28 namespace AbilityRuntime {
JsFormStateObserverStub()29 JsFormStateObserverStub::JsFormStateObserverStub()
30 {}
31 
~JsFormStateObserverStub()32 JsFormStateObserverStub::~JsFormStateObserverStub()
33 {}
34 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)35 int32_t JsFormStateObserverStub::OnRemoteRequest(uint32_t code, MessageParcel &data,
36     MessageParcel &reply, MessageOption &option)
37 {
38     HILOG_DEBUG("JsFormStateObserverStub::OnReceived,code=%{public}u,flags=%{public}d", code, option.GetFlags());
39     std::u16string descriptor = JsFormStateObserverStub::GetDescriptor();
40     std::u16string remoteDescriptor = data.ReadInterfaceToken();
41     if (descriptor != remoteDescriptor) {
42         HILOG_ERROR("local descriptor not equal to remote");
43         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
44     }
45 
46     switch (code) {
47         case static_cast<uint32_t>(IJsFormStateObserver::Message::FORM_STATE_OBSERVER_ON_ADD_FORM):
48             return HandleOnAddForm(data, reply);
49         case static_cast<uint32_t>(IJsFormStateObserver::Message::FORM_STATE_OBSERVER_ON_REMOVE_FORM):
50             return HandleOnRemoveForm(data, reply);
51         case static_cast<uint32_t>(IJsFormStateObserver::Message::FORM_STATE_OBSERVER_NOTIFY_WHETHER_FORMS_VISIBLE):
52             return HandleNotifyWhetherFormsVisible(data, reply);
53         case static_cast<uint32_t>(IJsFormStateObserver::Message::FORM_STATE_OBSERVER_ON_FORM_CLICK):
54             return HandleOnFormClick(data, reply);
55         default:
56             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
57     }
58 }
59 
HandleOnAddForm(MessageParcel & data,MessageParcel & reply)60 int32_t JsFormStateObserverStub::HandleOnAddForm(MessageParcel &data, MessageParcel &reply)
61 {
62     HILOG_DEBUG("call");
63     std::string bundleName = data.ReadString();
64     std::unique_ptr<AppExecFwk::RunningFormInfo> runningFormInfo(data.ReadParcelable<AppExecFwk::RunningFormInfo>());
65     if (!runningFormInfo) {
66         HILOG_ERROR("ReadParcelable<RunningFormInfo> failed");
67         return ERR_APPEXECFWK_PARCEL_ERROR;
68     }
69     int32_t result = OnAddForm(bundleName, *runningFormInfo);
70     reply.WriteInt32(result);
71     return result;
72 }
73 
HandleOnRemoveForm(MessageParcel & data,MessageParcel & reply)74 int32_t JsFormStateObserverStub::HandleOnRemoveForm(MessageParcel &data, MessageParcel &reply)
75 {
76     HILOG_DEBUG("call");
77     std::string bundleName = data.ReadString();
78     std::unique_ptr<AppExecFwk::RunningFormInfo> runningFormInfo(data.ReadParcelable<AppExecFwk::RunningFormInfo>());
79     if (!runningFormInfo) {
80         HILOG_ERROR("ReadParcelable<RunningFormInfo> failed");
81         return ERR_APPEXECFWK_PARCEL_ERROR;
82     }
83     int32_t result = OnRemoveForm(bundleName, *runningFormInfo);
84     reply.WriteInt32(result);
85     return result;
86 }
87 
HandleNotifyWhetherFormsVisible(MessageParcel & data,MessageParcel & reply)88 int32_t JsFormStateObserverStub::HandleNotifyWhetherFormsVisible(MessageParcel &data, MessageParcel &reply)
89 {
90     HILOG_DEBUG("call");
91     int32_t formVisiblityTypeInt = data.ReadInt32();
92     std::string bundleName = data.ReadString();
93     std::vector<AppExecFwk::FormInstance> infos;
94     if (GetParcelableInfos(data, infos) != ERR_OK) {
95         HILOG_ERROR("get parcel infos failed");
96         return ERR_APPEXECFWK_PARCEL_ERROR;
97     }
98     AppExecFwk::FormVisibilityType formVisiblityType =
99         static_cast<AppExecFwk::FormVisibilityType>(formVisiblityTypeInt);
100     int32_t result = NotifyWhetherFormsVisible(formVisiblityType, bundleName, infos);
101     reply.WriteInt32(result);
102     return result;
103 }
104 
105 template<typename T>
GetParcelableInfos(MessageParcel & data,std::vector<T> & parcelableInfos)106 int32_t JsFormStateObserverStub::GetParcelableInfos(MessageParcel &data, std::vector<T> &parcelableInfos)
107 {
108     HILOG_DEBUG("call");
109     int32_t infoSize = data.ReadInt32();
110     if (infoSize < 0 || infoSize > MAX_ALLOW_SIZE) {
111         HILOG_ERROR("invalid size:%{public}d", infoSize);
112         return ERR_APPEXECFWK_PARCEL_ERROR;
113     }
114     for (int32_t i = 0; i < infoSize; i++) {
115         std::unique_ptr<T> info(data.ReadParcelable<T>());
116         if (!info) {
117             HILOG_ERROR("fail Read Parcelable infos");
118             return ERR_APPEXECFWK_PARCEL_ERROR;
119         }
120         parcelableInfos.emplace_back(*info);
121     }
122     HILOG_DEBUG("get parcelable infos success");
123     return ERR_OK;
124 }
125 
HandleOnFormClick(MessageParcel & data,MessageParcel & reply)126 int32_t JsFormStateObserverStub::HandleOnFormClick(MessageParcel &data, MessageParcel &reply)
127 {
128     HILOG_DEBUG("call");
129     std::string bundleName = data.ReadString();
130     std::string callType = data.ReadString();
131     if (callType.empty()) {
132         HILOG_ERROR("empty callType");
133         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
134     }
135     std::unique_ptr<AppExecFwk::RunningFormInfo> runningFormInfo(data.ReadParcelable<AppExecFwk::RunningFormInfo>());
136     if (!runningFormInfo) {
137         HILOG_ERROR("ReadParcelable<RunningFormInfo> failed");
138         return ERR_APPEXECFWK_PARCEL_ERROR;
139     }
140     int32_t result = OnFormClickEvent(bundleName, callType, *runningFormInfo);
141     reply.WriteInt32(result);
142     return result;
143 }
144 } // namespace AbilityRuntime
145 } // namespace OHOS