diff --git a/frameworks/innerkits/file_access/BUILD.gn b/frameworks/innerkits/file_access/BUILD.gn index 91a335f7206d6cc89aa461921636e5fc430dd86a..3a391aa6588d196c46c8c0581053b688ac1bcb5e 100644 --- a/frameworks/innerkits/file_access/BUILD.gn +++ b/frameworks/innerkits/file_access/BUILD.gn @@ -51,7 +51,11 @@ ohos_shared_library("file_access_ability_kit") { sources = [ "src/file_ext_ability.cpp", + "src/file_ext_proxy.cpp", + "src/file_ext_stub_impl.cpp", + "src/file_ext_stub.cpp", "src/js_file_ext_ability.cpp", + "src/napi_common_fileaccess.cpp", "src/file_access_helper.cpp", "src/file_ext_connection.cpp", ] diff --git a/frameworks/innerkits/file_access/include/file_ext_proxy.h b/frameworks/innerkits/file_access/include/file_ext_proxy.h new file mode 100644 index 0000000000000000000000000000000000000000..5e96325da87ba9141375654cd5c5ef799fcaf3f9 --- /dev/null +++ b/frameworks/innerkits/file_access/include/file_ext_proxy.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_EXT_PROXY_H +#define FILE_EXT_PROXY_H + +#include + +#include "file_extension_info.h" +#include "ifile_ext_base.h" + +namespace OHOS { +namespace FileAccessFwk { +class FileExtProxy : public IRemoteProxy { +public: + explicit FileExtProxy(const sptr& remote) : IRemoteProxy(remote) {} + + virtual ~FileExtProxy() {} + +private: + static inline BrokerDelegator delegator_; +}; +} // namespace FileAccessFwk +} // namespace OHOS +#endif // FILE_EXT_PROXY_H + diff --git a/frameworks/innerkits/file_access/include/file_ext_stub.h b/frameworks/innerkits/file_access/include/file_ext_stub.h new file mode 100644 index 0000000000000000000000000000000000000000..a69bc95f5a2921719f3dca2e1596ffce7926a802 --- /dev/null +++ b/frameworks/innerkits/file_access/include/file_ext_stub.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FILE_EXT_STUB_H +#define FILE_EXT_STUB_H + +#include +#include + +#include "file_extension_info.h" +#include "ifile_ext_base.h" + +namespace OHOS { +namespace FileAccessFwk { +class FileExtStub : public IRemoteStub { +public: + FileExtStub(); + ~FileExtStub(); + int OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) override; +private: + using RequestFuncType = int (FileExtStub::*)(MessageParcel &data, MessageParcel &reply); + std::map stubFuncMap_; +}; +} // namespace FileAccessFwk +} // namespace OHOS +#endif // FILE_EXT_STUB_H + diff --git a/frameworks/innerkits/file_access/include/file_ext_stub_impl.h b/frameworks/innerkits/file_access/include/file_ext_stub_impl.h new file mode 100644 index 0000000000000000000000000000000000000000..e5a63868a8b87692e50a5fac93cc0cc1c40ab66a --- /dev/null +++ b/frameworks/innerkits/file_access/include/file_ext_stub_impl.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FILE_EXT_STUB_IMPL_H +#define FILE_EXT_STUB_IMPL_H + +#include +#include "file_ext_stub.h" +#include "file_extension_info.h" +#include "file_ext_ability.h" +#include "native_engine/native_value.h" + +namespace OHOS { +namespace FileAccessFwk { +class FileExtStubImpl : public FileExtStub { +public: + explicit FileExtStubImpl(const std::shared_ptr& extension, napi_env env) + : extension_(extension) {} + + virtual ~FileExtStubImpl() {} + +private: + std::shared_ptr GetOwner(); + +private: + std::shared_ptr extension_; +}; +} // namespace FileAccessFwk +} // namespace OHOS +#endif // FILE_EXT_STUB_IMPL_H diff --git a/frameworks/innerkits/file_access/include/file_extension_info.h b/frameworks/innerkits/file_access/include/file_extension_info.h new file mode 100644 index 0000000000000000000000000000000000000000..9cd7faaed4bad592e3a1240c2a59133c22eae884 --- /dev/null +++ b/frameworks/innerkits/file_access/include/file_extension_info.h @@ -0,0 +1,146 @@ +/* + * 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_EXTENSION_INFO_H +#define FILE_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 mimiType; + + 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(); + mimiType = 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(mimiType)) { + 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_EXTENSION_INFO_H \ No newline at end of file diff --git a/frameworks/innerkits/file_access/include/ifile_ext_base.h b/frameworks/innerkits/file_access/include/ifile_ext_base.h index fffafa449e07fbaffae27a2052b87c4b1c9fde5d..aef51e0000ea1477fc8293abe1a37d25a80989c3 100644 --- a/frameworks/innerkits/file_access/include/ifile_ext_base.h +++ b/frameworks/innerkits/file_access/include/ifile_ext_base.h @@ -30,4 +30,4 @@ public: }; } // namespace FileAccessFwk } // namespace OHOS -#endif // I_FILE_EXT_BASE_H \ No newline at end of file +#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..9ac9d64ed1acdd31e8f4cd42c57efcb2ad0a371f --- /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_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_ext_proxy.cpp b/frameworks/innerkits/file_access/src/file_ext_proxy.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aa411716c21b06e9f41ba07e82e45d5537669ea8 --- /dev/null +++ b/frameworks/innerkits/file_access/src/file_ext_proxy.cpp @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "file_ext_proxy.h" + +#include "hilog_wrapper.h" + +namespace OHOS { +namespace FileAccessFwk { +} // namespace FileAccessFwk +} // namespace OHOS diff --git a/frameworks/innerkits/file_access/src/file_ext_stub.cpp b/frameworks/innerkits/file_access/src/file_ext_stub.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6dffcb94af4f169da4dfb1f785f32fb43bdcdd1f --- /dev/null +++ b/frameworks/innerkits/file_access/src/file_ext_stub.cpp @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "file_ext_stub.h" + +#include "hilog_wrapper.h" + +namespace OHOS { +namespace FileAccessFwk { +FileExtStub::FileExtStub() +{ + HILOG_INFO("%{public}s begin.", __func__); + HILOG_INFO("%{public}s end.", __func__); +} + +FileExtStub::~FileExtStub() +{ + HILOG_INFO("%{public}s begin.", __func__); + HILOG_INFO("%{public}s end.", __func__); + stubFuncMap_.clear(); +} + +int FileExtStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, + MessageOption& option) +{ + HILOG_INFO("%{public}s Received stub message: %{public}d", __func__, code); + std::u16string descriptor = FileExtStub::GetDescriptor(); + std::u16string remoteDescriptor = data.ReadInterfaceToken(); + if (descriptor != remoteDescriptor) { + HILOG_INFO("%{public}s local descriptor is not equal to remote", __func__); + return ERR_INVALID_STATE; + } + + const auto &itFunc = stubFuncMap_.find(code); + if (itFunc != stubFuncMap_.end()) { + return (this->*(itFunc->second))(data, reply); + } + + HILOG_INFO("%{public}s remote request unhandled: %{public}d", __func__, code); + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); +} +} // namespace FileAccessFwk +} // namespace OHOS diff --git a/frameworks/innerkits/file_access/src/file_ext_stub_impl.cpp b/frameworks/innerkits/file_access/src/file_ext_stub_impl.cpp new file mode 100644 index 0000000000000000000000000000000000000000..50a76fd24a9b17a435f1c49c1554332e63c370f4 --- /dev/null +++ b/frameworks/innerkits/file_access/src/file_ext_stub_impl.cpp @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "file_ext_stub_impl.h" + +#include "hilog_wrapper.h" + +namespace OHOS { +namespace FileAccessFwk { +std::shared_ptr FileExtStubImpl::GetOwner() +{ + HILOG_INFO("%{public}s begin.", __func__); + HILOG_INFO("%{public}s end.", __func__); + return extension_; +} +} // namespace FileAccessFwk +} // namespace OHOS 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..3714a061a8133b00011eb4055a7fac856eebc7be --- /dev/null +++ b/frameworks/innerkits/file_access/src/napi_common_fileaccess.cpp @@ -0,0 +1,362 @@ +/* + * 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 "napi_common_fileaccess.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) +{ + HILOG_INFO("%{public}s,called begin", __func__); + bool result = false; + if (napi_get_value_uint32(env, param, &value) == napi_ok) { + result = true; + } + HILOG_INFO("%{public}s,called end, value: %{public}u", __func__, value); + return result; +} + +bool UnwrapUint32ByPropertyName(napi_env env, napi_value param, const char *propertyName, uint32_t &value) +{ + HILOG_INFO("%{public}s,called begin", __func__); + napi_value jsValue = GetPropertyValueByPropertyName(env, param, propertyName, napi_number); + if (jsValue != nullptr) { + HILOG_INFO("%{public}s,called end.", __func__); + return UnwrapUint32FromJS2(env, jsValue, value); + } else { + HILOG_INFO("%{public}s,called faild.", __func__); + 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) +{ + HILOG_INFO("%{public}s,called begin", __func__); + bool lossless = true; + uint64_t value = defaultValue; + if (napi_get_value_bigint_uint64(env, param, &value, &lossless) == napi_ok) { + HILOG_INFO("%{public}s,called value: %{public}llu.", __func__, defaultValue); + return value; + } else { + HILOG_INFO("%{public}s,called end with faild.", __func__); + return defaultValue; + } +} + +bool UnwrapBigIntUint64FromJS2(napi_env env, napi_value param, uint64_t &defaultValue) +{ + HILOG_INFO("%{public}s,called begin", __func__); + bool lossless = true; + if (napi_get_value_bigint_uint64(env, param, &defaultValue, &lossless) == napi_ok) { + HILOG_INFO("%{public}s,called value: %{public}llu.", __func__, defaultValue); + return true; + } else { + HILOG_INFO("%{public}s,called end with faild.", __func__); + return false; + } +} + +bool UnwrapBigIntUint64ByPropertyName(napi_env env, napi_value param, const char *propertyName, uint64_t &value) +{ + HILOG_INFO("%{public}s,called begin", __func__); + napi_value jsValue = GetPropertyValueByPropertyName(env, param, propertyName, napi_bigint); + if (jsValue != nullptr) { + HILOG_INFO("%{public}s,called end.", __func__); + return UnwrapBigIntUint64FromJS2(env, jsValue, value); + } else { + HILOG_INFO("%{public}s,called faild.", __func__); + return false; + } +} + +napi_value WrapFileInfo(napi_env env, const FileInfo &fileInfo) +{ + HILOG_INFO("%{public}s,called begin", __func__); + HILOG_INFO("%{public}s,uri: %{public}s", __func__, fileInfo.uri.ToString().c_str()); + HILOG_INFO("%{public}s,fileName: %{public}s", __func__, fileInfo.fileName.c_str()); + HILOG_INFO("%{public}s,mode: %{public}s", __func__, fileInfo.mode.c_str()); + HILOG_INFO("%{public}s,size: %{public}lld", __func__, fileInfo.size); + HILOG_INFO("%{public}s,mtime: %{public}lld", __func__, fileInfo.mtime); + HILOG_INFO("%{public}s,mimiType: %{public}s", __func__, fileInfo.mimiType.c_str()); + 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.mimiType); + SetPropertyValueByPropertyName(env, jsObject, "mimiType", jsValue); + + HILOG_INFO("%{public}s,called end", __func__); + return jsObject; +} + +bool UnwrapFileInfo(napi_env env, napi_value param, FileInfo &fileInfo) +{ + HILOG_INFO("%{public}s,called begin", __func__); + if (!IsTypeForNapiValue(env, param, napi_object)) { + HILOG_INFO("%{public}s called. Params is invalid.", __func__); + 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 = 0; + if (UnwrapInt64ByPropertyName(env, param, "size", natValueInt64)) { + fileInfo.size = natValueInt64; + } + + natValueInt64 = 0; + if (UnwrapInt64ByPropertyName(env, param, "mtime", natValueInt64)) { + fileInfo.mtime = natValueInt64; + } + + natValueString = ""; + if (OHOS::AppExecFwk::UnwrapStringByPropertyName(env, param, "mimiType", natValueString)) { + fileInfo.mimiType = natValueString; + } + + HILOG_INFO("%{public}s,uri: %{public}s", __func__, fileInfo.uri.ToString().c_str()); + HILOG_INFO("%{public}s,fileName: %{public}s", __func__, fileInfo.fileName.c_str()); + HILOG_INFO("%{public}s,mode: %{public}s", __func__, fileInfo.mode.c_str()); + HILOG_INFO("%{public}s,size: %{public}lld", __func__, fileInfo.size); + HILOG_INFO("%{public}s,mtime: %{public}lld", __func__, fileInfo.mtime); + HILOG_INFO("%{public}s,mimiType: %{public}s", __func__, fileInfo.mimiType.c_str()); + HILOG_INFO("%{public}s,end", __func__); + return true; +} + +napi_value WrapArrayFileInfoToJS(napi_env env, const std::vector &fileInfoVec) +{ + napi_value jsArray = nullptr; + napi_value jsValue = nullptr; + uint32_t index = 0; + + 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 = 0; + 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) +{ + HILOG_INFO("%{public}s,called begin", __func__); + HILOG_INFO("%{public}s,uri: %{public}s", __func__, deviceInfo.uri.ToString().c_str()); + HILOG_INFO("%{public}s,displayName: %{public}s", __func__, deviceInfo.displayName.c_str()); + HILOG_INFO("%{public}s,deviceId: %{public}s", __func__, deviceInfo.deviceId.c_str()); + HILOG_INFO("%{public}s,flags: %{public}u", __func__, deviceInfo.flags); + 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); + + HILOG_INFO("%{public}s,called end", __func__); + return jsObject; +} + +bool UnwrapDeviceInfo(napi_env env, napi_value param, DeviceInfo &deviceInfo) +{ + HILOG_INFO("%{public}s,called begin", __func__); + if (!IsTypeForNapiValue(env, param, napi_object)) { + HILOG_INFO("%{public}s called. Params is invalid.", __func__); + 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 = 0; + if (UnwrapUint32ByPropertyName(env, param, "flags", natValueUint32)) { + HILOG_INFO("%{public}s,flags: %{public}zu", __func__, natValueUint32); + deviceInfo.flags = natValueUint32; + } + HILOG_INFO("%{public}s,uri: %{public}s", __func__, deviceInfo.uri.ToString().c_str()); + HILOG_INFO("%{public}s,displayName: %{public}s", __func__, deviceInfo.displayName.c_str()); + HILOG_INFO("%{public}s,deviceId: %{public}s", __func__, deviceInfo.deviceId.c_str()); + HILOG_INFO("%{public}s,flags: %{public}u", __func__, deviceInfo.flags); + HILOG_INFO("%{public}s,end", __func__); + return true; +} + +napi_value WrapArrayDeviceInfoToJS(napi_env env, const std::vector &deviceInfoVec) +{ + napi_value jsArray = nullptr; + napi_value jsValue = nullptr; + uint32_t index = 0; + + 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 = 0; + 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