diff --git a/interfaces/inner_api/file_access/include/file_access_ext_connection.h b/interfaces/inner_api/file_access/include/file_access_ext_connection.h index d8f3b16b3c7b45749f8052fae479d01e8827434b..640901dbd689470a2286b3b239f0602cbfb8019b 100644 --- a/interfaces/inner_api/file_access/include/file_access_ext_connection.h +++ b/interfaces/inner_api/file_access/include/file_access_ext_connection.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2024 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 @@ -39,9 +39,11 @@ public: void ConnectFileExtAbility(const AAFwk::Want &want, const sptr &token); void DisconnectFileExtAbility(); bool IsExtAbilityConnected(); + void OnSchedulerDied(const wptr &remote); sptr GetFileExtProxy(); private: + void AddFileAccessDeathRecipient(const sptr &token); struct ThreadLockInfo { std::condition_variable condition; std::mutex mutex; @@ -52,6 +54,19 @@ private: static std::mutex mutex_; std::atomic isConnected_ = {false}; sptr fileExtProxy_; + std::mutex deathRecipientMutex_; + sptr callerDeathRecipient_ = nullptr; +}; + +class FileAccessDeathRecipient : public IRemoteObject::DeathRecipient { +public: + using RemoteDiedHandler = std::function &)>; + explicit FileAccessDeathRecipient(RemoteDiedHandler handler); + virtual ~FileAccessDeathRecipient(); + virtual void OnRemoteDied(const wptr &remote); + +private: + RemoteDiedHandler handler_; }; } // namespace FileAccessFwk } // namespace OHOS diff --git a/interfaces/inner_api/file_access/include/file_access_helper.h b/interfaces/inner_api/file_access/include/file_access_helper.h index e4a6ed4d6c1b0919c37ced5f4957f972652d3e3f..01902b62d847dced8df933a2c8faf400ffdbc4bc 100644 --- a/interfaces/inner_api/file_access/include/file_access_helper.h +++ b/interfaces/inner_api/file_access/include/file_access_helper.h @@ -103,9 +103,6 @@ private: FileAccessHelper(const sptr &token, const std::unordered_map> &cMap); - void AddFileAccessDeathRecipient(const sptr &token); - void OnSchedulerDied(const wptr &remote); - std::shared_ptr GetConnectInfo(const std::string &bundleName); std::shared_ptr GetConnectExtensionInfo(Uri &uri); @@ -115,20 +112,6 @@ private: sptr token_ = nullptr; std::unordered_map> cMap_; - std::mutex deathRecipientMutex_; - - sptr callerDeathRecipient_ = nullptr; -}; - -class FileAccessDeathRecipient : public IRemoteObject::DeathRecipient { -public: - using RemoteDiedHandler = std::function &)>; - explicit FileAccessDeathRecipient(RemoteDiedHandler handler); - virtual ~FileAccessDeathRecipient(); - virtual void OnRemoteDied(const wptr &remote); - -private: - RemoteDiedHandler handler_; }; } // namespace FileAccessFwk } // namespace OHOS diff --git a/interfaces/inner_api/file_access/src/file_access_ext_connection.cpp b/interfaces/inner_api/file_access/src/file_access_ext_connection.cpp index 95747bf4d5f3d00740c52992c1924c2bf9fa2a0b..395ef5d203b55ea28a96f4bad65c6c7b2c5f0f58 100644 --- a/interfaces/inner_api/file_access/src/file_access_ext_connection.cpp +++ b/interfaces/inner_api/file_access/src/file_access_ext_connection.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2024 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 @@ -40,6 +40,7 @@ void FileAccessExtConnection::OnAbilityConnectDone( HILOG_ERROR("fileExtProxy_ is nullptr"); return; } + AddFileAccessDeathRecipient(fileExtProxy_->AsObject()); HILOG_INFO("OnAbilityConnectDone set connected info"); isConnected_.store(true); std::lock_guard lock(connectLockInfo_.mutex); @@ -86,5 +87,50 @@ sptr FileAccessExtConnection::GetFileExtProxy() { return fileExtProxy_; } + +void FileAccessExtConnection::AddFileAccessDeathRecipient(const sptr &token) +{ + std::lock_guard lock(deathRecipientMutex_); + if (token != nullptr && callerDeathRecipient_ != nullptr) { + token->RemoveDeathRecipient(callerDeathRecipient_); + } + if (callerDeathRecipient_ == nullptr) { + callerDeathRecipient_ = + new FileAccessDeathRecipient( + std::bind(&FileAccessExtConnection::OnSchedulerDied, this, std::placeholders::_1)); + } + if (token != nullptr) { + token->AddDeathRecipient(callerDeathRecipient_); + } +} + +void FileAccessExtConnection::OnSchedulerDied(const wptr &remote) +{ + HILOG_ERROR("OnSchedulerDied"); + auto object = remote.promote(); + if (object) { + object = nullptr; + } + isConnected_.store(false); + if (fileExtProxy_) { + fileExtProxy_ = nullptr; + } +} + +void FileAccessDeathRecipient::OnRemoteDied(const wptr &remote) +{ + HILOG_ERROR("OnRemoteDied"); + if (handler_) { + handler_(remote); + } +} + +FileAccessDeathRecipient::FileAccessDeathRecipient(RemoteDiedHandler handler) : handler_(handler) +{ +} + +FileAccessDeathRecipient::~FileAccessDeathRecipient() +{ +} } // namespace FileAccessFwk } // namespace OHOS diff --git a/interfaces/inner_api/file_access/src/file_access_helper.cpp b/interfaces/inner_api/file_access/src/file_access_helper.cpp index 25735bf1aaf29056d54bbcd8df15a51a566e56cc..57b42c8bf0037030e95dacae34fdb114cc0d726a 100644 --- a/interfaces/inner_api/file_access/src/file_access_helper.cpp +++ b/interfaces/inner_api/file_access/src/file_access_helper.cpp @@ -128,27 +128,6 @@ FileAccessHelper::~FileAccessHelper() Release(); } -void FileAccessHelper::AddFileAccessDeathRecipient(const sptr &token) -{ - std::lock_guard lock(deathRecipientMutex_); - if (token != nullptr && callerDeathRecipient_ != nullptr) { - token->RemoveDeathRecipient(callerDeathRecipient_); - } - if (callerDeathRecipient_ == nullptr) { - callerDeathRecipient_ = - new FileAccessDeathRecipient(std::bind(&FileAccessHelper::OnSchedulerDied, this, std::placeholders::_1)); - } - if (token != nullptr) { - token->AddDeathRecipient(callerDeathRecipient_); - } -} - -void FileAccessHelper::OnSchedulerDied(const wptr &remote) -{ - auto object = remote.promote(); - object = nullptr; -} - std::shared_ptr FileAccessHelper::GetConnectInfo(const std::string &bundleName) { auto iterator = cMap_.find(bundleName); @@ -362,21 +341,14 @@ sptr FileAccessHelper::GetProxyByBundleName(const std::strin HILOG_ERROR("GetProxyByUri failed with invalid connectInfo"); return nullptr; } - if (!connectInfo->fileAccessExtConnection->IsExtAbilityConnected()) { connectInfo->fileAccessExtConnection->ConnectFileExtAbility(connectInfo->want, token_); } - auto fileAccessExtProxy = connectInfo->fileAccessExtConnection->GetFileExtProxy(); - if (fileAccessExtProxy) { - AddFileAccessDeathRecipient(fileAccessExtProxy->AsObject()); - } - if (fileAccessExtProxy == nullptr) { HILOG_ERROR("GetProxyByUri failed with invalid fileAccessExtProxy"); return nullptr; } - return fileAccessExtProxy; } @@ -387,12 +359,7 @@ bool FileAccessHelper::GetProxy() if (!connectInfo->fileAccessExtConnection->IsExtAbilityConnected()) { connectInfo->fileAccessExtConnection->ConnectFileExtAbility(connectInfo->want, token_); } - auto fileAccessExtProxy = connectInfo->fileAccessExtConnection->GetFileExtProxy(); - if (fileAccessExtProxy) { - AddFileAccessDeathRecipient(fileAccessExtProxy->AsObject()); - } - if (fileAccessExtProxy == nullptr) { HILOG_ERROR("GetProxy failed with invalid fileAccessExtProxy"); return false; @@ -938,9 +905,7 @@ int FileAccessHelper::GetRoots(std::vector &rootInfoVec) auto connectInfo = iter->second; auto fileAccessExtProxy = connectInfo->fileAccessExtConnection->GetFileExtProxy(); std::vector results; - if (fileAccessExtProxy) { - AddFileAccessDeathRecipient(fileAccessExtProxy->AsObject()); - } else { + if (!fileAccessExtProxy) { HILOG_ERROR("GetFileExtProxy return nullptr, bundle name is %{public}s", iter->first.c_str()); continue; } @@ -1218,20 +1183,5 @@ int FileAccessHelper::MoveFile(Uri &sourceFile, Uri &targetParent, std::string & return ERR_OK; } - -void FileAccessDeathRecipient::OnRemoteDied(const wptr &remote) -{ - if (handler_) { - handler_(remote); - } -} - -FileAccessDeathRecipient::FileAccessDeathRecipient(RemoteDiedHandler handler) : handler_(handler) -{ -} - -FileAccessDeathRecipient::~FileAccessDeathRecipient() -{ -} } // namespace FileAccessFwk } // namespace OHOS \ No newline at end of file