1 /*
2 * Copyright (C) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "napi_root_iterator_exporter.h"
17
18 #include "hilog_wrapper.h"
19 #include "napi_root_info_exporter.h"
20 #include "root_info_entity.h"
21 #include "root_iterator_entity.h"
22
23 namespace OHOS {
24 namespace FileAccessFwk {
Export()25 bool NapiRootIteratorExporter::Export()
26 {
27 std::vector<napi_property_descriptor> props = {
28 NVal::DeclareNapiFunction("next", Next),
29 };
30
31 std::string className = GetClassName();
32 bool succ = false;
33 napi_value classValue = nullptr;
34 std::tie(succ, classValue) = NClass::DefineClass(exports_.env_, className,
35 NapiRootIteratorExporter::Constructor, std::move(props));
36 if (!succ) {
37 NError(E_GETRESULT).ThrowErr(exports_.env_);
38 return false;
39 }
40
41 succ = NClass::SaveClass(exports_.env_, className, classValue);
42 if (!succ) {
43 NError(E_GETRESULT).ThrowErr(exports_.env_);
44 return false;
45 }
46
47 return exports_.AddProp(className, classValue);
48 }
49
Constructor(napi_env env,napi_callback_info info)50 napi_value NapiRootIteratorExporter::Constructor(napi_env env, napi_callback_info info)
51 {
52 NFuncArg funcArg(env, info);
53 if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
54 NError(EINVAL).ThrowErr(env);
55 return nullptr;
56 }
57
58 auto rootIteratorEntity = std::make_unique<RootIteratorEntity>();
59 if (rootIteratorEntity == nullptr) {
60 NError(E_GETRESULT).ThrowErr(env);
61 return nullptr;
62 }
63
64 if (!NClass::SetEntityFor<RootIteratorEntity>(env, funcArg.GetThisVar(), std::move(rootIteratorEntity))) {
65 NError(E_GETRESULT).ThrowErr(env);
66 return nullptr;
67 }
68
69 return funcArg.GetThisVar();
70 }
71
Next(napi_env env,napi_callback_info info)72 napi_value NapiRootIteratorExporter::Next(napi_env env, napi_callback_info info)
73 {
74 NFuncArg funcArg(env, info);
75 if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
76 NError(EINVAL).ThrowErr(env);
77 return nullptr;
78 }
79
80 napi_value thisVar = funcArg.GetThisVar();
81 auto rootIterator = NClass::GetEntityOf<RootIteratorEntity>(env, thisVar);
82 if (rootIterator == nullptr) {
83 NError(E_GETRESULT).ThrowErr(env);
84 return nullptr;
85 }
86
87 napi_value objRootInfoExporter = NClass::InstantiateClass(env, NapiRootInfoExporter::className_, {});
88 if (objRootInfoExporter == nullptr) {
89 NError(E_GETRESULT).ThrowErr(env);
90 return nullptr;
91 }
92
93 auto rootInfoEntity = NClass::GetEntityOf<RootInfoEntity>(env, objRootInfoExporter);
94 if (rootInfoEntity == nullptr) {
95 NError(E_GETRESULT).ThrowErr(env);
96 return nullptr;
97 }
98
99 auto retNVal = NVal::CreateObject(env);
100 bool done = true;
101 {
102 std::lock_guard<std::mutex> lock(rootIterator->entityOperateMutex);
103 auto len = rootIterator->devVec.size();
104 rootInfoEntity->fileAccessHelper = rootIterator->fileAccessHelper;
105 if (rootIterator->pos < len) {
106 rootInfoEntity->rootInfo = rootIterator->devVec[rootIterator->pos];
107 done = false;
108 rootIterator->pos++;
109 } else {
110 rootInfoEntity = nullptr;
111 objRootInfoExporter = NVal::CreateUndefined(env).val_;
112 done = true;
113 }
114 retNVal.AddProp("value", objRootInfoExporter);
115 retNVal.AddProp("done", NVal::CreateBool(env, done).val_);
116 }
117
118 return retNVal.val_;
119 }
120
GetClassName()121 std::string NapiRootIteratorExporter::GetClassName()
122 {
123 return NapiRootIteratorExporter::className_;
124 }
125 } // namespace FileAccessFwk
126 } // namespace OHOS