1 /*
2  * Copyright (c) 2023 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 OHOS_ROSEN_WINDOW_SCENE_EXTENSION_SESSION_H
17 #define OHOS_ROSEN_WINDOW_SCENE_EXTENSION_SESSION_H
18 #include <future>
19 
20 #include "key_event.h"
21 #include "want.h"
22 
23 #include "session/host/include/session.h"
24 
25 namespace OHOS::Rosen {
26 class WindowEventChannelListener : public IRemoteStub<IWindowEventChannelListener> {
27 public:
28     explicit WindowEventChannelListener() = default;
29     void SetTransferKeyEventForConsumedParams(int32_t keyEventId, bool isPreImeEvent,
30         const std::shared_ptr<std::promise<bool>>& isConsumedPromise, const std::shared_ptr<WSError>& retCode);
31     void ResetTransferKeyEventForConsumedParams();
32     void ResetTransferKeyEventForConsumedParams(bool isConsumed, WSError retCode);
33     void OnTransferKeyEventForConsumed(int32_t keyEventId, bool isPreImeEvent,
34                                        bool isConsumed, WSError retCode) override;
35     int32_t OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) override;
36 
37 private:
38     std::mutex transferKeyEventForConsumedMutex_;
39     int32_t keyEventId_ = 0;
40     bool isPreImeEvent_ = false;
41     std::shared_ptr<std::promise<bool>> isConsumedPromise_ = nullptr;
42     std::shared_ptr<WSError> retCode_ = nullptr;
43 };
44 
45 class ChannelDeathRecipient : public IRemoteObject::DeathRecipient {
46 public:
ChannelDeathRecipient(const sptr<WindowEventChannelListener>& listener)47     explicit ChannelDeathRecipient(const sptr<WindowEventChannelListener>& listener): listener_(listener) {}
48     virtual void OnRemoteDied(const wptr<IRemoteObject>& wptrDeath) override;
49 
50 private:
51     sptr<WindowEventChannelListener> listener_ = nullptr;
52 };
53 
54 using NotifyTransferAbilityResultFunc = std::function<void(uint32_t resultCode, const AAFwk::Want& want)>;
55 using NotifyTransferExtensionDataFunc = std::function<void(const AAFwk::WantParams& wantParams)>;
56 using NotifyRemoteReadyFunc = std::function<void()>;
57 using NotifySyncOnFunc = std::function<void()>;
58 using NotifyAsyncOnFunc = std::function<void()>;
59 using NotifyGetAvoidAreaByTypeFunc = std::function<AvoidArea(AvoidAreaType type)>;
60 using NotifyBindModalFunc = std::function<void()>;
61 using NotifyExtensionEventFunc = std::function<void(uint32_t notifyEvent)>;
62 
63 class ExtensionSession : public Session {
64 public:
65     struct ExtensionSessionEventCallback : public RefBase {
66         NotifyTransferAbilityResultFunc transferAbilityResultFunc_;
67         NotifyTransferExtensionDataFunc transferExtensionDataFunc_;
68         NotifyRemoteReadyFunc notifyRemoteReadyFunc_;
69         NotifySyncOnFunc notifySyncOnFunc_;
70         NotifyAsyncOnFunc notifyAsyncOnFunc_;
71         NotifyGetAvoidAreaByTypeFunc notifyGetAvoidAreaByTypeFunc_;
72         NotifyBindModalFunc notifyBindModalFunc_;
73         NotifyExtensionEventFunc notifyExtensionEventFunc_;
74     };
75 
76     explicit ExtensionSession(const SessionInfo& info);
77     virtual ~ExtensionSession();
78 
79     WSError Connect(const sptr<ISessionStage>& sessionStage, const sptr<IWindowEventChannel>& eventChannel,
80         const std::shared_ptr<RSSurfaceNode>& surfaceNode, SystemSessionConfig& systemConfig,
81         sptr<WindowSessionProperty> property, sptr<IRemoteObject> token,
82         const std::string& identityToken = "") override;
83     WSError ConnectInner(const sptr<ISessionStage>& sessionStage, const sptr<IWindowEventChannel>& eventChannel,
84         const std::shared_ptr<RSSurfaceNode>& surfaceNode, SystemSessionConfig& systemConfig,
85         sptr<WindowSessionProperty> property, sptr<IRemoteObject> token, int32_t pid, int32_t uid,
86         const std::string& identityToken = "") override;
87 
88     AvoidArea GetAvoidAreaByType(AvoidAreaType type) override;
89 
90     WSError UpdateAvoidArea(const sptr<AvoidArea>& avoidArea, AvoidAreaType type) override;
91     WSError TransferAbilityResult(uint32_t resultCode, const AAFwk::Want& want) override;
92     WSError TransferExtensionData(const AAFwk::WantParams& wantParams) override;
93     WSError TransferComponentData(const AAFwk::WantParams& wantParams);
94     WSErrorCode TransferComponentDataSync(const AAFwk::WantParams& wantParams,
95                                           AAFwk::WantParams& reWantParams);
96     WSError TransferAccessibilityEvent(const Accessibility::AccessibilityEventInfo& info,
97         int64_t uiExtensionIdLevel) override;
98     WSError TransferAccessibilityHoverEvent(float pointX, float pointY, int32_t sourceType, int32_t eventType,
99         int64_t timeMs);
100     WSError TransferAccessibilityChildTreeRegister(uint32_t windowId, int32_t treeId, int64_t accessibilityId);
101     WSError TransferAccessibilityChildTreeUnregister();
102     WSError TransferAccessibilityDumpChildInfo(const std::vector<std::string>& params, std::vector<std::string>& info);
103     void NotifySyncOn() override;
104     void NotifyAsyncOn() override;
105     WSError NotifyDensityFollowHost(bool isFollowHost, float densityValue = 1.0f);
106     WSError UpdateSessionViewportConfig(const SessionViewportConfig& config);
107     void TriggerBindModalUIExtension() override;
108     void RegisterExtensionSessionEventCallback(const sptr<ExtensionSessionEventCallback>& extSessionEventCallback);
109     WSError TransferKeyEventForConsumed(const std::shared_ptr<MMI::KeyEvent>& keyEvent, bool& isConsumed,
110         bool& isTimeOut, bool isPreImeEvent = false);
111     WSError TransferKeyEventAsync(const std::shared_ptr<MMI::KeyEvent>& keyEvent, bool isPreImeEvent = false);
112     sptr<ExtensionSessionEventCallback> GetExtensionSessionEventCallback();
113     WSError Background(bool isFromClient = false, const std::string& identityToken = "") override;
114     void NotifyExtensionEventAsync(uint32_t notifyEvent) override;
115     WSError NotifyDumpInfo(const std::vector<std::string>& params, std::vector<std::string>& info);
116 
117 private:
118     sptr<ExtensionSessionEventCallback> extSessionEventCallback_ = nullptr;
119     bool isFirstTriggerBindModal_ = true;
120     sptr<ChannelDeathRecipient> channelDeath_ = nullptr;
121     sptr<WindowEventChannelListener> channelListener_ = nullptr;
122 };
123 } // namespace OHOS::Rosen
124 
125 #endif // OHOS_ROSEN_WINDOW_SCENE_EXTENSION_SESSION_H
126