diff --git a/frameworks/innerkits/file_access/BUILD.gn b/frameworks/innerkits/file_access/BUILD.gn index be069752a4dc38d6118de4c023ba32a7bc2a0cef..91a335f7206d6cc89aa461921636e5fc430dd86a 100644 --- a/frameworks/innerkits/file_access/BUILD.gn +++ b/frameworks/innerkits/file_access/BUILD.gn @@ -52,6 +52,8 @@ ohos_shared_library("file_access_ability_kit") { sources = [ "src/file_ext_ability.cpp", "src/js_file_ext_ability.cpp", + "src/file_access_helper.cpp", + "src/file_ext_connection.cpp", ] configs = [ ":ability_config" ] public_configs = [ diff --git a/frameworks/innerkits/file_access/include/file_access_helper.h b/frameworks/innerkits/file_access/include/file_access_helper.h new file mode 100644 index 0000000000000000000000000000000000000000..df00dea5705432e9bbaba4cfce3ce1a8490b4160 --- /dev/null +++ b/frameworks/innerkits/file_access/include/file_access_helper.h @@ -0,0 +1,69 @@ +/* + * 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 FILE_ACCESS_HELPER_H +#define FILE_ACCESS_HELPER_H + +#include +#include +#include + +#include "file_ext_connection.h" +#include "foundation/ability/ability_runtime/frameworks/kits/appkit/native/ability_runtime/context/context.h" +#include "ifile_ext_base.h" +#include "want.h" +#include "hilog_wrapper.h" + + +namespace OHOS { +namespace FileAccessFwk { +using string = std::string; +class FileAccessHelper final : public std::enable_shared_from_this { +public: + ~FileAccessHelper() = default; + + static std::shared_ptr Creator(const std::shared_ptr &context, + const AAFwk::Want &want); + +private: + FileAccessHelper(const std::shared_ptr &context, const AAFwk::Want &want, + const sptr &fileExtProxy); + void AddFileAccessDeathRecipient(const sptr &token); + void OnSchedulerDied(const wptr &remote); + + sptr token_ = {}; + AAFwk::Want want_ = {}; + sptr fileExtProxy_ = nullptr; + sptr callerDeathRecipient_ = nullptr; + sptr fileExtConnection_ = 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 +#endif // FILE_ACCESS_HELPER_H diff --git a/frameworks/innerkits/file_access/include/file_ext_connection.h b/frameworks/innerkits/file_access/include/file_ext_connection.h new file mode 100644 index 0000000000000000000000000000000000000000..9777cde9660865e889b86cd3b9b5ef057663c431 --- /dev/null +++ b/frameworks/innerkits/file_access/include/file_ext_connection.h @@ -0,0 +1,56 @@ +/* + * 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 FILE_EXT_CONNECTION_H +#define FILE_EXT_CONNECTION_H + +#include + +#include "ability_connect_callback_stub.h" +#include "event_handler.h" +#include "ifile_ext_base.h" +#include "want.h" + +namespace OHOS { +namespace FileAccessFwk { +class FileExtConnection : public AAFwk::AbilityConnectionStub { +public: + FileExtConnection() = default; + virtual ~FileExtConnection() = 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: + static sptr instance_; + static std::mutex mutex_; + std::atomic isConnected_ = {false}; + sptr fileExtProxy_; +}; +} // namespace FileAccessFwk +} // namespace OHOS +#endif // FILE_EXT_CONNECTION_H diff --git a/frameworks/innerkits/file_access/include/ifile_ext_base.h b/frameworks/innerkits/file_access/include/ifile_ext_base.h new file mode 100644 index 0000000000000000000000000000000000000000..fffafa449e07fbaffae27a2052b87c4b1c9fde5d --- /dev/null +++ b/frameworks/innerkits/file_access/include/ifile_ext_base.h @@ -0,0 +1,33 @@ +/* + * 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 I_FILE_EXT_BASE_H +#define I_FILE_EXT_BASE_H + +#include +#include +#include + +#include "uri.h" + +namespace OHOS { +namespace FileAccessFwk { +class IFileExtBase : public IRemoteBroker { +public: + DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.AppExecFwk.IFileExtBase"); +}; +} // namespace FileAccessFwk +} // namespace OHOS +#endif // I_FILE_EXT_BASE_H \ No newline at end of file diff --git a/frameworks/innerkits/file_access/src/file_access_helper.cpp b/frameworks/innerkits/file_access/src/file_access_helper.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ad9ac2937563fc42d8d1ad252f9316c31b3307e0 --- /dev/null +++ b/frameworks/innerkits/file_access/src/file_access_helper.cpp @@ -0,0 +1,112 @@ +/* + * 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 "file_access_helper.h" + +#include "hilog_wrapper.h" +#include "ifile_ext_base.h" + +namespace OHOS { +namespace FileAccessFwk { +FileAccessHelper::FileAccessHelper(const std::shared_ptr &context, + const AAFwk::Want &want, const sptr &fileExtProxy) +{ + HILOG_INFO("%{public}s start", __func__); + token_ = context->GetToken(); + want_ = want; + fileExtProxy_ = fileExtProxy; + fileExtConnection_ = FileExtConnection::GetInstance(); + HILOG_INFO("%{public}s end", __func__); +} + +void FileAccessHelper::AddFileAccessDeathRecipient(const sptr &token) +{ + HILOG_INFO("%{public}s called begin", __func__); + if (token != nullptr && callerDeathRecipient_ != nullptr) { + HILOG_INFO("token RemoveDeathRecipient."); + token->RemoveDeathRecipient(callerDeathRecipient_); + } + if (callerDeathRecipient_ == nullptr) { + callerDeathRecipient_ = + new FileAccessDeathRecipient(std::bind(&FileAccessHelper::OnSchedulerDied, this, std::placeholders::_1)); + } + if (token != nullptr) { + HILOG_INFO("token AddDeathRecipient."); + token->AddDeathRecipient(callerDeathRecipient_); + } + HILOG_INFO("%{public}s called end", __func__); +} + +void FileAccessHelper::OnSchedulerDied(const wptr &remote) +{ + HILOG_INFO("%{public}s called begin", __func__); + auto object = remote.promote(); + object = nullptr; + fileExtProxy_ = nullptr; + HILOG_INFO("%{public}s called end", __func__); +} + +std::shared_ptr FileAccessHelper::Creator( + const std::shared_ptr &context, const AAFwk::Want &want) +{ + HILOG_INFO("%{public}s with runtime context, want and uri called start.", __func__); + if (context == nullptr) { + HILOG_ERROR("%{public}s failed, context == nullptr", __func__); + return nullptr; + } + + HILOG_INFO("%{public}s before ConnectFileExtAbility.", __func__); + sptr fileExtProxy = nullptr; + + sptr fileExtConnection = FileExtConnection::GetInstance(); + if (!fileExtConnection->IsExtAbilityConnected()) { + fileExtConnection->ConnectFileExtAbility(want, context->GetToken()); + } + fileExtProxy = fileExtConnection->GetFileExtProxy(); + if (fileExtProxy == nullptr) { + HILOG_WARN("%{public}s get invalid fileExtProxy", __func__); + } + HILOG_INFO("%{public}s after ConnectFileExtAbility.", __func__); + + FileAccessHelper *ptrFileAccessHelper = new (std::nothrow) FileAccessHelper(context, want, fileExtProxy); + if (ptrFileAccessHelper == nullptr) { + HILOG_ERROR("%{public}s failed, create FileAccessHelper failed", __func__); + return nullptr; + } + + HILOG_INFO("%{public}s with runtime context, want and uri called end.", __func__); + return std::shared_ptr(ptrFileAccessHelper); +} + +void FileAccessDeathRecipient::OnRemoteDied(const wptr &remote) +{ + HILOG_INFO("%{public}s begin.", __func__); + if (handler_) { + handler_(remote); + } + HILOG_INFO("%{public}s end.", __func__); +} + +FileAccessDeathRecipient::FileAccessDeathRecipient(RemoteDiedHandler handler) : handler_(handler) +{ + HILOG_INFO("%{public}s .", __func__); +} + +FileAccessDeathRecipient::~FileAccessDeathRecipient() +{ + HILOG_INFO("%{public}s .", __func__); +} +} // namespace FileAccessFwk +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/innerkits/file_access/src/file_ext_connection.cpp b/frameworks/innerkits/file_access/src/file_ext_connection.cpp new file mode 100644 index 0000000000000000000000000000000000000000..915c58aab85aa498d5fb97e853d534f85f7edbc8 --- /dev/null +++ b/frameworks/innerkits/file_access/src/file_ext_connection.cpp @@ -0,0 +1,91 @@ +/* + * 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 "file_ext_connection.h" + +#include "ability_manager_client.h" +#include "hilog_wrapper.h" + +namespace OHOS { +namespace FileAccessFwk { +sptr FileExtConnection::instance_ = nullptr; +std::mutex FileExtConnection::mutex_; + +sptr FileExtConnection::GetInstance() +{ + HILOG_INFO("%{public}s begin.", __func__); + if (instance_ == nullptr) { + std::lock_guard lock(mutex_); + if (instance_ == nullptr) { + instance_ = sptr(new (std::nothrow) FileExtConnection()); + } + } + HILOG_INFO("%{public}s end.", __func__); + return instance_; +} + +void FileExtConnection::OnAbilityConnectDone( + const AppExecFwk::ElementName &element, const sptr &remoteObject, int resultCode) +{ + HILOG_INFO("%{public}s called begin", __func__); + if (remoteObject == nullptr) { + HILOG_ERROR("%{public}s failed, remote is nullptr", __func__); + return; + } + if (fileExtProxy_ == nullptr) { + HILOG_ERROR("%{public}s failed, fileExtProxy_ is nullptr", __func__); + return; + } + isConnected_.store(true); + HILOG_INFO("%{public}s end.", __func__); +} + +void FileExtConnection::OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) +{ + HILOG_INFO("%{public}s called begin", __func__); + fileExtProxy_ = nullptr; + isConnected_.store(false); + HILOG_INFO("%{public}s called end", __func__); +} + +void FileExtConnection::ConnectFileExtAbility(const AAFwk::Want &want, const sptr &token) +{ + HILOG_INFO("%{public}s called begin", __func__); + ErrCode ret = AAFwk::AbilityManagerClient::GetInstance()->ConnectAbility(want, this, token); + HILOG_INFO("%{public}s called end, ret=%{public}d", __func__, ret); +} + +void FileExtConnection::DisconnectFileExtAbility() +{ + HILOG_INFO("%{public}s called begin", __func__); + fileExtProxy_ = nullptr; + isConnected_.store(false); + ErrCode ret = AAFwk::AbilityManagerClient::GetInstance()->DisconnectAbility(this); + HILOG_INFO("%{public}s called end, ret=%{public}d", __func__, ret); +} + +bool FileExtConnection::IsExtAbilityConnected() +{ + HILOG_INFO("%{public}s called ", __func__); + return isConnected_.load(); +} + +sptr FileExtConnection::GetFileExtProxy() +{ + HILOG_INFO("%{public}s called ", __func__); + return fileExtProxy_; +} +} // namespace FileAccessFwk +} // namespace OHOS \ No newline at end of file