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 "file_extension_info_napi.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <string>
21
22 #include "filemgmt_libn.h"
23 #include "file_access_extension_info.h"
24 #include "file_access_notify_common.h"
25 #include "hilog_wrapper.h"
26 #include "js_native_api.h"
27 #include "napi/native_common.h"
28
29 namespace OHOS {
30 namespace FileAccessFwk {
31 using namespace FileManagement::LibN;
32
CreateStringUtf8(napi_env env,const std::string & str)33 static napi_value CreateStringUtf8(napi_env env, const std::string &str)
34 {
35 napi_value value = nullptr;
36 if (napi_create_string_utf8(env, str.c_str(), str.length(), &value) != napi_ok) {
37 HILOG_ERROR("CreateStringUtf8, value is not napi_ok");
38 return nullptr;
39 }
40 return value;
41 }
42
FileInfoConstructor(napi_env env,napi_callback_info info)43 static napi_value FileInfoConstructor(napi_env env, napi_callback_info info)
44 {
45 size_t argc = 0;
46 napi_value args[1] = {0};
47 napi_value res = nullptr;
48 void *data = nullptr;
49
50 napi_status status = napi_get_cb_info(env, info, &argc, args, &res, &data);
51 if (status != napi_ok) {
52 HILOG_ERROR("FileInfoConstructor, status is not napi_ok");
53 return nullptr;
54 }
55
56 return res;
57 }
58
RootInfoConstructor(napi_env env,napi_callback_info info)59 static napi_value RootInfoConstructor(napi_env env, napi_callback_info info)
60 {
61 size_t argc = 0;
62 napi_value args[1] = {0};
63 napi_value res = nullptr;
64 void *data = nullptr;
65
66 napi_status status = napi_get_cb_info(env, info, &argc, args, &res, &data);
67 if (status != napi_ok) {
68 HILOG_ERROR("RootInfoConstructor, status is not napi_ok");
69 return nullptr;
70 }
71
72 return res;
73 }
74
InitDeviceFlag(napi_env env,napi_value exports)75 void InitDeviceFlag(napi_env env, napi_value exports)
76 {
77 char propertyName[] = "DeviceFlag";
78 napi_property_descriptor desc[] = {
79 DECLARE_NAPI_STATIC_PROPERTY("SUPPORTS_READ", NVal::CreateInt32(env, DEVICE_FLAG_SUPPORTS_READ).val_),
80 DECLARE_NAPI_STATIC_PROPERTY("SUPPORTS_WRITE", NVal::CreateInt32(env, DEVICE_FLAG_SUPPORTS_WRITE).val_)
81 };
82 napi_value obj = nullptr;
83 napi_create_object(env, &obj);
84 napi_define_properties(env, obj, sizeof(desc) / sizeof(desc[0]), desc);
85 napi_set_named_property(env, exports, propertyName, obj);
86 }
87
InitDocumentFlag(napi_env env,napi_value exports)88 void InitDocumentFlag(napi_env env, napi_value exports)
89 {
90 char propertyName[] = "DocumentFlag";
91 napi_property_descriptor desc[] = {
92 DECLARE_NAPI_STATIC_PROPERTY("REPRESENTS_FILE", NVal::CreateInt32(env, DOCUMENT_FLAG_REPRESENTS_FILE).val_),
93 DECLARE_NAPI_STATIC_PROPERTY("REPRESENTS_DIR", NVal::CreateInt32(env, DOCUMENT_FLAG_REPRESENTS_DIR).val_),
94 DECLARE_NAPI_STATIC_PROPERTY("SUPPORTS_READ", NVal::CreateInt32(env, DOCUMENT_FLAG_SUPPORTS_READ).val_),
95 DECLARE_NAPI_STATIC_PROPERTY("SUPPORTS_WRITE", NVal::CreateInt32(env, DOCUMENT_FLAG_SUPPORTS_WRITE).val_)
96 };
97 napi_value obj = nullptr;
98 napi_create_object(env, &obj);
99 napi_define_properties(env, obj, sizeof(desc) / sizeof(desc[0]), desc);
100 napi_set_named_property(env, exports, propertyName, obj);
101 }
102
InitNotifyType(napi_env env,napi_value exports)103 void InitNotifyType(napi_env env, napi_value exports)
104 {
105 char propertyName[] = "NotifyType";
106 napi_property_descriptor desc[] = {
107 DECLARE_NAPI_STATIC_PROPERTY("DEVICE_ONLINE", NVal::CreateInt32(env, NOTIFY_DEVICE_ONLINE).val_),
108 DECLARE_NAPI_STATIC_PROPERTY("DEVICE_OFFLINE", NVal::CreateInt32(env, NOTIFY_DEVICE_OFFLINE).val_)
109 };
110 napi_value obj = nullptr;
111 napi_create_object(env, &obj);
112 napi_define_properties(env, obj, sizeof(desc) / sizeof(desc[0]), desc);
113 napi_set_named_property(env, exports, propertyName, obj);
114 }
115
InitFileInfo(napi_env env,napi_value exports)116 void InitFileInfo(napi_env env, napi_value exports)
117 {
118 char className[] = "FileInfo";
119 napi_property_descriptor desc[] = {
120 { "uri", nullptr, nullptr, nullptr, nullptr, CreateStringUtf8(env, "uri"), napi_writable, nullptr },
121 { "fileName", nullptr, nullptr, nullptr, nullptr, CreateStringUtf8(env, "fileName"), napi_writable, nullptr },
122 { "mode", nullptr, nullptr, nullptr, nullptr, CreateStringUtf8(env, "mode"), napi_writable, nullptr },
123 { "size", nullptr, nullptr, nullptr, nullptr, CreateStringUtf8(env, "size"), napi_writable, nullptr },
124 { "mtime", nullptr, nullptr, nullptr, nullptr, CreateStringUtf8(env, "mtime"), napi_writable, nullptr },
125 { "mimiType", nullptr, nullptr, nullptr, nullptr, CreateStringUtf8(env, "mimiType"), napi_writable, nullptr }
126 };
127 napi_value obj = nullptr;
128 napi_define_class(env, className, NAPI_AUTO_LENGTH, FileInfoConstructor, nullptr,
129 sizeof(desc) / sizeof(*desc), desc, &obj);
130 napi_set_named_property(env, exports, className, obj);
131 }
132
InitDeviceType(napi_env env,napi_value exports)133 void InitDeviceType(napi_env env, napi_value exports)
134 {
135 char propertyName[] = "DeviceType";
136 napi_property_descriptor desc[] = {
137 DECLARE_NAPI_STATIC_PROPERTY("DEVICE_LOCAL_DISK", NVal::CreateInt32(env, DEVICE_LOCAL_DISK).val_),
138 DECLARE_NAPI_STATIC_PROPERTY("DEVICE_SHARED_DISK", NVal::CreateInt32(env, DEVICE_SHARED_DISK).val_),
139 DECLARE_NAPI_STATIC_PROPERTY("DEVICE_SHARED_TERMINAL", NVal::CreateInt32(env, DEVICE_SHARED_TERMINAL).val_),
140 DECLARE_NAPI_STATIC_PROPERTY("DEVICE_NETWORK_NEIGHBORHOODS",
141 NVal::CreateInt32(env, DEVICE_NETWORK_NEIGHBORHOODS).val_),
142 DECLARE_NAPI_STATIC_PROPERTY("DEVICE_EXTERNAL_MTP", NVal::CreateInt32(env, DEVICE_EXTERNAL_MTP).val_),
143 DECLARE_NAPI_STATIC_PROPERTY("DEVICE_EXTERNAL_USB", NVal::CreateInt32(env, DEVICE_EXTERNAL_USB).val_),
144 DECLARE_NAPI_STATIC_PROPERTY("DEVICE_EXTERNAL_CLOUD", NVal::CreateInt32(env, DEVICE_EXTERNAL_CLOUD).val_)
145 };
146 napi_value obj = nullptr;
147 napi_create_object(env, &obj);
148 napi_define_properties(env, obj, sizeof(desc) / sizeof(desc[0]), desc);
149 napi_set_named_property(env, exports, propertyName, obj);
150 }
151
InitRootInfo(napi_env env,napi_value exports)152 void InitRootInfo(napi_env env, napi_value exports)
153 {
154 char className[] = "RootInfo";
155 napi_property_descriptor desc[] = {
156 { "deviceType", nullptr, nullptr, nullptr, nullptr, CreateStringUtf8(env, "deviceType"),
157 napi_writable, nullptr },
158 { "uri", nullptr, nullptr, nullptr, nullptr, CreateStringUtf8(env, "uri"), napi_writable, nullptr },
159 { "displayName", nullptr, nullptr, nullptr, nullptr, CreateStringUtf8(env, "displayName"),
160 napi_writable, nullptr },
161 { "deviceFlags", nullptr, nullptr, nullptr, nullptr, CreateStringUtf8(env, "deviceFlags"),
162 napi_writable, nullptr }
163 };
164 napi_value obj = nullptr;
165 napi_define_class(env, className, NAPI_AUTO_LENGTH, RootInfoConstructor, nullptr,
166 sizeof(desc) / sizeof(*desc), desc, &obj);
167 napi_set_named_property(env, exports, className, obj);
168 }
169 } // namespace FileAccessFwk
170 } // namespace OHOS