diff --git a/frameworks/innerkits/file_access/include/file_access_extension_info.h b/frameworks/innerkits/file_access/include/file_access_extension_info.h new file mode 100644 index 0000000000000000000000000000000000000000..8d4b510f18d802b5442a9a032adfa91ae1073788 --- /dev/null +++ b/frameworks/innerkits/file_access/include/file_access_extension_info.h @@ -0,0 +1,151 @@ +/* + * 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_EXTENSION_INFO_H +#define FILE_ACCESS_EXTENSION_INFO_H + +#include +#include + +#include "parcel.h" +#include "uri.h" + +namespace OHOS { +namespace FileAccessFwk { +struct FileInfo : public virtual OHOS::Parcelable { +public: + Uri uri = Uri(""); + std::string fileName; + std::string mode; + int64_t size {0}; + int64_t mtime {0}; + std::string mimeType; + + bool ReadFromParcel(Parcel &parcel) + { + std::unique_ptr uriInfo(parcel.ReadParcelable()); + if (uriInfo == nullptr) { + return false; + } + uri = *uriInfo; + + fileName = parcel.ReadString(); + mode = parcel.ReadString(); + size = parcel.ReadUint64(); + mtime = parcel.ReadInt64(); + mimeType = parcel.ReadString(); + return true; + } + + virtual bool Marshalling(Parcel &parcel) const override + { + if (!parcel.WriteParcelable(&uri)) { + return false; + } + if (!parcel.WriteString(fileName)) { + return false; + } + if (!parcel.WriteString(mode)) { + return false; + } + if (!parcel.WriteUint64(size)) { + return false; + } + if (!parcel.WriteInt64(mtime)) { + return false; + } + if (!parcel.WriteString(mimeType)) { + return false; + } + return true; + } + + static FileInfo *Unmarshalling(Parcel &parcel) + { + FileInfo *info = new (std::nothrow) FileInfo(); + if (info == nullptr) { + return nullptr; + } + + if (!info->ReadFromParcel(parcel)) { + delete info; + info = nullptr; + } + return info; + } +}; + +struct DeviceInfo : public virtual OHOS::Parcelable { +public: + Uri uri = Uri(""); + std::string displayName; + std::string deviceId; + uint32_t flags {0}; + + bool ReadFromParcel(Parcel &parcel) + { + std::unique_ptr uriInfo(parcel.ReadParcelable()); + if (uriInfo == nullptr) { + return false; + } + uri = *uriInfo; + + displayName = parcel.ReadString(); + deviceId = parcel.ReadString(); + flags = parcel.ReadUint32(); + return true; + } + + virtual bool Marshalling(Parcel &parcel) const override + { + if (!parcel.WriteParcelable(&uri)) { + return false; + } + if (!parcel.WriteString(displayName)) { + return false; + } + if (!parcel.WriteString(deviceId)) { + return false; + } + if (!parcel.WriteUint32(flags)) { + return false; + } + return true; + } + + static DeviceInfo *Unmarshalling(Parcel &parcel) + { + DeviceInfo *info = new (std::nothrow) DeviceInfo(); + if (info == nullptr) { + return nullptr; + } + + if (!info->ReadFromParcel(parcel)) { + delete info; + info = nullptr; + } + return info; + } +}; + +const uint32_t FLAG_SUPPORTS_THUMBNAIL = 1; +const uint32_t FLAG_SUPPORTS_WRITE = 1 << 1; +const uint32_t FLAG_SUPPORTS_READ = 1 << 2; +const uint32_t FLAG_SUPPORTS_DELETE = 1 << 3; +const uint32_t FLAG_SUPPORTS_RENAME = 1 << 4; +const uint32_t FLAG_SUPPORTS_MOVE = 1 << 5; +} // namespace FileAccessFwk +} // namespace OHOS +#endif // FILE_ACCESS_EXTENSION_INFO_H \ No newline at end of file diff --git a/frameworks/innerkits/file_access/include/file_access_helper.h b/frameworks/innerkits/file_access/include/file_access_helper.h new file mode 100644 index 0000000000000000000000000000000000000000..be151480047119289242ca56f6042df89e7c62ab --- /dev/null +++ b/frameworks/innerkits/file_access/include/file_access_helper.h @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FILE_ACCESS_HELPER_H +#define FILE_ACCESS_HELPER_H + +#include +#include + +#include "context.h" +#include "file_access_ext_connection.h" +#include "file_access_extension_info.h" +#include "hilog_wrapper.h" +#include "ifile_access_ext_base.h" +#include "uri.h" +#include "want.h" + +using Uri = OHOS::Uri; + +namespace OHOS { +namespace FileAccessFwk { +using string = std::string; +class FileAccessHelper final : public std::enable_shared_from_this { +public: + ~FileAccessHelper() = default; + + static std::shared_ptr Creator(const std::shared_ptr &context, + const AAFwk::Want &want); + static std::shared_ptr Creator(const sptr &token, const AAFwk::Want &want); + + bool GetProxy(); + bool Release(); + int OpenFile(Uri &uri, int flags); + int CreateFile(Uri &parent, const std::string &displayName, Uri &newFile); + int Mkdir(Uri &parent, const std::string &displayName, Uri &newDir); + int Delete(Uri &selectFile); + int Move(Uri &sourceFile, Uri &targetParent, Uri &newFile); + int Rename(Uri &sourceFile, const std::string &displayName, Uri &newFile); + std::vector ListFile(Uri &sourceFile); + std::vector GetRoots(); + +private: + FileAccessHelper(const std::shared_ptr &context, const AAFwk::Want &want, + const sptr &fileAccessExtProxy); + FileAccessHelper(const sptr &token, + 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; +}; + +class FileAccessDeathRecipient : public IRemoteObject::DeathRecipient { +public: + using RemoteDiedHandler = std::function &)>; + explicit FileAccessDeathRecipient(RemoteDiedHandler handler); + virtual ~FileAccessDeathRecipient(); + virtual void OnRemoteDied(const wptr &remote); + +private: + RemoteDiedHandler handler_; +}; +} // namespace FileAccessFwk +} // namespace OHOS +#endif // FILE_ACCESS_HELPER_H diff --git a/frameworks/innerkits/file_access/include/ifile_access_ext_base.h b/frameworks/innerkits/file_access/include/ifile_access_ext_base.h new file mode 100644 index 0000000000000000000000000000000000000000..5c79a87b62b8fea0fa0cdb801b4171aeacd876d4 --- /dev/null +++ b/frameworks/innerkits/file_access/include/ifile_access_ext_base.h @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef I_FILE_EXT_BASE_H +#define I_FILE_EXT_BASE_H + +#include +#include +#include +#include + +#include "file_access_extension_info.h" +#include "uri.h" + +namespace OHOS { +namespace FileAccessFwk { +class IFileAccessExtBase : public IRemoteBroker { +public: + DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.FileAccessFwk.IFileAccessExtBase"); + + enum { + CMD_OPEN_FILE = 1, + CMD_CREATE_FILE = 2, + CMD_MKDIR = 3, + CMD_DELETE = 4, + CMD_MOVE = 5, + CMD_RENAME = 6, + CMD_LIST_FILE = 7, + CMD_GET_ROOTS = 8 + }; + + virtual int OpenFile(const Uri &uri, int flags) = 0; + virtual int CreateFile(const Uri &parent, const std::string &displayName, Uri &newFile) = 0; + virtual int Mkdir(const Uri &parent, const std::string &displayName, Uri &newFile) = 0; + 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; +}; +} // namespace FileAccessFwk +} // namespace OHOS +#endif // I_FILE_EXT_BASE_H diff --git a/frameworks/innerkits/file_access/include/napi_common_fileaccess.h b/frameworks/innerkits/file_access/include/napi_common_fileaccess.h new file mode 100644 index 0000000000000000000000000000000000000000..1051b06d6b55bcc304fcb3bc27f0d2a60bc3d73d --- /dev/null +++ b/frameworks/innerkits/file_access/include/napi_common_fileaccess.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 NAPI_COMMON_FILE_ACCESS_H +#define NAPI_COMMON_FILE_ACCESS_H + +#include +#include +#include + +#include "file_access_extension_info.h" +#include "napi_common_data.h" +#include "napi_common_util.h" + +namespace OHOS { +namespace FileAccessFwk { +bool UnwrapInt64ByPropertyName(napi_env env, napi_value param, const char *propertyName, int64_t &value); + +napi_value WrapUint32ToJS(napi_env env, uint32_t value); +uint32_t UnwrapUint32FromJS(napi_env env, napi_value param, uint32_t defaultValue = 0); +bool UnwrapUint32FromJS2(napi_env env, napi_value param, uint32_t &value); +bool UnwrapUint32ByPropertyName(napi_env env, napi_value param, const char *propertyName, uint32_t &value); + +napi_value WrapBigIntUint64ToJS(napi_env env, uint64_t value); +uint64_t UnwrapBigIntUint64FromJS(napi_env env, napi_value param, uint64_t defaultValue = 0); +bool UnwrapBigIntUint64FromJS2(napi_env env, napi_value param, uint64_t &defaultValue); +bool UnwrapBigIntUint64ByPropertyName(napi_env env, napi_value param, const char *propertyName, uint64_t &value); + +napi_value WrapFileInfo(napi_env env, const FileInfo &fileInfo); +bool UnwrapFileInfo(napi_env env, napi_value param, FileInfo &fileInfo); + +napi_value WrapArrayFileInfoToJS(napi_env env, const std::vector &fileInfoVec); +bool UnwrapArrayFileInfoFromJS(napi_env env, napi_value param, std::vector &fileInfoVec); + +napi_value WrapDeviceInfo(napi_env env, const DeviceInfo &deviceInfo); +bool UnwrapDeviceInfo(napi_env env, napi_value param, DeviceInfo &deviceInfo); + +napi_value WrapArrayDeviceInfoToJS(napi_env env, const std::vector &deviceInfoVec); +bool UnwrapArrayDeviceInfoFromJS(napi_env env, napi_value param, std::vector &deviceInfoVec); +} // namespace FileAccessFwk +} // namespace OHOS +#endif // NAPI_COMMON_FILE_ACCESS_H diff --git a/frameworks/innerkits/file_access/src/file_access_helper.cpp b/frameworks/innerkits/file_access/src/file_access_helper.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6e04cb8e01c379bba13bbf655de3cff069e5719e --- /dev/null +++ b/frameworks/innerkits/file_access/src/file_access_helper.cpp @@ -0,0 +1,252 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "file_access_helper.h" + +#include "file_access_framework_errno.h" +#include "hilog_wrapper.h" +#include "ifile_access_ext_base.h" + +namespace OHOS { +namespace FileAccessFwk { +FileAccessHelper::FileAccessHelper(const std::shared_ptr &context, + const AAFwk::Want &want, const sptr &fileAccessExtProxy) +{ + token_ = context->GetToken(); + want_ = want; + fileAccessExtProxy_ = fileAccessExtProxy; + fileAccessExtConnection_ = FileAccessExtConnection::GetInstance(); +} + +void FileAccessHelper::AddFileAccessDeathRecipient(const sptr &token) +{ + if (token != nullptr && callerDeathRecipient_ != nullptr) { + token->RemoveDeathRecipient(callerDeathRecipient_); + } + if (callerDeathRecipient_ == nullptr) { + callerDeathRecipient_ = + new FileAccessDeathRecipient(std::bind(&FileAccessHelper::OnSchedulerDied, this, std::placeholders::_1)); + } + if (token != nullptr) { + token->AddDeathRecipient(callerDeathRecipient_); + } +} + +void FileAccessHelper::OnSchedulerDied(const wptr &remote) +{ + auto object = remote.promote(); + object = nullptr; + fileAccessExtProxy_ = nullptr; +} + +FileAccessHelper::FileAccessHelper(const sptr &token, + const AAFwk::Want &want, const sptr &fileAccessExtProxy) +{ + token_ = token; + want_ = want; + fileAccessExtProxy_ = fileAccessExtProxy; + fileAccessExtConnection_ = FileAccessExtConnection::GetInstance(); +} + +std::shared_ptr FileAccessHelper::Creator(const sptr &token, const AAFwk::Want &want) +{ + sptr fileAccessExtProxy = nullptr; + + sptr fileAccessExtConnection = FileAccessExtConnection::GetInstance(); + if (!fileAccessExtConnection->IsExtAbilityConnected()) { + fileAccessExtConnection->ConnectFileExtAbility(want, token); + } + + fileAccessExtProxy = fileAccessExtConnection->GetFileExtProxy(); + if (fileAccessExtProxy == nullptr) { + HILOG_WARN("FileAccessHelper::Creator get invalid fileAccessExtProxy"); + } + + FileAccessHelper *ptrFileAccessHelper = new (std::nothrow) FileAccessHelper(token, want, fileAccessExtProxy); + if (ptrFileAccessHelper == nullptr) { + HILOG_ERROR("FileAccessHelper::Creator failed, create FileAccessHelper failed"); + return nullptr; + } + + return std::shared_ptr(ptrFileAccessHelper); +} + +std::shared_ptr FileAccessHelper::Creator( + const std::shared_ptr &context, const AAFwk::Want &want) +{ + if (context == nullptr) { + HILOG_ERROR("%{public}s failed, context == nullptr", __func__); + return nullptr; + } + + sptr fileAccessExtProxy = nullptr; + sptr fileAccessExtConnection = FileAccessExtConnection::GetInstance(); + if (!fileAccessExtConnection->IsExtAbilityConnected()) { + fileAccessExtConnection->ConnectFileExtAbility(want, context->GetToken()); + } + + fileAccessExtProxy = fileAccessExtConnection->GetFileExtProxy(); + if (fileAccessExtProxy == nullptr) { + HILOG_WARN("%{public}s get invalid fileAccessExtProxy", __func__); + } + + FileAccessHelper *ptrFileAccessHelper = new (std::nothrow) FileAccessHelper(context, want, fileAccessExtProxy); + if (ptrFileAccessHelper == nullptr) { + HILOG_ERROR("%{public}s failed, create FileAccessHelper failed", __func__); + return nullptr; + } + + return std::shared_ptr(ptrFileAccessHelper); +} + +bool FileAccessHelper::Release() +{ + if (fileAccessExtConnection_->IsExtAbilityConnected()) { + fileAccessExtConnection_->DisconnectFileExtAbility(); + } + fileAccessExtProxy_ = nullptr; + return true; +} + +bool FileAccessHelper::GetProxy() +{ + if (!fileAccessExtConnection_->IsExtAbilityConnected()) { + fileAccessExtConnection_->ConnectFileExtAbility(want_, token_); + } + + fileAccessExtProxy_ = fileAccessExtConnection_->GetFileExtProxy(); + if (isSystemCaller_ && fileAccessExtProxy_) { + AddFileAccessDeathRecipient(fileAccessExtProxy_->AsObject()); + } + + if (fileAccessExtProxy_ == nullptr) { + HILOG_ERROR("%{public}s failed with invalid fileAccessExtProxy_", __func__); + return false; + } + return true; +} + +int FileAccessHelper::OpenFile(Uri &uri, int flags) +{ + int fd = ERR_ERROR; + if (!GetProxy()) { + HILOG_ERROR("%{public}s failed with invalid fileAccessExtProxy_", __func__); + return fd; + } + + fd = fileAccessExtProxy_->OpenFile(uri, flags); + return fd; +} + +int FileAccessHelper::CreateFile(Uri &parent, const std::string &displayName, Uri &newFile) +{ + int index = ERR_ERROR; + if (!GetProxy()) { + HILOG_ERROR("%{public}s failed with invalid fileAccessExtProxy_", __func__); + return index; + } + + index = fileAccessExtProxy_->CreateFile(parent, displayName, newFile); + return index; +} + +int FileAccessHelper::Mkdir(Uri &parent, const std::string &displayName, Uri &newDir) +{ + int index = ERR_ERROR; + if (fileAccessExtProxy_ == nullptr) { + HILOG_ERROR("%{public}s failed with invalid fileAccessExtProxy_", __func__); + return index; + } + + index = fileAccessExtProxy_->Mkdir(parent, displayName, newDir); + return index; +} + +int FileAccessHelper::Delete(Uri &selectFile) +{ + int index = ERR_ERROR; + if (!GetProxy()) { + HILOG_ERROR("%{public}s failed with invalid fileAccessExtProxy_", __func__); + return index; + } + + index = fileAccessExtProxy_->Delete(selectFile); + return index; +} + +int FileAccessHelper::Move(Uri &sourceFile, Uri &targetParent, Uri &newFile) +{ + int index = ERR_ERROR; + if (!GetProxy()) { + HILOG_ERROR("%{public}s failed with invalid fileAccessExtProxy_", __func__); + return index; + } + + index = fileAccessExtProxy_->Move(sourceFile, targetParent, newFile); + return index; +} + +int FileAccessHelper::Rename(Uri &sourceFile, const std::string &displayName, Uri &newFile) +{ + int index = ERR_ERROR; + if (!GetProxy()) { + HILOG_ERROR("%{public}s failed with invalid fileAccessExtProxy_", __func__); + return index; + } + + index = fileAccessExtProxy_->Rename(sourceFile, displayName, newFile); + return index; +} + +std::vector FileAccessHelper::ListFile(Uri &sourceFile) +{ + std::vector results; + if (!GetProxy()) { + HILOG_ERROR("%{public}s failed with invalid fileAccessExtProxy_", __func__); + return results; + } + + results = fileAccessExtProxy_->ListFile(sourceFile); + return results; +} + +std::vector FileAccessHelper::GetRoots() +{ + std::vector results; + if (!GetProxy()) { + HILOG_ERROR("%{public}s failed with invalid fileAccessExtProxy_", __func__); + return results; + } + + results = fileAccessExtProxy_->GetRoots(); + return results; +} + +void FileAccessDeathRecipient::OnRemoteDied(const wptr &remote) +{ + if (handler_) { + handler_(remote); + } +} + +FileAccessDeathRecipient::FileAccessDeathRecipient(RemoteDiedHandler handler) : handler_(handler) +{ +} + +FileAccessDeathRecipient::~FileAccessDeathRecipient() +{ +} +} // namespace FileAccessFwk +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/innerkits/file_access/src/napi_common_fileaccess.cpp b/frameworks/innerkits/file_access/src/napi_common_fileaccess.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b8a02f7b60e53a1d3b7c3b7a1f612b26bc68ac7b --- /dev/null +++ b/frameworks/innerkits/file_access/src/napi_common_fileaccess.cpp @@ -0,0 +1,318 @@ +/* + * 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_common_fileaccess.h" + +#include "file_access_framework_errno.h" +#include "hilog_wrapper.h" + +namespace OHOS { +namespace FileAccessFwk { +using namespace OHOS::AppExecFwk; +bool UnwrapInt64ByPropertyName(napi_env env, napi_value param, const char *propertyName, int64_t &value) +{ + napi_value jsValue = GetPropertyValueByPropertyName(env, param, propertyName, napi_number); + if (jsValue != nullptr) { + return UnwrapInt64FromJS2(env, jsValue, value); + } else { + return false; + } +} + +napi_value WrapUint32ToJS(napi_env env, uint32_t value) +{ + napi_value result = nullptr; + NAPI_CALL(env, napi_create_uint32(env, value, &result)); + return result; +} + +uint32_t UnwrapUint32FromJS(napi_env env, napi_value param, uint32_t defaultValue) +{ + uint32_t value = defaultValue; + if (napi_get_value_uint32(env, param, &value) == napi_ok) { + return value; + } else { + return defaultValue; + } +} + +bool UnwrapUint32FromJS2(napi_env env, napi_value param, uint32_t &value) +{ + bool result = false; + if (napi_get_value_uint32(env, param, &value) == napi_ok) { + result = true; + } + return result; +} + +bool UnwrapUint32ByPropertyName(napi_env env, napi_value param, const char *propertyName, uint32_t &value) +{ + napi_value jsValue = GetPropertyValueByPropertyName(env, param, propertyName, napi_number); + if (jsValue != nullptr) { + return UnwrapUint32FromJS2(env, jsValue, value); + } else { + return false; + } +} + +napi_value WrapBigIntUint64ToJS(napi_env env, uint64_t value) +{ + napi_value result = nullptr; + NAPI_CALL(env, napi_create_bigint_uint64(env, value, &result)); + return result; +} + +uint64_t UnwrapBigIntUint64FromJS(napi_env env, napi_value param, uint64_t defaultValue) +{ + bool lossless = true; + uint64_t value = defaultValue; + if (napi_get_value_bigint_uint64(env, param, &value, &lossless) == napi_ok) { + return value; + } else { + return defaultValue; + } +} + +bool UnwrapBigIntUint64FromJS2(napi_env env, napi_value param, uint64_t &defaultValue) +{ + bool lossless = true; + if (napi_get_value_bigint_uint64(env, param, &defaultValue, &lossless) == napi_ok) { + return true; + } else { + return false; + } +} + +bool UnwrapBigIntUint64ByPropertyName(napi_env env, napi_value param, const char *propertyName, uint64_t &value) +{ + napi_value jsValue = GetPropertyValueByPropertyName(env, param, propertyName, napi_bigint); + if (jsValue != nullptr) { + return UnwrapBigIntUint64FromJS2(env, jsValue, value); + } else { + return false; + } +} + +napi_value WrapFileInfo(napi_env env, const FileInfo &fileInfo) +{ + napi_value jsObject = nullptr; + napi_value jsValue = nullptr; + + NAPI_CALL(env, napi_create_object(env, &jsObject)); + + jsValue = nullptr; + jsValue = OHOS::AppExecFwk::WrapStringToJS(env, fileInfo.uri.ToString()); + SetPropertyValueByPropertyName(env, jsObject, "uri", jsValue); + + jsValue = nullptr; + jsValue = OHOS::AppExecFwk::WrapStringToJS(env, fileInfo.fileName); + SetPropertyValueByPropertyName(env, jsObject, "fileName", jsValue); + + jsValue = nullptr; + jsValue = OHOS::AppExecFwk::WrapStringToJS(env, fileInfo.mode); + SetPropertyValueByPropertyName(env, jsObject, "mode", jsValue); + + jsValue = nullptr; + jsValue = OHOS::AppExecFwk::WrapInt64ToJS(env, fileInfo.size); + SetPropertyValueByPropertyName(env, jsObject, "size", jsValue); + + jsValue = nullptr; + jsValue = OHOS::AppExecFwk::WrapInt64ToJS(env, fileInfo.mtime); + SetPropertyValueByPropertyName(env, jsObject, "mtime", jsValue); + + jsValue = nullptr; + jsValue = OHOS::AppExecFwk::WrapStringToJS(env, fileInfo.mimeType); + SetPropertyValueByPropertyName(env, jsObject, "mimeType", jsValue); + + return jsObject; +} + +bool UnwrapFileInfo(napi_env env, napi_value param, FileInfo &fileInfo) +{ + if (!IsTypeForNapiValue(env, param, napi_object)) { + return false; + } + + std::string natValueString(""); + if (OHOS::AppExecFwk::UnwrapStringByPropertyName(env, param, "uri", natValueString)) { + fileInfo.uri = Uri(natValueString); + } + + natValueString = ""; + if (OHOS::AppExecFwk::UnwrapStringByPropertyName(env, param, "fileName", natValueString)) { + fileInfo.fileName = natValueString; + } + + natValueString = ""; + if (OHOS::AppExecFwk::UnwrapStringByPropertyName(env, param, "mode", natValueString)) { + fileInfo.mode = natValueString; + } + + int64_t natValueInt64 = ERR_OK; + if (UnwrapInt64ByPropertyName(env, param, "size", natValueInt64)) { + fileInfo.size = natValueInt64; + } + + natValueInt64 = ERR_OK; + if (UnwrapInt64ByPropertyName(env, param, "mtime", natValueInt64)) { + fileInfo.mtime = natValueInt64; + } + + natValueString = ""; + if (OHOS::AppExecFwk::UnwrapStringByPropertyName(env, param, "mimeType", natValueString)) { + fileInfo.mimeType = natValueString; + } + + return true; +} + +napi_value WrapArrayFileInfoToJS(napi_env env, const std::vector &fileInfoVec) +{ + napi_value jsArray = nullptr; + napi_value jsValue = nullptr; + uint32_t index = ERR_OK; + NAPI_CALL(env, napi_create_array(env, &jsArray)); + + for (uint32_t i = 0; i < fileInfoVec.size(); i++) { + jsValue = WrapFileInfo(env, fileInfoVec[i]); + if (napi_set_element(env, jsArray, index, jsValue) == napi_ok) { + index++; + } + } + return jsArray; +} + +bool UnwrapArrayFileInfoFromJS(napi_env env, napi_value param, std::vector &fileInfoVec) +{ + uint32_t arraySize = ERR_OK; + napi_value jsValue = nullptr; + if (!IsArrayForNapiValue(env, param, arraySize)) { + return false; + } + + fileInfoVec.clear(); + for (uint32_t i = 0; i < arraySize; i++) { + jsValue = nullptr; + FileInfo fileInfo; + if (napi_get_element(env, param, i, &jsValue) != napi_ok) { + return false; + } + + if (!UnwrapFileInfo(env, jsValue, fileInfo)) { + return false; + } + + fileInfoVec.push_back(fileInfo); + } + return true; +} + +napi_value WrapDeviceInfo(napi_env env, const DeviceInfo &deviceInfo) +{ + napi_value jsObject = nullptr; + napi_value jsValue = nullptr; + + NAPI_CALL(env, napi_create_object(env, &jsObject)); + + jsValue = nullptr; + jsValue = OHOS::AppExecFwk::WrapStringToJS(env, deviceInfo.uri.ToString()); + SetPropertyValueByPropertyName(env, jsObject, "uri", jsValue); + + jsValue = nullptr; + jsValue = OHOS::AppExecFwk::WrapStringToJS(env, deviceInfo.displayName); + SetPropertyValueByPropertyName(env, jsObject, "displayName", jsValue); + + jsValue = nullptr; + jsValue = OHOS::AppExecFwk::WrapStringToJS(env, deviceInfo.deviceId); + SetPropertyValueByPropertyName(env, jsObject, "deviceId", jsValue); + + jsValue = nullptr; + jsValue = WrapUint32ToJS(env, deviceInfo.flags); + SetPropertyValueByPropertyName(env, jsObject, "flags", jsValue); + + return jsObject; +} + +bool UnwrapDeviceInfo(napi_env env, napi_value param, DeviceInfo &deviceInfo) +{ + if (!IsTypeForNapiValue(env, param, napi_object)) { + return false; + } + + std::string natValueString(""); + if (OHOS::AppExecFwk::UnwrapStringByPropertyName(env, param, "uri", natValueString)) { + deviceInfo.uri = Uri(natValueString); + } + + natValueString = ""; + if (OHOS::AppExecFwk::UnwrapStringByPropertyName(env, param, "displayName", natValueString)) { + deviceInfo.displayName = natValueString; + } + + natValueString = ""; + if (OHOS::AppExecFwk::UnwrapStringByPropertyName(env, param, "deviceId", natValueString)) { + deviceInfo.deviceId = natValueString; + } + + uint32_t natValueUint32 = ERR_OK; + if (UnwrapUint32ByPropertyName(env, param, "flags", natValueUint32)) { + deviceInfo.flags = natValueUint32; + } + return true; +} + +napi_value WrapArrayDeviceInfoToJS(napi_env env, const std::vector &deviceInfoVec) +{ + napi_value jsArray = nullptr; + napi_value jsValue = nullptr; + uint32_t index = ERR_OK; + NAPI_CALL(env, napi_create_array(env, &jsArray)); + + for (uint32_t i = 0; i < deviceInfoVec.size(); i++) { + jsValue = WrapDeviceInfo(env, deviceInfoVec[i]); + if (napi_set_element(env, jsArray, index, jsValue) == napi_ok) { + index++; + } + } + return jsArray; +} + +bool UnwrapArrayDeviceInfoFromJS(napi_env env, napi_value param, std::vector &deviceInfoVec) +{ + uint32_t arraySize = ERR_OK; + napi_value jsValue = nullptr; + + if (!IsArrayForNapiValue(env, param, arraySize)) { + return false; + } + + deviceInfoVec.clear(); + for (uint32_t i = 0; i < arraySize; i++) { + jsValue = nullptr; + DeviceInfo deviceInfo; + if (napi_get_element(env, param, i, &jsValue) != napi_ok) { + return false; + } + + if (!UnwrapDeviceInfo(env, jsValue, deviceInfo)) { + return false; + } + + deviceInfoVec.push_back(deviceInfo); + } + return true; +} +} // namespace FileAccessFwk +} // namespace OHOS