1 /*
2 * Copyright (c) 2022-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 "form_status_change_callback.h"
17 #include "access_token.h"
18 #include "accesstoken_common_log.h"
19 #include "access_token_error.h"
20
21 namespace OHOS {
22 namespace Security {
23 namespace AccessToken {
24 namespace {
25 static constexpr int32_t MAX_ALLOW_SIZE = 8 * 1024;
26 }
27
FormStateObserverStub()28 FormStateObserverStub::FormStateObserverStub()
29 {
30 LOGI(ATM_DOMAIN, ATM_TAG, "FormStateObserverStub Instance create.");
31 }
32
~FormStateObserverStub()33 FormStateObserverStub::~FormStateObserverStub()
34 {
35 LOGI(ATM_DOMAIN, ATM_TAG, "FormStateObserverStub Instance destroy.");
36 }
37
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)38 int32_t FormStateObserverStub::OnRemoteRequest(
39 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
40 {
41 if (data.ReadInterfaceToken() != GetDescriptor()) {
42 LOGI(ATM_DOMAIN, ATM_TAG, "FormStateObserverStub: ReadInterfaceToken failed.");
43 return ERROR_IPC_REQUEST_FAIL;
44 }
45 switch (static_cast<IJsFormStateObserver::Message>(code)) {
46 case IJsFormStateObserver::Message::FORM_STATE_OBSERVER_NOTIFY_WHETHER_FORMS_VISIBLE: {
47 (void)HandleNotifyWhetherFormsVisible(data, reply);
48 return NO_ERROR;
49 }
50 default: {
51 LOGD(ATM_DOMAIN, ATM_TAG, "Default case code: %{public}d.", code);
52 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
53 }
54 }
55 return NO_ERROR;
56 }
57
HandleNotifyWhetherFormsVisible(MessageParcel & data,MessageParcel & reply)58 int32_t FormStateObserverStub::HandleNotifyWhetherFormsVisible(MessageParcel &data, MessageParcel &reply)
59 {
60 FormVisibilityType visibleType = static_cast<FormVisibilityType>(data.ReadInt32());
61 std::string bundleName = data.ReadString();
62 std::vector<FormInstance> formInstances;
63 int32_t infoSize = data.ReadInt32();
64 if (infoSize < 0 || infoSize > MAX_ALLOW_SIZE) {
65 LOGE(ATM_DOMAIN, ATM_TAG, "Invalid size: %{public}d.", infoSize);
66 return ERR_OVERSIZE;
67 }
68 for (int32_t i = 0; i < infoSize; i++) {
69 std::unique_ptr<FormInstance> info(data.ReadParcelable<FormInstance>());
70 if (info == nullptr) {
71 LOGE(ATM_DOMAIN, ATM_TAG, "Failed to Read Parcelable infos.");
72 return RET_FAILED;
73 }
74 formInstances.emplace_back(*info);
75 }
76 (void)NotifyWhetherFormsVisible(visibleType, bundleName, formInstances);
77 return NO_ERROR;
78 }
79 } // namespace AccessToken
80 } // namespace Security
81 } // namespace OHOS
82