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 "accessible_ability_client_stub.h"
17 #include "parcel.h"
18
19 namespace OHOS {
20 namespace Accessibility {
AccessibleAbilityClientStub()21 AccessibleAbilityClientStub::AccessibleAbilityClientStub()
22 {
23 HILOG_DEBUG("start.");
24 memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityClient::Message::INIT)] =
25 &AccessibleAbilityClientStub::HandleInit;
26 memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityClient::Message::DISCONNECT)] =
27 &AccessibleAbilityClientStub::HandleDisconnect;
28 memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityClient::Message::ON_ACCESSIBILITY_EVENT)] =
29 &AccessibleAbilityClientStub::HandleOnAccessibilityEvent;
30 memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityClient::Message::ON_KEY_PRESS_EVENT)] =
31 &AccessibleAbilityClientStub::HandleOnKeyPressEvent;
32 memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityClient::Message::ON_DISPALYRESIZE_CHANGED)] =
33 &AccessibleAbilityClientStub::HandleOnDisplayResized;
34 memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityClient::Message::ON_GESTURE_SIMULATE_RESULT)] =
35 &AccessibleAbilityClientStub::HandleOnGestureSimulateResult;
36 }
37
~AccessibleAbilityClientStub()38 AccessibleAbilityClientStub::~AccessibleAbilityClientStub()
39 {
40 HILOG_DEBUG("start.");
41 memberFuncMap_.clear();
42 }
43
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)44 int AccessibleAbilityClientStub::OnRemoteRequest(uint32_t code,
45 MessageParcel &data, MessageParcel &reply, MessageOption &option)
46 {
47 HILOG_DEBUG("AccessibleAbilityClientStub::OnRemoteRequest, cmd = %d, flags= %d", code, option.GetFlags());
48 std::u16string descriptor = AccessibleAbilityClientStub::GetDescriptor();
49 std::u16string remoteDescriptor = data.ReadInterfaceToken();
50 if (descriptor != remoteDescriptor) {
51 HILOG_INFO("local descriptor is not equal to remote");
52 return ERR_INVALID_STATE;
53 }
54
55 auto itFunc = memberFuncMap_.find(code);
56 if (itFunc != memberFuncMap_.end()) {
57 auto memberFunc = itFunc->second;
58 if (memberFunc != nullptr) {
59 return (this->*memberFunc)(data, reply);
60 }
61 }
62 HILOG_WARN("AccessibleAbilityClientStub::OnRemoteRequest, default case, need check.");
63 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
64 }
65
HandleInit(MessageParcel & data,MessageParcel & reply)66 ErrCode AccessibleAbilityClientStub::HandleInit(MessageParcel &data, MessageParcel &reply)
67 {
68 HILOG_DEBUG("start.");
69 sptr<IRemoteObject> object = data.ReadRemoteObject();
70 if (!object) {
71 HILOG_ERROR("object is nullptr.");
72 return ERR_INVALID_VALUE;
73 }
74
75 sptr<IAccessibleAbilityChannel> channel = iface_cast<IAccessibleAbilityChannel>(object);
76 if (!channel) {
77 HILOG_ERROR("channel is nullptr.");
78 return ERR_INVALID_VALUE;
79 }
80 int channelId = data.ReadInt32();
81
82 Init(channel, channelId);
83 return NO_ERROR;
84 }
85
HandleDisconnect(MessageParcel & data,MessageParcel & reply)86 ErrCode AccessibleAbilityClientStub::HandleDisconnect(MessageParcel &data, MessageParcel &reply)
87 {
88 HILOG_DEBUG("start.");
89 int channelId = data.ReadInt32();
90 Disconnect(channelId);
91 return NO_ERROR;
92 }
93
HandleOnAccessibilityEvent(MessageParcel & data,MessageParcel & reply)94 ErrCode AccessibleAbilityClientStub::HandleOnAccessibilityEvent(MessageParcel &data, MessageParcel &reply)
95 {
96 HILOG_DEBUG("start.");
97 std::unique_ptr<AccessibilityEventInfo> eventInfo(data.ReadParcelable<AccessibilityEventInfo>());
98 if (!eventInfo) {
99 HILOG_ERROR("ReadParcelable<AccessibilityEventInfo> failed");
100 return ERR_INVALID_VALUE;
101 }
102
103 OnAccessibilityEvent(*eventInfo);
104 return NO_ERROR;
105 }
106
HandleOnKeyPressEvent(MessageParcel & data,MessageParcel & reply)107 ErrCode AccessibleAbilityClientStub::HandleOnKeyPressEvent(MessageParcel &data, MessageParcel &reply)
108 {
109 HILOG_DEBUG("start.");
110 int sequence = data.ReadInt32();
111
112 std::shared_ptr<MMI::KeyEvent> keyEvent = MMI::KeyEvent::Create();
113 if (!keyEvent->ReadFromParcel(data)) {
114 HILOG_ERROR("keyEvent ReadFromParcel failed");
115 return ERR_INVALID_VALUE;
116 }
117 OnKeyPressEvent(*keyEvent, sequence);
118 return NO_ERROR;
119 }
120
HandleOnDisplayResized(MessageParcel & data,MessageParcel & reply)121 ErrCode AccessibleAbilityClientStub::HandleOnDisplayResized(MessageParcel &data, MessageParcel &reply)
122 {
123 HILOG_DEBUG("start.");
124 int displayId = data.ReadInt32();
125
126 sptr<Rect> rect = data.ReadStrongParcelable<Rect>();
127 if (!rect) {
128 HILOG_ERROR("ReadStrongParcelable<Rect> failed");
129 return ERR_INVALID_VALUE;
130 }
131
132 float scale = data.ReadFloat();
133 float centerX = data.ReadFloat();
134 float centerY = data.ReadFloat();
135
136 OnDisplayResized(displayId, *rect, scale, centerX, centerY);
137 return NO_ERROR;
138 }
139
HandleOnGestureSimulateResult(MessageParcel & data,MessageParcel & reply)140 ErrCode AccessibleAbilityClientStub::HandleOnGestureSimulateResult(MessageParcel &data, MessageParcel &reply)
141 {
142 HILOG_DEBUG("start.");
143 int sequence = data.ReadInt32();
144 bool completedSuccessfully = data.ReadBool();
145
146 OnGestureSimulateResult(sequence, completedSuccessfully);
147 return NO_ERROR;
148 }
149 } // namespace Accessibility
150 } // namespace OHOS