!429 对应无障碍服务异常时应该通知客户端连接状态

Merge pull request !429 from Mupceet/release_1015_ondisconnect
This commit is contained in:
openharmony_ci 2022-10-17 02:48:57 +00:00 committed by Gitee
commit a01eaee1ca
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 171 additions and 38 deletions

View File

@ -17,6 +17,7 @@
#define ACCESSIBLE_ABILITY_CLIENT_IMPL_H
#include <memory>
#include <mutex>
#include "accessible_ability_channel_client.h"
#include "accessible_ability_client.h"
#include "accessible_ability_client_stub.h"
@ -34,7 +35,7 @@ public:
/**
* @brief The deconstructor of AccessibleAbilityClientImpl.
*/
~AccessibleAbilityClientImpl() = default;
~AccessibleAbilityClientImpl();
/**
* @brief Gets remote object.
@ -242,6 +243,12 @@ public:
*/
void ResetAAClient(const wptr<IRemoteObject> &remote);
/**
* @brief Notify AA client and clean data when service is died.
* @param remote The object access to AAMS.
*/
void NotifyServiceDied(const wptr<IRemoteObject> &remote);
private:
class AccessibleAbilityDeathRecipient final : public IRemoteObject::DeathRecipient {
public:
@ -254,6 +261,17 @@ private:
AccessibleAbilityClientImpl &client_;
};
class AccessibilityServiceDeathRecipient final : public IRemoteObject::DeathRecipient {
public:
AccessibilityServiceDeathRecipient(AccessibleAbilityClientImpl &client) : client_(client) {}
~AccessibilityServiceDeathRecipient() = default;
DISALLOW_COPY_AND_MOVE(AccessibilityServiceDeathRecipient);
void OnRemoteDied(const wptr<IRemoteObject> &remote);
private:
AccessibleAbilityClientImpl &client_;
};
bool GetCacheElementInfo(const int32_t windowId,
const int32_t elementId, AccessibilityElementInfo &elementInfo) const;
void SetCacheElementInfo(const int32_t windowId,
@ -262,12 +280,14 @@ private:
const uint32_t mode, AccessibilityElementInfo &info);
sptr<IRemoteObject::DeathRecipient> deathRecipient_ = nullptr;
sptr<IRemoteObject::DeathRecipient> accessibilityServiceDeathRecipient_ = nullptr;
sptr<IAccessibleAbilityManagerService> serviceProxy_ = nullptr;
std::shared_ptr<AccessibleAbilityListener> listener_ = nullptr;
std::shared_ptr<AccessibleAbilityChannelClient> channelClient_ = nullptr;
uint32_t cacheMode_ = 0;
int32_t cacheWindowId_ = -1;
std::map<int32_t, AccessibilityElementInfo> cacheElementInfos_;
std::mutex mutex_;
};
} // namespace Accessibility
} // namespace OHOS

View File

@ -68,6 +68,29 @@ AccessibleAbilityClientImpl::AccessibleAbilityClientImpl()
HILOG_ERROR("Get aams proxy failed");
return;
}
// Add death recipient
if (!accessibilityServiceDeathRecipient_) {
accessibilityServiceDeathRecipient_ = new(std::nothrow) AccessibilityServiceDeathRecipient(*this);
if (!accessibilityServiceDeathRecipient_) {
HILOG_ERROR("Failed to create service deathRecipient.");
return;
}
}
if (serviceProxy_->AsObject()) {
HILOG_DEBUG("Add death recipient");
serviceProxy_->AsObject()->AddDeathRecipient(accessibilityServiceDeathRecipient_);
}
}
AccessibleAbilityClientImpl::~AccessibleAbilityClientImpl()
{
HILOG_DEBUG();
if (serviceProxy_ && serviceProxy_->AsObject()) {
HILOG_DEBUG("Remove service death recipient");
serviceProxy_->AsObject()->RemoveDeathRecipient(accessibilityServiceDeathRecipient_);
}
}
sptr<IRemoteObject> AccessibleAbilityClientImpl::GetRemoteObject()
@ -83,6 +106,7 @@ sptr<IRemoteObject> AccessibleAbilityClientImpl::GetRemoteObject()
bool AccessibleAbilityClientImpl::RegisterAbilityListener(const std::shared_ptr<AccessibleAbilityListener> &listener)
{
HILOG_INFO();
std::lock_guard<std::mutex> lock(mutex_);
if (listener_) {
HILOG_DEBUG("listener already exists.");
return false;
@ -100,11 +124,14 @@ void AccessibleAbilityClientImpl::Init(const sptr<IAccessibleAbilityChannel> &ch
return;
}
std::shared_ptr<AccessibleAbilityListener> listener = nullptr;
{
std::lock_guard<std::mutex> lock(mutex_);
if (!listener_) {
HILOG_ERROR("listener_ is nullptr.");
return;
}
listener = listener_;
channelClient_ = std::make_shared<AccessibleAbilityChannelClient>(channelId, channel);
// Add death recipient
@ -116,19 +143,25 @@ void AccessibleAbilityClientImpl::Init(const sptr<IAccessibleAbilityChannel> &ch
}
}
auto object = channelClient_->GetRemote();
if (object) {
if (channel->AsObject()) {
HILOG_DEBUG("Add death recipient");
object->AddDeathRecipient(deathRecipient_);
channel->AsObject()->AddDeathRecipient(deathRecipient_);
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_WINDOW_REGIST));
listener_->OnAbilityConnected();
if (listener) {
listener->OnAbilityConnected();
}
}
void AccessibleAbilityClientImpl::Disconnect(const int32_t channelId)
{
HILOG_INFO("channelId[%{public}d]", channelId);
std::shared_ptr<AccessibleAbilityListener> listener = nullptr;
{
std::lock_guard<std::mutex> lock(mutex_);
// Delete death recipient
if (channelClient_ && channelClient_->GetRemote()) {
HILOG_ERROR("Remove death recipient");
@ -137,42 +170,59 @@ void AccessibleAbilityClientImpl::Disconnect(const int32_t channelId)
// Remove channel
channelClient_ = nullptr;
if (listener_) {
listener_->OnAbilityDisconnected();
listener = listener_;
listener_ = nullptr;
}
if (listener) {
listener->OnAbilityDisconnected();
}
}
void AccessibleAbilityClientImpl::OnAccessibilityEvent(const AccessibilityEventInfo &eventInfo)
{
HILOG_INFO();
std::shared_ptr<AccessibleAbilityListener> listener = nullptr;
{
std::lock_guard<std::mutex> lock(mutex_);
if (!channelClient_) {
HILOG_ERROR("The channel is invalid.");
return;
}
if (listener_) {
listener_->OnAccessibilityEvent(eventInfo);
listener = listener_;
}
if (listener) {
listener->OnAccessibilityEvent(eventInfo);
}
}
void AccessibleAbilityClientImpl::OnKeyPressEvent(const MMI::KeyEvent &keyEvent, const int32_t sequence)
{
HILOG_INFO("sequence[%{public}d]", sequence);
if (!channelClient_) {
std::shared_ptr<AccessibleAbilityListener> listener = nullptr;
std::shared_ptr<AccessibleAbilityChannelClient> channel = nullptr;
{
std::lock_guard<std::mutex> lock(mutex_);
listener = listener_;
channel = channelClient_;
}
if (!channel) {
HILOG_ERROR("The channel is invalid.");
return;
}
if (listener_) {
bool handled = false;
if (listener) {
std::shared_ptr<MMI::KeyEvent> tmp = std::make_shared<MMI::KeyEvent>(keyEvent);
bool handled = listener_->OnKeyPressEvent(tmp);
channelClient_->SetOnKeyPressEventResult(handled, sequence);
handled = listener->OnKeyPressEvent(tmp);
}
channel->SetOnKeyPressEventResult(handled, sequence);
}
bool AccessibleAbilityClientImpl::GetFocus(const int32_t focusType, AccessibilityElementInfo &elementInfo)
{
HILOG_INFO("focusType[%{public}d]", focusType);
std::lock_guard<std::mutex> lock(mutex_);
if ((focusType != FOCUS_TYPE_INPUT) && (focusType != FOCUS_TYPE_ACCESSIBILITY)) {
HILOG_ERROR("focusType is not allowed.");
return false;
@ -190,6 +240,7 @@ bool AccessibleAbilityClientImpl::GetFocusByElementInfo(const AccessibilityEleme
const int32_t focusType, AccessibilityElementInfo &elementInfo)
{
HILOG_INFO("focusType[%{public}d]", focusType);
std::lock_guard<std::mutex> lock(mutex_);
if ((focusType != FOCUS_TYPE_INPUT) && (focusType != FOCUS_TYPE_ACCESSIBILITY)) {
HILOG_ERROR("focusType is not allowed.");
return false;
@ -210,6 +261,7 @@ bool AccessibleAbilityClientImpl::GetFocusByElementInfo(const AccessibilityEleme
bool AccessibleAbilityClientImpl::InjectGesture(const std::shared_ptr<AccessibilityGestureInjectPath> &gesturePath)
{
HILOG_INFO();
std::lock_guard<std::mutex> lock(mutex_);
if (!gesturePath) {
HILOG_ERROR("The gesturePath is null.");
@ -234,6 +286,7 @@ bool AccessibleAbilityClientImpl::InjectGesture(const std::shared_ptr<Accessibil
bool AccessibleAbilityClientImpl::GetRoot(AccessibilityElementInfo &elementInfo)
{
HILOG_DEBUG();
std::lock_guard<std::mutex> lock(mutex_);
if (!channelClient_ || !serviceProxy_) {
HILOG_ERROR("The channel is invalid.");
return false;
@ -257,6 +310,7 @@ bool AccessibleAbilityClientImpl::GetRootByWindow(const AccessibilityWindowInfo
AccessibilityElementInfo &elementInfo)
{
HILOG_DEBUG();
std::lock_guard<std::mutex> lock(mutex_);
if (!channelClient_) {
HILOG_ERROR("The channel is invalid.");
return false;
@ -279,6 +333,7 @@ bool AccessibleAbilityClientImpl::GetRootByWindow(const AccessibilityWindowInfo
bool AccessibleAbilityClientImpl::GetWindow(const int32_t windowId, AccessibilityWindowInfo &windowInfo)
{
HILOG_INFO("windowId[%{public}d]", windowId);
std::lock_guard<std::mutex> lock(mutex_);
if (!channelClient_) {
HILOG_ERROR("The channel is invalid.");
return false;
@ -289,6 +344,7 @@ bool AccessibleAbilityClientImpl::GetWindow(const int32_t windowId, Accessibilit
bool AccessibleAbilityClientImpl::GetWindows(std::vector<AccessibilityWindowInfo> &windows)
{
HILOG_INFO();
std::lock_guard<std::mutex> lock(mutex_);
if (!channelClient_) {
HILOG_ERROR("The channel is invalid.");
return false;
@ -299,6 +355,7 @@ bool AccessibleAbilityClientImpl::GetWindows(std::vector<AccessibilityWindowInfo
bool AccessibleAbilityClientImpl::GetWindows(const uint64_t displayId, std::vector<AccessibilityWindowInfo> &windows)
{
HILOG_INFO("displayId[%{public}" PRIu64 "]", displayId);
std::lock_guard<std::mutex> lock(mutex_);
if (!channelClient_) {
HILOG_ERROR("The channel is invalid.");
return false;
@ -311,6 +368,7 @@ bool AccessibleAbilityClientImpl::GetNext(const AccessibilityElementInfo &elemen
{
HILOG_INFO("windowId[%{public}d], elementId[%{public}d], direction[%{public}d]",
elementInfo.GetWindowId(), elementInfo.GetAccessibilityId(), direction);
std::lock_guard<std::mutex> lock(mutex_);
if (!channelClient_) {
HILOG_ERROR("The channel is invalid.");
return false;
@ -327,6 +385,7 @@ bool AccessibleAbilityClientImpl::GetChildElementInfo(const int32_t index, const
AccessibilityElementInfo &child)
{
HILOG_DEBUG();
std::lock_guard<std::mutex> lock(mutex_);
if (!channelClient_) {
HILOG_ERROR("The channel is invalid.");
return false;
@ -355,6 +414,7 @@ bool AccessibleAbilityClientImpl::GetChildren(const AccessibilityElementInfo &pa
std::vector<AccessibilityElementInfo> &children)
{
HILOG_DEBUG();
std::lock_guard<std::mutex> lock(mutex_);
if (!channelClient_) {
HILOG_ERROR("The channel is invalid.");
return false;
@ -390,6 +450,7 @@ bool AccessibleAbilityClientImpl::GetByContent(const AccessibilityElementInfo &e
std::vector<AccessibilityElementInfo> &elementInfos)
{
HILOG_DEBUG();
std::lock_guard<std::mutex> lock(mutex_);
if (!channelClient_) {
HILOG_ERROR("The channel is invalid.");
return false;
@ -404,6 +465,7 @@ bool AccessibleAbilityClientImpl::GetSource(const AccessibilityEventInfo &eventI
AccessibilityElementInfo &elementInfo)
{
HILOG_DEBUG();
std::lock_guard<std::mutex> lock(mutex_);
if (!channelClient_) {
HILOG_ERROR("The channel is invalid.");
return false;
@ -427,6 +489,7 @@ bool AccessibleAbilityClientImpl::GetParentElementInfo(const AccessibilityElemen
AccessibilityElementInfo &parent)
{
HILOG_DEBUG();
std::lock_guard<std::mutex> lock(mutex_);
if (!channelClient_) {
HILOG_ERROR("The channel is invalid.");
return false;
@ -450,6 +513,7 @@ bool AccessibleAbilityClientImpl::ExecuteAction(const AccessibilityElementInfo &
const std::map<std::string, std::string> &actionArguments)
{
HILOG_DEBUG();
std::lock_guard<std::mutex> lock(mutex_);
if (!channelClient_) {
HILOG_ERROR("The channel is invalid.");
return false;
@ -468,6 +532,7 @@ bool AccessibleAbilityClientImpl::ExecuteAction(const AccessibilityElementInfo &
bool AccessibleAbilityClientImpl::SetTargetBundleName(const std::vector<std::string> &targetBundleNames)
{
HILOG_INFO("targetBundleNames size[%{public}zu]", targetBundleNames.size());
std::lock_guard<std::mutex> lock(mutex_);
if (!channelClient_) {
HILOG_ERROR("The channel is invalid.");
return false;
@ -481,9 +546,41 @@ void AccessibleAbilityClientImpl::AccessibleAbilityDeathRecipient::OnRemoteDied(
client_.ResetAAClient(remote);
}
void AccessibleAbilityClientImpl::AccessibilityServiceDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
{
HILOG_ERROR();
client_.NotifyServiceDied(remote);
}
void AccessibleAbilityClientImpl::NotifyServiceDied(const wptr<IRemoteObject> &remote)
{
std::shared_ptr<AccessibleAbilityListener> listener = nullptr;
{
std::lock_guard<std::mutex> lock(mutex_);
if (!serviceProxy_) {
HILOG_ERROR("serviceProxy_ is nullptr");
return;
}
sptr<IRemoteObject> object = serviceProxy_->AsObject();
if (object && (remote == object)) {
listener = listener_;
listener_ = nullptr;
object->RemoveDeathRecipient(accessibilityServiceDeathRecipient_);
channelClient_ = nullptr;
HILOG_DEBUG("ResetAAClient OK");
}
}
if (listener) {
listener->OnAbilityDisconnected();
}
}
void AccessibleAbilityClientImpl::ResetAAClient(const wptr<IRemoteObject> &remote)
{
HILOG_DEBUG();
std::lock_guard<std::mutex> lock(mutex_);
if (channelClient_) {
sptr<IRemoteObject> object = channelClient_->GetRemote();
if (object && (remote == object)) {
@ -497,6 +594,7 @@ void AccessibleAbilityClientImpl::ResetAAClient(const wptr<IRemoteObject> &remot
void AccessibleAbilityClientImpl::SetCacheMode(const int32_t cacheMode)
{
HILOG_INFO("set cache mode: [%{public}d]", cacheMode);
std::lock_guard<std::mutex> lock(mutex_);
cacheWindowId_ = -1;
cacheElementInfos_.clear();
if (cacheMode < 0) {

View File

@ -844,5 +844,20 @@ HWTEST_F(AccessibleAbilityClientImplTest, GetWindow_002, TestSize.Level1)
EXPECT_TRUE(instance_->GetWindow(0, windowInfo));
GTEST_LOG_(INFO) << "GetWindow_002 end";
}
/**
* @tc.number: NotifyServiceDied_001
* @tc.name: NotifyServiceDied
* @tc.desc: Test function NotifyServiceDied
*/
HWTEST_F(AccessibleAbilityClientImplTest, NotifyServiceDied_001, TestSize.Level1)
{
GTEST_LOG_(INFO) << "NotifyServiceDied_001 start";
Connect();
wptr<IRemoteObject> remote = nullptr;
instance_->NotifyServiceDied(remote);
EXPECT_EQ(AccessibilityAbilityHelper::GetInstance().GetTestChannelId(), static_cast<int>(CHANNEL_ID));
GTEST_LOG_(INFO) << "NotifyServiceDied_001 end";
}
} // namespace Accessibility
} // namespace OHOS