修复连接aams的时候回调过慢时导致的crash

Signed-off-by: liyansheng <liyansheng4@huawei.com>
This commit is contained in:
liyansheng
2024-11-04 16:09:52 +08:00
parent 3d59cd1505
commit b6eb600e68
6 changed files with 106 additions and 12 deletions
+1
View File
@@ -36,6 +36,7 @@ ohos_executable("wukong") {
"${ability_runtime_root}/tools/aa/src/shell_command.cpp",
"./common/src/app_manager.cpp",
"./common/src/component_manager.cpp",
"./common/src/count_down_latch.cpp",
"./common/src/multimode_manager.cpp",
"./common/src/wukong_logger.cpp",
"./common/src/wukong_util.cpp",
+5
View File
@@ -63,6 +63,11 @@ public:
*/
ErrCode BackToPrePage();
bool GetConnectStatus()
{
return connected_;
}
/**
* @brief get locate info.
* @return Return startX_.
+40
View File
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef COUNT_DOWN_LATCH_H
#define COUNT_DOWN_LATCH_H
#include <mutex>
#include <condition_variable>
#include <chrono>
namespace OHOS {
namespace WuKong {
class CountDownLatch {
public:
CountDownLatch(int count);
void countDown();
bool await(std::chrono::milliseconds timeout);
private:
int count_;
bool done_;
std::mutex mtx_;
std::condition_variable cv_;
CountDownLatch();
};
} // namespace WuKong
} // namespace OHOS
#endif // COUNT_DOWN_LATCH_H
+15 -12
View File
@@ -17,6 +17,7 @@
#include "accessibility_ui_test_ability.h"
#include "multimode_manager.h"
#include "count_down_latch.h"
namespace OHOS {
namespace WuKong {
@@ -84,6 +85,10 @@ void ComponentEventMonitor::OnAbilityDisconnected()
void ComponentEventMonitor::OnAccessibilityEvent(const Accessibility::AccessibilityEventInfo& eventInfo)
{
auto cm = ComponentManager::GetInstance();
if (!cm->GetConnectStatus()) {
return;
}
TRACK_LOG_STR("OnAccessibilityEvent Start %u", eventInfo.GetEventType());
TRACK_LOG_STR("current bundle: %s", eventInfo.GetBundleName().c_str());
if (eventInfo.GetBundleName() == permissionBundleName) {
@@ -138,35 +143,33 @@ bool ComponentManager::Connect()
if (connected_ == true) {
return true;
}
std::mutex mtx;
std::unique_lock<std::mutex> uLock(mtx);
std::shared_ptr<ComponentEventMonitor> g_monitorInstance_ = std::make_shared<ComponentEventMonitor>();
std::condition_variable condition;
auto onConnectCallback = [&condition]() {
CountDownLatch latch(1);
auto onConnectCallback = [&latch]() {
std::cout << "Success connect to AAMS" << std::endl;
condition.notify_all();
latch.countDown();
};
g_monitorInstance_->SetOnAbilityConnectCallback(onConnectCallback);
auto ability = Accessibility::AccessibilityUITestAbility::GetInstance();
if (ability->RegisterAbilityListener(g_monitorInstance_) != Accessibility::RET_OK) {
std::cout << "Failed to register ComponentEventMonitor" << std::endl;
ERROR_LOG("Failed to register ComponentEventMonitor");
return false;
}
std::cout << "Start connect to AAMS" << std::endl;
INFO_LOG("Start connect to AAMS");
if (ability->Connect() != Accessibility::RET_OK) {
std::cout << "Failed to connect to AAMS" << std::endl;
ERROR_LOG("Failed to connect to AAMS");
return false;
}
const auto timeout = std::chrono::milliseconds(1000);
if (condition.wait_for(uLock, timeout) == std::cv_status::timeout) {
std::cout << "Wait connection to AAMS timed out" << std::endl;
const auto timeout = std::chrono::milliseconds(2000);
if (!latch.await(timeout)) {
Disconnect();
ERROR_LOG("Wait connection to AAMS timed out");
return false;
}
connected_ = true;
return true;
}
void ComponentManager::Disconnect()
{
auto auita = Accessibility::AccessibilityUITestAbility::GetInstance();
+44
View File
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "count_down_latch.h"
#include <mutex>
#include <condition_variable>
#include <chrono>
namespace OHOS {
namespace WuKong {
CountDownLatch::CountDownLatch(int count) : count_(count), done_(false), mtx_(), cv_() {}
void CountDownLatch::countDown()
{
std::unique_lock<std::mutex> lock(mtx_);
if (--count_ == 0) {
done_ = true;
cv_.notify_all();
}
}
bool CountDownLatch::await(std::chrono::milliseconds timeout)
{
std::unique_lock<std::mutex> lock(mtx_);
if (cv_.wait_for(lock, timeout, [this] { return done_ || count_ == 0; })) {
return true;
} else {
return false;
}
}
} // namespace WuKong
} // namespace OHOS
+1
View File
@@ -29,6 +29,7 @@ ohos_unittest("wukong_ut") {
sources = [
"../common/src/app_manager.cpp",
"../common/src/component_manager.cpp",
"../common/src/count_down_latch.cpp",
"../common/src/multimode_manager.cpp",
"../common/src/wukong_logger.cpp",
"../common/src/wukong_util.cpp",