diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 9aeefbad5dc9a6ae6841fc746aac0f6ff8c95dd9..47ecdfc165742d9a6667dd99b2246e59e72931d1 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -80,6 +80,7 @@ ohos_shared_library("fileuri") { include_dirs = [ ".", "../../common/include", + "${app_file_service_path}/interfaces/innerkits/native/file_uri/include", ] sources = [ @@ -87,9 +88,15 @@ ohos_shared_library("fileuri") { "../../common/src/sandbox_helper.cpp", "file_uri/file_uri_n_exporter.cpp", "file_uri/get_uri_from_path.cpp", + "file_uri/module.cpp", + "file_uri/prop_n_exporter.cpp", ] + deps = + [ "${app_file_service_path}/interfaces/innerkits/native:fileuri_native" ] + external_deps = [ + "ability_base:zuri", "ability_runtime:abilitykit_native", "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", @@ -102,6 +109,8 @@ ohos_shared_library("fileuri") { "samgr:samgr_proxy", ] + use_exceptions = true + relative_install_dir = "module/file" part_name = "app_file_service" diff --git a/interfaces/kits/js/file_uri/file_uri_entity.h b/interfaces/kits/js/file_uri/file_uri_entity.h new file mode 100644 index 0000000000000000000000000000000000000000..64e7a4c1fb52392928abd51c75baca6aab89eea5 --- /dev/null +++ b/interfaces/kits/js/file_uri/file_uri_entity.h @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023 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 INTERFACES_KITS_JS_FILE_URI_FILE_URI_ENTITY_H +#define INTERFACES_KITS_JS_FILE_URI_FILE_URI_ENTITY_H + +#include "file_uri.h" + +namespace OHOS::AppFileService::ModuleFileUri { +struct FileUriEntity { + FileUri fileUri_; + explicit FileUriEntity(const std::string &uriOrPath): fileUri_(FileUri(uriOrPath)){}; +}; +} // OHOS::AppFileService::ModuleFileUri +#endif // INTERFACES_KITS_JS_FILE_URI_FILE_URI_ENTITY_H \ No newline at end of file diff --git a/interfaces/kits/js/file_uri/file_uri_n_exporter.cpp b/interfaces/kits/js/file_uri/file_uri_n_exporter.cpp index 81118b23dae3260111f8b56ca2f87da6c0650e50..d9dec0e39463cc58097a224faf90669e42615f3c 100644 --- a/interfaces/kits/js/file_uri/file_uri_n_exporter.cpp +++ b/interfaces/kits/js/file_uri/file_uri_n_exporter.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 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 @@ -13,37 +13,130 @@ * limitations under the License. */ #include "file_uri_n_exporter.h" -#include "get_uri_from_path.h" + +#include "file_uri_entity.h" +#include "file_utils.h" +#include "log.h" namespace OHOS { namespace AppFileService { namespace ModuleFileUri { -/*********************************************** - * Module export and register - ***********************************************/ -napi_value FileUriExport(napi_env env, napi_value exports) +using namespace std; +using namespace FileManagement; +using namespace FileManagement::LibN; + +napi_value FileUriNExporter::Constructor(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE)) { + LOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + auto [succPath, path, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!succPath) { + LOGE("Failed to get path"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + auto fileuriEntity = CreateUniquePtr(string(path.get())); + if (fileuriEntity == nullptr) { + LOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } + if (!NClass::SetEntityFor(env, funcArg.GetThisVar(), move(fileuriEntity))) { + LOGE("Failed to set file entity"); + NError(EIO).ThrowErr(env); + return nullptr; + } + return funcArg.GetThisVar(); +} + + +napi_value FileUriNExporter::UriToString(napi_env env, napi_callback_info info) { - static napi_property_descriptor desc[] = { - DECLARE_NAPI_FUNCTION("getUriFromPath", GetUriFromPath::Sync), + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + LOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + auto fileuriEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (!fileuriEntity) { + LOGE("Failed to get file entity"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + return NVal::CreateUTF8String(env, fileuriEntity->fileUri_.ToString()).val_; +} + +napi_value FileUriNExporter::GetFileUriName(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + LOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + auto fileuriEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (!fileuriEntity) { + LOGE("Failed to get file entity"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + return NVal::CreateUTF8String(env, fileuriEntity->fileUri_.GetName()).val_; +} + +napi_value FileUriNExporter::GetFileUriPath(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + LOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + auto fileuriEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (!fileuriEntity) { + LOGE("Failed to get file entity"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + return NVal::CreateUTF8String(env, fileuriEntity->fileUri_.GetPath()).val_; +} + +bool FileUriNExporter::Export() +{ + vector props = { + NVal::DeclareNapiFunction("toString", UriToString), + NVal::DeclareNapiGetter("name", GetFileUriName), + NVal::DeclareNapiGetter("path", GetFileUriPath), }; - napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); - return exports; + + auto [succ, classValue] = NClass::DefineClass(exports_.env_, className, Constructor, std::move(props)); + if (!succ) { + LOGE("Failed to define class"); + NError(EIO).ThrowErr(exports_.env_); + return false; + } + succ = NClass::SaveClass(exports_.env_, className, classValue); + if (!succ) { + LOGE("Failed to save class"); + NError(EIO).ThrowErr(exports_.env_); + return false; + } + + return exports_.AddProp(className, classValue); } -static napi_module _module = { - .nm_version = 1, - .nm_flags = 0, - .nm_filename = nullptr, - .nm_register_func = FileUriExport, - .nm_modname = "file.fileuri", - .nm_priv = ((void *)0), - .reserved = {0} -}; - -extern "C" __attribute__((constructor)) void RegisterModule(void) +string FileUriNExporter::GetClassName() { - napi_module_register(&_module); + return FileUriNExporter::className; } + +FileUriNExporter::FileUriNExporter(napi_env env, napi_value exports) : NExporter(env, exports) {} + +FileUriNExporter::~FileUriNExporter() {} } // namespace ModuleFileUri } // namespace AppFileService } // namespace OHOS diff --git a/interfaces/kits/js/file_uri/file_uri_n_exporter.h b/interfaces/kits/js/file_uri/file_uri_n_exporter.h index b37a3d9487c88434e4adab1de874c38dd02b5ff5..4172c08c5f2942a91c90577ba895bcd7a8478609 100644 --- a/interfaces/kits/js/file_uri/file_uri_n_exporter.h +++ b/interfaces/kits/js/file_uri/file_uri_n_exporter.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 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 @@ -12,16 +12,30 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef FILE_URI_N_EXPOTER_H -#define FILE_URI_N_EXPOTER_H +#ifndef INTERFACES_KITS_JS_FILE_URI_FILE_URI_N_EXPOTER_H +#define INTERFACES_KITS_JS_FILE_URI_FILE_URI_N_EXPOTER_H #include "filemgmt_libn.h" namespace OHOS { namespace AppFileService { namespace ModuleFileUri { - napi_value FileUriExport(napi_env env, napi_value exports); +class FileUriNExporter final : public FileManagement::LibN::NExporter { +public: + inline static const std::string className = "FileUri"; + + bool Export() override; + std::string GetClassName() override; + + static napi_value Constructor(napi_env env, napi_callback_info cbinfo); + static napi_value UriToString(napi_env env, napi_callback_info info); + static napi_value GetFileUriName(napi_env env, napi_callback_info info); + static napi_value GetFileUriPath(napi_env env, napi_callback_info info); + + FileUriNExporter(napi_env env, napi_value exports); + ~FileUriNExporter() override; +}; } // namespace ModuleFileUri } // namespace AppFileService } // namespace OHOS -#endif // FILE_URI_N_EXPOTER_H +#endif // INTERFACES_KITS_JS_FILE_URI_FILE_URI_N_EXPOTER_H diff --git a/interfaces/kits/js/file_uri/module.cpp b/interfaces/kits/js/file_uri/module.cpp new file mode 100644 index 0000000000000000000000000000000000000000..378ed9f653762b30607bc73d4fcb32effb2ca6f6 --- /dev/null +++ b/interfaces/kits/js/file_uri/module.cpp @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2023 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 +#include + +#include "filemgmt_libn.h" +#include "file_uri_n_exporter.h" +#include "log.h" +#include "prop_n_exporter.h" + +namespace OHOS::AppFileService::ModuleFileUri { +using namespace std; +using namespace FileManagement::LibN; + +static napi_value Export(napi_env env, napi_value exports) +{ + std::vector> products; + products.emplace_back(make_unique(env, exports)); + products.emplace_back(make_unique(env, exports)); + for (auto &&product : products) { + if (!product->Export()) { + LOGE("INNER BUG. Failed to export class %{public}s for module backup", product->GetClassName().c_str()); + return nullptr; + } else { + LOGI("Class %{public}s for module fileio has been exported", product->GetClassName().c_str()); + } + } + return exports; +} + +static napi_module _module = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Export, + .nm_modname = "file.fileuri", + .nm_priv = ((void *)0), + .reserved = {0} +}; + +extern "C" __attribute__((constructor)) void RegisterModule(void) +{ + napi_module_register(&_module); +} +} // namespace OHOS::AppFileService::ModuleFileUri \ No newline at end of file diff --git a/interfaces/kits/js/file_uri/prop_n_exporter.cpp b/interfaces/kits/js/file_uri/prop_n_exporter.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ef5864e2c764f5fa78a673fe04084c3cf4319362 --- /dev/null +++ b/interfaces/kits/js/file_uri/prop_n_exporter.cpp @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 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 "prop_n_exporter.h" + +#include "get_uri_from_path.h" + +namespace OHOS::AppFileService::ModuleFileUri { +using namespace std; +using namespace FileManagement::LibN; + +bool PropNExporter::Export() +{ + return exports_.AddProp({ + NVal::DeclareNapiFunction("getUriFromPath", GetUriFromPath::Sync), + }); +} + +string PropNExporter::GetClassName() +{ + return PropNExporter::className; +} + +PropNExporter::PropNExporter(napi_env env, napi_value exports) : NExporter(env, exports) {} + +PropNExporter::~PropNExporter() {} +} // namespace OHOS::AppFileService::ModuleFileUri \ No newline at end of file diff --git a/interfaces/kits/js/file_uri/prop_n_exporter.h b/interfaces/kits/js/file_uri/prop_n_exporter.h new file mode 100644 index 0000000000000000000000000000000000000000..886fb7329312f7c4762590b871746a8959438c70 --- /dev/null +++ b/interfaces/kits/js/file_uri/prop_n_exporter.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 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 INTERFACES_KITS_JS_FILE_URI_PROP_N_EXPORTER_H +#define INTERFACES_KITS_JS_FILE_URI_PROP_N_EXPORTER_H + +#include + +#include "filemgmt_libn.h" + +namespace OHOS::AppFileService::ModuleFileUri { +class PropNExporter final : public FileManagement::LibN::NExporter { +public: + inline static const std::string className = "GetUriFromPath"; + + bool Export() override; + std::string GetClassName() override; + + PropNExporter(napi_env env, napi_value exports); + ~PropNExporter() override; +}; +} // namespace OHOS::AppFileService::ModuleFileUri +#endif // INTERFACES_KITS_JS_FILE_URI_PROP_N_EXPORTER_H \ No newline at end of file