diff --git a/LICENSE b/LICENSE
new file mode 100644
index 00000000..4947287f
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,177 @@
+
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
\ No newline at end of file
diff --git a/README.md b/README.md
index 1b2468c0..12b248b0 100644
--- a/README.md
+++ b/README.md
@@ -1,39 +1,69 @@
-# storage_user_file_manger
+# File Mangaer Service公共文件管理服务
-#### 介绍
-{**以下是 Gitee 平台说明,您可以替换此简介**
-Gitee 是 OSCHINA 推出的基于 Git 的代码托管平台(同时支持 SVN)。专为开发者提供稳定、高效、安全的云端软件开发协作平台
-无论是个人、团队、或是企业,都能够用 Gitee 实现代码托管、项目管理、协作开发。企业项目请看 [https://gitee.com/enterprises](https://gitee.com/enterprises)}
-
-#### 软件架构
-软件架构说明
+- [简介](#section1158716411637)
+- [目录](#section161941989596)
+- [使用说明](#usage-guidelines)
+ - [创建文件](#get-audioasset)
+- [相关仓](#section1533973044317)
-#### 安装教程
+## 简介
-1. xxxx
-2. xxxx
-3. xxxx
+**File Mangaer Service**公共文件管理服务仓库提供了公共文件管理接口。File Mangaer Service接口暂不对外部应用开放, 仅内部使用。
-#### 使用说明
-
-1. xxxx
-2. xxxx
-3. xxxx
-
-#### 参与贡献
-
-1. Fork 本仓库
-2. 新建 Feat_xxx 分支
-3. 提交代码
-4. 新建 Pull Request
+支持能力列举如下:
+- 公共文件路径下媒体文件查询,创建
+- 公共文件路径下文档文件查询、创建
-#### 特技
+## 目录
-1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md
-2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com)
-3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目
-4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目
-5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help)
-6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)
+仓目录结构如下:
+```
+/foundation/storage/user_file_manger # fms组件代码
+├── serivce # 框架代码
+│ ├── etc # 内部接口实现
+│ ├── src # 内部接口实现
+├── interfaces # 接口代码
+│ ├── innerkits # 内部 Native 接口
+│ └── kits # 外部 JS 接口
+├── LICENSE # 证书文件
+├── ohos.build # 编译文件
+├── sa_profile # 服务配置文件
+└── services # 服务实现
+```
+
+## 使用说明
+### 创建文件
+1.
+ ```
+ 接口流程
+ 应用通过接口让媒体库创建文件,返回文件uri, 应用自己通过openfile打开uri,获取fd进行文件操作。
+
+ // 应用A
+ // 拉起filepicker 获取 uri
+ import media_library from '@ohos.media_library'
+ fd = media_library.openfile(uri)
+ fileio.write(fd, data)
+ fileio.close()
+
+ // file picker 流程
+ // media_path通过filepicker获取待保存目录uri
+ import filemanager from '@ohos.filemanager'
+ let media_path = "xxxx"
+ let name = "xxxx"
+ filemanager.createFile(name,media_path)
+ .then((uri) => {
+ // success
+ })
+ .catch(function(err) {
+ //类型错误,重名...
+ // fail
+ })
+
+ // FMS
+ mediaLib.createAsset(name)
+ return fileuri
+ ```
+
+## 相关仓
diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn
new file mode 100644
index 00000000..607a7524
--- /dev/null
+++ b/interfaces/kits/js/BUILD.gn
@@ -0,0 +1,67 @@
+# Copyright (c) 2021 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.
+
+import("//build/ohos.gni")
+FMS_BASE_DIR = "//foundation/storage/services"
+
+ohos_shared_library("filemanager") {
+ subsystem_name = "storage"
+ part_name = "filemanager"
+
+ relative_install_dir = "module"
+
+ include_dirs = [
+ # "$BASE_DIR/napi",
+ "//third_party/node/src",
+ "//foundation/ace/napi/interfaces/kits",
+ "//utils/native/base/include",
+ "//third_party/libuv/include",
+ "$FMS_BASE_DIR/include",
+ "$FMS_BASE_DIR/src/client",
+ "$FMS_BASE_DIR/src/server",
+ "$FMS_BASE_DIR/src/fileoper",
+ ]
+
+ sources = [
+ "src/common/napi/n_async/n_async_work_callback.cpp",
+ "src/common/napi/n_async/n_async_work_promise.cpp",
+ "src/common/napi/n_async/n_ref.cpp",
+ "src/common/napi/n_class.cpp",
+ "src/common/napi/n_func_arg.cpp",
+ "src/common/napi/n_val.cpp",
+ "src/common/uni_error.cpp",
+ "src/module.cpp",
+ "src/fms_napi.cpp",
+ ]
+
+ deps = [
+ "//utils/native/base:utils",
+ "$FMS_BASE_DIR:fms_server",
+ "//foundation/ace/napi:ace_napi",
+ ]
+ cflags = []
+ if (target_cpu == "arm") {
+ cflags += [ "-DBINDER_IPC_32BIT" ]
+ }
+ external_deps = [
+ "hiviewdfx_hilog_native:libhilog",
+ "ipc:ipc_core",
+ "safwk:system_ability_fwk",
+ "samgr_standard:samgr_proxy",
+ ]
+}
+
+
+
+
+
diff --git a/interfaces/kits/js/src/common/napi/n_async/n_async_context.h b/interfaces/kits/js/src/common/napi/n_async/n_async_context.h
new file mode 100644
index 00000000..67dcdde7
--- /dev/null
+++ b/interfaces/kits/js/src/common/napi/n_async/n_async_context.h
@@ -0,0 +1,69 @@
+/*
+ * Copyright (c) 2021 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.
+ */
+
+#pragma once
+
+#include
+#include
+
+#include "../../uni_error.h"
+#include "../n_val.h"
+#include "../uni_header.h"
+#include "n_ref.h"
+
+namespace OHOS {
+namespace DistributedFS {
+typedef std::function NContextCBExec;
+typedef std::function NContextCBComplete;
+
+class NAsyncContext {
+public:
+ UniError err_;
+ NVal res_;
+ NContextCBExec cbExec_;
+ NContextCBComplete cbComplete_;
+ napi_async_work awork_;
+ NRef thisPtr_;
+
+ explicit NAsyncContext(NVal thisPtr) : thisPtr_(thisPtr) {}
+ ~NAsyncContext() = default;
+};
+
+class NAsyncContextPromise : public NAsyncContext {
+public:
+ napi_deferred deferred_;
+ explicit NAsyncContextPromise(NVal thisPtr) : NAsyncContext(thisPtr) {}
+ ~NAsyncContextPromise() = default;
+};
+
+class NAsyncContextCallback : public NAsyncContext {
+public:
+ NRef cb_;
+ NAsyncContextCallback(NVal thisPtr, NVal cb) : NAsyncContext(thisPtr), cb_(cb) {}
+ ~NAsyncContextCallback() = default;
+};
+
+class NAsyncContextLegacy : public NAsyncContext {
+public:
+ NRef cbSucc_;
+ NRef cbFail_;
+ NRef cbFinal_;
+ NAsyncContextLegacy(NVal thisPtr, NVal cbSucc, NVal cbFail, NVal cbFinal)
+ : NAsyncContext(thisPtr), cbSucc_(cbSucc), cbFail_(cbFail), cbFinal_(cbFinal)
+ {}
+ ~NAsyncContextLegacy() = default;
+};
+} // namespace DistributedFS
+} // namespace OHOS
\ No newline at end of file
diff --git a/interfaces/kits/js/src/common/napi/n_async/n_async_work_callback.cpp b/interfaces/kits/js/src/common/napi/n_async/n_async_work_callback.cpp
new file mode 100644
index 00000000..d9c05c02
--- /dev/null
+++ b/interfaces/kits/js/src/common/napi/n_async/n_async_work_callback.cpp
@@ -0,0 +1,103 @@
+/*
+ * Copyright (c) 2021 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 "n_async_work_callback.h"
+
+#include
+#include
+
+#include "log.h"
+
+namespace OHOS {
+namespace DistributedFS {
+using namespace std;
+
+NAsyncWorkCallback::NAsyncWorkCallback(napi_env env, NVal thisPtr, NVal cb) : NAsyncWorkFactory(env)
+{
+ ctx_ = new NAsyncContextCallback(thisPtr, cb);
+}
+NAsyncWorkCallback::NAsyncWorkCallback(const NAsyncWorkCallback &in, napi_env env) : NAsyncWorkFactory(env)
+{
+ delete ctx_;
+ this->ctx_ = new NAsyncContextCallback(in.ctx_->thisPtr_.Deref(env), in.ctx_->cb_.Deref(env));
+}
+
+static void CallbackExecute(napi_env env, void *data)
+{
+ auto ctx = static_cast(data);
+ if (ctx->cbExec_) {
+ ctx->err_ = ctx->cbExec_(env);
+ }
+}
+static void CallbackComplete(napi_env env, napi_status status, void *data)
+{
+ napi_handle_scope scope = nullptr;
+ napi_open_handle_scope(env, &scope);
+ auto ctx = static_cast(data);
+ if (ctx->cbComplete_) {
+ ctx->res_ = ctx->cbComplete_(env, ctx->err_);
+ ctx->cbComplete_ = nullptr;
+ }
+
+ vector argv;
+ if (!ctx->res_.TypeIsError(true)) {
+ argv = { UniError(ERRNO_NOERR).GetNapiErr(env), ctx->res_.val_ };
+ } else {
+ argv = { ctx->res_.val_ };
+ }
+
+ napi_value global = nullptr;
+ napi_value callback = ctx->cb_.Deref(env).val_;
+ napi_value tmp = nullptr;
+ napi_get_global(env, &global);
+ napi_status stat = napi_call_function(env, global, callback, argv.size(), argv.data(), &tmp);
+ if (stat != napi_ok) {
+ ERR_LOG("Failed to call function for %{public}d", stat);
+ }
+ napi_close_handle_scope(env, scope);
+ napi_delete_async_work(env, ctx->awork_);
+ delete ctx;
+}
+
+NVal NAsyncWorkCallback::Schedule(string procedureName, NContextCBExec cbExec, NContextCBComplete cbComplete)
+{
+ if (!ctx_->cb_ || !ctx_->cb_.Deref(env_).TypeIs(napi_function)) {
+ UniError(EINVAL).ThrowErr(env_, "The callback shall be a funciton");
+ return NVal();
+ }
+
+ ctx_->cbExec_ = move(cbExec);
+ ctx_->cbComplete_ = move(cbComplete);
+
+ napi_value resource = NVal::CreateUTF8String(env_, procedureName).val_;
+
+ napi_status status =
+ napi_create_async_work(env_, nullptr, resource, CallbackExecute, CallbackComplete, ctx_, &ctx_->awork_);
+ if (status != napi_ok) {
+ ERR_LOG("INNER BUG. Failed to create async work for %{public}d", status);
+ return NVal();
+ }
+
+ status = napi_queue_async_work(env_, ctx_->awork_);
+ if (status != napi_ok) {
+ ERR_LOG("INNER BUG. Failed to queue async work for %{public}d", status);
+ return NVal();
+ }
+
+ ctx_ = nullptr; // The ownership of ctx_ has been transfered
+ return NVal::CreateUndefined(env_);
+}
+} // namespace DistributedFS
+} // namespace OHOS
diff --git a/interfaces/kits/js/src/common/napi/n_async/n_async_work_callback.h b/interfaces/kits/js/src/common/napi/n_async/n_async_work_callback.h
new file mode 100644
index 00000000..a700a4ef
--- /dev/null
+++ b/interfaces/kits/js/src/common/napi/n_async/n_async_work_callback.h
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2021 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.
+ */
+
+#pragma once
+
+#include
+#include
+#include
+
+#include "n_async_work_factory.h"
+
+namespace OHOS {
+namespace DistributedFS {
+class NAsyncWorkCallback : public NAsyncWorkFactory {
+public:
+ NAsyncWorkCallback(napi_env env, NVal thisPtr, NVal cb);
+ ~NAsyncWorkCallback() = default;
+ NAsyncWorkCallback(const NAsyncWorkCallback &in, napi_env env);
+ NVal Schedule(std::string procedureName, NContextCBExec cbExec, NContextCBComplete cbComplete) final;
+
+private:
+ NAsyncContextCallback *ctx_ = nullptr;
+};
+} // namespace DistributedFS
+} // namespace OHOS
\ No newline at end of file
diff --git a/interfaces/kits/js/src/common/napi/n_async/n_async_work_factory.h b/interfaces/kits/js/src/common/napi/n_async/n_async_work_factory.h
new file mode 100644
index 00000000..bd993e5d
--- /dev/null
+++ b/interfaces/kits/js/src/common/napi/n_async/n_async_work_factory.h
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2021 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.
+ */
+
+#pragma once
+
+#include
+#include
+#include
+
+#include "n_async_context.h"
+
+namespace OHOS {
+namespace DistributedFS {
+class NAsyncWorkFactory {
+public:
+ explicit NAsyncWorkFactory(napi_env env) : env_(env) {}
+ ~NAsyncWorkFactory() = default;
+ virtual NVal Schedule(std::string procedureName, NContextCBExec cbExec, NContextCBComplete cbComplete) = 0;
+
+ napi_env env_ = nullptr;
+};
+} // namespace DistributedFS
+} // namespace OHOS
\ No newline at end of file
diff --git a/interfaces/kits/js/src/common/napi/n_async/n_async_work_promise.cpp b/interfaces/kits/js/src/common/napi/n_async/n_async_work_promise.cpp
new file mode 100644
index 00000000..727f8269
--- /dev/null
+++ b/interfaces/kits/js/src/common/napi/n_async/n_async_work_promise.cpp
@@ -0,0 +1,98 @@
+/*
+ * Copyright (c) 2021 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 "n_async_work_promise.h"
+
+#include
+#include
+#include
+
+#include "log.h"
+
+namespace OHOS {
+namespace DistributedFS {
+using namespace std;
+
+NAsyncWorkPromise::NAsyncWorkPromise(napi_env env, NVal thisPtr) : NAsyncWorkFactory(env)
+{
+ ctx_ = new NAsyncContextPromise(thisPtr);
+}
+NAsyncWorkPromise::NAsyncWorkPromise(const NAsyncWorkPromise &in, napi_env env) : NAsyncWorkFactory(env)
+{
+ delete ctx_;
+ ctx_ = new NAsyncContextPromise(in.ctx_->thisPtr_.Deref(env));
+}
+
+static void PromiseOnExec(napi_env env, void *data)
+{
+ auto ctx = static_cast(data);
+ if (ctx->cbExec_) {
+ ctx->err_ = ctx->cbExec_(env);
+ }
+}
+static void PromiseOnComplete(napi_env env, napi_status status, void *data)
+{
+ auto ctx = static_cast(data);
+ if (ctx->cbComplete_) {
+ ctx->res_ = ctx->cbComplete_(env, ctx->err_);
+ }
+ if (!ctx->res_.TypeIsError(true)) {
+ napi_status status = napi_resolve_deferred(env, ctx->deferred_, ctx->res_.val_);
+ if (status != napi_ok) {
+ ERR_LOG("Internal BUG, cannot resolve promise for %{public}d", status);
+ }
+ } else {
+ napi_status status = napi_reject_deferred(env, ctx->deferred_, ctx->res_.val_);
+ if (status != napi_ok) {
+ ERR_LOG("Internal BUG, cannot reject promise for %{public}d", status);
+ }
+ }
+ ctx->deferred_ = nullptr;
+ napi_delete_async_work(env, ctx->awork_);
+ delete ctx;
+}
+
+
+NVal NAsyncWorkPromise::Schedule(string procedureName, NContextCBExec cbExec, NContextCBComplete cbComplete)
+{
+ ctx_->cbExec_ = move(cbExec);
+ ctx_->cbComplete_ = move(cbComplete);
+
+ napi_status status;
+ napi_value result = nullptr;
+ status = napi_create_promise(env_, &ctx_->deferred_, &result);
+ if (status != napi_ok) {
+ ERR_LOG("INNER BUG. Cannot create promise for %{public}d", status);
+ return NVal();
+ }
+
+ napi_value resource = NVal::CreateUTF8String(env_, procedureName).val_;
+ status = napi_create_async_work(env_, nullptr, resource, PromiseOnExec, PromiseOnComplete, ctx_, &ctx_->awork_);
+ if (status != napi_ok) {
+ ERR_LOG("INNER BUG. Failed to create async work for %{public}d", status);
+ return NVal();
+ }
+
+ status = napi_queue_async_work(env_, ctx_->awork_);
+ if (status != napi_ok) {
+ ERR_LOG("INNER BUG. Failed to queue async work for %{public}d", status);
+ return NVal();
+ }
+
+ ctx_ = nullptr; // The ownership of ctx_ has been transfered
+ return { env_, result };
+}
+} // namespace DistributedFS
+} // namespace OHOS
diff --git a/interfaces/kits/js/src/common/napi/n_async/n_async_work_promise.h b/interfaces/kits/js/src/common/napi/n_async/n_async_work_promise.h
new file mode 100644
index 00000000..9bef0eba
--- /dev/null
+++ b/interfaces/kits/js/src/common/napi/n_async/n_async_work_promise.h
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2021 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.
+ */
+
+#pragma once
+
+#include
+#include
+
+#include "n_async_work_factory.h"
+
+namespace OHOS {
+namespace DistributedFS {
+class NAsyncWorkPromise : public NAsyncWorkFactory {
+public:
+ NAsyncWorkPromise(napi_env env, NVal thisPtr);
+ ~NAsyncWorkPromise() = default;
+ NAsyncWorkPromise(const NAsyncWorkPromise &in, napi_env env);
+ NVal Schedule(std::string procedureName, NContextCBExec cbExec, NContextCBComplete cbComplete) final;
+
+private:
+ NAsyncContextPromise *ctx_;
+};
+} // namespace DistributedFS
+} // namespace OHOS
\ No newline at end of file
diff --git a/interfaces/kits/js/src/common/napi/n_async/n_ref.cpp b/interfaces/kits/js/src/common/napi/n_async/n_ref.cpp
new file mode 100644
index 00000000..8b78ac9c
--- /dev/null
+++ b/interfaces/kits/js/src/common/napi/n_async/n_ref.cpp
@@ -0,0 +1,53 @@
+/*
+ * Copyright (c) 2021 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 "n_ref.h"
+
+namespace OHOS {
+namespace DistributedFS {
+NRef::NRef() {}
+
+NRef::NRef(NVal val)
+{
+ if (val) {
+ env_ = val.env_;
+ napi_create_reference(val.env_, val.val_, 1, &ref_);
+ }
+}
+
+NRef::~NRef()
+{
+ if (ref_) {
+ napi_delete_reference(env_, ref_);
+ }
+}
+
+NRef::operator bool() const
+{
+ return ref_ != nullptr;
+}
+
+NVal NRef::Deref(napi_env env)
+{
+ if (!ref_) {
+ return NVal();
+ }
+
+ napi_value val = nullptr;
+ napi_get_reference_value(env, ref_, &val);
+ return { env, val };
+}
+} // namespace DistributedFS
+} // namespace OHOS
diff --git a/interfaces/kits/js/src/common/napi/n_async/n_ref.h b/interfaces/kits/js/src/common/napi/n_async/n_ref.h
new file mode 100644
index 00000000..12b216ab
--- /dev/null
+++ b/interfaces/kits/js/src/common/napi/n_async/n_ref.h
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2021 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.
+ */
+
+#pragma once
+
+#include "../n_val.h"
+#include "../uni_header.h"
+
+namespace OHOS {
+namespace DistributedFS {
+class NRef {
+public:
+ NRef();
+ explicit NRef(NVal val);
+ ~NRef();
+
+ explicit operator bool() const;
+ NVal Deref(napi_env env);
+
+private:
+ napi_env env_ = nullptr;
+ napi_ref ref_ = nullptr;
+};
+} // namespace DistributedFS
+} // namespace OHOS
diff --git a/interfaces/kits/js/src/common/napi/n_class.cpp b/interfaces/kits/js/src/common/napi/n_class.cpp
new file mode 100644
index 00000000..c6c60ee9
--- /dev/null
+++ b/interfaces/kits/js/src/common/napi/n_class.cpp
@@ -0,0 +1,99 @@
+/*
+ * Copyright (c) 2021 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 "n_class.h"
+
+#include
+#include
+
+#include "log.h"
+
+namespace OHOS {
+namespace DistributedFS {
+using namespace std;
+NClass &NClass::GetInstance()
+{
+ static NClass nClass;
+ return nClass;
+}
+
+tuple NClass::DefineClass(napi_env env,
+ string className,
+ napi_callback constructor,
+ vector &&properties)
+{
+ napi_value classVal = nullptr;
+ napi_status stat = napi_define_class(env,
+ className.c_str(),
+ className.length(),
+ constructor,
+ nullptr,
+ properties.size(),
+ properties.data(),
+ &classVal);
+ if (stat != napi_ok) {
+ ERR_LOG("INNER BUG. Cannot define class %{public}s because of %{public}d", className.c_str(), stat);
+ }
+ return { stat == napi_ok, classVal };
+}
+
+bool NClass::SaveClass(napi_env env, string className, napi_value exClass)
+{
+ NClass &nClass = NClass::GetInstance();
+ lock_guard(nClass.exClassMapLock);
+
+ if (nClass.exClassMap.find(className) != nClass.exClassMap.end()) {
+ return true;
+ }
+
+ napi_ref constructor;
+ napi_status res = napi_create_reference(env, exClass, 1, &constructor);
+ if (res == napi_ok) {
+ nClass.exClassMap.insert({ className, constructor });
+ INFO_LOG("Class %{public}s has been saved", className.c_str());
+ } else {
+ ERR_LOG("INNER BUG. Cannot ref class constructor %{public}s because of %{public}d", className.c_str(), res);
+ }
+ return res == napi_ok;
+}
+
+napi_value NClass::InstantiateClass(napi_env env, string className, vector args)
+{
+ NClass &nClass = NClass::GetInstance();
+ lock_guard(nClass.exClassMapLock);
+
+ auto it = nClass.exClassMap.find(className);
+ if (it == nClass.exClassMap.end()) {
+ ERR_LOG("Class %{public}s hasn't been saved yet", className.c_str());
+ return nullptr;
+ }
+
+ napi_value cons = nullptr;
+ napi_status status = napi_get_reference_value(env, it->second, &cons);
+ if (status != napi_ok) {
+ ERR_LOG("INNER BUG. Cannot deref class %{public}s because of %{public}d", className.c_str(), status);
+ return nullptr;
+ }
+
+ napi_value instance = nullptr;
+ status = napi_new_instance(env, cons, args.size(), args.data(), &instance);
+ if (status != napi_ok) {
+ ERR_LOG("INNER BUG. Cannot instantiate the class %{public}s because of %{public}d", className.c_str(), status);
+ return nullptr;
+ }
+ return instance;
+}
+} // namespace DistributedFS
+} // namespace OHOS
\ No newline at end of file
diff --git a/interfaces/kits/js/src/common/napi/n_class.h b/interfaces/kits/js/src/common/napi/n_class.h
new file mode 100644
index 00000000..e048ae80
--- /dev/null
+++ b/interfaces/kits/js/src/common/napi/n_class.h
@@ -0,0 +1,82 @@
+/*
+ * Copyright (c) 2021 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.
+ */
+
+#pragma once
+
+#include "uni_header.h"
+
+#include