• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "accessibility_element_info_parcel.h"
18 #include "accessibility_event_info_parcel.h"
19 #include "hilog_wrapper.h"
20 
21 namespace OHOS {
22 namespace Accessibility {
AccessibleAbilityClientStub()23 AccessibleAbilityClientStub::AccessibleAbilityClientStub()
24 {
25     HILOG_DEBUG();
26     memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityClient::Message::INIT)] =
27         &AccessibleAbilityClientStub::HandleInit;
28     memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityClient::Message::DISCONNECT)] =
29         &AccessibleAbilityClientStub::HandleDisconnect;
30     memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityClient::Message::ON_ACCESSIBILITY_EVENT)] =
31         &AccessibleAbilityClientStub::HandleOnAccessibilityEvent;
32     memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityClient::Message::ON_KEY_PRESS_EVENT)] =
33         &AccessibleAbilityClientStub::HandleOnKeyPressEvent;
34 }
35 
~AccessibleAbilityClientStub()36 AccessibleAbilityClientStub::~AccessibleAbilityClientStub()
37 {
38     HILOG_DEBUG();
39     memberFuncMap_.clear();
40 }
41 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)42 int AccessibleAbilityClientStub::OnRemoteRequest(uint32_t code,
43     MessageParcel &data, MessageParcel &reply, MessageOption &option)
44 {
45     HILOG_DEBUG("AccessibleAbilityClientStub::OnRemoteRequest, cmd = %d, flags= %d", code, option.GetFlags());
46     std::u16string descriptor = AccessibleAbilityClientStub::GetDescriptor();
47     std::u16string remoteDescriptor = data.ReadInterfaceToken();
48     if (descriptor != remoteDescriptor) {
49         HILOG_ERROR("local descriptor is not equal to remote");
50         return ERR_INVALID_STATE;
51     }
52 
53     auto itFunc = memberFuncMap_.find(code);
54     if (itFunc != memberFuncMap_.end()) {
55         auto memberFunc = itFunc->second;
56         if (memberFunc != nullptr) {
57             return (this->*memberFunc)(data, reply);
58         }
59     }
60     HILOG_WARN("AccessibleAbilityClientStub::OnRemoteRequest, default case, need check.");
61     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
62 }
63 
HandleInit(MessageParcel & data,MessageParcel & reply)64 ErrCode AccessibleAbilityClientStub::HandleInit(MessageParcel &data, MessageParcel &reply)
65 {
66     HILOG_DEBUG();
67     sptr<IRemoteObject> remote = data.ReadRemoteObject();
68     if (!remote) {
69         HILOG_ERROR("object is nullptr.");
70         return ERR_INVALID_VALUE;
71     }
72 
73     sptr<IAccessibleAbilityChannel> channel = iface_cast<IAccessibleAbilityChannel>(remote);
74     if (!channel) {
75         HILOG_ERROR("channel is nullptr.");
76         return ERR_INVALID_VALUE;
77     }
78     int32_t channelId = data.ReadInt32();
79 
80     Init(channel, channelId);
81     return NO_ERROR;
82 }
83 
HandleDisconnect(MessageParcel & data,MessageParcel & reply)84 ErrCode AccessibleAbilityClientStub::HandleDisconnect(MessageParcel &data, MessageParcel &reply)
85 {
86     HILOG_DEBUG();
87     int32_t channelId = data.ReadInt32();
88     Disconnect(channelId);
89     return NO_ERROR;
90 }
91 
HandleOnAccessibilityEvent(MessageParcel & data,MessageParcel & reply)92 ErrCode AccessibleAbilityClientStub::HandleOnAccessibilityEvent(MessageParcel &data, MessageParcel &reply)
93 {
94     HILOG_DEBUG();
95     sptr<AccessibilityEventInfoParcel> eventInfo = data.ReadStrongParcelable<AccessibilityEventInfoParcel>();
96     if (!eventInfo) {
97         HILOG_ERROR("ReadStrongParcelable<AccessibilityEventInfo> failed");
98         return ERR_INVALID_VALUE;
99     }
100 
101     OnAccessibilityEvent(*eventInfo);
102     return NO_ERROR;
103 }
104 
HandleOnKeyPressEvent(MessageParcel & data,MessageParcel & reply)105 ErrCode AccessibleAbilityClientStub::HandleOnKeyPressEvent(MessageParcel &data, MessageParcel &reply)
106 {
107     HILOG_DEBUG();
108     int32_t sequence = data.ReadInt32();
109 
110     std::shared_ptr<MMI::KeyEvent> keyEvent = MMI::KeyEvent::Create();
111     if (!keyEvent->ReadFromParcel(data)) {
112         HILOG_ERROR("keyEvent ReadFromParcel failed");
113         return ERR_INVALID_VALUE;
114     }
115     OnKeyPressEvent(*keyEvent, sequence);
116     return NO_ERROR;
117 }
118 } // namespace Accessibility
119 } // namespace OHOS