[AT-SPI] do not keep window in ApplicationAccessible
[platform/core/uifw/dali-adaptor.git] / dali / internal / accessibility / bridge / bridge-base.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/accessibility/bridge/bridge-base.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/devel-api/common/stage.h>
23 #include <atomic>
24 #include <cstdlib>
25 #include <memory>
26
27 // INTERNAL INCLUDES
28 #include <dali/public-api/adaptor-framework/timer.h>
29
30 using namespace Dali::Accessibility;
31
32 static Dali::Timer tickTimer;
33
34 BridgeBase::BridgeBase()
35 {
36 }
37
38 BridgeBase::~BridgeBase()
39 {
40   mApplication.mChildren.clear();
41 }
42
43 void BridgeBase::AddFilteredEvent(FilteredEvents kind, Dali::Accessibility::Accessible* obj, float delay, std::function<void()> functor)
44 {
45   if(delay < 0)
46   {
47     delay = 0;
48   }
49
50   auto it = mFilteredEvents.insert({{kind, obj}, {static_cast<unsigned int>(delay * 10), {}}});
51   if(it.second)
52   {
53     functor();
54   }
55   else
56   {
57     it.first->second.second = std::move(functor);
58   }
59
60   if(!tickTimer)
61   {
62     tickTimer = Dali::Timer::New(100);
63     tickTimer.TickSignal().Connect(this, &BridgeBase::TickFilteredEvents);
64   }
65 }
66
67 bool BridgeBase::TickFilteredEvents()
68 {
69   for(auto it = mFilteredEvents.begin(); it != mFilteredEvents.end();)
70   {
71     if(it->second.first)
72     {
73       --it->second.first;
74     }
75     else
76     {
77       if(it->second.second)
78       {
79         it->second.second();
80         it->second.second = {};
81       }
82       else
83       {
84         it = mFilteredEvents.erase(it);
85         continue;
86       }
87     }
88     ++it;
89   }
90   return !mFilteredEvents.empty();
91 }
92
93 void BridgeBase::UpdateRegisteredEvents()
94 {
95   using ReturnType = std::vector<std::tuple<std::string, std::string>>;
96   mRegistry.method<DBus::ValueOrError<ReturnType>()>("GetRegisteredEvents").asyncCall([this](DBus::ValueOrError<ReturnType> msg) {
97     if(!msg)
98     {
99       LOG() << "Get registered events failed";
100       return;
101     }
102
103     IsBoundsChangedEventAllowed = false;
104
105     ReturnType values = std::get<ReturnType>(msg.getValues());
106     for(long unsigned int i = 0; i < values.size(); i++)
107     {
108       if(!std::get<1>(values[i]).compare("Object:BoundsChanged"))
109       {
110         IsBoundsChangedEventAllowed = true;
111       }
112     }
113   });
114 }
115
116 BridgeBase::ForceUpResult BridgeBase::ForceUp()
117 {
118   //TODO: checking mBusName is enough? or a new variable to check bridge state?
119   if(Bridge::ForceUp() == ForceUpResult::ALREADY_UP && !GetBusName().empty())
120   {
121     return ForceUpResult::ALREADY_UP;
122   }
123   auto proxy = DBus::DBusClient{dbusLocators::atspi::BUS, dbusLocators::atspi::OBJ_PATH, dbusLocators::atspi::BUS_INTERFACE, DBus::ConnectionType::SESSION};
124   auto addr  = proxy.method<std::string()>(dbusLocators::atspi::GET_ADDRESS).call();
125
126   if(!addr)
127   {
128     DALI_LOG_ERROR("failed at call '%s': %s\n", dbusLocators::atspi::GET_ADDRESS, addr.getError().message.c_str());
129     return ForceUpResult::FAILED;
130   }
131
132   mConnectionPtr  = DBusWrapper::Installed()->eldbus_address_connection_get_impl(std::get<0>(addr));
133   mData->mBusName = DBus::getConnectionName(mConnectionPtr);
134   mDbusServer     = {mConnectionPtr};
135
136   {
137     DBus::DBusInterfaceDescription desc{Accessible::GetInterfaceName(AtspiInterface::CACHE)};
138     AddFunctionToInterface(desc, "GetItems", &BridgeBase::GetItems);
139     mDbusServer.addInterface(AtspiDbusPathCache, desc);
140   }
141   {
142     DBus::DBusInterfaceDescription desc{Accessible::GetInterfaceName(AtspiInterface::APPLICATION)};
143     AddGetSetPropertyToInterface(desc, "Id", &BridgeBase::GetId, &BridgeBase::SetId);
144     mDbusServer.addInterface(AtspiPath, desc);
145   }
146
147   mRegistry = {AtspiDbusNameRegistry, AtspiDbusPathRegistry, Accessible::GetInterfaceName(AtspiInterface::REGISTRY), mConnectionPtr};
148
149   UpdateRegisteredEvents();
150
151   mRegistry.addSignal<void(void)>("EventListenerRegistered", [this](void) {
152     UpdateRegisteredEvents();
153   });
154
155   mRegistry.addSignal<void(void)>("EventListenerDeregistered", [this](void) {
156     UpdateRegisteredEvents();
157   });
158
159   return ForceUpResult::JUST_STARTED;
160 }
161
162 void BridgeBase::ForceDown()
163 {
164   Bridge::ForceDown();
165   mRegistry      = {};
166   mDbusServer    = {};
167   mConnectionPtr = {};
168 }
169
170 const std::string& BridgeBase::GetBusName() const
171 {
172   static std::string empty;
173   return mData ? mData->mBusName : empty;
174 }
175
176 Accessible* BridgeBase::FindByPath(const std::string& name) const
177 {
178   try
179   {
180     return Find(name);
181   }
182   catch(std::domain_error&)
183   {
184     return nullptr;
185   }
186 }
187
188 void BridgeBase::AddTopLevelWindow(Accessible* windowAccessible)
189 {
190   if(windowAccessible->GetInternalActor() == nullptr)
191   {
192     return;
193   }
194
195   // Prevent adding the default window twice.
196   if(!mApplication.mChildren.empty() &&
197      mApplication.mChildren[0]->GetInternalActor() == windowAccessible->GetInternalActor())
198   {
199     return;
200   }
201
202   // Adds Window to a list of Windows.
203   mApplication.mChildren.push_back(windowAccessible);
204   SetIsOnRootLevel(windowAccessible);
205
206   RegisterDefaultLabel(windowAccessible);
207 }
208
209 void BridgeBase::RemoveTopLevelWindow(Accessible* windowAccessible)
210 {
211   UnregisterDefaultLabel(windowAccessible);
212
213   for(auto i = 0u; i < mApplication.mChildren.size(); ++i)
214   {
215     if(mApplication.mChildren[i] == windowAccessible)
216     {
217       mApplication.mChildren.erase(mApplication.mChildren.begin() + i);
218       break;
219     }
220   }
221 }
222
223 void BridgeBase::RegisterDefaultLabel(Accessible* object)
224 {
225   if(std::find(mDefaultLabels.begin(), mDefaultLabels.end(), object) == mDefaultLabels.end())
226   {
227     mDefaultLabels.push_back(object);
228   }
229 }
230
231 void BridgeBase::UnregisterDefaultLabel(Accessible* object)
232 {
233   auto it = std::find(mDefaultLabels.begin(), mDefaultLabels.end(), object);
234   if(it != mDefaultLabels.end())
235   {
236     mDefaultLabels.erase(it);
237   }
238 }
239
240 std::string BridgeBase::StripPrefix(const std::string& path)
241 {
242   auto size = strlen(AtspiPath);
243   return path.substr(size + 1);
244 }
245
246 Accessible* BridgeBase::Find(const std::string& path) const
247 {
248   if(path == "root")
249   {
250     return &mApplication;
251   }
252
253   void*              accessible;
254   std::istringstream tmp{path};
255   if(!(tmp >> accessible))
256   {
257     throw std::domain_error{"invalid path '" + path + "'"};
258   }
259
260   auto it = mData->mKnownObjects.find(static_cast<Accessible*>(accessible));
261   if(it == mData->mKnownObjects.end() || (*it)->IsHidden())
262   {
263     throw std::domain_error{"unknown object '" + path + "'"};
264   }
265
266   return static_cast<Accessible*>(accessible);
267 }
268
269 Accessible* BridgeBase::Find(const Address& ptr) const
270 {
271   assert(ptr.GetBus() == mData->mBusName);
272   return Find(ptr.GetPath());
273 }
274
275 Accessible* BridgeBase::FindCurrentObject() const
276 {
277   auto path = DBus::DBusServer::getCurrentObjectPath();
278   auto size = strlen(AtspiPath);
279   if(path.size() <= size)
280   {
281     throw std::domain_error{"invalid path '" + path + "'"};
282   }
283   if(path.substr(0, size) != AtspiPath)
284   {
285     throw std::domain_error{"invalid path '" + path + "'"};
286   }
287   if(path[size] != '/')
288   {
289     throw std::domain_error{"invalid path '" + path + "'"};
290   }
291   return Find(StripPrefix(path));
292 }
293
294 void BridgeBase::SetId(int id)
295 {
296   this->mId = id;
297 }
298
299 int BridgeBase::GetId()
300 {
301   return this->mId;
302 }
303
304 auto BridgeBase::GetItems() -> DBus::ValueOrError<std::vector<CacheElementType>>
305 {
306   auto root = &mApplication;
307
308   std::vector<CacheElementType> res;
309
310   std::function<void(Accessible*)> proc =
311     [&](Accessible* item) {
312       res.emplace_back(std::move(CreateCacheElement(root)));
313       for(auto i = 0u; i < item->GetChildCount(); ++i)
314       {
315         proc(item->GetChildAtIndex(i));
316       }
317     };
318
319   return res;
320 }
321
322 auto BridgeBase::CreateCacheElement(Accessible* item) -> CacheElementType
323 {
324   if(!item)
325   {
326     return {};
327   }
328
329   auto root   = &mApplication;
330   auto parent = item->GetParent();
331
332   std::vector<Address> children;
333   for(auto i = 0u; i < item->GetChildCount(); ++i)
334   {
335     children.emplace_back(item->GetChildAtIndex(i)->GetAddress());
336   }
337
338   return std::make_tuple(
339     item->GetAddress(),
340     root->GetAddress(),
341     parent ? parent->GetAddress() : Address{},
342     children,
343     item->GetInterfacesAsStrings(),
344     item->GetName(),
345     item->GetRole(),
346     item->GetDescription(),
347     item->GetStates().GetRawData());
348 }