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