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     void HandleNoEventHandler(const AppExecFwk::ElementName &element);
39 
40     virtual void OnAbilityConnectDone(const AppExecFwk::ElementName &element,
41                                       const sptr<IRemoteObject> &remoteObject,
42                                       int32_t resultCode) override;
43 
44     virtual void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int32_t resultCode) override;
45 
46     // For AccessibleAbilityClientProxy
47     void OnAccessibilityEvent(AccessibilityEventInfo &eventInfo);
48 
49     bool OnKeyPressEvent(const MMI::KeyEvent &keyEvent, const int32_t sequence);
50 
51     void SetAbilityInfoTargetBundleName(const std::vector<std::string> &targetBundleNames);
52 
53     // Get Attribution
GetAbilityInfo()54     inline AccessibilityAbilityInfo& GetAbilityInfo()
55     {
56         return abilityInfo_;
57     }
58 
GetElementName()59     inline AppExecFwk::ElementName& GetElementName()
60     {
61         return elementName_;
62     }
63 
GetAbilityClient()64     inline sptr<IAccessibleAbilityClient> GetAbilityClient()
65     {
66         return abilityClient_;
67     }
68 
69     void Disconnect();
70 
71     bool Connect(const AppExecFwk::ElementName &element);
72 
73     int32_t GetChannelId();
74 
75     void OnAbilityConnectDoneSync(const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject);
76     void OnAbilityDisconnectDoneSync(const AppExecFwk::ElementName &element);
77 
78 private:
79     class AccessibleAbilityConnectionDeathRecipient final : public IRemoteObject::DeathRecipient {
80     public:
AccessibleAbilityConnectionDeathRecipient(int32_t accountId, AppExecFwk::ElementName& elementName, std::shared_ptr<AppExecFwk::EventHandler> handler)81         AccessibleAbilityConnectionDeathRecipient(int32_t accountId,
82             AppExecFwk::ElementName& elementName, std::shared_ptr<AppExecFwk::EventHandler> handler)
83             : accountId_(accountId), recipientElementName_(elementName), handler_(handler) {};
84         ~AccessibleAbilityConnectionDeathRecipient() = default;
85         DISALLOW_COPY_AND_MOVE(AccessibleAbilityConnectionDeathRecipient);
86 
87         void OnRemoteDied(const wptr<IRemoteObject>& remote);
88 
89         int32_t accountId_ = -1;
90         AppExecFwk::ElementName recipientElementName_;
91         std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr;
92     };
93 
94     bool IsWantedEvent(int32_t eventType);
95     void InitAbilityClient(const sptr<IRemoteObject> &remoteObject);
96 
97     int32_t accountId_ = -1;
98     int32_t connectionId_ = -1;
99     sptr<IRemoteObject::DeathRecipient> deathRecipient_ = nullptr;
100     sptr<IAccessibleAbilityClient> abilityClient_ = nullptr;
101     sptr<AccessibleAbilityChannel> channel_ = nullptr;
102     AccessibilityAbilityInfo abilityInfo_ {};
103     AppExecFwk::ElementName elementName_ {};
104     std::shared_ptr<AppExecFwk::EventHandler> eventHandler_ = nullptr;
105 };
106 } // namespace Accessibility
107 } // namespace OHOS
108 #endif // ACCESSIBLE_ABILITY_CONNECTION_H