• 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_manager_service_stub.h"
17 #include "accessibility_caption.h"
18 #include "hilog_wrapper.h"
19 #include "ipc_skeleton.h"
20 #include "ipc_types.h"
21 #include "iremote_object.h"
22 #include "refbase.h"
23 
24 namespace OHOS {
25 namespace Accessibility {
AccessibleAbilityManagerServiceClientStub()26 AccessibleAbilityManagerServiceClientStub::AccessibleAbilityManagerServiceClientStub()
27 {
28     HILOG_DEBUG("start");
29 
30     memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityManagerServiceClient::Message::SEND_EVENT)] =
31         &AccessibleAbilityManagerServiceClientStub::HandleSendEvent;
32     memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityManagerServiceClient::Message::REGISTER_STATE_CALLBACK)] =
33         &AccessibleAbilityManagerServiceClientStub::HandleRegisterStateCallback;
34     memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityManagerServiceClient::Message::GET_ABILITYLIST)] =
35         &AccessibleAbilityManagerServiceClientStub::HandleGetAbilityList;
36     memberFuncMap_[static_cast<uint32_t>(
37         IAccessibleAbilityManagerServiceClient::Message::REGISTER_INTERACTION_CONNECTION)] =
38         &AccessibleAbilityManagerServiceClientStub::HandleRegisterAccessibilityElementOperator;
39     memberFuncMap_[static_cast<uint32_t>(
40         IAccessibleAbilityManagerServiceClient::Message::DEREGISTER_INTERACTION_CONNECTION)] =
41         &AccessibleAbilityManagerServiceClientStub::HandleDeregisterAccessibilityElementOperator;
42 
43     memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityManagerServiceClient::Message::GET_CAPTION_PROPERTY)] =
44         &AccessibleAbilityManagerServiceClientStub::HandleGetCaptionProperty;
45     memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityManagerServiceClient::Message::SET_CAPTION_PROPERTY)] =
46         &AccessibleAbilityManagerServiceClientStub::HandleSetCaptionProperty;
47     memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityManagerServiceClient::Message::SET_CAPTION_STATE)] =
48         &AccessibleAbilityManagerServiceClientStub::HandleSetCaptionState;
49     memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityManagerServiceClient::Message::SET_ENABLED)] =
50         &AccessibleAbilityManagerServiceClientStub::HandleSetEnabled;
51     memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityManagerServiceClient::Message::GET_ENABLED)] =
52         &AccessibleAbilityManagerServiceClientStub::HandleGetEnabled;
53     memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityManagerServiceClient::Message::GET_CAPTION_STATE)] =
54         &AccessibleAbilityManagerServiceClientStub::HandleGetCaptionState;
55     memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityManagerServiceClient::Message::GET_TOUCH_GUIDE_STATE)] =
56         &AccessibleAbilityManagerServiceClientStub::HandleGetTouchGuideState;
57     memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityManagerServiceClient::Message::GET_GESTURE_STATE)] =
58         &AccessibleAbilityManagerServiceClientStub::HandleGetGestureState;
59     memberFuncMap_[static_cast<uint32_t>(
60         IAccessibleAbilityManagerServiceClient::Message::GET_KEY_EVENT_OBSERVE_STATE)] =
61         &AccessibleAbilityManagerServiceClientStub::HandleGetKeyEventObserverState;
62     memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityManagerServiceClient::Message::SET_TOUCH_GUIDE_STATE)] =
63         &AccessibleAbilityManagerServiceClientStub::HandleSetTouchGuideState;
64     memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityManagerServiceClient::Message::SET_GESTURE_STATE)] =
65         &AccessibleAbilityManagerServiceClientStub::HandleSetGestureState;
66     memberFuncMap_[static_cast<uint32_t>(
67         IAccessibleAbilityManagerServiceClient::Message::SET_KEY_EVENT_OBSERVE_STATE)] =
68         &AccessibleAbilityManagerServiceClientStub::HandleSetKeyEventObserverState;
69     memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityManagerServiceClient::Message::SET_ENABLED_OBJECT)] =
70         &AccessibleAbilityManagerServiceClientStub::HandleSetEnabledObj;
71     memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityManagerServiceClient::Message::GET_ENABLED_OBJECT)] =
72         &AccessibleAbilityManagerServiceClientStub::HandleGetEnabledAbilities;
73     memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityManagerServiceClient::Message::GET_INSTALLED)] =
74         &AccessibleAbilityManagerServiceClientStub::HandleGetInstalledAbilities;
75     memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityManagerServiceClient::Message::DISABLE_ABILITIES)] =
76         &AccessibleAbilityManagerServiceClientStub::HandleDisableAbilities;
77     memberFuncMap_[static_cast<uint32_t>(
78         IAccessibleAbilityManagerServiceClient::Message::REGISTER_CAPTION_PROPERTY_CALLBACK)] =
79         &AccessibleAbilityManagerServiceClientStub::HandleRegisterCaptionPropertyCallback;
80     memberFuncMap_[static_cast<uint32_t>(
81         IAccessibleAbilityManagerServiceClient::Message::REGISTER_UITEST_ABILITY_CONNECTION_CLIENT)] =
82         &AccessibleAbilityManagerServiceClientStub::HandleRegisterUITestAbilityConnectionClient;
83     memberFuncMap_[static_cast<uint32_t>(
84         IAccessibleAbilityManagerServiceClient::Message::DEREGISTER_UITEST_ABILITY_CONNECTION_CLIENT)] =
85         &AccessibleAbilityManagerServiceClientStub::HandleDeregisterUITestAbilityConnectionClient;
86     memberFuncMap_[static_cast<uint32_t>(
87         IAccessibleAbilityManagerServiceClient::Message::GET_ACTIVE_WINDOW)] =
88         &AccessibleAbilityManagerServiceClientStub::HandleGetActiveWindow;
89 }
90 
~AccessibleAbilityManagerServiceClientStub()91 AccessibleAbilityManagerServiceClientStub::~AccessibleAbilityManagerServiceClientStub()
92 {
93     HILOG_DEBUG("start.");
94     memberFuncMap_.clear();
95 }
96 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)97 int AccessibleAbilityManagerServiceClientStub::OnRemoteRequest(
98     uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)
99 {
100     HILOG_DEBUG("AccessibleAbilityManagerServiceClientStub::OnRemoteRequest, cmd = %{public}d, flags= %{public}d",
101         code,
102         option.GetFlags());
103     std::u16string descriptor = AccessibleAbilityManagerServiceClientStub::GetDescriptor();
104     std::u16string remoteDescriptor = data.ReadInterfaceToken();
105     if (descriptor != remoteDescriptor) {
106         HILOG_INFO("local descriptor is not equal to remote");
107         return ERR_INVALID_STATE;
108     }
109 
110     auto memFunc = memberFuncMap_.find(code);
111     if (memFunc != memberFuncMap_.end()) {
112         auto func = memFunc->second;
113         if (func != nullptr) {
114             return (this->*func)(data, reply);
115         }
116     }
117     HILOG_WARN("AccessibleAbilityManagerServiceClientStub::OnRemoteRequest, default case, need check.");
118     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
119 }
120 
HandleSendEvent(MessageParcel & data,MessageParcel & reply)121 ErrCode AccessibleAbilityManagerServiceClientStub::HandleSendEvent(MessageParcel& data, MessageParcel& reply)
122 {
123     HILOG_DEBUG("start");
124 
125     std::shared_ptr<AccessibilityEventInfo> uiEvent(data.ReadParcelable<AccessibilityEventInfo>());
126     if (!uiEvent) {
127         HILOG_DEBUG("ReadParcelable<AbilityInfo> failed");
128         return ERROR;
129     }
130     int userId = data.ReadInt32();
131     SendEvent(*uiEvent, userId);
132 
133     return ErrCode::NO_ERROR;
134 }
135 
HandleRegisterStateCallback(MessageParcel & data,MessageParcel & reply)136 ErrCode AccessibleAbilityManagerServiceClientStub::HandleRegisterStateCallback(
137     MessageParcel& data, MessageParcel& reply)
138 {
139     HILOG_DEBUG("start");
140 
141     sptr<IRemoteObject> obj = data.ReadRemoteObject();
142     sptr<IAccessibleAbilityManagerServiceState> client = iface_cast<IAccessibleAbilityManagerServiceState>(obj);
143     int userId = data.ReadInt32();
144     uint64_t result = RegisterStateCallback(client, userId);
145     reply.WriteUint64(result);
146 
147     return ErrCode::NO_ERROR;
148 }
149 
HandleGetAbilityList(MessageParcel & data,MessageParcel & reply)150 ErrCode AccessibleAbilityManagerServiceClientStub::HandleGetAbilityList(MessageParcel& data, MessageParcel& reply)
151 {
152     HILOG_DEBUG("start");
153 
154     uint32_t abilityTypes = data.ReadUint32();
155     int32_t stateType = data.ReadInt32();
156     std::vector<AccessibilityAbilityInfo> abilityInfos {};
157     abilityInfos = GetAbilityList(abilityTypes, stateType);
158 
159     int32_t abilityInfoSize = (int32_t)abilityInfos.size();
160     reply.WriteInt32(abilityInfoSize);
161     for (auto& abilityInfo : abilityInfos) {
162         if (!reply.WriteParcelable(&abilityInfo)) {
163             HILOG_ERROR("ReadParcelable<accessibilityAbilityInfo> failed");
164             return ERROR;
165         }
166     }
167     return ErrCode::NO_ERROR;
168 }
169 
HandleRegisterAccessibilityElementOperator(MessageParcel & data,MessageParcel & reply)170 ErrCode AccessibleAbilityManagerServiceClientStub::HandleRegisterAccessibilityElementOperator(
171     MessageParcel& data, MessageParcel& reply)
172 {
173     HILOG_DEBUG("start");
174 
175     int windowId = data.ReadInt32();
176     sptr<IRemoteObject> obj = data.ReadRemoteObject();
177     sptr<IAccessibilityElementOperator> operation = iface_cast<IAccessibilityElementOperator>(obj);
178     int userId = data.ReadInt32();
179     RegisterElementOperator(windowId, operation, userId);
180 
181     return ErrCode::NO_ERROR;
182 }
183 
HandleDeregisterAccessibilityElementOperator(MessageParcel & data,MessageParcel & reply)184 ErrCode AccessibleAbilityManagerServiceClientStub::HandleDeregisterAccessibilityElementOperator(
185     MessageParcel& data, MessageParcel& reply)
186 {
187     HILOG_DEBUG("start");
188 
189     int windowId = data.ReadInt32();
190     DeregisterElementOperator(windowId);
191 
192     return ErrCode::NO_ERROR;
193 }
194 
HandleGetCaptionProperty(MessageParcel & data,MessageParcel & reply)195 ErrCode AccessibleAbilityManagerServiceClientStub::HandleGetCaptionProperty(MessageParcel& data, MessageParcel& reply)
196 {
197     HILOG_DEBUG("start");
198 
199     CaptionProperty caption = GetCaptionProperty();
200 
201     reply.WriteParcelable(&caption);
202 
203     return ErrCode::NO_ERROR;
204 }
205 
HandleSetCaptionProperty(MessageParcel & data,MessageParcel & reply)206 ErrCode AccessibleAbilityManagerServiceClientStub::HandleSetCaptionProperty(MessageParcel& data, MessageParcel& reply)
207 {
208     HILOG_DEBUG("start");
209 
210     std::shared_ptr<CaptionProperty> caption(data.ReadParcelable<CaptionProperty>());
211     if (!caption) {
212         HILOG_DEBUG("ReadParcelable<CaptionProperty> failed");
213         return ERROR;
214     }
215     SetCaptionProperty(*caption);
216 
217     return ErrCode::NO_ERROR;
218 }
219 
HandleSetCaptionState(MessageParcel & data,MessageParcel & reply)220 ErrCode AccessibleAbilityManagerServiceClientStub::HandleSetCaptionState(MessageParcel& data, MessageParcel& reply)
221 {
222     HILOG_DEBUG("start");
223 
224     bool state = data.ReadBool();
225 
226     SetCaptionState(state);
227 
228     return ErrCode::NO_ERROR;
229 }
230 
HandleSetEnabled(MessageParcel & data,MessageParcel & reply)231 ErrCode AccessibleAbilityManagerServiceClientStub::HandleSetEnabled(MessageParcel& data, MessageParcel& reply)
232 {
233     HILOG_DEBUG("start");
234 
235     bool state = data.ReadBool();
236 
237     SetEnabled(state);
238 
239     return ErrCode::NO_ERROR;
240 }
241 
HandleRegisterCaptionPropertyCallback(MessageParcel & data,MessageParcel & reply)242 ErrCode AccessibleAbilityManagerServiceClientStub::HandleRegisterCaptionPropertyCallback(
243     MessageParcel& data, MessageParcel& reply)
244 {
245     HILOG_DEBUG("start");
246 
247     sptr<IRemoteObject> obj = data.ReadRemoteObject();
248     sptr<IAccessibleAbilityManagerServiceCaptionProperty> client =
249         iface_cast<IAccessibleAbilityManagerServiceCaptionProperty>(obj);
250     int userId = data.ReadInt32();
251     uint64_t result = RegisterCaptionPropertyCallback(client, userId);
252     reply.WriteUint64(result);
253 
254     return ErrCode::NO_ERROR;
255 }
256 
HandleGetEnabled(MessageParcel & data,MessageParcel & reply)257 ErrCode AccessibleAbilityManagerServiceClientStub::HandleGetEnabled(MessageParcel& data, MessageParcel& reply)
258 {
259     HILOG_DEBUG("start");
260 
261     bool result = GetEnabledState();
262     reply.WriteBool(result);
263 
264     return ErrCode::NO_ERROR;
265 }
266 
HandleGetCaptionState(MessageParcel & data,MessageParcel & reply)267 ErrCode AccessibleAbilityManagerServiceClientStub::HandleGetCaptionState(MessageParcel& data, MessageParcel& reply)
268 {
269     HILOG_DEBUG("start");
270 
271     bool result = GetCaptionState();
272     reply.WriteBool(result);
273     return ErrCode::NO_ERROR;
274 }
275 
HandleGetTouchGuideState(MessageParcel & data,MessageParcel & reply)276 ErrCode AccessibleAbilityManagerServiceClientStub::HandleGetTouchGuideState(MessageParcel& data, MessageParcel& reply)
277 {
278     HILOG_DEBUG("start");
279 
280     bool result = GetTouchGuideState();
281     reply.WriteBool(result);
282     return ErrCode::NO_ERROR;
283 }
284 
HandleGetGestureState(MessageParcel & data,MessageParcel & reply)285 ErrCode AccessibleAbilityManagerServiceClientStub::HandleGetGestureState(MessageParcel& data, MessageParcel& reply)
286 {
287     HILOG_DEBUG("start");
288 
289     bool result = GetGestureState();
290     reply.WriteBool(result);
291     return ErrCode::NO_ERROR;
292 }
293 
HandleGetKeyEventObserverState(MessageParcel & data,MessageParcel & reply)294 ErrCode AccessibleAbilityManagerServiceClientStub::HandleGetKeyEventObserverState(
295     MessageParcel& data, MessageParcel& reply)
296 {
297     HILOG_DEBUG("start");
298 
299     bool result = GetKeyEventObserverState();
300     reply.WriteBool(result);
301 
302     return ErrCode::NO_ERROR;
303 }
304 
HandleSetTouchGuideState(MessageParcel & data,MessageParcel & reply)305 ErrCode AccessibleAbilityManagerServiceClientStub::HandleSetTouchGuideState(MessageParcel& data, MessageParcel& reply)
306 {
307     HILOG_DEBUG("start");
308 
309     bool state = data.ReadBool();
310 
311     SetTouchGuideState(state);
312 
313     return ErrCode::NO_ERROR;
314 }
315 
HandleSetGestureState(MessageParcel & data,MessageParcel & reply)316 ErrCode AccessibleAbilityManagerServiceClientStub::HandleSetGestureState(MessageParcel& data, MessageParcel& reply)
317 {
318     HILOG_DEBUG("start");
319 
320     bool state = data.ReadBool();
321 
322     SetGestureState(state);
323 
324     return ErrCode::NO_ERROR;
325 }
326 
HandleSetKeyEventObserverState(MessageParcel & data,MessageParcel & reply)327 ErrCode AccessibleAbilityManagerServiceClientStub::HandleSetKeyEventObserverState(
328     MessageParcel& data, MessageParcel& reply)
329 {
330     HILOG_DEBUG("start");
331 
332     bool state = data.ReadBool();
333 
334     SetKeyEventObserverState(state);
335 
336     return ErrCode::NO_ERROR;
337 }
338 
HandleSetEnabledObj(MessageParcel & data,MessageParcel & reply)339 ErrCode AccessibleAbilityManagerServiceClientStub::HandleSetEnabledObj(MessageParcel& data, MessageParcel& reply)
340 {
341     HILOG_DEBUG("start");
342     std::map<std::string, AppExecFwk::ElementName> it {};
343     int dev_num = data.ReadInt32();
344     if (!dev_num) {
345         HILOG_DEBUG("ReadParcelable failed, dev_num is 0");
346         return ERROR;
347     }
348     std::vector<AppExecFwk::ElementName> temp {};
349     for (int i = dev_num; i > 0; i--) {
350         std::unique_ptr<AppExecFwk::ElementName> iter(data.ReadParcelable<AppExecFwk::ElementName>());
351         if (!iter) {
352             HILOG_ERROR("ReadParcelable<AppExecFwk::ElementName> failed");
353             return ERROR;
354         }
355         temp.push_back(*iter);
356     }
357     for (int i = 0; i < dev_num; i++) {
358         it.insert(make_pair(temp[i].GetURI(), temp[i]));
359     }
360     SetEnabledObj(it);
361 
362     return ErrCode::NO_ERROR;
363 }
364 
HandleGetEnabledAbilities(MessageParcel & data,MessageParcel & reply)365 ErrCode AccessibleAbilityManagerServiceClientStub::HandleGetEnabledAbilities(MessageParcel& data, MessageParcel& reply)
366 {
367     HILOG_DEBUG("start");
368 
369     std::map<std::string, AppExecFwk::ElementName> it = GetEnabledAbilities();
370     std::map<std::string, AppExecFwk::ElementName>::iterator iter;
371 
372     reply.WriteInt32(it.size());
373     for (iter = it.begin(); iter != it.end(); ++iter) {
374         bool ret = reply.WriteParcelable(&iter->second);
375         if (!ret) {
376             return ERROR;
377         }
378     }
379 
380     return ErrCode::NO_ERROR;
381 }
382 
HandleGetInstalledAbilities(MessageParcel & data,MessageParcel & reply)383 ErrCode AccessibleAbilityManagerServiceClientStub::HandleGetInstalledAbilities(
384     MessageParcel& data, MessageParcel& reply)
385 {
386     HILOG_DEBUG("start");
387 
388     std::vector<AccessibilityAbilityInfo> it = GetInstalledAbilities();
389     int32_t num = (int32_t)it.size();
390 
391     reply.WriteInt32(num);
392     for (int32_t i = 0; i < num; i++) {
393         bool ret = reply.WriteParcelable(&it[i]);
394         if (!ret) {
395             return ErrCode::ERROR;
396         }
397     }
398     return ErrCode::NO_ERROR;
399 }
400 
HandleDisableAbilities(MessageParcel & data,MessageParcel & reply)401 ErrCode AccessibleAbilityManagerServiceClientStub::HandleDisableAbilities(MessageParcel& data, MessageParcel& reply)
402 {
403     HILOG_DEBUG("start");
404 
405     std::map<std::string, AppExecFwk::ElementName> it {};
406     int dev_num = data.ReadInt32();
407     if (!dev_num) {
408         HILOG_DEBUG("ReadParcelable failed, dev_num is 0");
409         return ERROR;
410     }
411     std::vector<AppExecFwk::ElementName> temp {};
412     for (int i = dev_num; i > 0; i--) {
413         std::unique_ptr<AppExecFwk::ElementName> iter(data.ReadParcelable<AppExecFwk::ElementName>());
414         if (!iter) {
415             HILOG_ERROR("ReadParcelable<AppExecFwk::ElementName> failed");
416             return ERROR;
417         }
418         temp.push_back(*iter);
419     }
420     for (int i = 0; i < dev_num; i++) {
421         it.insert(make_pair(temp[i].GetURI(), temp[i]));
422     }
423     DisableAbilities(it);
424 
425     return ErrCode::NO_ERROR;
426 }
427 
HandleRegisterUITestAbilityConnectionClient(MessageParcel & data,MessageParcel & reply)428 ErrCode AccessibleAbilityManagerServiceClientStub::HandleRegisterUITestAbilityConnectionClient(
429     MessageParcel &data, MessageParcel &reply)
430 {
431     HILOG_DEBUG("start");
432     sptr<IRemoteObject> obj = data.ReadRemoteObject();
433     bool result = RegisterUITestAbilityConnectionClient(obj);
434     if (!reply.WriteBool(result)) {
435         HILOG_ERROR("WriteBool failed");
436         return ErrCode::ERROR;
437     }
438     return ErrCode::NO_ERROR;
439 }
440 
HandleGetActiveWindow(MessageParcel & data,MessageParcel & reply)441 ErrCode AccessibleAbilityManagerServiceClientStub::HandleGetActiveWindow(MessageParcel& data, MessageParcel& reply)
442 {
443     HILOG_DEBUG("start");
444 
445     int activeWindow = GetActiveWindow();
446     reply.WriteInt32(activeWindow);
447 
448     return ErrCode::NO_ERROR;
449 }
450 
HandleDeregisterUITestAbilityConnectionClient(MessageParcel & data,MessageParcel & reply)451 ErrCode AccessibleAbilityManagerServiceClientStub::HandleDeregisterUITestAbilityConnectionClient(
452     MessageParcel &data, MessageParcel &reply)
453 {
454     HILOG_DEBUG("start");
455     bool result = DeregisterUITestAbilityConnectionClient();
456     if (!reply.WriteBool(result)) {
457         HILOG_ERROR("WriteBool failed");
458         return ErrCode::ERROR;
459     }
460     return ErrCode::NO_ERROR;
461 }
462 } // namespace Accessibility
463 } // namespace OHOS
464