From b8ea8b43d7877dbfb28edc1459ec1e17f99918b6 Mon Sep 17 00:00:00 2001 From: linjun9528 Date: Fri, 1 Jul 2022 19:44:41 +0800 Subject: [PATCH 1/3] notify code Signed-off-by: linjun9528 --- frameworks/innerkits/file_access/BUILD.gn | 6 + .../include/file_access_ext_ability.h | 6 + .../include/file_access_ext_proxy.h | 2 + .../include/file_access_ext_stub.h | 3 +- .../include/file_access_ext_stub_impl.h | 3 +- .../file_access/include/file_access_helper.h | 7 +- .../include/ifile_access_ext_base.h | 8 +- .../notify/file_access_notify_client.h | 37 +++ .../notify/file_access_notify_manager.h | 54 ++++ .../include/notify/ifile_access_notify.h | 34 +++ .../include/notify/ifile_access_notify_ipc.h | 65 +++++ .../src/file_access_ext_ability.cpp | 59 ++++ .../file_access/src/file_access_ext_proxy.cpp | 50 ++++ .../file_access/src/file_access_ext_stub.cpp | 50 ++++ .../src/file_access_ext_stub_impl.cpp | 34 +++ .../file_access/src/file_access_helper.cpp | 42 +++ .../src/js_file_access_ext_ability.cpp | 2 + .../src/notify/file_access_notify_client.cpp | 43 +++ .../src/notify/file_access_notify_ipc.cpp | 108 +++++++ .../src/notify/file_access_notify_manager.cpp | 274 ++++++++++++++++++ .../kits/napi/file_access_module/BUILD.gn | 2 + .../napi_fileaccess_helper.cpp | 101 +++++++ .../napi_fileaccess_helper.h | 2 + .../napi_fileaccess_notify.cpp | 93 ++++++ .../napi_fileaccess_notify.h | 49 ++++ utils/file_access_framework_errno.h | 5 +- 26 files changed, 1134 insertions(+), 5 deletions(-) create mode 100644 frameworks/innerkits/file_access/include/notify/file_access_notify_client.h create mode 100644 frameworks/innerkits/file_access/include/notify/file_access_notify_manager.h create mode 100644 frameworks/innerkits/file_access/include/notify/ifile_access_notify.h create mode 100644 frameworks/innerkits/file_access/include/notify/ifile_access_notify_ipc.h create mode 100644 frameworks/innerkits/file_access/src/notify/file_access_notify_client.cpp create mode 100644 frameworks/innerkits/file_access/src/notify/file_access_notify_ipc.cpp create mode 100644 frameworks/innerkits/file_access/src/notify/file_access_notify_manager.cpp create mode 100644 interfaces/kits/napi/file_access_module/napi_fileaccess_notify.cpp create mode 100644 interfaces/kits/napi/file_access_module/napi_fileaccess_notify.h diff --git a/frameworks/innerkits/file_access/BUILD.gn b/frameworks/innerkits/file_access/BUILD.gn index 26aabbc6..fc5407c0 100644 --- a/frameworks/innerkits/file_access/BUILD.gn +++ b/frameworks/innerkits/file_access/BUILD.gn @@ -39,6 +39,7 @@ config("ability_public_config") { visibility = [ ":*" ] include_dirs = [ "include", + "include/notify", "${BASE_DIR}/utils", "${ability_runtime_kits_path}/appkit/native/ability_runtime/app", "${ability_runtime_kits_path}/appkit/native/app/include", @@ -56,6 +57,9 @@ ohos_shared_library("file_access_extension_ability_kit") { "src/file_access_ext_stub.cpp", "src/file_access_ext_stub_impl.cpp", "src/file_access_helper.cpp", + "src/notify/file_access_notify_client.cpp", + "src/notify/file_access_notify_ipc.cpp", + "src/notify/file_access_notify_manager.cpp", "src/js_file_access_ext_ability.cpp", "src/napi_common_fileaccess.cpp", ] @@ -79,6 +83,7 @@ ohos_shared_library("file_access_extension_ability_kit") { "ability_runtime:runtime", "ability_runtime:wantagent_innerkits", "access_token:libaccesstoken_sdk", + "eventhandler:libeventhandler", "ipc:ipc_core", "ipc_js:rpc", "utils_base:utils", @@ -108,6 +113,7 @@ ohos_shared_library("file_access_extension_ability_module") { "ability_base:want", "ability_runtime:runtime", "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", "utils_base:utils", ] diff --git a/frameworks/innerkits/file_access/include/file_access_ext_ability.h b/frameworks/innerkits/file_access/include/file_access_ext_ability.h index 7689afb0..9357af73 100644 --- a/frameworks/innerkits/file_access/include/file_access_ext_ability.h +++ b/frameworks/innerkits/file_access/include/file_access_ext_ability.h @@ -18,6 +18,7 @@ #include "extension_base.h" #include "file_access_extension_info.h" +#include "file_access_notify_manager.h" namespace OHOS { namespace AbilityRuntime { @@ -49,8 +50,13 @@ public: virtual std::vector ListFile(const Uri &sourceFile); virtual std::vector GetRoots(); static void SetCreator(const CreatorFunc& creator); + virtual int RegisterNotify(sptr ¬ify); + virtual int UnregisterNotify(sptr ¬ify); + virtual int Notify(const Uri &uri, const NotifyType notifyType); private: + bool GetNotifyManager(); static CreatorFunc creator_; + std::unique_ptr notifyManager_; }; } // namespace FileAccessFwk } // namespace OHOS diff --git a/frameworks/innerkits/file_access/include/file_access_ext_proxy.h b/frameworks/innerkits/file_access/include/file_access_ext_proxy.h index d4918e22..6aa9f8fe 100644 --- a/frameworks/innerkits/file_access/include/file_access_ext_proxy.h +++ b/frameworks/innerkits/file_access/include/file_access_ext_proxy.h @@ -38,6 +38,8 @@ public: virtual std::vector ListFile(const Uri &sourceFile) override; virtual std::vector GetRoots() override; + virtual int RegisterNotify(sptr ¬ify) override; + virtual int UnregisterNotify(sptr ¬ify) override; private: static inline BrokerDelegator delegator_; }; diff --git a/frameworks/innerkits/file_access/include/file_access_ext_stub.h b/frameworks/innerkits/file_access/include/file_access_ext_stub.h index 73c1552a..f79129e3 100644 --- a/frameworks/innerkits/file_access/include/file_access_ext_stub.h +++ b/frameworks/innerkits/file_access/include/file_access_ext_stub.h @@ -38,7 +38,8 @@ private: ErrCode CmdRename(MessageParcel &data, MessageParcel &reply); ErrCode CmdListFile(MessageParcel &data, MessageParcel &reply); ErrCode CmdGetRoots(MessageParcel &data, MessageParcel &reply); - bool CheckCallingPermission(const std::string &permission); + ErrCode CmdRegisterNotify(MessageParcel &data, MessageParcel &reply); + ErrCode CmdUnregisterNotify(MessageParcel &data, MessageParcel &reply); using RequestFuncType = int (FileAccessExtStub::*)(MessageParcel &data, MessageParcel &reply); std::map stubFuncMap_; }; diff --git a/frameworks/innerkits/file_access/include/file_access_ext_stub_impl.h b/frameworks/innerkits/file_access/include/file_access_ext_stub_impl.h index 1cd130ba..34528539 100644 --- a/frameworks/innerkits/file_access/include/file_access_ext_stub_impl.h +++ b/frameworks/innerkits/file_access/include/file_access_ext_stub_impl.h @@ -40,7 +40,8 @@ public: int Rename(const Uri &sourceFile, const std::string &displayName, Uri &newFile) override; std::vector ListFile(const Uri &sourceFileUri) override; std::vector GetRoots() override; - + int RegisterNotify(sptr ¬ify) override; + int UnregisterNotify(sptr ¬ify) override; private: std::shared_ptr GetOwner(); std::shared_ptr extension_; diff --git a/frameworks/innerkits/file_access/include/file_access_helper.h b/frameworks/innerkits/file_access/include/file_access_helper.h index be151480..63068f9b 100644 --- a/frameworks/innerkits/file_access/include/file_access_helper.h +++ b/frameworks/innerkits/file_access/include/file_access_helper.h @@ -22,8 +22,10 @@ #include "context.h" #include "file_access_ext_connection.h" #include "file_access_extension_info.h" +#include "file_access_notify_client.h" #include "hilog_wrapper.h" #include "ifile_access_ext_base.h" +#include "ifile_access_notify.h" #include "uri.h" #include "want.h" @@ -50,7 +52,8 @@ public: int Rename(Uri &sourceFile, const std::string &displayName, Uri &newFile); std::vector ListFile(Uri &sourceFile); std::vector GetRoots(); - + int On(std::shared_ptr ¬ify); + int Off(); private: FileAccessHelper(const std::shared_ptr &context, const AAFwk::Want &want, const sptr &fileAccessExtProxy); @@ -65,6 +68,8 @@ private: bool isSystemCaller_ = false; sptr callerDeathRecipient_ = nullptr; sptr fileAccessExtConnection_ = nullptr; + + sptr notifyClient_; }; class FileAccessDeathRecipient : public IRemoteObject::DeathRecipient { diff --git a/frameworks/innerkits/file_access/include/ifile_access_ext_base.h b/frameworks/innerkits/file_access/include/ifile_access_ext_base.h index 5c79a87b..089253f3 100644 --- a/frameworks/innerkits/file_access/include/ifile_access_ext_base.h +++ b/frameworks/innerkits/file_access/include/ifile_access_ext_base.h @@ -22,6 +22,8 @@ #include #include "file_access_extension_info.h" +#include "file_access_framework_errno.h" +#include "ifile_access_notify_ipc.h" #include "uri.h" namespace OHOS { @@ -38,7 +40,9 @@ public: CMD_MOVE = 5, CMD_RENAME = 6, CMD_LIST_FILE = 7, - CMD_GET_ROOTS = 8 + CMD_GET_ROOTS = 8, + CMD_REGISTER_NOTIFY = 9, + CMD_UNREGISTER_NOTIFY = 10, }; virtual int OpenFile(const Uri &uri, int flags) = 0; @@ -50,6 +54,8 @@ public: virtual std::vector ListFile(const Uri &sourceFile) = 0; virtual std::vector GetRoots() = 0; + virtual int RegisterNotify(sptr ¬ify) = 0; + virtual int UnregisterNotify(sptr ¬ify) = 0; }; } // namespace FileAccessFwk } // namespace OHOS diff --git a/frameworks/innerkits/file_access/include/notify/file_access_notify_client.h b/frameworks/innerkits/file_access/include/notify/file_access_notify_client.h new file mode 100644 index 00000000..318d75e2 --- /dev/null +++ b/frameworks/innerkits/file_access/include/notify/file_access_notify_client.h @@ -0,0 +1,37 @@ +/* + * 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_NOTIFY_CLIENT_H__ +#define __FILE_ACCESS_NOTIFY_CLIENT_H__ + +#include +#include "ifile_access_notify.h" +#include "ifile_access_notify_ipc.h" +#include "iremote_object.h" + +namespace OHOS { +namespace FileAccessFwk { +class FileAccessNotifyClient final : public FileAccessNotifyStub { +public: + FileAccessNotifyClient(std::shared_ptr ¬ify); + ~FileAccessNotifyClient(); + void OnNotify(const Uri &uri, const NotifyType notifyType) override; + +private: + std::shared_ptr notify_; +}; +} // namespace FileAccessFwk +} // namespace OHOS +#endif // __FILE_ACCESS_NOTIFY_CLIENT_H__ \ No newline at end of file diff --git a/frameworks/innerkits/file_access/include/notify/file_access_notify_manager.h b/frameworks/innerkits/file_access/include/notify/file_access_notify_manager.h new file mode 100644 index 00000000..8efb2201 --- /dev/null +++ b/frameworks/innerkits/file_access/include/notify/file_access_notify_manager.h @@ -0,0 +1,54 @@ +/* + * 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_NOTIFY_MANAGER_H__ +#define __FILE_ACCESS_NOTIFY_MANAGER_H__ + +#include +#include +#include + +#include "event_handler.h" +#include "ifile_access_notify_ipc.h" +#include "iremote_object.h" + +namespace OHOS { +namespace FileAccessFwk { +using EventHandler = OHOS::AppExecFwk::EventHandler; +// /home/darkknight/huawei/harmony_master/foundation/ability/ability_runtime/services/dataobsmgr/include/dataobs_mgr_inner.h +class FileAccessNotifyManager final : public std::enable_shared_from_this { +public: + using RecipientMapType = std::map, sptr>; + + FileAccessNotifyManager(); + ~FileAccessNotifyManager(); + + int RegisterNotify(sptr ¬ify); + int UnregisterNotify(sptr ¬ify); + int Notify(const Uri uri, const NotifyType notifyType); + void OnCallBackDied(const wptr &remote); + +private: + void AddDeathRecipient(const sptr ¬ify); + void RemoveDeathRecipient(const sptr ¬ify); + void HandleCallBackDiedTask(const sptr &remote); + std::shared_ptr handler_ = nullptr; + RecipientMapType recipientMap_; + static std::mutex notifyVecMutex_; + std::vector> notifyVec; +}; +} // namespace FileAccessFwk +} // namespace OHOS +#endif // __FILE_ACCESS_NOTIFY_MANAGER_H__ \ No newline at end of file diff --git a/frameworks/innerkits/file_access/include/notify/ifile_access_notify.h b/frameworks/innerkits/file_access/include/notify/ifile_access_notify.h new file mode 100644 index 00000000..0bb801fe --- /dev/null +++ b/frameworks/innerkits/file_access/include/notify/ifile_access_notify.h @@ -0,0 +1,34 @@ +/* + * 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_ACCESS_NOTIFY_H__ +#define __I_FILE_ACCESS_NOTIFY_H__ + +#include "uri.h" + +namespace OHOS { +namespace FileAccessFwk { +enum NotifyType { + NOTIFY_DEVICE_CHANGE, + NOTIFY_FILE_CHANGE, +}; + +class IFileAccessNotify { +public: + virtual void OnNotify(const Uri &uri, const NotifyType notifyType) = 0; +}; +} // namespace FileAccessFwk +} // namespace OHOS +#endif // __I_FILE_ACCESS_NOTIFY_H__ \ No newline at end of file diff --git a/frameworks/innerkits/file_access/include/notify/ifile_access_notify_ipc.h b/frameworks/innerkits/file_access/include/notify/ifile_access_notify_ipc.h new file mode 100644 index 00000000..4110b86a --- /dev/null +++ b/frameworks/innerkits/file_access/include/notify/ifile_access_notify_ipc.h @@ -0,0 +1,65 @@ +/* + * 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_ACCESS_NOTIFY_IPC_H__ +#define __I_FILE_ACCESS_NOTIFY_IPC_H__ + +#include "ifile_access_notify.h" +#include "iremote_broker.h" +#include "iremote_proxy.h" +#include "iremote_stub.h" +#include "uri.h" + +namespace OHOS { +namespace FileAccessFwk { +class IFileAccessNotifyIPC : public IFileAccessNotify, public IRemoteBroker { +public: + DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.FileAccessFwk.IFileAccessNotifyIPC"); + + enum { + CMD_ON_NOTIFY = 1, + }; + + virtual void OnNotify(const Uri &uri, const NotifyType notifyType) override = 0; +}; + +class FileAccessNotifyStub : public IRemoteStub { +public: + virtual int OnRemoteRequest(uint32_t code, MessageParcel &data, + MessageParcel &reply, MessageOption &option) override; +}; + +class FileAccessNotifyCallbackRecipient : public IRemoteObject::DeathRecipient { +public: + using RemoteDiedHandler = std::function &)>; + FileAccessNotifyCallbackRecipient(RemoteDiedHandler handler); + virtual ~FileAccessNotifyCallbackRecipient(); + virtual void OnRemoteDied(const wptr &remote); + +private: + RemoteDiedHandler handler_; +}; + +class FileAccessNotifyProxy : public IRemoteProxy { +public: + explicit FileAccessNotifyProxy(const sptr &impl); + ~FileAccessNotifyProxy() = default; + void OnNotify(const Uri &uri, const NotifyType notifyType) override; +private: + static inline BrokerDelegator delegator_; +}; +} // namespace FileAccessFwk +} // namespace OHOS +#endif // __I_FILE_ACCESS_NOTIFY_IPC_H__ \ No newline at end of file diff --git a/frameworks/innerkits/file_access/src/file_access_ext_ability.cpp b/frameworks/innerkits/file_access/src/file_access_ext_ability.cpp index a570e63d..ce49c3b6 100644 --- a/frameworks/innerkits/file_access/src/file_access_ext_ability.cpp +++ b/frameworks/innerkits/file_access/src/file_access_ext_ability.cpp @@ -99,5 +99,64 @@ std::vector FileAccessExtAbility::GetRoots() std::vector vec; return vec; } + +bool FileAccessExtAbility::GetNotifyManager() +{ + if (notifyManager_ == nullptr) { + notifyManager_ = std::make_unique(); + if (notifyManager_ == nullptr) { + return false; + } + } + return true; +} + +int FileAccessExtAbility::RegisterNotify(sptr ¬ify) +{ + HILOG_INFO("%{public}s, called start", __func__); + if (!GetNotifyManager()) { + HILOG_ERROR("GetNotifyManager fail."); + return ERR_ERROR; + } + HILOG_INFO("%{public}s, called 1", __func__); + int ret = notifyManager_->RegisterNotify(notify); + if (ret != ERR_OK) { + HILOG_ERROR("NotifyManager register notify fail."); + } + HILOG_INFO("%{public}s, called end", __func__); + return ret; +} + +int FileAccessExtAbility::UnregisterNotify(sptr ¬ify) +{ + HILOG_INFO("%{public}s, called start", __func__); + if (!GetNotifyManager()) { + HILOG_ERROR("GetNotifyManager fail."); + return ERR_ERROR; + } + HILOG_INFO("%{public}s, called 1", __func__); + int ret = notifyManager_->UnregisterNotify(notify); + if (ret != ERR_OK) { + HILOG_ERROR("NotifyManager unregister notify fail."); + } + HILOG_INFO("%{public}s, called end", __func__); + return ret; +} + +int FileAccessExtAbility::Notify(const Uri &uri, const NotifyType notifyType) +{ + HILOG_INFO("%{public}s, called start", __func__); + if (!GetNotifyManager()) { + HILOG_ERROR("GetNotifyManager fail."); + return ERR_ERROR; + } + HILOG_INFO("%{public}s, called 1", __func__); + int ret = notifyManager_->Notify(uri, notifyType); + if (ret != ERR_OK) { + HILOG_ERROR("NotifyManager handle notify fail."); + } + HILOG_INFO("%{public}s, called end", __func__); + return ret; +} } // namespace FileAccessFwk } // namespace OHOS \ No newline at end of file diff --git a/frameworks/innerkits/file_access/src/file_access_ext_proxy.cpp b/frameworks/innerkits/file_access/src/file_access_ext_proxy.cpp index 129d687d..9d296d8e 100644 --- a/frameworks/innerkits/file_access/src/file_access_ext_proxy.cpp +++ b/frameworks/innerkits/file_access/src/file_access_ext_proxy.cpp @@ -334,5 +334,55 @@ std::vector FileAccessExtProxy::GetRoots() return vec; } + +int FileAccessExtProxy::RegisterNotify(sptr ¬ify) +{ + HILOG_INFO("%{public}s, called start", __func__); + MessageParcel data; + if (!data.WriteInterfaceToken(FileAccessExtProxy::GetDescriptor())) { + HILOG_ERROR("%{public}s WriteInterfaceToken failed", __func__); + return ERR_PARCEL_ERROR; + } + HILOG_INFO("%{public}s, called 1", __func__); + if (!data.WriteRemoteObject(notify->AsObject())) { + HILOG_ERROR("write subscribe type or parcel failed."); + return ERR_PARCEL_ERROR; + } + HILOG_INFO("%{public}s, called 2", __func__); + MessageParcel reply; + MessageOption option; + int err = Remote()->SendRequest(CMD_REGISTER_NOTIFY, data, reply, option); + if (err != NO_ERROR) { + HILOG_ERROR("%{public}s fail to SendRequest. err: %d", __func__, err); + return err; + } + HILOG_INFO("%{public}s, called end", __func__); + return err; +} + +int FileAccessExtProxy::UnregisterNotify(sptr ¬ify) +{ + HILOG_INFO("%{public}s, called start", __func__); + MessageParcel data; + if (!data.WriteInterfaceToken(FileAccessExtProxy::GetDescriptor())) { + HILOG_ERROR("%{public}s WriteInterfaceToken failed", __func__); + return ERR_PARCEL_ERROR; + } + HILOG_INFO("%{public}s, called 1", __func__); + if (!data.WriteRemoteObject(notify->AsObject())) { + HILOG_ERROR("write subscribe type or parcel failed."); + return ERR_PARCEL_ERROR; + } + HILOG_INFO("%{public}s, called 2", __func__); + MessageParcel reply; + MessageOption option; + int err = Remote()->SendRequest(CMD_UNREGISTER_NOTIFY, data, reply, option); + if (err != NO_ERROR) { + HILOG_ERROR("%{public}s fail to SendRequest. err: %d", __func__, err); + return err; + } + HILOG_INFO("%{public}s, called end", __func__); + return err; +} } // namespace FileAccessFwk } // namespace OHOS diff --git a/frameworks/innerkits/file_access/src/file_access_ext_stub.cpp b/frameworks/innerkits/file_access/src/file_access_ext_stub.cpp index a7a5b8c1..249343b9 100644 --- a/frameworks/innerkits/file_access/src/file_access_ext_stub.cpp +++ b/frameworks/innerkits/file_access/src/file_access_ext_stub.cpp @@ -35,6 +35,8 @@ FileAccessExtStub::FileAccessExtStub() stubFuncMap_[CMD_RENAME] = &FileAccessExtStub::CmdRename; stubFuncMap_[CMD_LIST_FILE] = &FileAccessExtStub::CmdListFile; stubFuncMap_[CMD_GET_ROOTS] = &FileAccessExtStub::CmdGetRoots; + stubFuncMap_[CMD_REGISTER_NOTIFY] = &FileAccessExtStub::CmdRegisterNotify; + stubFuncMap_[CMD_UNREGISTER_NOTIFY] = &FileAccessExtStub::CmdUnregisterNotify; } FileAccessExtStub::~FileAccessExtStub() @@ -319,5 +321,53 @@ bool FileAccessExtStub::CheckCallingPermission(const std::string &permission) } return true; } + +ErrCode FileAccessExtStub::CmdRegisterNotify(MessageParcel &data, MessageParcel &reply) +{ + HILOG_INFO("%{public}s, called start", __func__); + auto remote = data.ReadRemoteObject(); + HILOG_INFO("%{public}s, called 4, remote:%{public}p", __func__, remote.GetRefPtr()); + if (remote == nullptr) { + HILOG_INFO("%{public}s, called 1", __func__); + return ERR_PARCEL_ERROR; + } + HILOG_INFO("%{public}s, called 4", __func__); + auto notify = iface_cast(remote); + if (notify == nullptr) { + HILOG_INFO("%{public}s, called 3", __func__); + return ERR_PARCEL_ERROR; + } + int ret = RegisterNotify(notify); + if (!reply.WriteInt32(ret)) { + HILOG_ERROR("WriteInt32 failed"); + return ERR_PARCEL_ERROR; + } + HILOG_INFO("%{public}s, called end", __func__); + return ERR_OK; +} + +ErrCode FileAccessExtStub::CmdUnregisterNotify(MessageParcel &data, MessageParcel &reply) +{ + HILOG_INFO("%{public}s, called start", __func__); + auto remote = data.ReadRemoteObject(); + HILOG_INFO("%{public}s, called 4, remote:%{public}p", __func__, remote.GetRefPtr()); + if (remote == nullptr) { + HILOG_INFO("%{public}s, called 1", __func__); + return ERR_PARCEL_ERROR; + } + HILOG_INFO("%{public}s, called 4", __func__); + auto notify = iface_cast(remote); + if (notify == nullptr) { + HILOG_INFO("%{public}s, called 3", __func__); + return ERR_PARCEL_ERROR; + } + int ret = UnregisterNotify(notify); + if (!reply.WriteInt32(ret)) { + HILOG_ERROR("WriteInt32 failed"); + return ERR_PARCEL_ERROR; + } + HILOG_INFO("%{public}s, called end", __func__); + return ERR_OK; +} } // namespace FileAccessFwk } // namespace OHOS \ No newline at end of file diff --git a/frameworks/innerkits/file_access/src/file_access_ext_stub_impl.cpp b/frameworks/innerkits/file_access/src/file_access_ext_stub_impl.cpp index d7355fa7..d7227206 100644 --- a/frameworks/innerkits/file_access/src/file_access_ext_stub_impl.cpp +++ b/frameworks/innerkits/file_access/src/file_access_ext_stub_impl.cpp @@ -122,5 +122,39 @@ std::vector FileAccessExtStubImpl::GetRoots() vec = extension->GetRoots(); return vec; } + +int FileAccessExtStubImpl::RegisterNotify(sptr ¬ify) +{ + HILOG_INFO("%{public}s, called start", __func__); + auto extension = GetOwner(); + if (extension == nullptr) { + HILOG_ERROR("get extension failed."); + return ERR_ERROR; + } + HILOG_INFO("%{public}s, called 1", __func__); + int ret = extension->RegisterNotify(notify); + if (ret != ERR_OK) { + HILOG_ERROR("RegisterNotify failed."); + } + HILOG_INFO("%{public}s, called end", __func__); + return ret; +} + +int FileAccessExtStubImpl::UnregisterNotify(sptr ¬ify) +{ + HILOG_INFO("%{public}s, called start", __func__); + auto extension = GetOwner(); + if (extension == nullptr) { + HILOG_ERROR("get extension failed."); + return ERR_ERROR; + } + HILOG_INFO("%{public}s, called 1", __func__); + int ret = extension->UnregisterNotify(notify); + if (ret != ERR_OK) { + HILOG_ERROR("UnregisterNotify failed."); + } + HILOG_INFO("%{public}s, called end", __func__); + return ret; +} } // namespace FileAccessFwk } // namespace OHOS \ 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 index 6e04cb8e..a88a538a 100644 --- a/frameworks/innerkits/file_access/src/file_access_helper.cpp +++ b/frameworks/innerkits/file_access/src/file_access_helper.cpp @@ -234,6 +234,48 @@ std::vector FileAccessHelper::GetRoots() return results; } +int FileAccessHelper::On(std::shared_ptr ¬ify) +{ + HILOG_INFO("%{public}s, called start", __func__); + notifyClient_ = new (std::nothrow) FileAccessNotifyClient(notify); + if (notifyClient_ == nullptr) { + HILOG_INFO("%{public}s, 1", __func__); + return ERR_NOTIFY_NOT_EXIST; + } + HILOG_INFO("%{public}s, 2, notifyClient:%{public}p", __func__, notifyClient_.GetRefPtr()); + if (!GetProxy()) { + HILOG_ERROR("%{public}s failed with invalid fileAccessExtProxy_", __func__); + return ERR_IPC_ERROR; + } + HILOG_INFO("%{public}s, 3", __func__); + int ret = fileAccessExtProxy_->RegisterNotify(notifyClient_); + if (ret != ERR_OK) { + HILOG_ERROR("fileAccessExtProxy_ RegisterNotify fail"); + } + HILOG_INFO("%{public}s, called end", __func__); + return ret; +} + +int FileAccessHelper::Off() +{ + HILOG_INFO("%{public}s, called start", __func__); + if (notifyClient_ == nullptr) { + HILOG_ERROR("not registered notify"); + return ERR_NOTIFY_NOT_EXIST; + } + if (!GetProxy()) { + HILOG_ERROR("%{public}s failed with invalid fileAccessExtProxy_", __func__); + return ERR_IPC_ERROR; + } + HILOG_INFO("%{public}s, 3", __func__); + int ret = fileAccessExtProxy_->UnregisterNotify(notifyClient_); + if (ret != ERR_OK) { + HILOG_ERROR("fileAccessExtProxy_ unregisterNotify fail"); + } + HILOG_INFO("%{public}s, called end", __func__); + return ret; +} + void FileAccessDeathRecipient::OnRemoteDied(const wptr &remote) { if (handler_) { diff --git a/frameworks/innerkits/file_access/src/js_file_access_ext_ability.cpp b/frameworks/innerkits/file_access/src/js_file_access_ext_ability.cpp index 9bdefbef..ba67290d 100644 --- a/frameworks/innerkits/file_access/src/js_file_access_ext_ability.cpp +++ b/frameworks/innerkits/file_access/src/js_file_access_ext_ability.cpp @@ -269,6 +269,8 @@ int JsFileAccessExtAbility::CreateFile(const Uri &parent, const std::string &dis ret = NO_ERROR; } newFile = Uri(uriStr); + + Notify(newFile, NotifyType::NOTIFY_FILE_CHANGE); return ret; } diff --git a/frameworks/innerkits/file_access/src/notify/file_access_notify_client.cpp b/frameworks/innerkits/file_access/src/notify/file_access_notify_client.cpp new file mode 100644 index 00000000..49e768d2 --- /dev/null +++ b/frameworks/innerkits/file_access/src/notify/file_access_notify_client.cpp @@ -0,0 +1,43 @@ +/* + * 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 "hilog_wrapper.h" +#include "file_access_notify_client.h" + +namespace OHOS { +namespace FileAccessFwk { +FileAccessNotifyClient::FileAccessNotifyClient(std::shared_ptr ¬ify) + : notify_(notify) +{ + HILOG_INFO("%{public}s, called start", __func__); + HILOG_INFO("%{public}s, called end", __func__); +} + +FileAccessNotifyClient::~FileAccessNotifyClient() +{ + HILOG_INFO("%{public}s, called start", __func__); + HILOG_INFO("%{public}s, called end", __func__); +} + +void FileAccessNotifyClient::OnNotify(const Uri &uri, const NotifyType notifyType) +{ + HILOG_INFO("%{public}s, called start", __func__); + if (notify_ != nullptr) { + notify_->OnNotify(uri, notifyType); + } + HILOG_INFO("%{public}s, called end", __func__); +} +} // namespace FileAccessFwk +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/innerkits/file_access/src/notify/file_access_notify_ipc.cpp b/frameworks/innerkits/file_access/src/notify/file_access_notify_ipc.cpp new file mode 100644 index 00000000..f78ffbb1 --- /dev/null +++ b/frameworks/innerkits/file_access/src/notify/file_access_notify_ipc.cpp @@ -0,0 +1,108 @@ +/* + * 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 "ifile_access_notify_ipc.h" + +#include "file_access_framework_errno.h" +#include "hilog_wrapper.h" +#include "message_parcel.h" + +namespace OHOS { +namespace FileAccessFwk { +FileAccessNotifyProxy::FileAccessNotifyProxy(const sptr &impl) : IRemoteProxy(impl) +{ + HILOG_INFO("%{public}s, called start", __func__); + HILOG_INFO("%{public}s, called end", __func__); +} + +void FileAccessNotifyProxy::OnNotify(const Uri &uri, const NotifyType notifyType) +{ + HILOG_INFO("%{public}s, called start", __func__); + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(FileAccessNotifyProxy::GetDescriptor())) { + HILOG_ERROR("write descriptor failed"); + return; + } + HILOG_INFO("%{public}s, called 1", __func__); + if (!data.WriteParcelable(&uri)) { + HILOG_ERROR("write parcel uri failed"); + return; + } + HILOG_INFO("%{public}s, called 2", __func__); + if (!data.WriteInt32((int32_t)notifyType)) { + HILOG_ERROR("write parcel notifyType failed"); + return; + } + HILOG_INFO("%{public}s, called 3", __func__); + int error = Remote()->SendRequest(CMD_ON_NOTIFY, data, reply, option); + if (error != 0) { + HILOG_ERROR("SendRequest failed, error %d", error); + } + HILOG_INFO("%{public}s, called end", __func__); +} + +int32_t FileAccessNotifyStub::OnRemoteRequest(uint32_t code, MessageParcel &data, + MessageParcel &reply, MessageOption &option) +{ + HILOG_INFO("%{public}s, called start", __func__); + auto descriptor = FileAccessNotifyStub::GetDescriptor(); + auto remoteDescriptor = data.ReadInterfaceToken(); + if (descriptor != remoteDescriptor) { + HILOG_ERROR("local descriptor is not equal to remote"); + return ERR_ERROR; + } + HILOG_INFO("%{public}s, called 1", __func__); + switch (code) { + case CMD_ON_NOTIFY: { + std::shared_ptr uri(data.ReadParcelable()); + if (uri == nullptr) { + HILOG_ERROR("read parcel uri fail"); + return ERR_PARCEL_ERROR; + } + NotifyType notifyType = (NotifyType)data.ReadInt32(); + OnNotify(*uri, notifyType); + HILOG_DEBUG("FileAccessNotifyStub::OnRemoteRequest End"); + return ERR_OK; + } + default: + HILOG_DEBUG("code error, FileAccessNotifyStub::OnRemoteRequest End"); + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); + } +} + +void FileAccessNotifyCallbackRecipient::OnRemoteDied(const wptr &remote) +{ + HILOG_INFO("%{public}s, called start", __func__); + if (handler_) { + handler_(remote); + } + HILOG_INFO("%{public}s, called end", __func__); +} + +FileAccessNotifyCallbackRecipient::FileAccessNotifyCallbackRecipient(RemoteDiedHandler handler) : handler_(handler) +{ + HILOG_INFO("%{public}s, called start", __func__); + HILOG_INFO("%{public}s, called end", __func__); +} + +FileAccessNotifyCallbackRecipient::~FileAccessNotifyCallbackRecipient() +{ + HILOG_INFO("%{public}s, called start", __func__); + HILOG_INFO("%{public}s, called end", __func__); +} +} // namespace FileAccessFwk +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/innerkits/file_access/src/notify/file_access_notify_manager.cpp b/frameworks/innerkits/file_access/src/notify/file_access_notify_manager.cpp new file mode 100644 index 00000000..e6824f0f --- /dev/null +++ b/frameworks/innerkits/file_access/src/notify/file_access_notify_manager.cpp @@ -0,0 +1,274 @@ +/* + * 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_notify_manager.h" + +#include + +#include "file_access_framework_errno.h" +#include "hilog_wrapper.h" + +namespace OHOS { +namespace FileAccessFwk { +std::mutex FileAccessNotifyManager::notifyVecMutex_; + +FileAccessNotifyManager::FileAccessNotifyManager() +{ +} + +FileAccessNotifyManager::~FileAccessNotifyManager() +{ +} + +int FileAccessNotifyManager::RegisterNotify(sptr ¬ify) +{ + HILOG_INFO("%{public}s, called start", __func__); + HILOG_INFO("%{public}s, notify:%{public}p", __func__, notify.GetRefPtr()); + HILOG_INFO("%{public}s, notifyVec.size():%{public}zu", __func__, notifyVec.size()); + std::lock_guard lock(notifyVecMutex_); + for (auto notify : notifyVec) { + if (notify->AsObject() == notify->AsObject()) { + HILOG_ERROR("the notify exist. no need to register."); + return ERR_NOTIFY_EXIST; + } + } + // std::vector>::iterator it = std::find(notifyVec.begin(), notifyVec.end(), notify); + // if (it != notifyVec.end()) { + // if ((*it)->AsObject() == notify->AsObject()) { + // HILOG_ERROR("the notify exist. no need to register."); + // return ERR_NOTIFY_EXIST; + // } + // } + notifyVec.emplace_back(notify); + // AddDeathRecipient(notify); + HILOG_INFO("%{public}s, called end notifyVec size = %{public}zu", __func__, notifyVec.size()); + return ERR_OK; +} + +int FileAccessNotifyManager::UnregisterNotify(sptr ¬ify) +{ + HILOG_INFO("%{public}s, called start", __func__); + HILOG_INFO("%{public}s, notify:%{public}p", __func__, notify.GetRefPtr()); + HILOG_INFO("%{public}s, notifyVec.size():%{public}zu", __func__, notifyVec.size()); + std::lock_guard lock(notifyVecMutex_); + std::vector>::iterator it = notifyVec.begin(); + for (; it != notifyVec.end(); it++) { + if ((*it)->AsObject() == notify->AsObject()) { + notifyVec.erase(it); + // RemoveDeathRecipient(notify); + HILOG_INFO("%{public}s, called 1", __func__); + return ERR_OK; + } + } + // std::vector>::iterator it = std::find(notifyVec.begin(), notifyVec.end(), notify); + // if (it == notifyVec.end()) { + // HILOG_ERROR("the notify not exist."); + // return ERR_NOTIFY_EXIST; + // } + + // if ((*it)->AsObject() == notify->AsObject()) { + // notifyVec.erase(it); + // RemoveDeathRecipient(notify); + // HILOG_INFO("%{public}s, called 1", __func__); + // return ERR_OK; + // } + HILOG_INFO("%{public}s, called end notifyVec size = %{public}zu", __func__, notifyVec.size()); + return ERR_NOTIFY_EXIST; +} + +int FileAccessNotifyManager::Notify(const Uri uri, const NotifyType notifyType) +{ + HILOG_INFO("%{public}s, called start", __func__); + HILOG_INFO("%{public}s, notifyVec.size():%{public}zu", __func__, notifyVec.size()); + std::lock_guard lock(notifyVecMutex_); + for (auto notify : notifyVec) { + HILOG_INFO("%{public}s, called 1", __func__); + if (notify != nullptr) { + HILOG_INFO("%{public}s, called 2", __func__); + notify->OnNotify(uri, notifyType); + } + } + HILOG_INFO("%{public}s, called end", __func__); + return ERR_OK; +} + +void FileAccessNotifyManager::AddDeathRecipient(const sptr ¬ify) +{ + HILOG_INFO("%{public}s, called start", __func__); + if ((notify == nullptr) || notify->AsObject() == nullptr) { + HILOG_ERROR("the death recipient obj is nullptr."); + return; + } + HILOG_INFO("%{public}s, called 1", __func__); + auto it = recipientMap_.find(notify->AsObject()); + if (it != recipientMap_.end()) { + HILOG_ERROR("the death recipient obj has been added."); + return; + } + HILOG_INFO("%{public}s, called 2", __func__); + std::weak_ptr thisWeakPtr(shared_from_this()); + HILOG_INFO("%{public}s, called 2.0", __func__); + sptr deathRecipient = + new FileAccessNotifyCallbackRecipient([thisWeakPtr](const wptr &remote) { + HILOG_INFO("%{public}s, called 2.1", __func__); + auto fileAccessNotifyManager = thisWeakPtr.lock(); + if (fileAccessNotifyManager) { + fileAccessNotifyManager->OnCallBackDied(remote); + } + HILOG_INFO("%{public}s, called 2.2", __func__); + }); + HILOG_INFO("%{public}s, called 3", __func__); + notify->AsObject()->AddDeathRecipient(deathRecipient); + HILOG_INFO("%{public}s, called 4", __func__); + recipientMap_.emplace(notify->AsObject(), deathRecipient); + HILOG_INFO("%{public}s, called end", __func__); +} + +void FileAccessNotifyManager::RemoveDeathRecipient(const sptr ¬ify) +{ + HILOG_INFO("%{public}s, called start", __func__); + if ((notify == nullptr) || notify->AsObject() == nullptr) { + HILOG_INFO("%{public}s, called 1", __func__); + return; + } + HILOG_INFO("%{public}s, called 2", __func__); + auto it = recipientMap_.find(notify->AsObject()); + if (it != recipientMap_.end()) { + it->first->RemoveDeathRecipient(it->second); + recipientMap_.erase(it); + } + HILOG_INFO("%{public}s, called end", __func__); +} + +void FileAccessNotifyManager::OnCallBackDied(const wptr &remote) +{ + HILOG_INFO("%{public}s, called start", __func__); + auto object = remote.promote(); + if (object == nullptr) { + HILOG_INFO("%{public}s, called 1", __func__); + return; + } + HILOG_INFO("%{public}s, called 2", __func__); + if (handler_) { + auto task = [object, fileAccessNotifyManager = shared_from_this()]() { + fileAccessNotifyManager->HandleCallBackDiedTask(object); + }; + handler_->PostTask(task); + } + HILOG_INFO("%{public}s, called end", __func__); +} + +void FileAccessNotifyManager::HandleCallBackDiedTask(const sptr &remote) +{ + HILOG_INFO("%{public}s, called start", __func__); + std::lock_guard lock_l(notifyVecMutex_); + if (remote == nullptr) { + return; + } + HILOG_INFO("%{public}s, called 1", __func__); + sptr notify = iface_cast(remote); + if (notify == nullptr) { + HILOG_INFO("%{public}s, called 2", __func__); + return; + } + HILOG_INFO("%{public}s, called 3", __func__); + auto recipientIter = recipientMap_.find(notify->AsObject()); + if (recipientIter != recipientMap_.end()) { + recipientIter->first->RemoveDeathRecipient(recipientIter->second); + recipientMap_.erase(recipientIter); + } + HILOG_INFO("%{public}s, called 4", __func__); + auto it = std::find(notifyVec.begin(), notifyVec.end(), notify); + if (it != notifyVec.end()) { + if ((*it)->AsObject() == notify->AsObject()) { + notifyVec.erase(it); + } + } + HILOG_INFO("%{public}s, called 5", __func__); +} +#if 0 +bool FileAccessNotifyManager::GetNotifyMap(const NotifyType notifyType, NotifyMapType ¬ifyMap) +{ + switch (notifyType) + { + case NOTIFY_DEVICE_CHANGE: + notifyMap = ¬ifyDeviceMap_; + break; + case NOTIFY_FILE_CHANGE: + notifyMap = ¬ifyFileMap_; + break; + default: + HILOG_ERROR("error notify type."); + return false; + } + return true; +} + +bool FileAccessNotifyManager::GetNotifyListFromMap(const Uri &uri, const NotifyType notifyType, + NotifyListType ¬ifylist) +{ + NotifyMapType targetMap; + if (!GetNotifyMap(notifyType, targetMap)) { + return false; + } + auto it = targetMap.find(uri.ToString()); + if (it == targetMap.end()) { + return false; + } + + notifylist = it->second; + return true; +} + +void FileAccessNotifyManager::RemoveNotifyFromMap(const NotifyType notifyType, const sptr ¬ify) +{ + NotifyMapType targetMap; + if (!GetNotifyMap(notifyType, targetMap)) { + HILOG_WARN("the type of notify is not in map."); + return; + } + for (auto ¬ifyCallback : targetMap) { + auto ¬ifyList = notifyCallback.second; + auto targetNotify = std::find(notifyList.begin(), notifyList.end(), notify); + if (targetNotify != notifyList.end()) { + notifyList.remove(notify); + if (notifyList.empty()) { + HILOG_INFO("%{public}s: remove notifyList from map ", __func__); + targetMap.erase(notifyCallback.first); + } + } + } + RemoveDeathRecipient(notify); +} + +bool FileAccessNotifyManager::NotifyExistInMap(const NotifyType notifyType, const sptr ¬ify) +{ + NotifyMapType targetMap; + if (!GetNotifyMap(notifyType, targetMap)) { + HILOG_WARN("the type of notify is not in map."); + return; + } + for (auto ¬ifyCallback : targetMap) { + auto ¬ifyList = notifyCallback.second; + auto targetNotify = std::find(notifyList.begin(), notifyList.end(), notify); + if (targetNotify != notifyList.end()) { + return true; + } + } + return false; +} +#endif +} // namespace FileAccessFwk +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/napi/file_access_module/BUILD.gn b/interfaces/kits/napi/file_access_module/BUILD.gn index c3317d50..1a4949e5 100644 --- a/interfaces/kits/napi/file_access_module/BUILD.gn +++ b/interfaces/kits/napi/file_access_module/BUILD.gn @@ -31,6 +31,7 @@ ohos_shared_library("fileaccess") { sources = [ "${BASE_DIR}/interfaces/kits/napi/common/file_extension_info_napi.cpp", "napi_fileaccess_helper.cpp", + "napi_fileaccess_notify.cpp", "native_fileaccess_module.cpp", ] @@ -47,6 +48,7 @@ ohos_shared_library("fileaccess") { "ability_runtime:abilitykit_native", "ability_runtime:napi_base_context", "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", "napi:ace_napi", "utils_base:utils", ] diff --git a/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.cpp b/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.cpp index 074bdba8..fc0c09e0 100644 --- a/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.cpp +++ b/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.cpp @@ -20,7 +20,9 @@ #include "file_access_framework_errno.h" #include "hilog_wrapper.h" +#include "ifile_access_notify.h" #include "napi_base_context.h" +#include "napi_fileaccess_notify.h" #include "napi_common_fileaccess.h" #include "n_func_arg.h" #include "n_val.h" @@ -174,6 +176,8 @@ napi_value FileAccessHelperInit(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("rename", NAPI_Rename), DECLARE_NAPI_FUNCTION("listFile", NAPI_ListFile), DECLARE_NAPI_FUNCTION("getRoots", NAPI_GetRoots), + DECLARE_NAPI_FUNCTION("on", NAPI_On), + DECLARE_NAPI_FUNCTION("off", NAPI_Off) }; napi_value cons = nullptr; NAPI_CALL(env, @@ -1700,5 +1704,102 @@ napi_value NAPI_GetRoots(napi_env env, napi_callback_info info) } return ret; } + +napi_value NAPI_On(napi_env env, napi_callback_info info) +{ + HILOG_INFO("%{public}s, called start", __func__); + size_t argc = 1; + napi_value argv[1] = {nullptr}; + napi_value thisVar = nullptr; + void *data = nullptr; + napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); + if (status != napi_ok) { + napi_throw_type_error(env, "-1", "get arguments fail."); + return nullptr; + } + HILOG_INFO("%{public}s, called 1", __func__); + if (argc != 1) { + napi_throw_type_error(env, "-1", "Wrong number of arguments, requires 1."); + return nullptr; + } + HILOG_INFO("%{public}s, called 2", __func__); + FileAccessHelper *helper = nullptr; + status = napi_unwrap(env, thisVar, (void **)&helper); + if (status != napi_ok) { + napi_throw_type_error(env, "-1", "get FileAccessHelper argument fail."); + return nullptr; + } + HILOG_INFO("%{public}s, called 3", __func__); + if (helper == nullptr) { + napi_throw_type_error(env, "-1", "FileAccessHelper is nullptr."); + return nullptr; + } + HILOG_INFO("%{public}s, called 4", __func__); + napi_valuetype valueType = napi_undefined; + status = napi_typeof(env, argv[0], &valueType); + if (status != napi_ok) { + napi_throw_type_error(env, "-1", "get callback argument fail."); + return nullptr; + } + HILOG_INFO("%{public}s, called 5", __func__); + if (valueType != napi_function) { + napi_throw_type_error(env, "-1", "callback is not a function."); + return nullptr; + } + HILOG_INFO("%{public}s, called 6", __func__); + std::shared_ptr notify = std::make_shared(env, argv[0]); + if (notify == nullptr) { + napi_throw_type_error(env, "-1", "new NapiFileAccessNotify fail."); + return nullptr; + } + HILOG_INFO("%{public}s, called 7 ----- notify:%{public}p, notify.get():%{public}p", __func__, (void *)¬ify, notify.get()); + if (helper->On(notify) != ERR_OK) { + napi_throw_type_error(env, "-1", "FileAccessHelper::On fail."); + return nullptr; + } + + napi_value result = nullptr; + napi_get_undefined(env, &result); + HILOG_INFO("%{public}s, called end", __func__); + return result; +} + +napi_value NAPI_Off(napi_env env, napi_callback_info info) +{ + HILOG_INFO("%{public}s, called start", __func__); + size_t argc = 1; + napi_value argv[1] = {nullptr}; + napi_value thisVar = nullptr; + void *data = nullptr; + napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); + if (status != napi_ok) { + napi_throw_type_error(env, "-1", "get arguments fail."); + return nullptr; + } + HILOG_INFO("%{public}s, called 1", __func__); + if (argc != 0) { + napi_throw_type_error(env, "-1", "Wrong number of arguments, requires 1."); + return nullptr; + } + HILOG_INFO("%{public}s, called 2", __func__); + FileAccessHelper *helper = nullptr; + status = napi_unwrap(env, thisVar, (void **)&helper); + if (status != napi_ok) { + napi_throw_type_error(env, "-1", "get FileAccessHelper argument fail."); + return nullptr; + } + HILOG_INFO("%{public}s, called 3", __func__); + if (helper == nullptr) { + napi_throw_type_error(env, "-1", "FileAccessHelper is nullptr."); + return nullptr; + } + if (helper->Off() != ERR_OK) { + napi_throw_type_error(env, "-1", "FileAccessHelper::Off fail."); + return nullptr; + } + napi_value result = nullptr; + napi_get_undefined(env, &result); + return result; +} } // namespace AppExecFwk } // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.h b/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.h index 748a528f..38744147 100644 --- a/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.h +++ b/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.h @@ -29,6 +29,8 @@ namespace FileAccessFwk { napi_value NAPI_Rename(napi_env env, napi_callback_info info); napi_value NAPI_ListFile(napi_env env, napi_callback_info info); napi_value NAPI_GetRoots(napi_env env, napi_callback_info info); + napi_value NAPI_On(napi_env env, napi_callback_info info); + napi_value NAPI_Off(napi_env env, napi_callback_info info); } } // namespace FileAccessFwk #endif // FILE_ACCESS_NAPI_H \ No newline at end of file diff --git a/interfaces/kits/napi/file_access_module/napi_fileaccess_notify.cpp b/interfaces/kits/napi/file_access_module/napi_fileaccess_notify.cpp new file mode 100644 index 00000000..1ce782c4 --- /dev/null +++ b/interfaces/kits/napi/file_access_module/napi_fileaccess_notify.cpp @@ -0,0 +1,93 @@ +/* + * 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 "napi_fileaccess_notify.h" + +#include +#include + +namespace OHOS { +namespace FileAccessFwk { +namespace { + const int ARGS_TWO = 2; +} +NapiFileAccessNotify::NapiFileAccessNotify(napi_env env, napi_value callback) + : env_(env) +{ + HILOG_INFO("%{public}s, called start", __func__); + napi_create_reference(env, callback, 1, &callback_); + napi_get_uv_event_loop(env, &loop_); + HILOG_INFO("%{public}s, called end", __func__); +} + +NapiFileAccessNotify::~NapiFileAccessNotify() +{ + HILOG_INFO("%{public}s, called start", __func__); + napi_delete_reference(env_, callback_); + HILOG_INFO("%{public}s, called end", __func__); +} + +void NapiFileAccessNotify::OnNotify(const Uri &uri, const NotifyType notifyType) +{ + HILOG_INFO("%{public}s, called start", __func__); + HILOG_INFO("uri = %{public}s, notifyType = %{public}d", uri.ToString().c_str(), notifyType); + std::shared_ptr work = std::make_shared(); + if (work == nullptr) { + HILOG_ERROR("failed to new uv_work_t"); + return; + } + HILOG_INFO("%{public}s, 1", __func__); + CallbackParam* param = new CallbackParam(this, uri.ToString(), notifyType); + if (param == nullptr) { + HILOG_ERROR("failed to new param"); + return; + } + work->data = std::move(param); + uv_queue_work(loop_, work.get(), + [](uv_work_t *work) {}, + [](uv_work_t *work, int status) { + HILOG_INFO("%{public}s, called start", __func__); + std::shared_ptr param; + param.reset(std::move(reinterpret_cast(work->data))); + + napi_value uri = nullptr; + napi_status ret = napi_create_string_utf8(param->notify_->env_, param->uri_.c_str(), + param->uri_.length(), &uri); + if (ret != napi_ok) { + return; + } + + napi_value notifyType = nullptr; + ret = napi_create_int32(param->notify_->env_, (int32_t)(param->notifyType_), ¬ifyType); + if (ret != napi_ok) { + return; + } + HILOG_INFO("uri = %{public}s, notifyType = %{public}d", param->uri_.c_str(), param->notifyType_); + napi_value callback = nullptr; + napi_value args[ARGS_TWO] = {uri, notifyType}; + napi_get_reference_value(param->notify_->env_, param->notify_->callback_, &callback); + napi_value global = nullptr; + napi_get_global(param->notify_->env_, &global); + napi_value result = nullptr; + ret = napi_call_function(param->notify_->env_, global, callback, ARGS_TWO, args, &result); + if (ret != napi_ok) { + HILOG_ERROR("notify failed status:%{public}d.", ret); + } + HILOG_INFO("%{public}s, called end", __func__); + }); + HILOG_INFO("%{public}s, called end", __func__); +} +} // namespace FileAccessFwk +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/napi/file_access_module/napi_fileaccess_notify.h b/interfaces/kits/napi/file_access_module/napi_fileaccess_notify.h new file mode 100644 index 00000000..0cdd43c9 --- /dev/null +++ b/interfaces/kits/napi/file_access_module/napi_fileaccess_notify.h @@ -0,0 +1,49 @@ +/* + * 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 __NAPI_FILE_ACCESS_NOTIFY_H__ +#define __NAPI_FILE_ACCESS_NOTIFY_H__ + +#include "hilog_wrapper.h" +#include "ifile_access_notify.h" +#include "napi/native_common.h" +#include "napi/native_node_api.h" +#include "napi/native_api.h" +#include "uri.h" + +namespace OHOS { +namespace FileAccessFwk { +class NapiFileAccessNotify : public IFileAccessNotify { +public: + NapiFileAccessNotify(napi_env env, napi_value callback); + virtual ~NapiFileAccessNotify(); + void OnNotify(const Uri &uri, const NotifyType notifyType) override; + + struct CallbackParam { + NapiFileAccessNotify *notify_; + std::string uri_; + NotifyType notifyType_; + CallbackParam(NapiFileAccessNotify *notify, std::string uri, NotifyType notifyType) + : notify_(notify), uri_(uri), notifyType_(notifyType) + {} + }; +private: + napi_ref callback_ = nullptr; + napi_env env_; + uv_loop_s *loop_ = nullptr; +}; +} // FileAccessFwk +} // OHOS +#endif // __NAPI_FILE_ACCESS_NOTIFY_H__ \ No newline at end of file diff --git a/utils/file_access_framework_errno.h b/utils/file_access_framework_errno.h index 6f8bfd17..c25ec286 100644 --- a/utils/file_access_framework_errno.h +++ b/utils/file_access_framework_errno.h @@ -26,12 +26,15 @@ enum { ERR_OK = 0, ERR_ERROR = -1, ERR_IPC_ERROR = BASE_OFFSET, // ipc error + ERR_PARCEL_ERROR, // parcel write/read fail ERR_PERMISSION_DENIED, // no permission ERR_INVALID_FD, // invalid fd ERR_INVALID_URI, // invalid uri ERR_URI_CHECK, // check uri head fail ERR_FILEIO_FAIL, // fileio fail - ERR_INVALID_PARAM // invalid parameter + ERR_INVALID_PARAM, // invalid parameter + ERR_NOTIFY_EXIST, // the notify to register exist + ERR_NOTIFY_NOT_EXIST // the notify to unregister not exist }; } // namespace FileAccessFwk } // namespace OHOS -- Gitee From c150e8cc6b6c6a60aca1f3859755e24989fff5b9 Mon Sep 17 00:00:00 2001 From: linjun9528 Date: Fri, 1 Jul 2022 19:51:32 +0800 Subject: [PATCH 2/3] notify code Signed-off-by: linjun9528 --- .../src/notify/file_access_notify_manager.cpp | 89 ------------------- 1 file changed, 89 deletions(-) diff --git a/frameworks/innerkits/file_access/src/notify/file_access_notify_manager.cpp b/frameworks/innerkits/file_access/src/notify/file_access_notify_manager.cpp index e6824f0f..a57140f9 100644 --- a/frameworks/innerkits/file_access/src/notify/file_access_notify_manager.cpp +++ b/frameworks/innerkits/file_access/src/notify/file_access_notify_manager.cpp @@ -44,13 +44,6 @@ int FileAccessNotifyManager::RegisterNotify(sptr ¬ify) return ERR_NOTIFY_EXIST; } } - // std::vector>::iterator it = std::find(notifyVec.begin(), notifyVec.end(), notify); - // if (it != notifyVec.end()) { - // if ((*it)->AsObject() == notify->AsObject()) { - // HILOG_ERROR("the notify exist. no need to register."); - // return ERR_NOTIFY_EXIST; - // } - // } notifyVec.emplace_back(notify); // AddDeathRecipient(notify); HILOG_INFO("%{public}s, called end notifyVec size = %{public}zu", __func__, notifyVec.size()); @@ -72,18 +65,7 @@ int FileAccessNotifyManager::UnregisterNotify(sptr ¬ify return ERR_OK; } } - // std::vector>::iterator it = std::find(notifyVec.begin(), notifyVec.end(), notify); - // if (it == notifyVec.end()) { - // HILOG_ERROR("the notify not exist."); - // return ERR_NOTIFY_EXIST; - // } - // if ((*it)->AsObject() == notify->AsObject()) { - // notifyVec.erase(it); - // RemoveDeathRecipient(notify); - // HILOG_INFO("%{public}s, called 1", __func__); - // return ERR_OK; - // } HILOG_INFO("%{public}s, called end notifyVec size = %{public}zu", __func__, notifyVec.size()); return ERR_NOTIFY_EXIST; } @@ -198,77 +180,6 @@ void FileAccessNotifyManager::HandleCallBackDiedTask(const sptr & } HILOG_INFO("%{public}s, called 5", __func__); } -#if 0 -bool FileAccessNotifyManager::GetNotifyMap(const NotifyType notifyType, NotifyMapType ¬ifyMap) -{ - switch (notifyType) - { - case NOTIFY_DEVICE_CHANGE: - notifyMap = ¬ifyDeviceMap_; - break; - case NOTIFY_FILE_CHANGE: - notifyMap = ¬ifyFileMap_; - break; - default: - HILOG_ERROR("error notify type."); - return false; - } - return true; -} - -bool FileAccessNotifyManager::GetNotifyListFromMap(const Uri &uri, const NotifyType notifyType, - NotifyListType ¬ifylist) -{ - NotifyMapType targetMap; - if (!GetNotifyMap(notifyType, targetMap)) { - return false; - } - auto it = targetMap.find(uri.ToString()); - if (it == targetMap.end()) { - return false; - } - - notifylist = it->second; - return true; -} -void FileAccessNotifyManager::RemoveNotifyFromMap(const NotifyType notifyType, const sptr ¬ify) -{ - NotifyMapType targetMap; - if (!GetNotifyMap(notifyType, targetMap)) { - HILOG_WARN("the type of notify is not in map."); - return; - } - for (auto ¬ifyCallback : targetMap) { - auto ¬ifyList = notifyCallback.second; - auto targetNotify = std::find(notifyList.begin(), notifyList.end(), notify); - if (targetNotify != notifyList.end()) { - notifyList.remove(notify); - if (notifyList.empty()) { - HILOG_INFO("%{public}s: remove notifyList from map ", __func__); - targetMap.erase(notifyCallback.first); - } - } - } - RemoveDeathRecipient(notify); -} - -bool FileAccessNotifyManager::NotifyExistInMap(const NotifyType notifyType, const sptr ¬ify) -{ - NotifyMapType targetMap; - if (!GetNotifyMap(notifyType, targetMap)) { - HILOG_WARN("the type of notify is not in map."); - return; - } - for (auto ¬ifyCallback : targetMap) { - auto ¬ifyList = notifyCallback.second; - auto targetNotify = std::find(notifyList.begin(), notifyList.end(), notify); - if (targetNotify != notifyList.end()) { - return true; - } - } - return false; -} -#endif } // namespace FileAccessFwk } // namespace OHOS \ No newline at end of file -- Gitee From f60a1ea3d1a58ca1a13c31b504c09ea49cf12b4a Mon Sep 17 00:00:00 2001 From: linjun9528 Date: Fri, 1 Jul 2022 20:03:38 +0800 Subject: [PATCH 3/3] notify code Signed-off-by: linjun9528 --- .../notify/file_access_notify_manager.h | 2 +- .../src/file_access_ext_ability.cpp | 9 ----- .../file_access/src/file_access_ext_proxy.cpp | 14 ++----- .../file_access/src/file_access_ext_stub.cpp | 12 ------ .../src/file_access_ext_stub_impl.cpp | 6 --- .../file_access/src/file_access_helper.cpp | 12 +----- .../src/notify/file_access_notify_client.cpp | 6 --- .../src/notify/file_access_notify_ipc.cpp | 7 ---- .../src/notify/file_access_notify_manager.cpp | 38 +------------------ .../napi_fileaccess_helper.cpp | 13 ------- .../napi_fileaccess_notify.cpp | 13 +------ 11 files changed, 9 insertions(+), 123 deletions(-) diff --git a/frameworks/innerkits/file_access/include/notify/file_access_notify_manager.h b/frameworks/innerkits/file_access/include/notify/file_access_notify_manager.h index 8efb2201..0d208102 100644 --- a/frameworks/innerkits/file_access/include/notify/file_access_notify_manager.h +++ b/frameworks/innerkits/file_access/include/notify/file_access_notify_manager.h @@ -27,7 +27,7 @@ namespace OHOS { namespace FileAccessFwk { using EventHandler = OHOS::AppExecFwk::EventHandler; -// /home/darkknight/huawei/harmony_master/foundation/ability/ability_runtime/services/dataobsmgr/include/dataobs_mgr_inner.h + class FileAccessNotifyManager final : public std::enable_shared_from_this { public: using RecipientMapType = std::map, sptr>; diff --git a/frameworks/innerkits/file_access/src/file_access_ext_ability.cpp b/frameworks/innerkits/file_access/src/file_access_ext_ability.cpp index ce49c3b6..416f3ca7 100644 --- a/frameworks/innerkits/file_access/src/file_access_ext_ability.cpp +++ b/frameworks/innerkits/file_access/src/file_access_ext_ability.cpp @@ -113,49 +113,40 @@ bool FileAccessExtAbility::GetNotifyManager() int FileAccessExtAbility::RegisterNotify(sptr ¬ify) { - HILOG_INFO("%{public}s, called start", __func__); if (!GetNotifyManager()) { HILOG_ERROR("GetNotifyManager fail."); return ERR_ERROR; } - HILOG_INFO("%{public}s, called 1", __func__); int ret = notifyManager_->RegisterNotify(notify); if (ret != ERR_OK) { HILOG_ERROR("NotifyManager register notify fail."); } - HILOG_INFO("%{public}s, called end", __func__); return ret; } int FileAccessExtAbility::UnregisterNotify(sptr ¬ify) { - HILOG_INFO("%{public}s, called start", __func__); if (!GetNotifyManager()) { HILOG_ERROR("GetNotifyManager fail."); return ERR_ERROR; } - HILOG_INFO("%{public}s, called 1", __func__); int ret = notifyManager_->UnregisterNotify(notify); if (ret != ERR_OK) { HILOG_ERROR("NotifyManager unregister notify fail."); } - HILOG_INFO("%{public}s, called end", __func__); return ret; } int FileAccessExtAbility::Notify(const Uri &uri, const NotifyType notifyType) { - HILOG_INFO("%{public}s, called start", __func__); if (!GetNotifyManager()) { HILOG_ERROR("GetNotifyManager fail."); return ERR_ERROR; } - HILOG_INFO("%{public}s, called 1", __func__); int ret = notifyManager_->Notify(uri, notifyType); if (ret != ERR_OK) { HILOG_ERROR("NotifyManager handle notify fail."); } - HILOG_INFO("%{public}s, called end", __func__); return ret; } } // namespace FileAccessFwk diff --git a/frameworks/innerkits/file_access/src/file_access_ext_proxy.cpp b/frameworks/innerkits/file_access/src/file_access_ext_proxy.cpp index 9d296d8e..b3f154dc 100644 --- a/frameworks/innerkits/file_access/src/file_access_ext_proxy.cpp +++ b/frameworks/innerkits/file_access/src/file_access_ext_proxy.cpp @@ -337,43 +337,36 @@ std::vector FileAccessExtProxy::GetRoots() int FileAccessExtProxy::RegisterNotify(sptr ¬ify) { - HILOG_INFO("%{public}s, called start", __func__); MessageParcel data; if (!data.WriteInterfaceToken(FileAccessExtProxy::GetDescriptor())) { - HILOG_ERROR("%{public}s WriteInterfaceToken failed", __func__); + HILOG_ERROR("WriteInterfaceToken failed"); return ERR_PARCEL_ERROR; } - HILOG_INFO("%{public}s, called 1", __func__); if (!data.WriteRemoteObject(notify->AsObject())) { HILOG_ERROR("write subscribe type or parcel failed."); return ERR_PARCEL_ERROR; } - HILOG_INFO("%{public}s, called 2", __func__); MessageParcel reply; MessageOption option; int err = Remote()->SendRequest(CMD_REGISTER_NOTIFY, data, reply, option); if (err != NO_ERROR) { - HILOG_ERROR("%{public}s fail to SendRequest. err: %d", __func__, err); + HILOG_ERROR("fail to SendRequest. err: %d", err); return err; } - HILOG_INFO("%{public}s, called end", __func__); return err; } int FileAccessExtProxy::UnregisterNotify(sptr ¬ify) { - HILOG_INFO("%{public}s, called start", __func__); MessageParcel data; if (!data.WriteInterfaceToken(FileAccessExtProxy::GetDescriptor())) { - HILOG_ERROR("%{public}s WriteInterfaceToken failed", __func__); + HILOG_ERROR("WriteInterfaceToken failed"); return ERR_PARCEL_ERROR; } - HILOG_INFO("%{public}s, called 1", __func__); if (!data.WriteRemoteObject(notify->AsObject())) { HILOG_ERROR("write subscribe type or parcel failed."); return ERR_PARCEL_ERROR; } - HILOG_INFO("%{public}s, called 2", __func__); MessageParcel reply; MessageOption option; int err = Remote()->SendRequest(CMD_UNREGISTER_NOTIFY, data, reply, option); @@ -381,7 +374,6 @@ int FileAccessExtProxy::UnregisterNotify(sptr ¬ify) HILOG_ERROR("%{public}s fail to SendRequest. err: %d", __func__, err); return err; } - HILOG_INFO("%{public}s, called end", __func__); return err; } } // namespace FileAccessFwk diff --git a/frameworks/innerkits/file_access/src/file_access_ext_stub.cpp b/frameworks/innerkits/file_access/src/file_access_ext_stub.cpp index 249343b9..86455856 100644 --- a/frameworks/innerkits/file_access/src/file_access_ext_stub.cpp +++ b/frameworks/innerkits/file_access/src/file_access_ext_stub.cpp @@ -324,17 +324,12 @@ bool FileAccessExtStub::CheckCallingPermission(const std::string &permission) ErrCode FileAccessExtStub::CmdRegisterNotify(MessageParcel &data, MessageParcel &reply) { - HILOG_INFO("%{public}s, called start", __func__); auto remote = data.ReadRemoteObject(); - HILOG_INFO("%{public}s, called 4, remote:%{public}p", __func__, remote.GetRefPtr()); if (remote == nullptr) { - HILOG_INFO("%{public}s, called 1", __func__); return ERR_PARCEL_ERROR; } - HILOG_INFO("%{public}s, called 4", __func__); auto notify = iface_cast(remote); if (notify == nullptr) { - HILOG_INFO("%{public}s, called 3", __func__); return ERR_PARCEL_ERROR; } int ret = RegisterNotify(notify); @@ -342,23 +337,17 @@ ErrCode FileAccessExtStub::CmdRegisterNotify(MessageParcel &data, MessageParcel HILOG_ERROR("WriteInt32 failed"); return ERR_PARCEL_ERROR; } - HILOG_INFO("%{public}s, called end", __func__); return ERR_OK; } ErrCode FileAccessExtStub::CmdUnregisterNotify(MessageParcel &data, MessageParcel &reply) { - HILOG_INFO("%{public}s, called start", __func__); auto remote = data.ReadRemoteObject(); - HILOG_INFO("%{public}s, called 4, remote:%{public}p", __func__, remote.GetRefPtr()); if (remote == nullptr) { - HILOG_INFO("%{public}s, called 1", __func__); return ERR_PARCEL_ERROR; } - HILOG_INFO("%{public}s, called 4", __func__); auto notify = iface_cast(remote); if (notify == nullptr) { - HILOG_INFO("%{public}s, called 3", __func__); return ERR_PARCEL_ERROR; } int ret = UnregisterNotify(notify); @@ -366,7 +355,6 @@ ErrCode FileAccessExtStub::CmdUnregisterNotify(MessageParcel &data, MessageParce HILOG_ERROR("WriteInt32 failed"); return ERR_PARCEL_ERROR; } - HILOG_INFO("%{public}s, called end", __func__); return ERR_OK; } } // namespace FileAccessFwk diff --git a/frameworks/innerkits/file_access/src/file_access_ext_stub_impl.cpp b/frameworks/innerkits/file_access/src/file_access_ext_stub_impl.cpp index d7227206..d565ed35 100644 --- a/frameworks/innerkits/file_access/src/file_access_ext_stub_impl.cpp +++ b/frameworks/innerkits/file_access/src/file_access_ext_stub_impl.cpp @@ -125,35 +125,29 @@ std::vector FileAccessExtStubImpl::GetRoots() int FileAccessExtStubImpl::RegisterNotify(sptr ¬ify) { - HILOG_INFO("%{public}s, called start", __func__); auto extension = GetOwner(); if (extension == nullptr) { HILOG_ERROR("get extension failed."); return ERR_ERROR; } - HILOG_INFO("%{public}s, called 1", __func__); int ret = extension->RegisterNotify(notify); if (ret != ERR_OK) { HILOG_ERROR("RegisterNotify failed."); } - HILOG_INFO("%{public}s, called end", __func__); return ret; } int FileAccessExtStubImpl::UnregisterNotify(sptr ¬ify) { - HILOG_INFO("%{public}s, called start", __func__); auto extension = GetOwner(); if (extension == nullptr) { HILOG_ERROR("get extension failed."); return ERR_ERROR; } - HILOG_INFO("%{public}s, called 1", __func__); int ret = extension->UnregisterNotify(notify); if (ret != ERR_OK) { HILOG_ERROR("UnregisterNotify failed."); } - HILOG_INFO("%{public}s, called end", __func__); return ret; } } // namespace FileAccessFwk diff --git a/frameworks/innerkits/file_access/src/file_access_helper.cpp b/frameworks/innerkits/file_access/src/file_access_helper.cpp index a88a538a..f46c3cf0 100644 --- a/frameworks/innerkits/file_access/src/file_access_helper.cpp +++ b/frameworks/innerkits/file_access/src/file_access_helper.cpp @@ -236,43 +236,35 @@ std::vector FileAccessHelper::GetRoots() int FileAccessHelper::On(std::shared_ptr ¬ify) { - HILOG_INFO("%{public}s, called start", __func__); notifyClient_ = new (std::nothrow) FileAccessNotifyClient(notify); if (notifyClient_ == nullptr) { - HILOG_INFO("%{public}s, 1", __func__); return ERR_NOTIFY_NOT_EXIST; } - HILOG_INFO("%{public}s, 2, notifyClient:%{public}p", __func__, notifyClient_.GetRefPtr()); if (!GetProxy()) { - HILOG_ERROR("%{public}s failed with invalid fileAccessExtProxy_", __func__); + HILOG_ERROR("failed with invalid fileAccessExtProxy_"); return ERR_IPC_ERROR; } - HILOG_INFO("%{public}s, 3", __func__); int ret = fileAccessExtProxy_->RegisterNotify(notifyClient_); if (ret != ERR_OK) { HILOG_ERROR("fileAccessExtProxy_ RegisterNotify fail"); } - HILOG_INFO("%{public}s, called end", __func__); return ret; } int FileAccessHelper::Off() { - HILOG_INFO("%{public}s, called start", __func__); if (notifyClient_ == nullptr) { HILOG_ERROR("not registered notify"); return ERR_NOTIFY_NOT_EXIST; } if (!GetProxy()) { - HILOG_ERROR("%{public}s failed with invalid fileAccessExtProxy_", __func__); + HILOG_ERROR("failed with invalid fileAccessExtProxy_"); return ERR_IPC_ERROR; } - HILOG_INFO("%{public}s, 3", __func__); int ret = fileAccessExtProxy_->UnregisterNotify(notifyClient_); if (ret != ERR_OK) { HILOG_ERROR("fileAccessExtProxy_ unregisterNotify fail"); } - HILOG_INFO("%{public}s, called end", __func__); return ret; } diff --git a/frameworks/innerkits/file_access/src/notify/file_access_notify_client.cpp b/frameworks/innerkits/file_access/src/notify/file_access_notify_client.cpp index 49e768d2..f37ea206 100644 --- a/frameworks/innerkits/file_access/src/notify/file_access_notify_client.cpp +++ b/frameworks/innerkits/file_access/src/notify/file_access_notify_client.cpp @@ -21,23 +21,17 @@ namespace FileAccessFwk { FileAccessNotifyClient::FileAccessNotifyClient(std::shared_ptr ¬ify) : notify_(notify) { - HILOG_INFO("%{public}s, called start", __func__); - HILOG_INFO("%{public}s, called end", __func__); } FileAccessNotifyClient::~FileAccessNotifyClient() { - HILOG_INFO("%{public}s, called start", __func__); - HILOG_INFO("%{public}s, called end", __func__); } void FileAccessNotifyClient::OnNotify(const Uri &uri, const NotifyType notifyType) { - HILOG_INFO("%{public}s, called start", __func__); if (notify_ != nullptr) { notify_->OnNotify(uri, notifyType); } - HILOG_INFO("%{public}s, called end", __func__); } } // namespace FileAccessFwk } // namespace OHOS \ No newline at end of file diff --git a/frameworks/innerkits/file_access/src/notify/file_access_notify_ipc.cpp b/frameworks/innerkits/file_access/src/notify/file_access_notify_ipc.cpp index f78ffbb1..4c61e70d 100644 --- a/frameworks/innerkits/file_access/src/notify/file_access_notify_ipc.cpp +++ b/frameworks/innerkits/file_access/src/notify/file_access_notify_ipc.cpp @@ -23,13 +23,10 @@ namespace OHOS { namespace FileAccessFwk { FileAccessNotifyProxy::FileAccessNotifyProxy(const sptr &impl) : IRemoteProxy(impl) { - HILOG_INFO("%{public}s, called start", __func__); - HILOG_INFO("%{public}s, called end", __func__); } void FileAccessNotifyProxy::OnNotify(const Uri &uri, const NotifyType notifyType) { - HILOG_INFO("%{public}s, called start", __func__); MessageParcel data; MessageParcel reply; MessageOption option; @@ -37,22 +34,18 @@ void FileAccessNotifyProxy::OnNotify(const Uri &uri, const NotifyType notifyType HILOG_ERROR("write descriptor failed"); return; } - HILOG_INFO("%{public}s, called 1", __func__); if (!data.WriteParcelable(&uri)) { HILOG_ERROR("write parcel uri failed"); return; } - HILOG_INFO("%{public}s, called 2", __func__); if (!data.WriteInt32((int32_t)notifyType)) { HILOG_ERROR("write parcel notifyType failed"); return; } - HILOG_INFO("%{public}s, called 3", __func__); int error = Remote()->SendRequest(CMD_ON_NOTIFY, data, reply, option); if (error != 0) { HILOG_ERROR("SendRequest failed, error %d", error); } - HILOG_INFO("%{public}s, called end", __func__); } int32_t FileAccessNotifyStub::OnRemoteRequest(uint32_t code, MessageParcel &data, diff --git a/frameworks/innerkits/file_access/src/notify/file_access_notify_manager.cpp b/frameworks/innerkits/file_access/src/notify/file_access_notify_manager.cpp index a57140f9..41423987 100644 --- a/frameworks/innerkits/file_access/src/notify/file_access_notify_manager.cpp +++ b/frameworks/innerkits/file_access/src/notify/file_access_notify_manager.cpp @@ -34,9 +34,6 @@ FileAccessNotifyManager::~FileAccessNotifyManager() int FileAccessNotifyManager::RegisterNotify(sptr ¬ify) { - HILOG_INFO("%{public}s, called start", __func__); - HILOG_INFO("%{public}s, notify:%{public}p", __func__, notify.GetRefPtr()); - HILOG_INFO("%{public}s, notifyVec.size():%{public}zu", __func__, notifyVec.size()); std::lock_guard lock(notifyVecMutex_); for (auto notify : notifyVec) { if (notify->AsObject() == notify->AsObject()) { @@ -46,139 +43,106 @@ int FileAccessNotifyManager::RegisterNotify(sptr ¬ify) } notifyVec.emplace_back(notify); // AddDeathRecipient(notify); - HILOG_INFO("%{public}s, called end notifyVec size = %{public}zu", __func__, notifyVec.size()); return ERR_OK; } int FileAccessNotifyManager::UnregisterNotify(sptr ¬ify) { - HILOG_INFO("%{public}s, called start", __func__); - HILOG_INFO("%{public}s, notify:%{public}p", __func__, notify.GetRefPtr()); - HILOG_INFO("%{public}s, notifyVec.size():%{public}zu", __func__, notifyVec.size()); std::lock_guard lock(notifyVecMutex_); std::vector>::iterator it = notifyVec.begin(); for (; it != notifyVec.end(); it++) { if ((*it)->AsObject() == notify->AsObject()) { notifyVec.erase(it); // RemoveDeathRecipient(notify); - HILOG_INFO("%{public}s, called 1", __func__); return ERR_OK; } } - HILOG_INFO("%{public}s, called end notifyVec size = %{public}zu", __func__, notifyVec.size()); + return ERR_NOTIFY_EXIST; } int FileAccessNotifyManager::Notify(const Uri uri, const NotifyType notifyType) { - HILOG_INFO("%{public}s, called start", __func__); - HILOG_INFO("%{public}s, notifyVec.size():%{public}zu", __func__, notifyVec.size()); std::lock_guard lock(notifyVecMutex_); for (auto notify : notifyVec) { - HILOG_INFO("%{public}s, called 1", __func__); if (notify != nullptr) { - HILOG_INFO("%{public}s, called 2", __func__); notify->OnNotify(uri, notifyType); } } - HILOG_INFO("%{public}s, called end", __func__); return ERR_OK; } void FileAccessNotifyManager::AddDeathRecipient(const sptr ¬ify) { - HILOG_INFO("%{public}s, called start", __func__); if ((notify == nullptr) || notify->AsObject() == nullptr) { HILOG_ERROR("the death recipient obj is nullptr."); return; } - HILOG_INFO("%{public}s, called 1", __func__); auto it = recipientMap_.find(notify->AsObject()); if (it != recipientMap_.end()) { HILOG_ERROR("the death recipient obj has been added."); return; } - HILOG_INFO("%{public}s, called 2", __func__); std::weak_ptr thisWeakPtr(shared_from_this()); - HILOG_INFO("%{public}s, called 2.0", __func__); sptr deathRecipient = new FileAccessNotifyCallbackRecipient([thisWeakPtr](const wptr &remote) { - HILOG_INFO("%{public}s, called 2.1", __func__); auto fileAccessNotifyManager = thisWeakPtr.lock(); if (fileAccessNotifyManager) { fileAccessNotifyManager->OnCallBackDied(remote); } - HILOG_INFO("%{public}s, called 2.2", __func__); }); - HILOG_INFO("%{public}s, called 3", __func__); notify->AsObject()->AddDeathRecipient(deathRecipient); - HILOG_INFO("%{public}s, called 4", __func__); recipientMap_.emplace(notify->AsObject(), deathRecipient); - HILOG_INFO("%{public}s, called end", __func__); } void FileAccessNotifyManager::RemoveDeathRecipient(const sptr ¬ify) { - HILOG_INFO("%{public}s, called start", __func__); if ((notify == nullptr) || notify->AsObject() == nullptr) { - HILOG_INFO("%{public}s, called 1", __func__); return; } - HILOG_INFO("%{public}s, called 2", __func__); auto it = recipientMap_.find(notify->AsObject()); if (it != recipientMap_.end()) { it->first->RemoveDeathRecipient(it->second); recipientMap_.erase(it); } - HILOG_INFO("%{public}s, called end", __func__); } void FileAccessNotifyManager::OnCallBackDied(const wptr &remote) { - HILOG_INFO("%{public}s, called start", __func__); auto object = remote.promote(); if (object == nullptr) { - HILOG_INFO("%{public}s, called 1", __func__); return; } - HILOG_INFO("%{public}s, called 2", __func__); if (handler_) { auto task = [object, fileAccessNotifyManager = shared_from_this()]() { fileAccessNotifyManager->HandleCallBackDiedTask(object); }; handler_->PostTask(task); } - HILOG_INFO("%{public}s, called end", __func__); } void FileAccessNotifyManager::HandleCallBackDiedTask(const sptr &remote) { - HILOG_INFO("%{public}s, called start", __func__); std::lock_guard lock_l(notifyVecMutex_); if (remote == nullptr) { return; } - HILOG_INFO("%{public}s, called 1", __func__); sptr notify = iface_cast(remote); if (notify == nullptr) { - HILOG_INFO("%{public}s, called 2", __func__); return; } - HILOG_INFO("%{public}s, called 3", __func__); auto recipientIter = recipientMap_.find(notify->AsObject()); if (recipientIter != recipientMap_.end()) { recipientIter->first->RemoveDeathRecipient(recipientIter->second); recipientMap_.erase(recipientIter); } - HILOG_INFO("%{public}s, called 4", __func__); auto it = std::find(notifyVec.begin(), notifyVec.end(), notify); if (it != notifyVec.end()) { if ((*it)->AsObject() == notify->AsObject()) { notifyVec.erase(it); } } - HILOG_INFO("%{public}s, called 5", __func__); } } // namespace FileAccessFwk diff --git a/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.cpp b/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.cpp index fc0c09e0..027be3f6 100644 --- a/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.cpp +++ b/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.cpp @@ -1707,7 +1707,6 @@ napi_value NAPI_GetRoots(napi_env env, napi_callback_info info) napi_value NAPI_On(napi_env env, napi_callback_info info) { - HILOG_INFO("%{public}s, called start", __func__); size_t argc = 1; napi_value argv[1] = {nullptr}; napi_value thisVar = nullptr; @@ -1717,42 +1716,35 @@ napi_value NAPI_On(napi_env env, napi_callback_info info) napi_throw_type_error(env, "-1", "get arguments fail."); return nullptr; } - HILOG_INFO("%{public}s, called 1", __func__); if (argc != 1) { napi_throw_type_error(env, "-1", "Wrong number of arguments, requires 1."); return nullptr; } - HILOG_INFO("%{public}s, called 2", __func__); FileAccessHelper *helper = nullptr; status = napi_unwrap(env, thisVar, (void **)&helper); if (status != napi_ok) { napi_throw_type_error(env, "-1", "get FileAccessHelper argument fail."); return nullptr; } - HILOG_INFO("%{public}s, called 3", __func__); if (helper == nullptr) { napi_throw_type_error(env, "-1", "FileAccessHelper is nullptr."); return nullptr; } - HILOG_INFO("%{public}s, called 4", __func__); napi_valuetype valueType = napi_undefined; status = napi_typeof(env, argv[0], &valueType); if (status != napi_ok) { napi_throw_type_error(env, "-1", "get callback argument fail."); return nullptr; } - HILOG_INFO("%{public}s, called 5", __func__); if (valueType != napi_function) { napi_throw_type_error(env, "-1", "callback is not a function."); return nullptr; } - HILOG_INFO("%{public}s, called 6", __func__); std::shared_ptr notify = std::make_shared(env, argv[0]); if (notify == nullptr) { napi_throw_type_error(env, "-1", "new NapiFileAccessNotify fail."); return nullptr; } - HILOG_INFO("%{public}s, called 7 ----- notify:%{public}p, notify.get():%{public}p", __func__, (void *)¬ify, notify.get()); if (helper->On(notify) != ERR_OK) { napi_throw_type_error(env, "-1", "FileAccessHelper::On fail."); return nullptr; @@ -1760,13 +1752,11 @@ napi_value NAPI_On(napi_env env, napi_callback_info info) napi_value result = nullptr; napi_get_undefined(env, &result); - HILOG_INFO("%{public}s, called end", __func__); return result; } napi_value NAPI_Off(napi_env env, napi_callback_info info) { - HILOG_INFO("%{public}s, called start", __func__); size_t argc = 1; napi_value argv[1] = {nullptr}; napi_value thisVar = nullptr; @@ -1776,19 +1766,16 @@ napi_value NAPI_Off(napi_env env, napi_callback_info info) napi_throw_type_error(env, "-1", "get arguments fail."); return nullptr; } - HILOG_INFO("%{public}s, called 1", __func__); if (argc != 0) { napi_throw_type_error(env, "-1", "Wrong number of arguments, requires 1."); return nullptr; } - HILOG_INFO("%{public}s, called 2", __func__); FileAccessHelper *helper = nullptr; status = napi_unwrap(env, thisVar, (void **)&helper); if (status != napi_ok) { napi_throw_type_error(env, "-1", "get FileAccessHelper argument fail."); return nullptr; } - HILOG_INFO("%{public}s, called 3", __func__); if (helper == nullptr) { napi_throw_type_error(env, "-1", "FileAccessHelper is nullptr."); return nullptr; diff --git a/interfaces/kits/napi/file_access_module/napi_fileaccess_notify.cpp b/interfaces/kits/napi/file_access_module/napi_fileaccess_notify.cpp index 1ce782c4..affe8365 100644 --- a/interfaces/kits/napi/file_access_module/napi_fileaccess_notify.cpp +++ b/interfaces/kits/napi/file_access_module/napi_fileaccess_notify.cpp @@ -26,29 +26,23 @@ namespace { NapiFileAccessNotify::NapiFileAccessNotify(napi_env env, napi_value callback) : env_(env) { - HILOG_INFO("%{public}s, called start", __func__); napi_create_reference(env, callback, 1, &callback_); napi_get_uv_event_loop(env, &loop_); - HILOG_INFO("%{public}s, called end", __func__); } NapiFileAccessNotify::~NapiFileAccessNotify() { - HILOG_INFO("%{public}s, called start", __func__); napi_delete_reference(env_, callback_); - HILOG_INFO("%{public}s, called end", __func__); } void NapiFileAccessNotify::OnNotify(const Uri &uri, const NotifyType notifyType) { - HILOG_INFO("%{public}s, called start", __func__); - HILOG_INFO("uri = %{public}s, notifyType = %{public}d", uri.ToString().c_str(), notifyType); + std::shared_ptr work = std::make_shared(); if (work == nullptr) { HILOG_ERROR("failed to new uv_work_t"); return; } - HILOG_INFO("%{public}s, 1", __func__); CallbackParam* param = new CallbackParam(this, uri.ToString(), notifyType); if (param == nullptr) { HILOG_ERROR("failed to new param"); @@ -58,7 +52,6 @@ void NapiFileAccessNotify::OnNotify(const Uri &uri, const NotifyType notifyType) uv_queue_work(loop_, work.get(), [](uv_work_t *work) {}, [](uv_work_t *work, int status) { - HILOG_INFO("%{public}s, called start", __func__); std::shared_ptr param; param.reset(std::move(reinterpret_cast(work->data))); @@ -74,7 +67,6 @@ void NapiFileAccessNotify::OnNotify(const Uri &uri, const NotifyType notifyType) if (ret != napi_ok) { return; } - HILOG_INFO("uri = %{public}s, notifyType = %{public}d", param->uri_.c_str(), param->notifyType_); napi_value callback = nullptr; napi_value args[ARGS_TWO] = {uri, notifyType}; napi_get_reference_value(param->notify_->env_, param->notify_->callback_, &callback); @@ -85,9 +77,8 @@ void NapiFileAccessNotify::OnNotify(const Uri &uri, const NotifyType notifyType) if (ret != napi_ok) { HILOG_ERROR("notify failed status:%{public}d.", ret); } - HILOG_INFO("%{public}s, called end", __func__); }); - HILOG_INFO("%{public}s, called end", __func__); + } } // namespace FileAccessFwk } // namespace OHOS \ No newline at end of file -- Gitee