diff --git a/frameworks/innerkits/file_access/include/file_access_ext_connection.h b/frameworks/innerkits/file_access/include/file_access_ext_connection.h index e47b0cffd9a9cd1cb0abdcc1414f3caf827135d0..e0565b5135e2bd575c8b9e2f018639feee47803d 100644 --- a/frameworks/innerkits/file_access/include/file_access_ext_connection.h +++ b/frameworks/innerkits/file_access/include/file_access_ext_connection.h @@ -31,21 +31,22 @@ public: virtual ~FileAccessExtConnection() = default; static sptr GetInstance(); - void OnAbilityConnectDone( const AppExecFwk::ElementName &element, const sptr &remoteObject, int resultCode) override; - void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) override; - void ConnectFileExtAbility(const AAFwk::Want &want, const sptr &token); - void DisconnectFileExtAbility(); - bool IsExtAbilityConnected(); - sptr GetFileExtProxy(); private: + struct ThreadLockInfo { + std::condition_variable condition; + std::mutex mutex; + bool isReady = false; + }; + ThreadLockInfo connectLockInfo_; + static sptr instance_; static std::mutex mutex_; std::atomic isConnected_ = {false}; diff --git a/frameworks/innerkits/file_access/src/file_access_ext_connection.cpp b/frameworks/innerkits/file_access/src/file_access_ext_connection.cpp index e7e9604213e8ac10430d82dd8cc09d3760a57b9b..a07a3fb784d57d11a9257c15b0cd3cdc3849853e 100644 --- a/frameworks/innerkits/file_access/src/file_access_ext_connection.cpp +++ b/frameworks/innerkits/file_access/src/file_access_ext_connection.cpp @@ -21,6 +21,9 @@ namespace OHOS { namespace FileAccessFwk { +namespace { + constexpr int WAIT_TIME = 1; // second +} sptr FileAccessExtConnection::instance_ = nullptr; std::mutex FileAccessExtConnection::mutex_; @@ -39,15 +42,18 @@ void FileAccessExtConnection::OnAbilityConnectDone( const AppExecFwk::ElementName &element, const sptr &remoteObject, int resultCode) { if (remoteObject == nullptr) { - HILOG_ERROR("%{public}s failed, remote is nullptr", __func__); + HILOG_ERROR("remote is nullptr"); return; } fileExtProxy_ = iface_cast(remoteObject); if (fileExtProxy_ == nullptr) { - HILOG_ERROR("%{public}s failed, fileExtProxy_ is nullptr", __func__); + HILOG_ERROR("fileExtProxy_ is nullptr"); return; } isConnected_.store(true); + std::lock_guard lock(connectLockInfo_.mutex); + connectLockInfo_.isReady = true; + connectLockInfo_.condition.notify_all(); } void FileAccessExtConnection::OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) @@ -59,7 +65,15 @@ void FileAccessExtConnection::OnAbilityDisconnectDone(const AppExecFwk::ElementN void FileAccessExtConnection::ConnectFileExtAbility(const AAFwk::Want &want, const sptr &token) { ErrCode ret = AAFwk::AbilityManagerClient::GetInstance()->ConnectAbility(want, this, token); - HILOG_INFO("%{public}s called end, ret=%{public}d", __func__, ret); + if (ret != ERR_OK) { + HILOG_INFO("ConnectAbility ret=%{public}d", ret); + return; + } + std::unique_lock lock(connectLockInfo_.mutex); + if (!connectLockInfo_.condition.wait_for(lock, std::chrono::seconds(WAIT_TIME), + [this] { return fileExtProxy_ != nullptr && connectLockInfo_.isReady; })) { + HILOG_INFO("Wait connect timeout."); + } } void FileAccessExtConnection::DisconnectFileExtAbility()