From f6545da659bd74cfcf664cfa46f0a83a6af7840c Mon Sep 17 00:00:00 2001 From: zhangxiaoliang25 Date: Tue, 23 Jul 2024 20:34:10 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=AE=89=E5=85=A8=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhangxiaoliang25 Change-Id: Ia92f4474e9f7a7806ae13b6c77437f84eb0de8a3 Signed-off-by: zhangxiaoliang25 --- bundle.json | 3 +- interfaces/inner_api/file_access/BUILD.gn | 2 +- ...ion.h => app_file_access_ext_connection.h} | 14 +- .../file_access/include/file_access_helper.h | 4 +- .../libfile_access_extension_ability_kit.map | 2 +- ...cpp => app_file_access_ext_connection.cpp} | 49 +++--- .../file_access/src/file_access_helper.cpp | 8 +- .../src/js_file_access_ext_ability.cpp | 2 +- services/BUILD.gn | 2 + .../include/file_access_ext_connection.h | 77 ++++++++++ .../include/file_access_service.h | 25 ++- .../file_access_service_ipc_interface_code.h | 4 +- .../include/file_access_service_proxy.h | 7 +- .../include/file_access_service_stub.h | 2 + .../include/holder_manager.h | 7 + .../include/ifile_access_service_base.h | 6 +- .../src/file_access_ext_connection.cpp | 144 ++++++++++++++++++ .../src/file_access_service.cpp | 122 ++++++++++++++- .../src/file_access_service_proxy.cpp | 97 +++++++++++- .../src/file_access_service_stub.cpp | 53 ++++++- .../fileaccessextconnection_fuzzer/BUILD.gn | 4 +- .../fileaccessextconnection_fuzzer.cpp | 17 +++ .../fileaccessservicemock.h | 5 +- .../fileaccessserviceproxy_fuzzer.cpp | 2 +- .../fileaccessservicemock.h | 5 +- .../file_access_service_proxy_test.cpp | 16 +- .../js_file_access_ext_ability_test.cpp | 13 +- test/unittest/mock/file_access_service_mock.h | 5 +- 28 files changed, 631 insertions(+), 66 deletions(-) rename interfaces/inner_api/file_access/include/{file_access_ext_connection.h => app_file_access_ext_connection.h} (86%) rename interfaces/inner_api/file_access/src/{file_access_ext_connection.cpp => app_file_access_ext_connection.cpp} (71%) create mode 100644 services/native/file_access_service/include/file_access_ext_connection.h create mode 100644 services/native/file_access_service/src/file_access_ext_connection.cpp diff --git a/bundle.json b/bundle.json index 450283ee..a3be8dd5 100644 --- a/bundle.json +++ b/bundle.json @@ -66,7 +66,8 @@ "header_files": [ "file_access_ext_ability.h", "file_access_extension_info.h", - "file_access_ext_stub_impl.h" + "file_access_ext_stub_impl.h", + "file_access_ext_proxy.h" ], "header_base": "//foundation/filemanagement/user_file_service/interfaces/inner_api/file_access/include" } diff --git a/interfaces/inner_api/file_access/BUILD.gn b/interfaces/inner_api/file_access/BUILD.gn index 4cb4a40e..fa96e19c 100644 --- a/interfaces/inner_api/file_access/BUILD.gn +++ b/interfaces/inner_api/file_access/BUILD.gn @@ -56,8 +56,8 @@ ohos_shared_library("file_access_extension_ability_kit") { sources = [ "${user_file_service_path}/services/native/file_access_service/src/file_access_service_proxy.cpp", + "src/app_file_access_ext_connection.cpp", "src/file_access_ext_ability.cpp", - "src/file_access_ext_connection.cpp", "src/file_access_ext_proxy.cpp", "src/file_access_ext_stub.cpp", "src/file_access_ext_stub_impl.cpp", diff --git a/interfaces/inner_api/file_access/include/file_access_ext_connection.h b/interfaces/inner_api/file_access/include/app_file_access_ext_connection.h similarity index 86% rename from interfaces/inner_api/file_access/include/file_access_ext_connection.h rename to interfaces/inner_api/file_access/include/app_file_access_ext_connection.h index ee18f6aa..b41bd4b6 100644 --- a/interfaces/inner_api/file_access/include/file_access_ext_connection.h +++ b/interfaces/inner_api/file_access/include/app_file_access_ext_connection.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef FILE_ACCESS_EXT_CONNECTION_H -#define FILE_ACCESS_EXT_CONNECTION_H +#ifndef APP_FILE_ACCESS_EXT_CONNECTION_H +#define APP_FILE_ACCESS_EXT_CONNECTION_H #include #include @@ -28,15 +28,15 @@ namespace OHOS { namespace FileAccessFwk { -class FileAccessExtConnection : public AAFwk::AbilityConnectionStub { +class AppFileAccessExtConnection : public AAFwk::AbilityConnectionStub { public: - FileAccessExtConnection() = default; - virtual ~FileAccessExtConnection(); + AppFileAccessExtConnection() = default; + virtual ~AppFileAccessExtConnection(); 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 ConnectFileExtAbility(const AAFwk::Want &want); void DisconnectFileExtAbility(); bool IsExtAbilityConnected(); void OnSchedulerDied(const wptr &remote); @@ -71,4 +71,4 @@ private: }; } // namespace FileAccessFwk } // namespace OHOS -#endif // FILE_ACCESS_EXT_CONNECTION_H +#endif // APP_FILE_ACCESS_EXT_CONNECTION_H 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 01902b62..ade315c6 100644 --- a/interfaces/inner_api/file_access/include/file_access_helper.h +++ b/interfaces/inner_api/file_access/include/file_access_helper.h @@ -25,7 +25,7 @@ #include "bundle_mgr_interface.h" #include "context.h" -#include "file_access_ext_connection.h" +#include "app_file_access_ext_connection.h" #include "file_access_extension_info.h" #include "ifile_access_ext_base.h" #include "iobserver_callback.h" @@ -50,7 +50,7 @@ namespace { struct ConnectInfo { AAFwk::Want want = {}; - sptr fileAccessExtConnection = nullptr; + sptr fileAccessExtConnection = nullptr; }; class FileAccessHelper final : public std::enable_shared_from_this { diff --git a/interfaces/inner_api/file_access/libfile_access_extension_ability_kit.map b/interfaces/inner_api/file_access/libfile_access_extension_ability_kit.map index 2bcb7a53..78a0937a 100644 --- a/interfaces/inner_api/file_access/libfile_access_extension_ability_kit.map +++ b/interfaces/inner_api/file_access/libfile_access_extension_ability_kit.map @@ -21,7 +21,7 @@ *OHOS::FileAccessFwk::FileAccessNotifyManager*; *OHOS::FileAccessFwk::FileAccessExtStub*; *OHOS::FileAccessFwk::FileAccessExtStubImpl*; - *OHOS::FileAccessFwk::FileAccessExtConnection*; + *OHOS::FileAccessFwk::FileAccessExtProxy*; }; local: *; diff --git a/interfaces/inner_api/file_access/src/file_access_ext_connection.cpp b/interfaces/inner_api/file_access/src/app_file_access_ext_connection.cpp similarity index 71% rename from interfaces/inner_api/file_access/src/file_access_ext_connection.cpp rename to interfaces/inner_api/file_access/src/app_file_access_ext_connection.cpp index be93711e..5ce13649 100644 --- a/interfaces/inner_api/file_access/src/file_access_ext_connection.cpp +++ b/interfaces/inner_api/file_access/src/app_file_access_ext_connection.cpp @@ -13,22 +13,23 @@ * limitations under the License. */ -#include "file_access_ext_connection.h" +#include "app_file_access_ext_connection.h" #include +#include #include "ability_connect_callback_interface.h" #include "ability_manager_client.h" -#include "file_access_ext_proxy.h" +#include "file_access_service_proxy.h" #include "hilog_wrapper.h" #include "iremote_broker.h" namespace OHOS { + namespace FileAccessFwk { +std::mutex AppFileAccessExtConnection::mutex_; -std::mutex FileAccessExtConnection::mutex_; - -void FileAccessExtConnection::OnAbilityConnectDone( +void AppFileAccessExtConnection::OnAbilityConnectDone( const AppExecFwk::ElementName &element, const sptr &remoteObject, int resultCode) { std::lock_guard lock(proxyMutex_); @@ -36,7 +37,7 @@ void FileAccessExtConnection::OnAbilityConnectDone( HILOG_ERROR("remote is nullptr"); return; } - fileExtProxy_ = iface_cast(remoteObject); + fileExtProxy_ = iface_cast(remoteObject); if (fileExtProxy_ == nullptr) { HILOG_ERROR("fileExtProxy_ is nullptr"); return; @@ -48,7 +49,7 @@ void FileAccessExtConnection::OnAbilityConnectDone( connectLockInfo_.condition.notify_all(); } -void FileAccessExtConnection::OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) +void AppFileAccessExtConnection::OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) { std::lock_guard lock(proxyMutex_); if (fileExtProxy_) { @@ -58,14 +59,20 @@ void FileAccessExtConnection::OnAbilityDisconnectDone(const AppExecFwk::ElementN HILOG_INFO("OnAbilityDisconnectDone resultCode=%{public}d", resultCode); } -void FileAccessExtConnection::ConnectFileExtAbility(const AAFwk::Want &want, const sptr &token) +void AppFileAccessExtConnection::ConnectFileExtAbility(const AAFwk::Want &want) { std::unique_lock lock(proxyMutex_); if (fileExtProxy_) { fileExtProxy_ = nullptr; } isConnected_.store(false); - ErrCode ret = AAFwk::AbilityManagerClient::GetInstance()->ConnectAbility(want, this, token); + auto proxy = FileAccessServiceProxy::GetInstance(); + if (proxy == nullptr) { + HILOG_ERROR("ConnectFileExtAbility FileAccessServiceProxy GetInstance fail"); + return; + } + + ErrCode ret = proxy->ConnectFileExtAbility(want, this); if (ret != ERR_OK) { HILOG_ERROR("ConnectAbility failed, ret=%{public}d", ret); return; @@ -79,13 +86,18 @@ void FileAccessExtConnection::ConnectFileExtAbility(const AAFwk::Want &want, con HILOG_INFO("ConnectFileExtAbility success"); } -void FileAccessExtConnection::DisconnectFileExtAbility() +void AppFileAccessExtConnection::DisconnectFileExtAbility() { std::lock_guard lock(proxyMutex_); if (fileExtProxy_ != nullptr) { RemoveFileAccessDeathRecipient(fileExtProxy_->AsObject()); } - ErrCode ret = AAFwk::AbilityManagerClient::GetInstance()->DisconnectAbility(this); + auto proxy = FileAccessServiceProxy::GetInstance(); + if (proxy == nullptr) { + HILOG_ERROR("DisconnectFileExtAbility FileAccessServiceProxy GetInstance fail"); + return; + } + ErrCode ret = proxy->DisConnectFileExtAbility(this); if (ret != ERR_OK) { HILOG_ERROR("DisconnectAbility failed, ret=%{public}d", ret); return; @@ -97,17 +109,17 @@ void FileAccessExtConnection::DisconnectFileExtAbility() HILOG_INFO("DisconnectFileExtAbility done"); } -bool FileAccessExtConnection::IsExtAbilityConnected() +bool AppFileAccessExtConnection::IsExtAbilityConnected() { return isConnected_.load(); } -sptr FileAccessExtConnection::GetFileExtProxy() +sptr AppFileAccessExtConnection::GetFileExtProxy() { return fileExtProxy_; } -void FileAccessExtConnection::AddFileAccessDeathRecipient(const sptr &token) +void AppFileAccessExtConnection::AddFileAccessDeathRecipient(const sptr &token) { std::lock_guard lock(deathRecipientMutex_); if (token != nullptr && callerDeathRecipient_ != nullptr) { @@ -116,14 +128,14 @@ void FileAccessExtConnection::AddFileAccessDeathRecipient(const sptrAddDeathRecipient(callerDeathRecipient_); } } -void FileAccessExtConnection::RemoveFileAccessDeathRecipient(const sptr &token) +void AppFileAccessExtConnection::RemoveFileAccessDeathRecipient(const sptr &token) { std::lock_guard lock(deathRecipientMutex_); if (token != nullptr && callerDeathRecipient_ != nullptr) { @@ -131,7 +143,7 @@ void FileAccessExtConnection::RemoveFileAccessDeathRecipient(const sptr &remote) +void AppFileAccessExtConnection::OnSchedulerDied(const wptr &remote) { std::lock_guard lock(proxyMutex_); HILOG_ERROR("OnSchedulerDied"); @@ -145,8 +157,9 @@ void FileAccessExtConnection::OnSchedulerDied(const wptr &remote) } } -FileAccessExtConnection::~FileAccessExtConnection() +AppFileAccessExtConnection::~AppFileAccessExtConnection() { + HILOG_INFO("~AppFileAccessExtConnection"); if (isConnected_.load()) { DisconnectFileExtAbility(); } 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 b65cc040..36419b3c 100644 --- a/interfaces/inner_api/file_access/src/file_access_helper.cpp +++ b/interfaces/inner_api/file_access/src/file_access_helper.cpp @@ -198,13 +198,13 @@ std::pair, int> FileAccessHelper::DoCreatorHel { std::unordered_map> cMap; for (size_t i = 0; i < wants.size(); i++) { - sptr fileAccessExtConnection(new(std::nothrow) FileAccessExtConnection()); + sptr fileAccessExtConnection(new(std::nothrow) AppFileAccessExtConnection()); if (fileAccessExtConnection == nullptr) { HILOG_ERROR("new fileAccessExtConnection fail"); return {nullptr, E_GETRESULT}; } if (!fileAccessExtConnection->IsExtAbilityConnected()) { - fileAccessExtConnection->ConnectFileExtAbility(wants[i], token); + fileAccessExtConnection->ConnectFileExtAbility(wants[i]); } sptr fileExtProxy = fileAccessExtConnection->GetFileExtProxy(); if (fileExtProxy == nullptr) { @@ -346,7 +346,7 @@ sptr FileAccessHelper::GetProxyByBundleName(const std::strin return nullptr; } if (!connectInfo->fileAccessExtConnection->IsExtAbilityConnected()) { - connectInfo->fileAccessExtConnection->ConnectFileExtAbility(connectInfo->want, token_); + connectInfo->fileAccessExtConnection->ConnectFileExtAbility(connectInfo->want); } auto fileAccessExtProxy = connectInfo->fileAccessExtConnection->GetFileExtProxy(); if (fileAccessExtProxy == nullptr) { @@ -361,7 +361,7 @@ bool FileAccessHelper::GetProxy() for (auto iter = cMap_.begin(); iter != cMap_.end(); ++iter) { auto connectInfo = iter->second; if (!connectInfo->fileAccessExtConnection->IsExtAbilityConnected()) { - connectInfo->fileAccessExtConnection->ConnectFileExtAbility(connectInfo->want, token_); + connectInfo->fileAccessExtConnection->ConnectFileExtAbility(connectInfo->want); } auto fileAccessExtProxy = connectInfo->fileAccessExtConnection->GetFileExtProxy(); if (fileAccessExtProxy == nullptr) { diff --git a/interfaces/inner_api/file_access/src/js_file_access_ext_ability.cpp b/interfaces/inner_api/file_access/src/js_file_access_ext_ability.cpp index e6712d21..5bd801d4 100644 --- a/interfaces/inner_api/file_access/src/js_file_access_ext_ability.cpp +++ b/interfaces/inner_api/file_access/src/js_file_access_ext_ability.cpp @@ -1929,7 +1929,7 @@ int JsFileAccessExtAbility::MoveFile(const Uri &sourceFile, const Uri &targetPar napi_create_string_utf8(env, targetParent.ToString().c_str(), targetParent.ToString().length(), &dstUri); napi_value name = nullptr; napi_create_string_utf8(env, fileName.c_str(), fileName.length(), &name); - if (srcUri == nullptr || dstUri == nullptr || name ==nullptr) { + if (srcUri == nullptr || dstUri == nullptr || name == nullptr) { HILOG_ERROR("create sourceFile uri native js value fail."); return false; } diff --git a/services/BUILD.gn b/services/BUILD.gn index 977071bb..0fbc6854 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -98,9 +98,11 @@ ohos_shared_library("file_access_service") { include_dirs = [ "${user_file_service_path}/services/native/file_access_service/include", + "${user_file_service_path}/interfaces/inner_api/file_access/include", ] shlib_type = "sa" sources = [ + "native/file_access_service/src/file_access_ext_connection.cpp", "native/file_access_service/src/file_access_service.cpp", "native/file_access_service/src/file_access_service_proxy.cpp", "native/file_access_service/src/file_access_service_stub.cpp", diff --git a/services/native/file_access_service/include/file_access_ext_connection.h b/services/native/file_access_service/include/file_access_ext_connection.h new file mode 100644 index 00000000..0e6c5dba --- /dev/null +++ b/services/native/file_access_service/include/file_access_ext_connection.h @@ -0,0 +1,77 @@ +/* + * 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 + * + * 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 FILE_ACCESS_EXT_CONNECTION_H +#define FILE_ACCESS_EXT_CONNECTION_H + +#include +#include + +#include "ability_connect_callback_stub.h" +#include "element_name.h" +#include "ifile_access_ext_base.h" +#include "file_access_ext_proxy.h" +#include "file_access_service.h" +#include "iremote_object.h" +#include "refbase.h" +#include "want.h" + +namespace OHOS { +namespace FileAccessFwk { +class FileAccessService; + +class FileAccessExtConnection : public AAFwk::AbilityConnectionStub { +public: + FileAccessExtConnection() = default; + virtual ~FileAccessExtConnection() = default; + + 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 std::mutex mutex_; + std::atomic isConnected_ = {false}; + sptr fileExtProxy_; +}; + + +class AgentFileAccessExtConnection : public AAFwk::AbilityConnectionStub { +public: + AgentFileAccessExtConnection(const sptr& connection) : connection_(connection){}; + virtual ~AgentFileAccessExtConnection(); + 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); + void DisconnectFileExtAbility(); +private: + const sptr connection_ = nullptr; +}; + +} // namespace FileAccessFwk +} // namespace OHOS +#endif // FILE_ACCESS_EXT_CONNECTION_H diff --git a/services/native/file_access_service/include/file_access_service.h b/services/native/file_access_service/include/file_access_service.h index 5d4984c2..9aebad9c 100644 --- a/services/native/file_access_service/include/file_access_service.h +++ b/services/native/file_access_service/include/file_access_service.h @@ -25,6 +25,7 @@ #include #include "bundle_mgr_interface.h" +#include "file_access_ext_connection.h" #include "file_access_service_stub.h" #include "ifile_access_ext_base.h" #include "holder_manager.h" @@ -36,7 +37,7 @@ namespace OHOS { namespace FileAccessFwk { - +class AgentFileAccessExtConnection; class ObserverContext { public: ObserverContext(sptr obs): obs_(obs) {} @@ -225,6 +226,9 @@ public: virtual void OnStart() override; virtual void OnStop() override; int32_t Dump(int32_t fd, const std::vector &args) override; + + void DisconnectAppProxy(const sptr& connection); + void RemoveAppProxy(const sptr& connection); protected: int32_t RegisterNotify(Uri uri, bool notifyForDescendants, const sptr &observer, @@ -233,8 +237,11 @@ protected: const std::shared_ptr &info = nullptr) override; int32_t CleanAllNotify(Uri uri, const std::shared_ptr &info) override; int32_t OnChange(Uri uri, NotifyType notifyType) override; - int32_t GetExensionProxy(const std::shared_ptr &info, + int32_t GetExtensionProxy(const std::shared_ptr &info, sptr &extensionProxy) override; + int32_t ConnectFileExtAbility(const AAFwk::Want &want, + const sptr& connection) override; + int32_t DisConnectFileExtAbility(const sptr& connection) override; private: class ExtensionDeathRecipient : public IRemoteObject::DeathRecipient { @@ -272,6 +279,8 @@ private: const std::shared_ptr &info); sptr FindExtProxyByBundleName(std::string bundleName); void AddExtProxyInfo(std::string bundleName, sptr extProxy); + // 管理对象 方法 + void AddAppProxy(const sptr& key, sptr& value); std::shared_ptr unLoadTimer_ = nullptr; std::shared_ptr onDemandTimer_ = nullptr; static sptr instance_; @@ -285,6 +294,18 @@ private: HolderManager> obsManager_; std::mutex mapMutex_; std::unordered_map> cMap_; + + class AppDeathRecipient : public IRemoteObject::DeathRecipient { + public: + AppDeathRecipient() {} + virtual void OnRemoteDied(const wptr& remote); + virtual ~AppDeathRecipient() = default; + }; + + std::mutex appProxyMutex_; + std::unordered_map> appProxyMap_; + std::unordered_map> appConnection_; + sptr appDeathRecipient_; }; } // namespace FileAccessFwk } // namespace OHOS diff --git a/services/native/file_access_service/include/file_access_service_ipc_interface_code.h b/services/native/file_access_service/include/file_access_service_ipc_interface_code.h index 6e16398d..00312b0d 100644 --- a/services/native/file_access_service/include/file_access_service_ipc_interface_code.h +++ b/services/native/file_access_service/include/file_access_service_ipc_interface_code.h @@ -22,7 +22,9 @@ namespace FileAccessFwk { CMD_REGISTER_NOTIFY = 0, CMD_UNREGISTER_NOTIFY, CMD_ONCHANGE, - CMD_GET_EXTENSION_PROXY + CMD_GET_EXTENSION_PROXY, + CMD_CONNECT_FILE_EXT_ABILITY, + CMD_DISCONNECT_FILE_EXT_ABILITY }; } // namespace FileAccessFwk } // namespace OHOS diff --git a/services/native/file_access_service/include/file_access_service_proxy.h b/services/native/file_access_service/include/file_access_service_proxy.h index 8233430d..b32375af 100644 --- a/services/native/file_access_service/include/file_access_service_proxy.h +++ b/services/native/file_access_service/include/file_access_service_proxy.h @@ -33,9 +33,12 @@ public: const std::shared_ptr &info) override; int32_t UnregisterNotify(Uri uri, const sptr &observer, const std::shared_ptr &info) override; - int32_t GetExensionProxy(const std::shared_ptr &info, + int32_t GetExtensionProxy(const std::shared_ptr &info, sptr &extensionProxy) override; - + int32_t ConnectFileExtAbility(const AAFwk::Want &want, + const sptr& connection) override; + int32_t DisConnectFileExtAbility(const sptr& connection) override; + class ServiceProxyLoadCallback : public SystemAbilityLoadCallbackStub { public: void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr &remoteObject) override; diff --git a/services/native/file_access_service/include/file_access_service_stub.h b/services/native/file_access_service/include/file_access_service_stub.h index 94823f82..96067c08 100644 --- a/services/native/file_access_service/include/file_access_service_stub.h +++ b/services/native/file_access_service/include/file_access_service_stub.h @@ -38,6 +38,8 @@ private: ErrCode CmdRegisterNotify(MessageParcel &data, MessageParcel &reply); ErrCode CmdUnregisterNotify(MessageParcel &data, MessageParcel &reply); ErrCode CmdGetExensionProxy(MessageParcel &data, MessageParcel &reply); + ErrCode CmdConnectFileExtAbility(MessageParcel &data, MessageParcel &reply); + ErrCode CmdDisConnectFileExtAbility(MessageParcel &data, MessageParcel &reply); bool CheckCallingPermission(const std::string &permission); using RequestFuncType = int (FileAccessServiceStub::*)(MessageParcel &data, MessageParcel &reply); std::map stubFuncMap_; diff --git a/services/native/file_access_service/include/holder_manager.h b/services/native/file_access_service/include/holder_manager.h index 173710ab..ce2d47c8 100644 --- a/services/native/file_access_service/include/holder_manager.h +++ b/services/native/file_access_service/include/holder_manager.h @@ -37,6 +37,7 @@ public: { std::lock_guard guard(holderMutex_); holder_.clear(); + HILOG_INFO("~HolderManager holder size: %{public}zu", holder_.size()); } uint32_t save(Type content) @@ -47,6 +48,7 @@ public: } while (exist(id)); std::lock_guard guard(holderMutex_); holder_.insert(std::pair(id, content)); + HILOG_INFO("save id:%{public}d holder size: %{public}zu", id, holder_.size()); return id; } @@ -67,8 +69,10 @@ public: if (iter != holder_.end()) { auto res = iter->second; holder_.erase(id); + HILOG_INFO("pop id: %{public}d find holder size: %{public}zu", id, holder_.size()); return res; } + HILOG_INFO("pop id: %{public}d not find holder size: %{public}zu", id, holder_.size()); return Type(); } @@ -76,7 +80,10 @@ public: { std::lock_guard guard(holderMutex_); if (holder_.count(id)) { + HILOG_INFO("release id: %{public}d find holder size: %{public}zu", id, holder_.size()); holder_.erase(id); + } else { + HILOG_INFO("release id: %{public}d not find holder size: %{public}zu", id, holder_.size()); } } diff --git a/services/native/file_access_service/include/ifile_access_service_base.h b/services/native/file_access_service/include/ifile_access_service_base.h index f9748567..02cb61a3 100644 --- a/services/native/file_access_service/include/ifile_access_service_base.h +++ b/services/native/file_access_service/include/ifile_access_service_base.h @@ -16,6 +16,7 @@ #ifndef I_FILE_ACCESS_SERVICE_BASE_H #define I_FILE_ACCESS_SERVICE_BASE_H +#include "ability_connect_callback_interface.h" #include "file_access_helper.h" #include "iremote_broker.h" #include "iremote_object.h" @@ -36,8 +37,11 @@ protected: virtual int32_t UnregisterNotify(Uri uri, const sptr &observer, const std::shared_ptr &info) = 0; //Get exension proxy by SA - virtual int32_t GetExensionProxy(const std::shared_ptr &info, + virtual int32_t GetExtensionProxy(const std::shared_ptr &info, sptr &extensionProxy) = 0; + virtual int32_t ConnectFileExtAbility(const AAFwk::Want &want, + const sptr& connection) = 0; + virtual int32_t DisConnectFileExtAbility(const sptr& connection) = 0; }; } // namespace FileAccessFwk } // namespace OHOS diff --git a/services/native/file_access_service/src/file_access_ext_connection.cpp b/services/native/file_access_service/src/file_access_ext_connection.cpp new file mode 100644 index 00000000..a2928a3d --- /dev/null +++ b/services/native/file_access_service/src/file_access_ext_connection.cpp @@ -0,0 +1,144 @@ +/* + * Copyright (c) 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 + * + * 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 "file_access_ext_connection.h" + +#include + +#include "ability_connect_callback_interface.h" +#include "ability_manager_client.h" +#include "file_access_ext_proxy.h" +#include "hilog_wrapper.h" +#include "iremote_broker.h" +#include "ipc_skeleton.h" + +namespace OHOS { +namespace FileAccessFwk { + +std::mutex FileAccessExtConnection::mutex_; + +const int32_t DEFAULT_USER_ID = -1; + +void FileAccessExtConnection::OnAbilityConnectDone( + const AppExecFwk::ElementName &element, const sptr &remoteObject, int resultCode) +{ + HILOG_INFO("OnAbilityConnectDone start"); + if (remoteObject == nullptr) { + HILOG_ERROR("remote is nullptr"); + return; + } + + fileExtProxy_ = iface_cast(remoteObject); + if (fileExtProxy_ == nullptr) { + HILOG_ERROR("FileAccessExtConnection fileExtProxy_ is nullptr"); + return; + } + HILOG_INFO("OnAbilityConnectDone set connected info"); + 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) +{ + fileExtProxy_ = nullptr; + isConnected_.store(false); +} + +void FileAccessExtConnection::ConnectFileExtAbility(const AAFwk::Want &want, const sptr &token) +{ + ErrCode ret = AAFwk::AbilityManagerClient::GetInstance()->ConnectAbility(want, this, DEFAULT_USER_ID); + HILOG_INFO("ConnectFileExtAbility ret: %{public}d ", ret); + if (ret != ERR_OK) { + HILOG_INFO("ConnectAbility ret=%{public}d", ret); + return; + } + std::unique_lock lock(connectLockInfo_.mutex); + const int WAIT_TIME = 3; // second + 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() +{ + fileExtProxy_ = nullptr; + isConnected_.store(false); + ErrCode ret = AAFwk::AbilityManagerClient::GetInstance()->DisconnectAbility(this); + HILOG_INFO("DisconnectFileExtAbility called end, ret=%{public}d", ret); +} + +bool FileAccessExtConnection::IsExtAbilityConnected() +{ + return isConnected_.load(); +} + +sptr FileAccessExtConnection::GetFileExtProxy() +{ + return fileExtProxy_; +} + +void AgentFileAccessExtConnection::OnAbilityConnectDone( + const AppExecFwk::ElementName &element, const sptr &remoteObject, int resultCode) +{ + HILOG_INFO("start OnAbilityConnectDone"); + if (connection_ == nullptr) { + return ; + } + connection_->OnAbilityConnectDone(element, remoteObject, resultCode); + HILOG_INFO("end OnAbilityConnectDone"); +} + +void AgentFileAccessExtConnection::OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) +{ + HILOG_INFO("start OnAbilityDisconnectDone"); + if (connection_ == nullptr) { + HILOG_WARN("OnAbilityDisconnectDone connection_ is nullptr"); + return; + } + connection_->OnAbilityDisconnectDone(element, resultCode); + FileAccessService::GetInstance()->DisconnectAppProxy(connection_); + FileAccessService::GetInstance()->RemoveAppProxy(connection_); + HILOG_INFO("OnAbilityDisconnectDone resultCode=%{public}d", resultCode); +} + +void AgentFileAccessExtConnection::ConnectFileExtAbility(const AAFwk::Want &want) +{ + ErrCode ret = AAFwk::AbilityManagerClient::GetInstance()->ConnectAbility(want, this, DEFAULT_USER_ID); + if (ret != ERR_OK) { + HILOG_ERROR("AgentFileAccessExtConnection ConnectAbility failed, ret=%{public}d", ret); + return; + } + HILOG_INFO("AgentFileAccessExtConnection ConnectFileExtAbility success"); +} + +void AgentFileAccessExtConnection::DisconnectFileExtAbility() +{ + ErrCode ret = AAFwk::AbilityManagerClient::GetInstance()->DisconnectAbility(this); + if (ret != ERR_OK) { + HILOG_ERROR("DisconnectAbility failed, ret=%{public}d", ret); + return; + } + HILOG_INFO("DisconnectFileExtAbility done"); +} + +AgentFileAccessExtConnection::~AgentFileAccessExtConnection() +{ +} + +} // namespace FileAccessFwk +} // namespace OHOS diff --git a/services/native/file_access_service/src/file_access_service.cpp b/services/native/file_access_service/src/file_access_service.cpp index 526a54f9..f6e83b07 100644 --- a/services/native/file_access_service/src/file_access_service.cpp +++ b/services/native/file_access_service/src/file_access_service.cpp @@ -15,13 +15,12 @@ #include "file_access_service.h" +#include #include #include "user_access_tracer.h" #include "file_access_framework_errno.h" -#include "file_access_ext_connection.h" #include "file_access_extension_info.h" -#include "file_access_ext_connection.h" #include "hilog_wrapper.h" #include "hitrace_meter.h" #include "ipc_skeleton.h" @@ -141,6 +140,9 @@ void FileAccessService::Init() if (observerDeathRecipient_ == nullptr) { observerDeathRecipient_ = sptr(new ObserverDeathRecipient()); } + if (appDeathRecipient_ == nullptr) { + appDeathRecipient_ = sptr(new AppDeathRecipient()); + } } static bool GetBundleNameFromUri(Uri &uri, string &bundleName) @@ -186,7 +188,7 @@ sptr FileAccessService::ConnectExtension(Uri &uri, const sha sptr extensionProxy; { lock_guard lock(mutex_); - int32_t ret = GetExensionProxy(info, extensionProxy); + int32_t ret = GetExtensionProxy(info, extensionProxy); if (ret != ERR_OK || extensionProxy == nullptr) { return nullptr; } @@ -288,6 +290,7 @@ int32_t FileAccessService::OperateObsNode(Uri &uri, bool notifyForDescendants, u const std::shared_ptr &info) { string uriStr = uri.ToString(); + HILOG_INFO("OperateObsNode uriStr: %{public}s", uriStr.c_str()); lock_guard lock(nodeMutex_); auto iter = relationshipMap_.find(uriStr); shared_ptr obsNode; @@ -340,7 +343,7 @@ int32_t FileAccessService::RegisterNotifyImpl(Uri uri, bool notifyForDescendants { UserAccessTracer trace; trace.Start("RegisterNotifyImpl"); - std::string token = IPCSkeleton::ResetCallingIdentity(); + // std::string token = IPCSkeleton::ResetCallingIdentity(); shared_ptr obsContext = make_shared(observer); // find if obsManager_ has this callback. uint32_t code = obsManager_.getId([obsContext](const shared_ptr &afterContext) { @@ -384,6 +387,7 @@ void FileAccessService::RemoveRelations(string &uriStr, shared_ptr int FileAccessService::FindUri(const string &uriStr, shared_ptr &outObsNode) { + HILOG_INFO("uriStr: %{public}s", uriStr.c_str()); lock_guard lock(nodeMutex_); auto iter = relationshipMap_.find(uriStr); if (iter == relationshipMap_.end()) { @@ -600,7 +604,9 @@ int32_t FileAccessService::OnChange(Uri uri, NotifyType notifyType) bool FileAccessService::IsUnused() { - return obsManager_.isEmpty(); + HILOG_INFO("IsUnused: obsManager_: %{public}d, appProxyMap_: %{public}d", + obsManager_.isEmpty(), appProxyMap_.empty()); + return obsManager_.isEmpty() && appProxyMap_.empty(); } void FileAccessService::InitTimer() @@ -648,7 +654,41 @@ void FileAccessService::InitTimer() unLoadTimer_->start(); } -int32_t FileAccessService::GetExensionProxy(const std::shared_ptr &info, +int32_t FileAccessService::ConnectFileExtAbility(const AAFwk::Want &want, + const sptr& connection) +{ + HILOG_INFO("ConnectFileExtAbility start"); + if (connection == nullptr) { + HILOG_ERROR("connection is nullptr"); + return E_CONNECT; + } + + sptr fileAccessExtConnection( + new(std::nothrow) AgentFileAccessExtConnection(connection)); + if (fileAccessExtConnection == nullptr) { + HILOG_ERROR("new fileAccessExtConnection fail"); + return E_CONNECT; + } + + fileAccessExtConnection->ConnectFileExtAbility(want); + AddAppProxy(connection, fileAccessExtConnection); + return ERR_OK; +} + +int32_t FileAccessService::DisConnectFileExtAbility(const sptr& connection) +{ + HILOG_INFO("ConnectFileExtAbility start"); + if (connection == nullptr) { + HILOG_ERROR("connection is nullptr"); + return E_CONNECT; + } + DisconnectAppProxy(connection); + RemoveAppProxy(connection); + return ERR_OK; +} + + +int32_t FileAccessService::GetExtensionProxy(const std::shared_ptr &info, sptr &extensionProxy) { sptr fileAccessExtConnection(new(std::nothrow) FileAccessExtConnection()); @@ -703,5 +743,75 @@ void FileAccessService::AddExtProxyInfo(std::string bundleName, sptr lock(mapMutex_); cMap_.emplace(bundleName, extProxy); } + +void FileAccessService::AddAppProxy(const sptr& connection, + sptr& value) +{ + if (connection == nullptr || value == nullptr) { + HILOG_ERROR("key is null or value is null"); + return; + } + size_t key = reinterpret_cast(connection->AsObject().GetRefPtr()); + HILOG_INFO("sa add key, %{public}zu", key); + lock_guard lock(appProxyMutex_); + if (appProxyMap_.count(key)) { + HILOG_INFO("sa had proxy,needn't create connection"); + return; + } + connection->AsObject()->AddDeathRecipient(appDeathRecipient_); + appProxyMap_[key] = value; + appConnection_[key] = connection; + HILOG_INFO("appProxyMap_ size: %{public}zu", appProxyMap_.size()); +} + +void FileAccessService::RemoveAppProxy(const sptr& connection) +{ + if (connection == nullptr) { + HILOG_WARN("key is null"); + return; + } + size_t key = reinterpret_cast(connection->AsObject().GetRefPtr()); + if (appProxyMap_.find(key) == appProxyMap_.end()) { + HILOG_WARN("appProxyMap_ not key: %{public}zu", key); + return; + } + lock_guard lock(appProxyMutex_); + if (appProxyMap_.find(key) == appProxyMap_.end()) { + HILOG_INFO("appProxyMap_ not key"); + return; + } + + appProxyMap_.erase(key); + appConnection_.erase(key); + HILOG_INFO("appProxyMap_ size: %{public}zu", appProxyMap_.size()); +} + +void FileAccessService::DisconnectAppProxy(const sptr& connection) +{ + size_t key = reinterpret_cast(connection->AsObject().GetRefPtr()); + lock_guard lock(appProxyMutex_); + if (appProxyMap_.find(key) == appProxyMap_.end()) { + HILOG_WARN("appProxyMap_ not key: %{public}zu", key); + return; + } + HILOG_INFO("DisconnectAppProxy DisconnectFileExtAbility key: %{public}zu", key); + if (appProxyMap_[key] != nullptr) { + appProxyMap_[key]->DisconnectFileExtAbility(); + } +} + +void FileAccessService::AppDeathRecipient::OnRemoteDied(const wptr &remote) +{ + HILOG_INFO("FileAccessService::AppDeathRecipient::OnRemoteDied, remote obj died."); + if (remote == nullptr) { + HILOG_ERROR("remote is nullptr"); + return; + } + auto remoteBroker = iface_cast(remote.promote()); + size_t key = reinterpret_cast(remoteBroker->AsObject().GetRefPtr()); + HILOG_INFO("remote: %{public}zu", key); + FileAccessService::GetInstance()->RemoveAppProxy(remoteBroker); +} + } // namespace FileAccessFwk } // namespace OHOS diff --git a/services/native/file_access_service/src/file_access_service_proxy.cpp b/services/native/file_access_service/src/file_access_service_proxy.cpp index 8438774d..b476ad71 100644 --- a/services/native/file_access_service/src/file_access_service_proxy.cpp +++ b/services/native/file_access_service/src/file_access_service_proxy.cpp @@ -295,12 +295,103 @@ int32_t FileAccessServiceProxy::UnregisterNotify(Uri uri, const sptr& connection) +{ + UserAccessTracer trace; + trace.Start("ConnectFileExtAbility"); + MessageParcel data; + if (!data.WriteInterfaceToken(FileAccessServiceProxy::GetDescriptor())) { + HILOG_ERROR("WriteInterfaceToken failed"); + return E_IPCS; + } -int32_t FileAccessServiceProxy::GetExensionProxy(const std::shared_ptr &info, + if (!data.WriteParcelable(&want)) { + HILOG_ERROR("fail to WriteParcelable want"); + return E_IPCS; + } + + if (connection == nullptr || connection->AsObject() == nullptr) { + HILOG_ERROR("connection is nullptr"); + return E_GETINFO; + } + + if (!data.WriteRemoteObject(connection->AsObject())) { + HILOG_ERROR("fail to WriteParcelable connection"); + return E_IPCS; + } + + MessageParcel reply; + MessageOption option; + auto remote = Remote(); + if (!remote) { + HILOG_ERROR("failed to get remote"); + return E_IPCS; + } + int err = remote->SendRequest(static_cast( + FileAccessServiceInterfaceCode::CMD_CONNECT_FILE_EXT_ABILITY), data, reply, option); + if (err != ERR_OK) { + HILOG_ERROR("fail to SendRequest. err: %{public}d", err); + return err; + } + + int ret = E_IPCS; + if (!reply.ReadInt32(ret) || ret != ERR_OK) { + HILOG_ERROR("ConnectFileExtAbility operation failed ret : %{public}d", ret); + return ret; + } + + return ERR_OK; +} + +int32_t FileAccessServiceProxy::DisConnectFileExtAbility(const sptr& connection) +{ + UserAccessTracer trace; + trace.Start("DisConnectFileExtAbility"); + MessageParcel data; + if (!data.WriteInterfaceToken(FileAccessServiceProxy::GetDescriptor())) { + HILOG_ERROR("WriteInterfaceToken failed"); + return E_IPCS; + } + + if (connection == nullptr || connection->AsObject() == nullptr) { + HILOG_ERROR("connection is nullptr"); + return E_GETINFO; + } + + if (!data.WriteRemoteObject(connection->AsObject())) { + HILOG_ERROR("fail to WriteParcelable connection"); + return E_IPCS; + } + + MessageParcel reply; + MessageOption option; + auto remote = Remote(); + if (!remote) { + HILOG_ERROR("failed to get remote"); + return E_IPCS; + } + int err = remote->SendRequest(static_cast( + FileAccessServiceInterfaceCode::CMD_DISCONNECT_FILE_EXT_ABILITY), data, reply, option); + if (err != ERR_OK) { + HILOG_ERROR("fail to SendRequest. err: %{public}d", err); + return err; + } + + int ret = E_IPCS; + if (!reply.ReadInt32(ret) || ret != ERR_OK) { + HILOG_ERROR("DisConnectFileExtAbility operation failed ret : %{public}d", ret); + return ret; + } + + return ERR_OK; +} + +int32_t FileAccessServiceProxy::GetExtensionProxy(const std::shared_ptr &info, sptr &extensionProxy) { UserAccessTracer trace; - trace.Start("GetExensionProxy"); + trace.Start("GetExtensionProxy"); MessageParcel data; if (!data.WriteInterfaceToken(FileAccessServiceProxy::GetDescriptor())) { HILOG_ERROR("WriteInterfaceToken failed"); @@ -333,7 +424,7 @@ int32_t FileAccessServiceProxy::GetExensionProxy(const std::shared_ptr(FileAccessServiceInterfaceCode::CMD_ONCHANGE)] = &FileAccessServiceStub::CmdOnChange; + stubFuncMap_[static_cast(FileAccessServiceInterfaceCode::CMD_CONNECT_FILE_EXT_ABILITY)] = + &FileAccessServiceStub::CmdConnectFileExtAbility; + stubFuncMap_[static_cast(FileAccessServiceInterfaceCode::CMD_DISCONNECT_FILE_EXT_ABILITY)] = + &FileAccessServiceStub::CmdDisConnectFileExtAbility; } FileAccessServiceStub::~FileAccessServiceStub() @@ -190,6 +194,51 @@ bool FileAccessServiceStub::CheckCallingPermission(const std::string &permission return true; } +ErrCode FileAccessServiceStub::CmdConnectFileExtAbility(MessageParcel &data, MessageParcel &reply) +{ + UserAccessTracer trace; + trace.Start("CmdConnectFileExtAbility"); + std::shared_ptr wantPtr(data.ReadParcelable()); + if (wantPtr == nullptr) { + return E_IPCS; + } + AAFwk::Want want = AAFwk::Want(*wantPtr); + + sptr connection = iface_cast(data.ReadRemoteObject()); + if (!connection) { + HILOG_ERROR("fail to read connection"); + return E_IPCS; + } + + int ret = ConnectFileExtAbility(want, connection); + if (!reply.WriteInt32(ret)) { + HILOG_ERROR("fail to CmdConnectFileExtAbility"); + return E_IPCS; + } + + return ERR_OK; +} + + +ErrCode FileAccessServiceStub::CmdDisConnectFileExtAbility(MessageParcel &data, MessageParcel &reply) +{ + UserAccessTracer trace; + trace.Start("CmdDisConnectFileExtAbility"); + sptr connection = iface_cast(data.ReadRemoteObject()); + if (!connection) { + HILOG_ERROR("fail to read connection"); + return E_IPCS; + } + + int ret = DisConnectFileExtAbility(connection); + if (!reply.WriteInt32(ret)) { + HILOG_ERROR("fail to CmdDisConnectFileExtAbility"); + return E_IPCS; + } + + return ERR_OK; +} + ErrCode FileAccessServiceStub::CmdGetExensionProxy(MessageParcel &data, MessageParcel &reply) { UserAccessTracer trace; @@ -201,9 +250,9 @@ ErrCode FileAccessServiceStub::CmdGetExensionProxy(MessageParcel &data, MessageP } sptr extensionProxy; - int ret = GetExensionProxy(connectExtensionInfo, extensionProxy); + int ret = GetExtensionProxy(connectExtensionInfo, extensionProxy); if (!reply.WriteInt32(ret) || extensionProxy == nullptr) { - HILOG_ERROR("fail to GetExensionProxy"); + HILOG_ERROR("fail to GetExtensionProxy"); return E_IPCS; } if (!reply.WriteRemoteObject(extensionProxy->AsObject())) { diff --git a/test/fuzztest/fileaccessextconnection_fuzzer/BUILD.gn b/test/fuzztest/fileaccessextconnection_fuzzer/BUILD.gn index 4dabd0bd..6d169121 100644 --- a/test/fuzztest/fileaccessextconnection_fuzzer/BUILD.gn +++ b/test/fuzztest/fileaccessextconnection_fuzzer/BUILD.gn @@ -21,7 +21,9 @@ ohos_fuzztest("FileAccessExtConnectionFuzzTest") { "${user_file_service_path}/test/fuzztest/fileaccessextconnection_fuzzer" include_dirs = [ "${ability_runtime_path}/interfaces/inner_api/ability_manager/include", + "${ability_runtime_path}/interfaces/kits/native/appkit/ability_runtime/context", "${user_file_service_path}/interfaces/inner_api/file_access/include", + "${user_file_service_path}/services/native/file_access_service/include", "${user_file_service_path}/interfaces/kits/js/src/common", "${user_file_service_path}/utils", ] @@ -32,8 +34,8 @@ ohos_fuzztest("FileAccessExtConnectionFuzzTest") { "-fno-omit-frame-pointer", ] sources = [ - "${user_file_service_path}/interfaces/inner_api/file_access/src/file_access_ext_connection.cpp", "${user_file_service_path}/interfaces/inner_api/file_access/src/file_access_ext_proxy.cpp", + "${user_file_service_path}/services/native/file_access_service/src/file_access_ext_connection.cpp", "${user_file_service_path}/test/fuzztest/fileaccessextconnection_fuzzer/fileaccessextconnection_fuzzer.cpp", ] diff --git a/test/fuzztest/fileaccessextconnection_fuzzer/fileaccessextconnection_fuzzer.cpp b/test/fuzztest/fileaccessextconnection_fuzzer/fileaccessextconnection_fuzzer.cpp index d7a29ad0..6392cae8 100644 --- a/test/fuzztest/fileaccessextconnection_fuzzer/fileaccessextconnection_fuzzer.cpp +++ b/test/fuzztest/fileaccessextconnection_fuzzer/fileaccessextconnection_fuzzer.cpp @@ -51,6 +51,21 @@ bool GetFileExtProxyFuzzTest(shared_ptr conn) return true; } +bool ConnectFileExtAbility(shared_ptr conn) +{ + AAFwk::Want want; + sptr remoteObject = nullptr; + conn->ConnectFileExtAbility(want, remoteObject); + return true; +} + +bool DisconnectFileExtAbility(shared_ptr conn) +{ + conn->DisconnectFileExtAbility(); + return true; +} + + } // namespace OHOS /* Fuzzer entry point */ @@ -65,6 +80,8 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) OHOS::OnAbilityDisconnectDoneFuzzTest(conn); OHOS::IsExtAbilityConnectedFuzzTest(conn); OHOS::GetFileExtProxyFuzzTest(conn); + OHOS::ConnectFileExtAbility(conn); + OHOS::DisconnectFileExtAbility(conn); return 0; } diff --git a/test/fuzztest/fileaccessserviceproxy_fuzzer/fileaccessservicemock.h b/test/fuzztest/fileaccessserviceproxy_fuzzer/fileaccessservicemock.h index 363527a7..0918b9cf 100644 --- a/test/fuzztest/fileaccessserviceproxy_fuzzer/fileaccessservicemock.h +++ b/test/fuzztest/fileaccessserviceproxy_fuzzer/fileaccessservicemock.h @@ -29,9 +29,12 @@ public: const std::shared_ptr &info) override { return 0; } int32_t UnregisterNotify(Uri uri, const sptr &observer, const std::shared_ptr &info) override { return 0; } - int32_t GetExensionProxy(const std::shared_ptr &info, + int32_t GetExtensionProxy(const std::shared_ptr &info, sptr &extensionProxy) override { return 0; } int32_t CleanAllNotify(Uri uri, const std::shared_ptr &info) override { return 0; } + int32_t ConnectFileExtAbility(const AAFwk::Want &want, + const sptr& connection) override { return 0; }; + int32_t DisConnectFileExtAbility(const sptr& connection) override { return 0; }; }; } // namespace FileAccessFwk } // namespace OHOS diff --git a/test/fuzztest/fileaccessserviceproxy_fuzzer/fileaccessserviceproxy_fuzzer.cpp b/test/fuzztest/fileaccessserviceproxy_fuzzer/fileaccessserviceproxy_fuzzer.cpp index 039d075b..da529976 100644 --- a/test/fuzztest/fileaccessserviceproxy_fuzzer/fileaccessserviceproxy_fuzzer.cpp +++ b/test/fuzztest/fileaccessserviceproxy_fuzzer/fileaccessserviceproxy_fuzzer.cpp @@ -92,7 +92,7 @@ bool GetExensionProxyFuzzTest(shared_ptr proxy, const ui auto info = make_shared(); sptr extensionProxy = nullptr; - proxy->GetExensionProxy(info, extensionProxy); + proxy->GetExtensionProxy(info, extensionProxy); return true; } } // namespace OHOS diff --git a/test/fuzztest/fileaccessservicestub_fuzzer/fileaccessservicemock.h b/test/fuzztest/fileaccessservicestub_fuzzer/fileaccessservicemock.h index 363527a7..0bbc68f0 100644 --- a/test/fuzztest/fileaccessservicestub_fuzzer/fileaccessservicemock.h +++ b/test/fuzztest/fileaccessservicestub_fuzzer/fileaccessservicemock.h @@ -29,9 +29,12 @@ public: const std::shared_ptr &info) override { return 0; } int32_t UnregisterNotify(Uri uri, const sptr &observer, const std::shared_ptr &info) override { return 0; } - int32_t GetExensionProxy(const std::shared_ptr &info, + int32_t GetExtensionProxy(const std::shared_ptr &info, sptr &extensionProxy) override { return 0; } int32_t CleanAllNotify(Uri uri, const std::shared_ptr &info) override { return 0; } + int32_t ConnectFileExtAbility(const AAFwk::Want &want, + const sptr& connection) override { return 0; } + int32_t DisConnectFileExtAbility(const sptr& connection) override { return 0; } }; } // namespace FileAccessFwk } // namespace OHOS diff --git a/test/unittest/file_access_service_proxy_test.cpp b/test/unittest/file_access_service_proxy_test.cpp index 84467630..72af1e08 100644 --- a/test/unittest/file_access_service_proxy_test.cpp +++ b/test/unittest/file_access_service_proxy_test.cpp @@ -350,7 +350,7 @@ HWTEST_F(FileAccessServiceProxyTest, file_access_service_proxy_UnregisterNotify_ /** * @tc.number: user_file_service_file_access_service_proxy_GetExensionProxy_0000 * @tc.name: file_access_service_proxy_GetExensionProxy_0000 - * @tc.desc: Test function of GetExensionProxy interface for ERROR. + * @tc.desc: Test function of GetExtensionProxy interface for ERROR. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 3 @@ -365,36 +365,36 @@ HWTEST_F(FileAccessServiceProxyTest, file_access_service_proxy_GetExensionProxy_ shared_ptr info = nullptr; sptr extensionProxy = nullptr; EXPECT_CALL(*insMoc, Bool()).WillOnce(Return(false)); - auto result = proxy->GetExensionProxy(info, extensionProxy); + auto result = proxy->GetExtensionProxy(info, extensionProxy); EXPECT_EQ(result, E_IPCS); EXPECT_CALL(*insMoc, Bool()).WillOnce(Return(true)); - result = proxy->GetExensionProxy(info, extensionProxy); + result = proxy->GetExtensionProxy(info, extensionProxy); EXPECT_EQ(result, E_GETINFO); info = make_shared(); EXPECT_CALL(*insMoc, Bool()).WillOnce(Return(true)).WillOnce(Return(false)); - result = proxy->GetExensionProxy(info, extensionProxy); + result = proxy->GetExtensionProxy(info, extensionProxy); EXPECT_EQ(result, E_IPCS); EXPECT_CALL(*insMoc, Bool()).WillOnce(Return(true)).WillOnce(Return(true)).WillOnce(Return(true)); EXPECT_CALL(*impl, SendRequest(_, _, _, _)).WillOnce(Return(E_URIS)); - result = proxy->GetExensionProxy(info, extensionProxy); + result = proxy->GetExtensionProxy(info, extensionProxy); EXPECT_EQ(result, E_URIS); EXPECT_CALL(*insMoc, Bool()).WillOnce(Return(true)).WillOnce(Return(true)).WillOnce(Return(true)) .WillOnce(Return(false)); EXPECT_CALL(*insMoc, Int()).WillOnce(Return(ERR_OK)); EXPECT_CALL(*impl, SendRequest(_, _, _, _)).WillOnce(Return(ERR_OK)); - result = proxy->GetExensionProxy(info, extensionProxy); + result = proxy->GetExtensionProxy(info, extensionProxy); EXPECT_EQ(result, ERR_OK); EXPECT_CALL(*insMoc, Bool()).WillOnce(Return(true)).WillOnce(Return(true)).WillOnce(Return(true)) .WillOnce(Return(true)); EXPECT_CALL(*insMoc, Int()).WillOnce(Return(E_IPCS)); EXPECT_CALL(*impl, SendRequest(_, _, _, _)).WillOnce(Return(ERR_OK)); - result = proxy->GetExensionProxy(info, extensionProxy); + result = proxy->GetExtensionProxy(info, extensionProxy); EXPECT_EQ(result, E_IPCS); EXPECT_CALL(*insMoc, Bool()).WillOnce(Return(true)).WillOnce(Return(true)).WillOnce(Return(true)) .WillOnce(Return(true)); EXPECT_CALL(*insMoc, Int()).WillOnce(Return(ERR_OK)); EXPECT_CALL(*impl, SendRequest(_, _, _, _)).WillOnce(Return(ERR_OK)); - result = proxy->GetExensionProxy(info, extensionProxy); + result = proxy->GetExtensionProxy(info, extensionProxy); EXPECT_EQ(result, E_IPCS); } catch (...) { GTEST_LOG_(ERROR) << "FileAccessServiceProxyTest occurs an exception."; diff --git a/test/unittest/js_file_access_ext_ability_test.cpp b/test/unittest/js_file_access_ext_ability_test.cpp index bcb52230..60b1e613 100644 --- a/test/unittest/js_file_access_ext_ability_test.cpp +++ b/test/unittest/js_file_access_ext_ability_test.cpp @@ -58,7 +58,7 @@ int32_t FileAccessServiceProxy::OnChange(Uri uri, NotifyType notifyType) return ERR_OK; } -int32_t FileAccessServiceProxy::GetExensionProxy(const std::shared_ptr &, +int32_t FileAccessServiceProxy::GetExtensionProxy(const std::shared_ptr &, sptr &) { return 0; @@ -70,6 +70,17 @@ int32_t FileAccessServiceProxy::UnregisterNotify(Uri, const sptr&) +{ + return 0; +} + +int32_t FileAccessServiceProxy::DisConnectFileExtAbility(const sptr&) +{ + return 0; +} + void FileAccessExtAbility::Init(const std::shared_ptr &record, const std::shared_ptr &application, std::shared_ptr &handler, diff --git a/test/unittest/mock/file_access_service_mock.h b/test/unittest/mock/file_access_service_mock.h index 1bdfc317..830cc95d 100644 --- a/test/unittest/mock/file_access_service_mock.h +++ b/test/unittest/mock/file_access_service_mock.h @@ -28,13 +28,16 @@ public: const sptr &observer, const std::shared_ptr &info)); MOCK_METHOD3(UnregisterNotify, int32_t(Uri uri, const sptr &observer, const std::shared_ptr &info)); - MOCK_METHOD2(GetExensionProxy, int32_t(const std::shared_ptr &info, + MOCK_METHOD2(GetExtensionProxy, int32_t(const std::shared_ptr &info, sptr &extensionProxy)); MOCK_METHOD0(AsObject, sptr()); MOCK_METHOD2(CleanAllNotify, int32_t(Uri uri, const std::shared_ptr &info)); MOCK_METHOD4(OnRemoteRequest, int32_t(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)); MOCK_METHOD4(SendRequest, int(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)); + MOCK_METHOD2(ConnectFileExtAbility, int32_t(const AAFwk::Want &want, + const sptr& connection)); + MOCK_METHOD1(DisConnectFileExtAbility, int32_t(const sptr& connection)); }; } // namespace FileAccessFwk } // namespace OHOS