1 /*
2  * Copyright (C) 2022 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 "accessibility_window_info_parcel.h"
17 #include "accessibility_element_info_parcel.h"
18 #include "hilog_wrapper.h"
19 #include "parcel_util.h"
20 
21 namespace OHOS {
22 namespace Accessibility {
AccessibilityWindowInfoParcel(const AccessibilityWindowInfo & accessibilityWindowInfo)23 AccessibilityWindowInfoParcel::AccessibilityWindowInfoParcel(const AccessibilityWindowInfo &accessibilityWindowInfo)
24     : AccessibilityWindowInfo(accessibilityWindowInfo)
25 {
26 }
27 
ReadFromParcel(Parcel & parcel)28 bool AccessibilityWindowInfoParcel::ReadFromParcel(Parcel &parcel)
29 {
30     int32_t accessibilityWindowType = TYPE_WINDOW_INVALID;
31     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, accessibilityWindowType);
32     accessibilityWindowType_ = static_cast<AccessibilityWindowType>(accessibilityWindowType);
33     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, windowType_);
34     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, windowMode_);
35     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, windowLayer_);
36     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, windowId_);
37     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, active_);
38     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, focused_);
39     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, accessibilityFocused_);
40     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isDecorEnable_);
41     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, mainWindowId_);
42     sptr<RectParcel> boundsInScreen = parcel.ReadStrongParcelable<RectParcel>();
43     if (boundsInScreen == nullptr) {
44         HILOG_ERROR("ReadStrongParcelable boundsInScreen failed.");
45         return false;
46     }
47     boundsInScreen_ = *boundsInScreen;
48 
49     return true;
50 }
51 
Marshalling(Parcel & parcel) const52 bool AccessibilityWindowInfoParcel::Marshalling(Parcel &parcel) const
53 {
54     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(accessibilityWindowType_));
55     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, windowType_);
56     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, windowMode_);
57     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, windowLayer_);
58     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, windowId_);
59     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, active_);
60     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, focused_);
61     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, accessibilityFocused_);
62     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isDecorEnable_);
63     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, mainWindowId_);
64     RectParcel rectParcel(boundsInScreen_);
65     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Parcelable, parcel, &rectParcel);
66 
67     return true;
68 };
69 
Unmarshalling(Parcel & parcel)70 sptr<AccessibilityWindowInfoParcel> AccessibilityWindowInfoParcel::Unmarshalling(Parcel &parcel)
71 {
72     sptr<AccessibilityWindowInfoParcel> info = new(std::nothrow) AccessibilityWindowInfoParcel();
73     if (info == nullptr) {
74         HILOG_ERROR("Failed to create info.");
75         return nullptr;
76     }
77     if (!info->ReadFromParcel(parcel)) {
78         HILOG_ERROR("ReadFromParcel failed.");
79         info = nullptr;
80         return nullptr;
81     }
82     return info;
83 }
84 } // namespace Accessibility
85 } // namespace OHOS