libaurum: Reduce dbus method call for performance 42/290342/2
authorWoochan Lee <wc0917.lee@samsung.com>
Thu, 23 Mar 2023 09:14:15 +0000 (18:14 +0900)
committerWoochan Lee <wc0917.lee@samsung.com>
Fri, 24 Mar 2023 05:34:47 +0000 (14:34 +0900)
Change-Id: Icd2695fbf24937bd9be624db5b261badb3090d5d

libaurum/src/AurumXML.cc
libaurum/src/Comparer.cc
libaurum/src/Impl/Accessibility/AtspiAccessibleNode.cc
libaurum/src/Impl/Accessibility/AtspiAccessibleWatcher.cc
libaurum/src/UiObject.cc

index ea41c31..e6ef9f6 100644 (file)
@@ -41,7 +41,11 @@ void AurumXML::traverse(xml_node& element, const std::shared_ptr<AccessibleNode>
 {
     if (!node) return;
 
-    node->refresh(false);
+    node->updateUniqueId();
+    node->updateName();
+    node->updateRoleName();
+    node->updateAttributes();
+    node->updateToolkitName();
 
     std::string name;
     if (node->getType().empty())
@@ -57,45 +61,18 @@ void AurumXML::traverse(xml_node& element, const std::shared_ptr<AccessibleNode>
     element.set_name(name.c_str());
 
     element.append_attribute("name") = node->getText().c_str();
-    element.append_attribute("type") = node->getType().c_str();
     element.append_attribute("id") = node->getId().c_str();
     element.append_attribute("automationid") = node->getAutomationId().c_str();
-    element.append_attribute("package") = node->getPkg().c_str();
-
-    const Rect<int> &size = node->getScreenBoundingBox();
-    element.append_attribute("x") = size.mTopLeft.x;
-    element.append_attribute("y") = size.mTopLeft.y;
-    element.append_attribute("width") = size.width();
-    element.append_attribute("height") = size.height();
-
-    const std::vector<std::pair<std::string, bool>> attributes = {
-        {"checked", node->isChecked()},
-        {"checkable", node->isCheckable()},
-        {"clickable", node->isClickable()},
-        {"enabled", node->isEnabled()},
-        {"focused", node->isFocused()},
-        {"focusable", node->isFocusable()},
-        {"scrollable", node->isScrollable()},
-        {"selected", node->isSelected()},
-        {"showing", node->isShowing()},
-        {"active", node->isActive()},
-        {"visible", node->isVisible()},
-        {"selectable", node->isSelectable()},
-        {"highlightable", node->isHighlightable()}
-    };
-    for (const auto& attribute : attributes) {
-        element.append_attribute(attribute.first.c_str()) = attribute.second;
-    }
 
     mXNodeMap[node->getId()] = node;
 
-    int childCnt = node->getChildCount();
-    for (int i = 0; i < childCnt; i++) {
-        const std::shared_ptr<AccessibleNode>& childNode = node->getChildAt(i);
-        if (childNode->getRawHandler() == nullptr) continue;
+    auto children = node->getChildren();
+    for (auto &child : children)
+    {
+        if (child->getRawHandler() == nullptr) continue;
 
         xml_node childElement = element.append_child("");
-        traverse(childElement, childNode);
+        traverse(childElement, child);
     }
 }
 
@@ -188,13 +165,12 @@ std::shared_ptr<AccessibleNode> AurumXML::checkParentNode(const std::shared_ptr<
     xml_node xmlNode = mDoc->select_node(query.c_str()).node();
 
     if (xmlNode) {
-       int childCnt = parent->getChildCount();
-        for (int i = 0; i < childCnt; i++) {
-            const std::shared_ptr<AccessibleNode>& childNode = parent->getChildAt(i);
-            if (childNode->getRawHandler() == nullptr) continue;
+        auto children = node->getChildren();
+        for (auto &child : children) {
+            if (child->getRawHandler() == nullptr) continue;
 
             xml_node childElement = xmlNode.append_child("");
-            traverse(childElement, childNode);
+            traverse(childElement, child);
         }
         return parent;
     }
index 3082249..ec1e402 100644 (file)
@@ -97,13 +97,13 @@ std::vector<std::shared_ptr<AccessibleNode>> Comparer::findObjects(
     if (currentMatch) partialMatches.push_front(currentMatch);
 
     if (!(mSelector->mMaxDepth && (depth+1 > mSelector->mMaxDepth))) {
-        int childCnt = root->getChildCount();
-        for (int i = 0; i < childCnt; i++) {
-            std::shared_ptr<AccessibleNode> childNode = root->getChildAt(i);
-            if (childNode->getRawHandler() == nullptr) continue;
+        auto children = root->getChildren();
+        for (int i = 0; i < (int)children.size(); i++) {
+            auto child = children[i];
+            if (child->getRawHandler() == nullptr) continue;
 
             std::vector<std::shared_ptr<AccessibleNode>> childret =
-                findObjects(childNode, i, depth + 1, partialMatches);
+                findObjects(child, i, depth + 1, partialMatches);
             std::move(std::begin(childret), std::end(childret), std::back_inserter(ret));
             if (!ret.empty() && mEarlyReturn) {
                 LOGI("Object found and earlyReturn");
index 2dc48ce..1301c8b 100644 (file)
@@ -33,7 +33,6 @@ AtspiAccessibleNode::AtspiAccessibleNode(AtspiAccessible *node)
     watcher->attach(shared_from_this());
 
     if (mNode) {
-        this->updateApplication();
         this->updateUniqueId();
         this->updateStates();
     } else {
@@ -70,11 +69,20 @@ std::shared_ptr<AccessibleNode> AtspiAccessibleNode::getChildAt(int index) const
 std::vector<std::shared_ptr<AccessibleNode>> AtspiAccessibleNode::getChildren() const
 {
     std::vector<std::shared_ptr<AccessibleNode>> ret{};
-    int nchild = this->getChildCount();
-    for (int i = 0; i < nchild; i++) {
-        auto child = getChildAt(i);
-        if (child) ret.push_back(child);
+
+    GArray *children = AtspiWrapper::Atspi_accessible_get_children(mNode, NULL);
+    if (children) {
+        ret.reserve(children->len);
+        AtspiAccessible *child = nullptr;
+        for (unsigned int i = 0; i < children->len; i++) {
+            child = g_array_index(children, AtspiAccessible *, i);
+            if (child) {
+                ret.push_back(std::make_shared<AtspiAccessibleNode>(child));
+            }
+        }
+        g_array_free(children, true);
     }
+
     return ret;
 }
 
@@ -109,6 +117,8 @@ void* AtspiAccessibleNode::getRawHandler(void) const
 
 void AtspiAccessibleNode::updateRoleName()
 {
+    if (!mRole.empty()) return;
+
     AtspiWrapper::Atspi_accessible_clear_cache(mNode);
 
     gchar *rolename = AtspiWrapper::Atspi_accessible_get_role_name(mNode, NULL);
@@ -120,6 +130,8 @@ void AtspiAccessibleNode::updateRoleName()
 
 void AtspiAccessibleNode::updateUniqueId()
 {
+    if (!mId.empty()) return;
+
     AtspiWrapper::Atspi_accessible_clear_cache(mNode);
 
     #ifdef TIZEN
@@ -146,6 +158,8 @@ void AtspiAccessibleNode::updateName()
 
 void AtspiAccessibleNode::updateToolkitName()
 {
+    if (!mToolkitName.empty()) return;
+
     AtspiAccessible *app = AtspiWrapper::Atspi_accessible_get_application(mNode, NULL);
     if (app) {
         gchar *toolkitName = AtspiWrapper::Atspi_accessible_get_toolkit_name(app, NULL);
@@ -159,6 +173,8 @@ void AtspiAccessibleNode::updateToolkitName()
 
 void AtspiAccessibleNode::updateApplication()
 {
+    if (!mPkg.empty()) return;
+
     AtspiWrapper::Atspi_accessible_clear_cache(mNode);
 
     AtspiAccessible *app = AtspiWrapper::Atspi_accessible_get_application(mNode, NULL);
@@ -174,6 +190,8 @@ void AtspiAccessibleNode::updateApplication()
 
 void AtspiAccessibleNode::updateAttributes()
 {
+    if (!mType.empty()) return;
+
     AtspiWrapper::Atspi_accessible_clear_cache(mNode);
 
     GHashTable *attributes = AtspiWrapper::Atspi_accessible_get_attributes(mNode, NULL);
@@ -218,8 +236,6 @@ void AtspiAccessibleNode::updateStates()
 
 void AtspiAccessibleNode::updateExtents()
 {
-    AtspiWrapper::Atspi_accessible_clear_cache(mNode);
-
     AtspiComponent *component = AtspiWrapper::Atspi_accessible_get_component_iface(mNode);
     if (component) {
         AtspiRect *screenExtent = AtspiWrapper::Atspi_component_get_extents(
@@ -267,15 +283,13 @@ void AtspiAccessibleNode::updateValue()
 
 void AtspiAccessibleNode::updatePid()
 {
-    AtspiWrapper::Atspi_accessible_clear_cache(mNode);
+    if (mPid > 0) return;
 
     mPid = AtspiWrapper::Atspi_accessible_get_process_id(mNode, NULL);
 }
 
 void AtspiAccessibleNode::updateTextMinBoundingRect()
 {
-    AtspiWrapper::Atspi_accessible_clear_cache(mNode);
-
     AtspiText *text = atspi_accessible_get_text_iface(mNode);
     if (text)
     {
@@ -310,57 +324,66 @@ void AtspiAccessibleNode::refresh(bool updateAll)
     AtspiWrapper::Atspi_accessible_clear_cache(mNode);
 
     if (isValid()) {
-        gchar *rolename = AtspiWrapper::Atspi_accessible_get_role_name(mNode, NULL);
-        if (rolename) {
-            mRole = rolename;
-            g_free(rolename);
+        if (mRole.empty()) {
+            gchar *rolename = AtspiWrapper::Atspi_accessible_get_role_name(mNode, NULL);
+            if (rolename) {
+                mRole = rolename;
+                g_free(rolename);
+            }
         }
     #ifdef TIZEN
-        gchar *uID = AtspiWrapper::Atspi_accessible_get_unique_id(mNode, NULL);
-        if (uID) {
-            mId = uID;
-            g_free(uID);
+        if (mId.empty()) {
+            gchar *uID = AtspiWrapper::Atspi_accessible_get_unique_id(mNode, NULL);
+            if (uID) {
+                mId = uID;
+                g_free(uID);
+            }
         }
     #else
         mId = std::string{"N/A"};
     #endif
-
         gchar *name = AtspiWrapper::Atspi_accessible_get_name(mNode, NULL);
         if (name) {
             mText = name;
             g_free(name);
         }
 
-        gchar *toolkitName = AtspiWrapper::Atspi_accessible_get_toolkit_name(mNode, NULL);
-        if (toolkitName) {
-            mToolkitName = toolkitName;
-            g_free(toolkitName);
+        if (mToolkitName.empty()) {
+            gchar *toolkitName = AtspiWrapper::Atspi_accessible_get_toolkit_name(mNode, NULL);
+            if (toolkitName) {
+                mToolkitName = toolkitName;
+                g_free(toolkitName);
+            }
         }
 
-        AtspiAccessible *app = AtspiWrapper::Atspi_accessible_get_application(mNode, NULL);
-        if (app) {
-            gchar *pkg = AtspiWrapper::Atspi_accessible_get_name(app, NULL);
-            if (pkg) {
-                mPkg = pkg;
-                g_free(pkg);
+        if (mPkg.empty()) {
+            AtspiAccessible *app = AtspiWrapper::Atspi_accessible_get_application(mNode, NULL);
+            if (app) {
+                gchar *pkg = AtspiWrapper::Atspi_accessible_get_name(app, NULL);
+                if (pkg) {
+                    mPkg = pkg;
+                    g_free(pkg);
+                }
+                g_object_unref(app);
             }
-            g_object_unref(app);
         }
 
-        GHashTable *attributes = AtspiWrapper::Atspi_accessible_get_attributes(mNode, NULL);
-        if (attributes) {
-            char *t = (char*)g_hash_table_lookup(attributes, "type");
-            if (!t) t = (char*)g_hash_table_lookup(attributes, "t");
-            if (!t) t = (char*)g_hash_table_lookup(attributes, "class");
-            char *s = (char*)g_hash_table_lookup(attributes, "style");
-            char *a = (char*)g_hash_table_lookup(attributes, "automationId");
-
-            if (t) mType =  std::string(t);
-            else mType = mRole;
-            if (s) mStyle = std::string(s);
-            if (a) mAutomationId = std::string(a);
-
-            g_hash_table_unref(attributes);
+        if (mType.empty()) {
+            GHashTable *attributes = AtspiWrapper::Atspi_accessible_get_attributes(mNode, NULL);
+            if (attributes) {
+                char *t = (char*)g_hash_table_lookup(attributes, "type");
+                if (!t) t = (char*)g_hash_table_lookup(attributes, "t");
+                if (!t) t = (char*)g_hash_table_lookup(attributes, "class");
+                char *s = (char*)g_hash_table_lookup(attributes, "style");
+                char *a = (char*)g_hash_table_lookup(attributes, "automationId");
+
+                if (t) mType =  std::string(t);
+                else mType = mRole;
+                if (s) mStyle = std::string(s);
+                if (a) mAutomationId = std::string(a);
+
+                g_hash_table_unref(attributes);
+            }
         }
 
         AtspiStateSet *st = AtspiWrapper::Atspi_accessible_get_state_set(mNode);
index 1f1ef65..25feacf 100644 (file)
@@ -139,6 +139,8 @@ AtspiAccessibleWatcher::AtspiAccessibleWatcher()
 
     atspi_init();
 
+    AtspiWrapper::Atspi_accessible_set_cache_mask(AtspiWrapper::Atspi_get_desktop(0), ATSPI_CACHE_ALL);
+
     mEventThread = g_thread_new("AtspiEventThread", eventThreadLoop, this);
 
     mDbusProxy = g_dbus_proxy_new_for_bus_sync(
@@ -191,7 +193,6 @@ AtspiAccessibleWatcher::~AtspiAccessibleWatcher()
 
 void AtspiAccessibleWatcher::appendApp(AtspiAccessibleWatcher *instance, AtspiAccessible *app, char *pkg)
 {
-    AtspiWrapper::Atspi_accessible_set_cache_mask(app, ATSPI_CACHE_ALL);
     LOGI("window activated in app(%s)", pkg);
     if (!instance->mActiveAppMap.count(app)) {
         LOGI("add activated window's app in map");
index 82fa073..41ef208 100644 (file)
@@ -144,8 +144,14 @@ std::shared_ptr<UiObject> UiObject::getChildAt(int index) const {
 
 std::vector<std::shared_ptr<UiObject>> UiObject::getChildren() const
 {
-    auto sel = Sel::depth(2);
-    return this->findObjects(sel);
+    std::vector<std::shared_ptr<UiObject>> ret{};
+
+    auto children = getAccessibleNode()->getChildren();
+    for (auto &child : children) {
+        ret.push_back(std::make_shared<UiObject>(mDevice, mSelector, child));
+    }
+
+    return ret;
 }
 
 std::shared_ptr<Node> UiObject::getDescendant()