diff --git a/frameworks/innerkits/file_access/BUILD.gn b/frameworks/innerkits/file_access/BUILD.gn index 1fd5425143ddaab43f594b741844a743e974514f..eb3bc52b8af79bd3f8cbb15400735baf8ef0adcf 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", @@ -58,6 +59,10 @@ ohos_shared_library("file_access_extension_ability_kit") { "src/file_access_helper.cpp", "src/js_file_access_ext_ability.cpp", "src/napi_common_fileaccess.cpp", + "src/notify/file_access_notify_client.cpp", + "src/notify/file_access_notify_manager.cpp", + "src/notify/file_access_notify_proxy.cpp", + "src/notify/file_access_notify_stub.cpp", ] configs = [ ":ability_config" ] public_configs = [ @@ -79,6 +84,7 @@ ohos_shared_library("file_access_extension_ability_kit") { "ability_runtime:runtime", "ability_runtime:wantagent_innerkits", "access_token:libaccesstoken_sdk", + "eventhandler:libeventhandler", "hitrace_native:hitrace_meter", "ipc:ipc_core", "ipc_js:rpc", @@ -109,6 +115,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 7689afb012fc2e765a6746b0093e6616db4b2827..2a80937dd831dba9f7d9de50eb99fc48428b3461 100644 --- a/frameworks/innerkits/file_access/include/file_access_ext_ability.h +++ b/frameworks/innerkits/file_access/include/file_access_ext_ability.h @@ -13,11 +13,12 @@ * limitations under the License. */ -#ifndef FILE_EXT_ABILITY_H -#define FILE_EXT_ABILITY_H +#ifndef __FILE_ACCESS_EXT_ABILITY_H__ +#define __FILE_ACCESS_EXT_ABILITY_H__ #include "extension_base.h" #include "file_access_extension_info.h" +#include "file_access_notify_manager.h" namespace OHOS { namespace AbilityRuntime { @@ -48,10 +49,15 @@ public: virtual std::vector ListFile(const Uri &sourceFile); virtual std::vector GetRoots(); + virtual int RegisterNotify(sptr ¬ify); + virtual int UnregisterNotify(sptr ¬ify); + virtual int Notify(const Uri &uri, const NotifyType notifyType); static void SetCreator(const CreatorFunc& creator); private: + bool GetNotifyManager(); static CreatorFunc creator_; + std::unique_ptr notifyManager_; }; } // namespace FileAccessFwk } // namespace OHOS -#endif // FILE_EXT_ABILITY_H \ No newline at end of file +#endif // __FILE_ACCESS_EXT_ABILITY_H__ \ No newline at end of file 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 d4918e22c9c5107d9f5e83953be78387cad94773..927e8967c7fe6f08bfafab3dceabf382d431cd51 100644 --- a/frameworks/innerkits/file_access/include/file_access_ext_proxy.h +++ b/frameworks/innerkits/file_access/include/file_access_ext_proxy.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef FILE_EXT_PROXY_H -#define FILE_EXT_PROXY_H +#ifndef __FILE_ACCESS_EXT_PROXY_H__ +#define __FILE_ACCESS_EXT_PROXY_H__ #include @@ -38,9 +38,11 @@ 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_; }; } // namespace FileAccessFwk } // namespace OHOS -#endif // FILE_EXT_PROXY_H +#endif // __FILE_ACCESS_EXT_PROXY_H__ 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 73c1552a19cc3d038137adf15fd9db787d7a3ca2..fe431f327839427cb31ba8c1ef23d916b314b6fa 100644 --- a/frameworks/innerkits/file_access/include/file_access_ext_stub.h +++ b/frameworks/innerkits/file_access/include/file_access_ext_stub.h @@ -39,6 +39,8 @@ private: 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 1cd130ba5c0ec93b4b4003c6dbf30038c3060341..46d65d153edbcdfa6f1449dd82f08ae749f9ca50 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 be151480047119289242ca56f6042df89e7c62ab..9e19e07ea4dbe73f6f199c630af0995149b1befc 100644 --- a/frameworks/innerkits/file_access/include/file_access_helper.h +++ b/frameworks/innerkits/file_access/include/file_access_helper.h @@ -16,14 +16,17 @@ #ifndef FILE_ACCESS_HELPER_H #define FILE_ACCESS_HELPER_H +#include #include #include #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 "inotify_callback.h" #include "uri.h" #include "want.h" @@ -50,7 +53,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 &callback); + int Off(); private: FileAccessHelper(const std::shared_ptr &context, const AAFwk::Want &want, const sptr &fileAccessExtProxy); @@ -58,13 +62,13 @@ private: const AAFwk::Want &want, const sptr &fileAccessExtProxy); void AddFileAccessDeathRecipient(const sptr &token); void OnSchedulerDied(const wptr &remote); - sptr token_ = {}; AAFwk::Want want_ = {}; sptr fileAccessExtProxy_ = nullptr; bool isSystemCaller_ = false; sptr callerDeathRecipient_ = nullptr; sptr fileAccessExtConnection_ = nullptr; + sptr notifyClient_ = nullptr; }; 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 fdc837b8ce8d9612634cd5570e0555cb1e7845a8..e9db9f4f92146c06173509d21355738c3a0dd194 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.h" #include "uri.h" namespace OHOS { @@ -38,7 +40,9 @@ public: CMD_MOVE, CMD_RENAME, CMD_LIST_FILE, - CMD_GET_ROOTS + CMD_GET_ROOTS, + CMD_REGISTER_NOTIFY, + CMD_UNREGISTER_NOTIFY, }; virtual int OpenFile(const Uri &uri, int flags) = 0; @@ -47,9 +51,10 @@ public: virtual int Delete(const Uri &sourceFile) = 0; virtual int Move(const Uri &sourceFile, const Uri &targetParent, Uri &newFile) = 0; virtual int Rename(const Uri &sourceFile, const std::string &displayName, Uri &newFile) = 0; - 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/js_file_access_ext_ability.h b/frameworks/innerkits/file_access/include/js_file_access_ext_ability.h index 49d26194d18b5db388475caaae0850f78016741e..cef2bdab055ff2903551de86c66ad231a6487910 100644 --- a/frameworks/innerkits/file_access/include/js_file_access_ext_ability.h +++ b/frameworks/innerkits/file_access/include/js_file_access_ext_ability.h @@ -67,6 +67,7 @@ public: private: NativeValue* AsnycCallObjectMethod(const char *name, NativeValue * const *argv = nullptr, size_t argc = 0); NativeValue* CallObjectMethod(const char *name, NativeValue * const *argv = nullptr, size_t argc = 0); + static NativeValue* FuncCallback(NativeEngine* engine, NativeCallbackInfo* info); void GetSrcPath(std::string &srcPath); JsRuntime &jsRuntime_; 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 0000000000000000000000000000000000000000..e01940b68b38c38efb0b946c1da3048e6796489f --- /dev/null +++ b/frameworks/innerkits/file_access/include/notify/file_access_notify_client.h @@ -0,0 +1,38 @@ +/* + * 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 "file_access_notify_stub.h" +#include "inotify_callback.h" +#include "iremote_object.h" + +namespace OHOS { +namespace FileAccessFwk { +class FileAccessNotifyClient final : public FileAccessNotifyStub { +public: + FileAccessNotifyClient(std::shared_ptr ¬ifyCallback); + ~FileAccessNotifyClient(); + void OnNotify(const Uri &uri, const NotifyType notifyType) override; + +private: + std::shared_ptr notifyCallback_; +}; +} // 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_common.h b/frameworks/innerkits/file_access/include/notify/file_access_notify_common.h new file mode 100644 index 0000000000000000000000000000000000000000..ab6aa3354fbf4a8975394bb195a22fc00f3859c8 --- /dev/null +++ b/frameworks/innerkits/file_access/include/notify/file_access_notify_common.h @@ -0,0 +1,30 @@ +/* + * 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_COMMON_H__ +#define __FILE_ACCESS_NOTIFY_COMMON_H__ + +namespace OHOS { +namespace FileAccessFwk { +enum NotifyType { + NOTIFY_DEVICE_ONLINE = 1, + NOTIFY_DEVICE_OFFLINE, + NOTIFY_FILE_CREATE, + NOTIFY_FILE_DELETE, + NOTIFY_FILE_MODIFY +}; +} // namespace FileAccessFwk +} // namespace OHOS +#endif // __FILE_ACCESS_NOTIFY_COMMON_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 0000000000000000000000000000000000000000..6669c7336a4e044793e1380088ca7e9d109d9971 --- /dev/null +++ b/frameworks/innerkits/file_access/include/notify/file_access_notify_manager.h @@ -0,0 +1,61 @@ +/* + * 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.h" +#include "iremote_object.h" + +namespace OHOS { +namespace FileAccessFwk { +using EventHandler = OHOS::AppExecFwk::EventHandler; +class FileAccessNotifyManager final : public std::enable_shared_from_this { +public: + using NotifyMapType = std::map, sptr>; + + FileAccessNotifyManager(); + ~FileAccessNotifyManager(); + + int RegisterNotify(sptr ¬ify); + int UnregisterNotify(sptr ¬ify); + int Notify(const Uri uri, const NotifyType notifyType); + +private: + void OnCallBackDied(const wptr &remote); + bool AddNotifyToMap(const sptr ¬ify); + bool RemoveNotifyFromMap(const sptr &remote); + NotifyMapType notifyMap_; + static std::mutex notifyMapMutex_; +}; + +class FileAccessNotifyCallbackRecipient : public IRemoteObject::DeathRecipient { +public: + using RemoteDiedHandler = std::function &)>; + FileAccessNotifyCallbackRecipient(RemoteDiedHandler handler); + virtual ~FileAccessNotifyCallbackRecipient(); + virtual void OnRemoteDied(const wptr &remote); + +private: + RemoteDiedHandler handler_; +}; +} // 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/file_access_notify_proxy.h b/frameworks/innerkits/file_access/include/notify/file_access_notify_proxy.h new file mode 100644 index 0000000000000000000000000000000000000000..092064e4b8f51072c1b763f8dc0810fd95c7bef8 --- /dev/null +++ b/frameworks/innerkits/file_access/include/notify/file_access_notify_proxy.h @@ -0,0 +1,36 @@ +/* + * 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_PROXY_H__ +#define __FILE_ACCESS_NOTIFY_PROXY_H__ + +#include "ifile_access_notify.h" +#include "iremote_broker.h" +#include "iremote_proxy.h" +#include "uri.h" + +namespace OHOS { +namespace FileAccessFwk { +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 // __FILE_ACCESS_NOTIFY_PROXY_H__ \ No newline at end of file diff --git a/frameworks/innerkits/file_access/include/notify/file_access_notify_stub.h b/frameworks/innerkits/file_access/include/notify/file_access_notify_stub.h new file mode 100644 index 0000000000000000000000000000000000000000..3e466f8cf67430a72b1613af6a92d5014bf65b09 --- /dev/null +++ b/frameworks/innerkits/file_access/include/notify/file_access_notify_stub.h @@ -0,0 +1,32 @@ +/* + * 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_STUB_H__ +#define __FILE_ACCESS_NOTIFY_STUB_H__ + +#include "ifile_access_notify.h" +#include "iremote_stub.h" +#include "uri.h" + +namespace OHOS { +namespace FileAccessFwk { +class FileAccessNotifyStub : public IRemoteStub { +public: + virtual int OnRemoteRequest(uint32_t code, MessageParcel &data, + MessageParcel &reply, MessageOption &option) override; +}; +} // namespace FileAccessFwk +} // namespace OHOS +#endif // __FILE_ACCESS_NOTIFY_STUB_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 0000000000000000000000000000000000000000..18fdaaea894a113256a97c48d4867a246ee40141 --- /dev/null +++ b/frameworks/innerkits/file_access/include/notify/ifile_access_notify.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 __I_FILE_ACCESS_NOTIFY_H__ +#define __I_FILE_ACCESS_NOTIFY_H__ + +#include "file_access_notify_common.h" +#include "iremote_broker.h" +#include "uri.h" + +namespace OHOS { +namespace FileAccessFwk { +class IFileAccessNotify : public IRemoteBroker { +public: + DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.FileAccessFwk.IFileAccessNotify"); + + enum { + CMD_ON_NOTIFY = 1, + }; + + 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/inotify_callback.h b/frameworks/innerkits/file_access/include/notify/inotify_callback.h new file mode 100644 index 0000000000000000000000000000000000000000..e9e0afcc989139678c07edd83e4052d0c99abc8d --- /dev/null +++ b/frameworks/innerkits/file_access/include/notify/inotify_callback.h @@ -0,0 +1,30 @@ +/* + * 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_NOTIFY_CALLBACK_H__ +#define __I_NOTIFY_CALLBACK_H__ + +#include "file_access_notify_common.h" +#include "uri.h" + +namespace OHOS { +namespace FileAccessFwk { +class INotifyCallback { +public: + virtual void OnNotify(const Uri &uri, const NotifyType notifyType) = 0; +}; +} // namespace FileAccessFwk +} // namespace OHOS +#endif // __I_NOTIFY_CALLBACK_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 a570e63dfeb44e19b89cac3d42911277400e88fe..5b42a91ae5a08ae289f30f760bc2d592cf874170 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,55 @@ 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) +{ + if (!GetNotifyManager()) { + HILOG_ERROR("GetNotifyManager fail."); + return ERR_ERROR; + } + int ret = notifyManager_->RegisterNotify(notify); + if (ret != ERR_OK) { + HILOG_ERROR("NotifyManager register notify fail."); + } + return ret; +} + +int FileAccessExtAbility::UnregisterNotify(sptr ¬ify) +{ + if (!GetNotifyManager()) { + HILOG_ERROR("GetNotifyManager fail."); + return ERR_ERROR; + } + int ret = notifyManager_->UnregisterNotify(notify); + if (ret != ERR_OK) { + HILOG_ERROR("NotifyManager unregister notify fail."); + } + return ret; +} + +int FileAccessExtAbility::Notify(const Uri &uri, const NotifyType notifyType) +{ + if (!GetNotifyManager()) { + HILOG_ERROR("GetNotifyManager fail."); + return ERR_ERROR; + } + int ret = notifyManager_->Notify(uri, notifyType); + if (ret != ERR_OK) { + HILOG_ERROR("NotifyManager handle notify fail."); + } + 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 46a754421ce84381009fb7e909aa78647715ecc2..61c93fb75dda87c6ab77b95e5245929863ba0d09 100644 --- a/frameworks/innerkits/file_access/src/file_access_ext_proxy.cpp +++ b/frameworks/innerkits/file_access/src/file_access_ext_proxy.cpp @@ -395,5 +395,57 @@ std::vector FileAccessExtProxy::GetRoots() FinishTrace(HITRACE_TAG_FILEMANAGEMENT); return vec; } + +int FileAccessExtProxy::RegisterNotify(sptr ¬ify) +{ + MessageParcel data; + if (!data.WriteInterfaceToken(FileAccessExtProxy::GetDescriptor())) { + HILOG_ERROR("WriteInterfaceToken failed"); + return ERR_PARCEL_ERROR; + } + if (!data.WriteRemoteObject(notify->AsObject())) { + HILOG_ERROR("write subscribe type or parcel failed."); + return ERR_PARCEL_ERROR; + } + MessageParcel reply; + MessageOption option; + int err = Remote()->SendRequest(CMD_REGISTER_NOTIFY, data, reply, option); + if (err != ERR_OK) { + HILOG_ERROR("fail to SendRequest. err: %{public}d", err); + return err; + } + err = reply.ReadInt32(); + if (err != ERR_OK) { + HILOG_ERROR("fail to RegisterNotify. err: %{public}d", err); + return err; + } + return err; +} + +int FileAccessExtProxy::UnregisterNotify(sptr ¬ify) +{ + MessageParcel data; + if (!data.WriteInterfaceToken(FileAccessExtProxy::GetDescriptor())) { + HILOG_ERROR("WriteInterfaceToken failed"); + return ERR_PARCEL_ERROR; + } + if (!data.WriteRemoteObject(notify->AsObject())) { + HILOG_ERROR("write subscribe type or parcel failed."); + return ERR_PARCEL_ERROR; + } + MessageParcel reply; + MessageOption option; + int err = Remote()->SendRequest(CMD_UNREGISTER_NOTIFY, data, reply, option); + if (err != ERR_OK) { + HILOG_ERROR("fail to SendRequest. err: %{public}d", err); + return err; + } + err = reply.ReadInt32(); + if (err != ERR_OK) { + HILOG_ERROR("fail to UnregisterNotify. err: %{public}d", err); + return err; + } + 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 ddc2ee80dbd9a4f2cda1fe3aacc2db89dac76b80..a7c1450615ed7158d0e969915aecf3a20b46d831 100644 --- a/frameworks/innerkits/file_access/src/file_access_ext_stub.cpp +++ b/frameworks/innerkits/file_access/src/file_access_ext_stub.cpp @@ -36,6 +36,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() @@ -386,5 +388,45 @@ bool FileAccessExtStub::CheckCallingPermission(const std::string &permission) FinishTrace(HITRACE_TAG_FILEMANAGEMENT); return true; } + +ErrCode FileAccessExtStub::CmdRegisterNotify(MessageParcel &data, MessageParcel &reply) +{ + auto remote = data.ReadRemoteObject(); + if (remote == nullptr) { + HILOG_INFO("get remote obj fail."); + return ERR_PARCEL_ERROR; + } + auto notify = iface_cast(remote); + if (notify == nullptr) { + HILOG_INFO("get notify fail"); + return ERR_PARCEL_ERROR; + } + int ret = RegisterNotify(notify); + if (!reply.WriteInt32(ret)) { + HILOG_ERROR("WriteInt32 failed"); + return ERR_PARCEL_ERROR; + } + return ERR_OK; +} + +ErrCode FileAccessExtStub::CmdUnregisterNotify(MessageParcel &data, MessageParcel &reply) +{ + auto remote = data.ReadRemoteObject(); + if (remote == nullptr) { + HILOG_INFO("get remote obj fail."); + return ERR_PARCEL_ERROR; + } + auto notify = iface_cast(remote); + if (notify == nullptr) { + HILOG_INFO("get notify fail"); + return ERR_PARCEL_ERROR; + } + int ret = UnregisterNotify(notify); + if (!reply.WriteInt32(ret)) { + HILOG_ERROR("WriteInt32 failed"); + return ERR_PARCEL_ERROR; + } + 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 26fd26e2976157ed40236435964b6e91bd3c8ec8..834e24e13922bbb49712a1921c3f85064791d52b 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 @@ -153,5 +153,33 @@ std::vector FileAccessExtStubImpl::GetRoots() FinishTrace(HITRACE_TAG_FILEMANAGEMENT); return vec; } + +int FileAccessExtStubImpl::RegisterNotify(sptr ¬ify) +{ + auto extension = GetOwner(); + if (extension == nullptr) { + HILOG_ERROR("get extension failed."); + return ERR_ERROR; + } + int ret = extension->RegisterNotify(notify); + if (ret != ERR_OK) { + HILOG_ERROR("RegisterNotify failed."); + } + return ret; +} + +int FileAccessExtStubImpl::UnregisterNotify(sptr ¬ify) +{ + auto extension = GetOwner(); + if (extension == nullptr) { + HILOG_ERROR("get extension failed."); + return ERR_ERROR; + } + int ret = extension->UnregisterNotify(notify); + if (ret != ERR_OK) { + HILOG_ERROR("UnregisterNotify failed."); + } + 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 9a3a497a74c6272f60f5fba99355966be7e97a64..8e4e891b372922eec62cebea537a7bb59d608aea 100644 --- a/frameworks/innerkits/file_access/src/file_access_helper.cpp +++ b/frameworks/innerkits/file_access/src/file_access_helper.cpp @@ -272,6 +272,50 @@ std::vector FileAccessHelper::GetRoots() return results; } +int FileAccessHelper::On(std::shared_ptr &callback) +{ + if (notifyClient_ != nullptr) { + HILOG_INFO("notifyClient registered yet."); + int ret = fileAccessExtProxy_->UnregisterNotify(notifyClient_); + if (ret != ERR_OK) { + HILOG_ERROR("fileAccessExtProxy_ unregisterNotify fail"); + } + notifyClient_.clear(); + } + notifyClient_ = new (std::nothrow) FileAccessNotifyClient(callback); + if (notifyClient_ == nullptr) { + HILOG_INFO("new FileAccessNotifyClient fail"); + return ERR_ERROR; + } + if (!GetProxy()) { + HILOG_ERROR("failed with invalid fileAccessExtProxy_"); + return ERR_IPC_ERROR; + } + int ret = fileAccessExtProxy_->RegisterNotify(notifyClient_); + if (ret != ERR_OK) { + HILOG_ERROR("fileAccessExtProxy_ RegisterNotify fail"); + } + return ret; +} + +int FileAccessHelper::Off() +{ + if (notifyClient_ == nullptr) { + HILOG_ERROR("not registered notify"); + return ERR_NOTIFY_NOT_EXIST; + } + if (!GetProxy()) { + HILOG_ERROR("failed with invalid fileAccessExtProxy_"); + return ERR_IPC_ERROR; + } + int ret = fileAccessExtProxy_->UnregisterNotify(notifyClient_); + if (ret != ERR_OK) { + HILOG_ERROR("fileAccessExtProxy_ unregisterNotify fail"); + } + notifyClient_.clear(); + 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 30764f8d05040b5c9eec9b73f67c9335fc126a7d..4d4802e32588c39765495d2c07db382834eba24b 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 @@ -87,6 +87,34 @@ void JsFileAccessExtAbility::Init(const std::shared_ptr &rec FinishTrace(HITRACE_TAG_FILEMANAGEMENT); } +NativeValue* JsFileAccessExtAbility::FuncCallback(NativeEngine* engine, NativeCallbackInfo* info) +{ + if (engine == nullptr || info == nullptr) { + HILOG_INFO("invalid param."); + return engine->CreateUndefined(); + } + if (info->argc != ARGC_TWO || info->argv[ARGC_ZERO] == nullptr || info->argv[ARGC_ONE] == nullptr) { + HILOG_ERROR("invalid args."); + return engine->CreateUndefined(); + } + std::string uri = UnwrapStringFromJS(reinterpret_cast(engine), + reinterpret_cast(info->argv[ARGC_ZERO])); + NotifyType notifyType = (NotifyType)UnwrapUint32FromJS(reinterpret_cast(engine), + reinterpret_cast(info->argv[ARGC_ONE])); + + if (info->functionInfo == nullptr || info->functionInfo->data == nullptr) { + HILOG_INFO("invalid object."); + return engine->CreateUndefined(); + } + JsFileAccessExtAbility* jsExtension = static_cast(info->functionInfo->data); + if (jsExtension == nullptr) { + HILOG_INFO("invalid JsFileAccessExtAbility."); + return engine->CreateUndefined(); + } + jsExtension->Notify(Uri(uri), notifyType); + return engine->CreateUndefined(); +} + void JsFileAccessExtAbility::OnStart(const AAFwk::Want &want) { StartTrace(HITRACE_TAG_FILEMANAGEMENT, "OnStart"); @@ -97,6 +125,13 @@ void JsFileAccessExtAbility::OnStart(const AAFwk::Want &want) NativeValue* nativeWant = reinterpret_cast(napiWant); NativeValue* argv[] = {nativeWant}; CallObjectMethod("onCreate", argv, ARGC_ONE); + + const std::string funcName = "FuncCallback"; + auto& nativeEngine = jsRuntime_.GetNativeEngine(); + NativeValue* func = nativeEngine.CreateFunction(funcName.c_str(), funcName.length(), + JsFileAccessExtAbility::FuncCallback, this); + NativeValue* argvCallback[] = {func}; + CallObjectMethod("registerCallback", argvCallback, ARGC_ONE); FinishTrace(HITRACE_TAG_FILEMANAGEMENT); } 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 0000000000000000000000000000000000000000..852bd3e24e3a3a46bc7bc48a7ba69496554c1397 --- /dev/null +++ b/frameworks/innerkits/file_access/src/notify/file_access_notify_client.cpp @@ -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. + */ + +#include "file_access_notify_client.h" +#include "hilog_wrapper.h" + +namespace OHOS { +namespace FileAccessFwk { +FileAccessNotifyClient::FileAccessNotifyClient(std::shared_ptr ¬ifyCallback) + : notifyCallback_(notifyCallback) +{ +} + +FileAccessNotifyClient::~FileAccessNotifyClient() +{ +} + +void FileAccessNotifyClient::OnNotify(const Uri &uri, const NotifyType notifyType) +{ + if (notifyCallback_ != nullptr) { + notifyCallback_->OnNotify(uri, notifyType); + } +} +} // 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 0000000000000000000000000000000000000000..72809ff62a8144b019ff6ec1cb6cc311b75e1247 --- /dev/null +++ b/frameworks/innerkits/file_access/src/notify/file_access_notify_manager.cpp @@ -0,0 +1,132 @@ +/* + * 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 "file_access_framework_errno.h" +#include "hilog_wrapper.h" + +namespace OHOS { +namespace FileAccessFwk { +std::mutex FileAccessNotifyManager::notifyMapMutex_; + +FileAccessNotifyManager::FileAccessNotifyManager() +{ +} + +FileAccessNotifyManager::~FileAccessNotifyManager() +{ +} + +int FileAccessNotifyManager::RegisterNotify(sptr ¬ify) +{ + std::lock_guard lock(notifyMapMutex_); + if (!AddNotifyToMap(notify)) { + HILOG_ERROR("the remote obj is nullptr."); + return ERR_INVALID_NOTIFY; + } + return ERR_OK; +} + +int FileAccessNotifyManager::UnregisterNotify(sptr ¬ify) +{ + if (notify == nullptr || notify->AsObject() == nullptr) { + HILOG_ERROR("the remote obj is nullptr."); + return ERR_INVALID_NOTIFY; + } + std::lock_guard lock(notifyMapMutex_); + if (!RemoveNotifyFromMap(notify->AsObject())) { + HILOG_ERROR("remove remote obj from map fail."); + return ERR_REMOVE_NOTIFY_FAIL; + } + return ERR_OK; +} + +int FileAccessNotifyManager::Notify(const Uri uri, const NotifyType notifyType) +{ + std::lock_guard lock(notifyMapMutex_); + for (auto iter = notifyMap_.begin(); iter != notifyMap_.end(); iter++) { + if (iter->first != nullptr) { + iter->first->OnNotify(uri, notifyType); + } + } + return ERR_OK; +} + +bool FileAccessNotifyManager::AddNotifyToMap(const sptr ¬ify) +{ + if (notify == nullptr || notify->AsObject() == nullptr) { + HILOG_ERROR("the death notify obj is nullptr."); + return false; + } + for (auto iter = notifyMap_.begin(); iter != notifyMap_.end(); iter++) { + if (iter->first == notify) { + iter->first->AsObject()->RemoveDeathRecipient(iter->second); + notifyMap_.erase(iter); + } + } + sptr deathRecipient = new FileAccessNotifyCallbackRecipient( + std::bind(&FileAccessNotifyManager::OnCallBackDied, this, std::placeholders::_1)); + notify->AsObject()->AddDeathRecipient(deathRecipient); + notifyMap_.emplace(notify, deathRecipient); + return true; +} + +bool FileAccessNotifyManager::RemoveNotifyFromMap(const sptr &remote) +{ + if (remote == nullptr) { + HILOG_ERROR("the death remote obj is nullptr."); + return false; + } + for (auto iter = notifyMap_.begin(); iter != notifyMap_.end(); iter++) { + if (iter->first->AsObject() == remote) { + iter->first->AsObject()->RemoveDeathRecipient(iter->second); + notifyMap_.erase(iter); + break; + } + } + return true; +} + +void FileAccessNotifyManager::OnCallBackDied(const wptr &remote) +{ + auto object = remote.promote(); + if (object == nullptr) { + HILOG_INFO("the death remote obj is nullptr"); + return; + } + std::lock_guard lock(notifyMapMutex_); + if (!RemoveNotifyFromMap(object)) { + HILOG_ERROR("remove remote obj from map fail."); + return; + } +} + +FileAccessNotifyCallbackRecipient::FileAccessNotifyCallbackRecipient(RemoteDiedHandler handler) : handler_(handler) +{ +} + +FileAccessNotifyCallbackRecipient::~FileAccessNotifyCallbackRecipient() +{ +} + +void FileAccessNotifyCallbackRecipient::OnRemoteDied(const wptr &remote) +{ + if (handler_) { + handler_(remote); + } +} +} // namespace FileAccessFwk +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/innerkits/file_access/src/notify/file_access_notify_proxy.cpp b/frameworks/innerkits/file_access/src/notify/file_access_notify_proxy.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5bd352b5705ea6ea0bb8d9b06d8d3e2d66cfc5b1 --- /dev/null +++ b/frameworks/innerkits/file_access/src/notify/file_access_notify_proxy.cpp @@ -0,0 +1,50 @@ +/* + * 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_proxy.h" + +#include "hilog_wrapper.h" +#include "message_parcel.h" + +namespace OHOS { +namespace FileAccessFwk { +FileAccessNotifyProxy::FileAccessNotifyProxy(const sptr &impl) : IRemoteProxy(impl) +{ +} + +void FileAccessNotifyProxy::OnNotify(const Uri &uri, const NotifyType notifyType) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(FileAccessNotifyProxy::GetDescriptor())) { + HILOG_ERROR("write descriptor failed"); + return; + } + if (!data.WriteParcelable(&uri)) { + HILOG_ERROR("write parcel uri failed"); + return; + } + if (!data.WriteInt32((int32_t)notifyType)) { + HILOG_ERROR("write parcel notifyType failed"); + return; + } + int error = Remote()->SendRequest(CMD_ON_NOTIFY, data, reply, option); + if (error != 0) { + HILOG_ERROR("SendRequest failed, error %d", error); + } +} +} // namespace FileAccessFwk +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/innerkits/file_access/src/notify/file_access_notify_stub.cpp b/frameworks/innerkits/file_access/src/notify/file_access_notify_stub.cpp new file mode 100644 index 0000000000000000000000000000000000000000..901b38ff0166bfdd8350fee77276fd2374db5b2e --- /dev/null +++ b/frameworks/innerkits/file_access/src/notify/file_access_notify_stub.cpp @@ -0,0 +1,50 @@ +/* + * 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_stub.h" + +#include "file_access_framework_errno.h" +#include "hilog_wrapper.h" +#include "message_parcel.h" + +namespace OHOS { +namespace FileAccessFwk { +int32_t FileAccessNotifyStub::OnRemoteRequest(uint32_t code, MessageParcel &data, + MessageParcel &reply, MessageOption &option) +{ + auto descriptor = FileAccessNotifyStub::GetDescriptor(); + auto remoteDescriptor = data.ReadInterfaceToken(); + if (descriptor != remoteDescriptor) { + HILOG_ERROR("local descriptor is not equal to remote"); + return ERR_ERROR; + } + 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); + return ERR_OK; + } + default: + HILOG_DEBUG("code error, FileAccessNotifyStub::OnRemoteRequest"); + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); + } +} +} // namespace FileAccessFwk +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/napi/file_access_ext_ability/file_access_ext_ability.js b/interfaces/kits/napi/file_access_ext_ability/file_access_ext_ability.js index 97f0eb6ab4dd5d2504a8fe46af5aa4af6232638f..9c6da32a9dcf1f8bc0f8fdccd1d7b557ad0f1da3 100644 --- a/interfaces/kits/napi/file_access_ext_ability/file_access_ext_ability.js +++ b/interfaces/kits/napi/file_access_ext_ability/file_access_ext_ability.js @@ -17,6 +17,9 @@ class FileAccessExtensionAbility { onCreate(want) { } + registerCallback(callback) { + } + openFile(uri, mode) { return 0; } @@ -36,7 +39,7 @@ class FileAccessExtensionAbility { move(sourceFileUri, targetParentUri) { return ""; } - + rename(sourceFileUri, displayName) { return ""; } diff --git a/interfaces/kits/napi/file_access_module/BUILD.gn b/interfaces/kits/napi/file_access_module/BUILD.gn index fd97e1a0cf445599f98b557b7d05c307a9676325..a020039591ffe4953f54e26156d7524d934a5963 100644 --- a/interfaces/kits/napi/file_access_module/BUILD.gn +++ b/interfaces/kits/napi/file_access_module/BUILD.gn @@ -24,18 +24,20 @@ ohos_shared_library("fileaccess") { "./", "${BASE_DIR}/utils", "${BASE_DIR}/interfaces/kits/napi/common", + "//foundation/distributeddatamgr/distributedfile/utils/filemgmt_libn/include", ] sources = [ "${BASE_DIR}/interfaces/kits/napi/common/file_extension_info_napi.cpp", "napi_fileaccess_helper.cpp", + "napi_notify_callback.cpp", "native_fileaccess_module.cpp", ] deps = [ "${BASE_DIR}/frameworks/innerkits/file_access:file_access_extension_ability_kit", "//foundation/ability/ability_runtime/frameworks/js/napi/inner/napi_common:napi_common", - "//foundation/distributeddatamgr/distributedfile/utils/filemgmt_libn", + "//foundation/distributeddatamgr/distributedfile/utils/filemgmt_libn:filemgmt_libn", ] external_deps = [ @@ -45,6 +47,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 c8edcf9b34785e3385a7b6a1288230c0f82be584..f0d2c5cb3994398a852f13462524661f94317f0f 100644 --- a/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.cpp +++ b/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.cpp @@ -22,8 +22,10 @@ #include "file_access_helper.h" #include "filemgmt_libn.h" #include "hilog_wrapper.h" +#include "ifile_access_notify.h" #include "napi_base_context.h" #include "napi_common_fileaccess.h" +#include "napi_notify_callback.h" #include "n_val.h" #include "securec.h" #include "uri.h" @@ -74,7 +76,7 @@ static napi_value FileAccessHelperConstructor(napi_env env, napi_callback_info i return nullptr; } g_fileAccessHelperList.emplace_back(fileAccessHelper); - + auto finalize = [](napi_env env, void *data, void *hint) { FileAccessHelper *objectInfo = static_cast(data); g_fileAccessHelperList.remove_if([objectInfo](const std::shared_ptr &fileAccessHelper) { @@ -155,6 +157,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, @@ -599,5 +603,59 @@ napi_value NAPI_GetRoots(napi_env env, napi_callback_info info) } return NVal::CreateUndefined(env).val_; } -} // namespace AppExecFwk + +napi_value NAPI_On(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::ONE)) { + NError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + FileAccessHelper *fileAccessHelper = GetFileAccessHelper(env, funcArg.GetThisVar()); + if (fileAccessHelper == nullptr) { + NError(EINVAL).ThrowErr(env, "Get FileAccessHelper fail"); + return nullptr; + } + + napi_value napiCallback = funcArg[NARG_POS::FIRST]; + if (!NVal(env, napiCallback).TypeIs(napi_function)) { + NError(EINVAL).ThrowErr(env, "Argument must be function"); + return nullptr; + } + + std::shared_ptr callback = std::make_shared(env, napiCallback); + if (callback == nullptr) { + NError(EINVAL).ThrowErr(env, "new NapiNotifyCallback fail."); + return nullptr; + } + + if (fileAccessHelper->On(callback) != ERR_OK) { + NError(EINVAL).ThrowErr(env, "FileAccessHelper::On fail."); + return nullptr; + } + return NVal::CreateUndefined(env).val_; +} + +napi_value NAPI_Off(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ZERO)) { + NError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + FileAccessHelper *fileAccessHelper = GetFileAccessHelper(env, funcArg.GetThisVar()); + if (fileAccessHelper == nullptr) { + NError(EINVAL).ThrowErr(env, "Get FileAccessHelper fail"); + return nullptr; + } + + if (fileAccessHelper->Off() != ERR_OK) { + NError(EINVAL).ThrowErr(env, "FileAccessHelper::Off fail."); + return nullptr; + } + return NVal::CreateUndefined(env).val_; +} +} // namespace FileAccessFwk } // 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 5f3836cea0a49df77fe61aff8d0590cbc5ca11dd..6b05abda03fb837c424c698b0458e7c079b11b9f 100644 --- a/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.h +++ b/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.h @@ -32,6 +32,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_notify_callback.cpp b/interfaces/kits/napi/file_access_module/napi_notify_callback.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ec7ea62be37f62026830d724008a2e5f300acd19 --- /dev/null +++ b/interfaces/kits/napi/file_access_module/napi_notify_callback.cpp @@ -0,0 +1,90 @@ +/* + * 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_notify_callback.h" + +#include +#include + +namespace OHOS { +namespace FileAccessFwk { +namespace { + const int ARGS_TWO = 2; +} +NapiNotifyCallback::NapiNotifyCallback(napi_env env, napi_value callback) + : env_(env) +{ + napi_create_reference(env, callback, 1, &callback_); + napi_get_uv_event_loop(env, &loop_); +} + +NapiNotifyCallback::~NapiNotifyCallback() +{ + napi_delete_reference(env_, callback_); +} + +void NapiNotifyCallback::OnNotify(const Uri &uri, const NotifyType notifyType) +{ + std::shared_ptr work = std::make_shared(); + if (work == nullptr) { + HILOG_ERROR("failed to new uv_work_t"); + return; + } + CallbackParam* param = new CallbackParam(this, uri.ToString(), notifyType); + if (param == nullptr) { + HILOG_ERROR("failed to new param"); + return; + } + work->data = std::move(param); + int ret = uv_queue_work(loop_, work.get(), + [](uv_work_t *work) {}, + [](uv_work_t *work, int status) { + std::shared_ptr param; + param.reset(std::move(reinterpret_cast(work->data))); + + napi_value uri = nullptr; + napi_status ret = napi_create_string_utf8(param->callback_->env_, param->uri_.c_str(), + param->uri_.length(), &uri); + if (ret != napi_ok) { + HILOG_ERROR("failed to create param Uri"); + return; + } + + napi_value notifyType = nullptr; + ret = napi_create_int32(param->callback_->env_, (int32_t)(param->notifyType_), ¬ifyType); + if (ret != napi_ok) { + HILOG_ERROR("failed to create param NotifyType"); + return; + } + napi_value callback = nullptr; + napi_value args[ARGS_TWO] = {uri, notifyType}; + napi_get_reference_value(param->callback_->env_, param->callback_->callback_, &callback); + napi_value global = nullptr; + napi_get_global(param->callback_->env_, &global); + napi_value result = nullptr; + ret = napi_call_function(param->callback_->env_, global, callback, ARGS_TWO, args, &result); + if (ret != napi_ok) { + HILOG_ERROR("notify failed status:%{public}d.", ret); + } + }); + if (ret != 0) { + if (work->data != nullptr) { + delete (CallbackParam *)(work->data); + work->data = nullptr; + } + } +} +} // namespace FileAccessFwk +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/napi/file_access_module/napi_notify_callback.h b/interfaces/kits/napi/file_access_module/napi_notify_callback.h new file mode 100644 index 0000000000000000000000000000000000000000..f2e2d741a262f881c61963b4bff77dd15d7191d3 --- /dev/null +++ b/interfaces/kits/napi/file_access_module/napi_notify_callback.h @@ -0,0 +1,50 @@ +/* + * 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_NOTIFY_CALLBACK_H__ +#define __NAPI_NOTIFY_CALLBACK_H__ + +#include "hilog_wrapper.h" +#include "inotify_callback.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 NapiNotifyCallback : public INotifyCallback { +public: + NapiNotifyCallback(napi_env env, napi_value callback); + virtual ~NapiNotifyCallback(); + void OnNotify(const Uri &uri, const NotifyType notifyType) override; + + struct CallbackParam { + NapiNotifyCallback *callback_; + std::string uri_; + NotifyType notifyType_; + CallbackParam(NapiNotifyCallback *callback, std::string uri, NotifyType notifyType) + : callback_(callback), uri_(uri), notifyType_(notifyType) + { + } + }; +private: + napi_ref callback_ = nullptr; + napi_env env_; + uv_loop_s *loop_ = nullptr; +}; +} // FileAccessFwk +} // OHOS +#endif // __NAPI_NOTIFY_CALLBACK_H__ \ No newline at end of file diff --git a/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/FileExtensionAbility.ts b/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/FileExtensionAbility.ts index cc1f349791acd8307f165d801b9d2de1616ca6e7..a85bc2daca733ac265bbeb233c8b76821090e337 100644 --- a/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/FileExtensionAbility.ts +++ b/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/FileExtensionAbility.ts @@ -14,32 +14,46 @@ */ import Extension from '@ohos.application.FileAccessExtensionAbility' import fileio from '@ohos.fileio' -import { init, delVolumeInfo, getVolumeInfoList } from './VolumeManager' +import { init, findVolumeInfo, delVolumeInfo, getVolumeInfoList, path2uri } from './VolumeManager' import { onReceiveEvent } from './Subcriber' import fileExtensionInfo from "@ohos.fileExtensionInfo" import hilog from '@ohos.hilog' import process from '@ohos.process'; const FLAG = fileExtensionInfo.FLAG; +const NotifyType = fileExtensionInfo.NotifyType; const BUNDLE_NAME = 'com.ohos.UserFile.ExternalFileManager'; const DEFAULT_MODE = 0o666; const CREATE_FILE_FLAGS = 0o100; const FILE_ACCESS = 'fileAccess://'; const DOMAIN_CODE = 0x0001; const TAG = 'js_server'; +let callbackFun = null; export default class FileExtAbility extends Extension { onCreate(want) { init(); onReceiveEvent(function (data) { if (data.event == 'usual.event.data.VOLUME_MOUNTED') { + let uri = path2uri('', data.parameters.path); + callbackFun(uri, NotifyType.DEVICE_ONLINE); process.exit(0); } else { + let uri = ''; + let volumeInfo = findVolumeInfo(data.parameters.id); + if (volumeInfo) { + uri = volumeInfo.uri; + } + callbackFun(uri, NotifyType.DEVICE_OFFLINE); delVolumeInfo(data.parameters.id); } }); } + registerCallback(callback) { + callbackFun = callback; + } + checkUri(uri) { if(uri.indexOf(FILE_ACCESS) == 0) { uri = uri.replace(FILE_ACCESS, ''); diff --git a/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/VolumeManager.ts b/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/VolumeManager.ts index 63c5bf912c828ccb245e84b466ae52a82840c959..2944442cc721c6ed711ba8dedae0e3366ebd011e 100644 --- a/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/VolumeManager.ts +++ b/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/VolumeManager.ts @@ -49,6 +49,10 @@ function path2uri(id, path) { return `fileAccess://${id}${path}`; } +function findVolumeInfo(volumeId) { + return globalThis.volumeInfoList.find((volume) => volume.volumeId == volumeId); +} + function delVolumeInfo(volumeId) { globalThis.volumeInfoList = globalThis.volumeInfoList.filter((volume) => volume.volumeId !== volumeId); } @@ -57,4 +61,4 @@ function getVolumeInfoList() { return globalThis.volumeInfoList; } -export { init, addVolumeInfo, delVolumeInfo, getVolumeInfoList, path2uri } \ No newline at end of file +export { init, addVolumeInfo, findVolumeInfo, delVolumeInfo, getVolumeInfoList, path2uri } \ No newline at end of file diff --git a/utils/file_access_framework_errno.h b/utils/file_access_framework_errno.h index 6f8bfd17d80e235503e9b3584a5dc147b90c949d..fd941cffb78b0d7b612a8dfe967e448c58284cc6 100644 --- a/utils/file_access_framework_errno.h +++ b/utils/file_access_framework_errno.h @@ -21,15 +21,22 @@ namespace FileAccessFwk { enum { MODULE_FILE_ACCESS_FRAMEWORK = 0x01 }; + constexpr ErrCode BASE_OFFSET = ErrCodeOffset(SUBSYS_FILEMANAGEMENT, MODULE_FILE_ACCESS_FRAMEWORK); + 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_INVALID_NOTIFY, // invalid notify + ERR_REMOVE_NOTIFY_FAIL, // remove notify fail + ERR_NOTIFY_NOT_EXIST, // the notify is not exist + ERR_NOTIFY_EXIST, // the notify is exist ERR_FILEIO_FAIL, // fileio fail ERR_INVALID_PARAM // invalid parameter };