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 #ifndef ACCESSIBLE_ABILITY_CONNECTION_H
17 #define ACCESSIBLE_ABILITY_CONNECTION_H
18 
19 #include "ability_connect_callback_stub.h"
20 #include "accessibility_ability_info.h"
21 #include "accessible_ability_channel.h"
22 #include "accessible_ability_client_proxy.h"
23 #include "ffrt.h"
24 #include "common_event_manager.h"
25 
26 namespace OHOS {
27 namespace Accessibility {
28 #define UID_MASK 200000
29 
30 class AccessibilityAccountData;
31 
32 class AccessibleAbilityConnection : public AAFwk::AbilityConnectionStub {
33 public:
34     AccessibleAbilityConnection(int32_t accountId, int32_t connectionId, AccessibilityAbilityInfo &abilityInfo);
35 
36     virtual ~AccessibleAbilityConnection();
37 
38     virtual void OnAbilityConnectDone(const AppExecFwk::ElementName &element,
39                                       const sptr<IRemoteObject> &remoteObject,
40                                       int32_t resultCode) override;
41 
42     virtual void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int32_t resultCode) override;
43 
44     // For AccessibleAbilityClientProxy
45     void OnAccessibilityEvent(AccessibilityEventInfo &eventInfo);
46 
47     bool OnKeyPressEvent(const MMI::KeyEvent &keyEvent, const int32_t sequence);
48 
49     void SetAbilityInfoTargetBundleName(const std::vector<std::string> &targetBundleNames);
50 
51     // Get Attribution
GetAbilityInfo()52     inline AccessibilityAbilityInfo& GetAbilityInfo()
53     {
54         return abilityInfo_;
55     }
56 
GetElementName()57     inline AppExecFwk::ElementName& GetElementName()
58     {
59         return elementName_;
60     }
61 
GetAbilityClient()62     inline sptr<IAccessibleAbilityClient> GetAbilityClient()
63     {
64         return abilityClient_;
65     }
66 
67     void Disconnect();
68 
69     bool Connect(const AppExecFwk::ElementName &element);
70 
71     int32_t GetChannelId();
72 
73     void OnAbilityConnectDoneSync(const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject);
74     void OnAbilityDisconnectDoneSync(const AppExecFwk::ElementName &element);
75 
76 private:
77     class AccessibleAbilityConnectionDeathRecipient final : public IRemoteObject::DeathRecipient {
78     public:
AccessibleAbilityConnectionDeathRecipient(int32_t accountId,AppExecFwk::ElementName & elementName,std::shared_ptr<AppExecFwk::EventHandler> handler)79         AccessibleAbilityConnectionDeathRecipient(int32_t accountId,
80             AppExecFwk::ElementName& elementName, std::shared_ptr<AppExecFwk::EventHandler> handler)
81             : accountId_(accountId), recipientElementName_(elementName), handler_(handler) {};
82         ~AccessibleAbilityConnectionDeathRecipient() = default;
83         DISALLOW_COPY_AND_MOVE(AccessibleAbilityConnectionDeathRecipient);
84 
85         void OnRemoteDied(const wptr<IRemoteObject>& remote);
86 
87         int32_t accountId_ = -1;
88         AppExecFwk::ElementName recipientElementName_;
89         std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr;
90     };
91 
92     bool IsWantedEvent(int32_t eventType);
93     void InitAbilityClient(const sptr<IRemoteObject> &remoteObject);
94 
95     int32_t accountId_ = -1;
96     int32_t connectionId_ = -1;
97     sptr<IRemoteObject::DeathRecipient> deathRecipient_ = nullptr;
98     sptr<IAccessibleAbilityClient> abilityClient_ = nullptr;
99     sptr<AccessibleAbilityChannel> channel_ = nullptr;
100     AccessibilityAbilityInfo abilityInfo_ {};
101     AppExecFwk::ElementName elementName_ {};
102     std::shared_ptr<AppExecFwk::EventHandler> eventHandler_ = nullptr;
103 };
104 } // namespace Accessibility
105 } // namespace OHOS
106 #endif // ACCESSIBLE_ABILITY_CONNECTION_H