1 /*
2 * Copyright (c) 2025 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 "adapter/ohos/entrance/event_pass_through_subscriber.h"
17
18 namespace OHOS::Ace {
19 const std::string TOUCH_EVENTS_PASS_THROUGH = "touch.events.pass.through";
20 namespace {
21 constexpr int32_t PUBLISHER_UID = 7800;
22 } // namespace
23
SubscribeEvent(int32_t instanceId)24 void EventPassThroughSubscribeProxy::SubscribeEvent(int32_t instanceId)
25 {
26 TAG_LOGI(AceLogTag::ACE_INPUTKEYFLOW, "Subscribe touch.events.pass.through Event");
27 if (eventPassThroughReceiver_ == nullptr) {
28 // create subscribe info
29 MatchingSkills matchingSkills;
30 // add common events
31 matchingSkills.AddEvent(TOUCH_EVENTS_PASS_THROUGH);
32 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
33 subscribeInfo.SetPublisherUid(PUBLISHER_UID);
34 subscribeInfo.SetThreadMode(EventFwk::CommonEventSubscribeInfo::ThreadMode::HANDLER);
35
36 // init Receiver
37 eventPassThroughReceiver_ = std::make_shared<EventPassThroughSubscriber>(subscribeInfo);
38 eventPassThroughReceiver_->AddInstanceId(instanceId);
39 eventReceiver_ = std::shared_ptr<CommonEventSubscriber>(eventPassThroughReceiver_);
40
41 // create subscription
42 CommonEventManager::SubscribeCommonEvent(eventReceiver_);
43 } else {
44 eventPassThroughReceiver_->AddInstanceId(instanceId);
45 }
46 }
47
UnSubscribeEvent()48 void EventPassThroughSubscribeProxy::UnSubscribeEvent()
49 {
50 if (eventReceiver_ != nullptr) {
51 CommonEventManager::UnSubscribeCommonEvent(eventReceiver_);
52 eventReceiver_ = nullptr;
53 eventPassThroughReceiver_ = nullptr;
54 }
55 }
56
UnSubscribeEvent(int32_t instanceId)57 void EventPassThroughSubscribeProxy::UnSubscribeEvent(int32_t instanceId)
58 {
59 if (eventReceiver_ != nullptr) {
60 if (eventPassThroughReceiver_->EraseContainerAddCheckUnSubscribe(instanceId)) {
61 CommonEventManager::UnSubscribeCommonEvent(eventReceiver_);
62 eventReceiver_ = nullptr;
63 eventPassThroughReceiver_ = nullptr;
64 }
65 }
66 }
67
OnReceiveEvent(const CommonEventData & data)68 void EventPassThroughSubscriber::OnReceiveEvent(const CommonEventData& data)
69 {
70 auto want = data.GetWant();
71 std::string action = want.GetAction();
72 std::string bundleName = want.GetBundle();
73 if (bundleName.empty()) {
74 TAG_LOGI(AceLogTag::ACE_INPUTKEYFLOW, "OnReceiveEvent empty bundleName");
75 return;
76 }
77 bool needPassThrough = false;
78 for (const auto& instanceId : instanceMap_) {
79 auto container = Platform::AceContainer::GetContainer(instanceId);
80 if (!container) {
81 continue;
82 }
83 if (container->GetBundleName() == bundleName) {
84 needPassThrough = true;
85 break;
86 }
87 }
88 if (!needPassThrough) {
89 TAG_LOGI(AceLogTag::ACE_INPUTKEYFLOW, "OnReceiveEvent no matched bundleName");
90 return;
91 }
92
93 if (action == TOUCH_EVENTS_PASS_THROUGH) {
94 TAG_LOGI(AceLogTag::ACE_INPUTKEYFLOW, "OnReceiveEvent touch.events.pass.through Event");
95 AceApplicationInfo::GetInstance().SetTouchEventsPassThroughMode(true);
96 for (const auto& instanceId : instanceMap_) {
97 auto container = Platform::AceContainer::GetContainer(instanceId);
98 if (!container) {
99 continue;
100 }
101 auto taskExecutor = container->GetTaskExecutor();
102 CHECK_NULL_VOID(taskExecutor);
103 taskExecutor->PostTask(
104 [instanceId]() {
105 auto container = Platform::AceContainer::GetContainer(instanceId);
106 CHECK_NULL_VOID(container);
107 container->SetTouchEventsPassThroughMode(true);
108 },
109 TaskExecutor::TaskType::UI, "ArkUIReceiveEventsPassThroughAsync");
110 }
111 }
112 }
113
AddInstanceId(int32_t instanceId)114 void EventPassThroughSubscriber::AddInstanceId(int32_t instanceId)
115 {
116 instanceMap_.emplace(instanceId);
117 }
118
EraseContainerAddCheckUnSubscribe(int32_t instanceId)119 bool EventPassThroughSubscriber::EraseContainerAddCheckUnSubscribe(int32_t instanceId)
120 {
121 instanceMap_.erase(instanceId);
122 return instanceMap_.empty();
123 }
124 } // namespace OHOS::Ace
125