1 /*
2  * Copyright (C) 2023-2024 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/native_api.h"
17 #include "napi/native_node_api.h"
18 #include "picker_n_exporter.h"
19 
20 extern const char _binary_picker_js_start[];
21 extern const char _binary_picker_js_end[];
22 extern const char _binary_picker_abc_start[];
23 extern const char _binary_picker_abc_end[];
24 
25 namespace OHOS {
26 namespace FileAccessFwk {
27 using namespace Picker;
28 using namespace FileManagement::LibN;
29 
30 EXTERN_C_START
Export(napi_env env,napi_value exports)31 static napi_value Export(napi_env env, napi_value exports)
32 {
33     std::vector<unique_ptr<NExporter>> products;
34     products.emplace_back(make_unique<PickerNExporter>(env, exports));
35 
36     for (auto &&product : products) {
37         std::string nExporterName = product->GetClassName();
38         if (!product->Export()) {
39             HILOG_ERROR("INNER BUG. Failed to export class %{public}s for module trash", nExporterName.c_str());
40             return nullptr;
41         } else {
42             HILOG_INFO("Class %{public}s for module trash has been exported", nExporterName.c_str());
43         }
44     }
45     return exports;
46 }
47 EXTERN_C_END
48 
NAPI_file_picker_GetJSCode(const char ** buf,int * bufLen)49 extern "C" __attribute__((visibility("default"))) void NAPI_file_picker_GetJSCode(const char** buf,
50     int* bufLen)
51 {
52     if (buf != nullptr) {
53         *buf = _binary_picker_js_start;
54     }
55 
56     if (bufLen != nullptr) {
57         *bufLen = _binary_picker_js_end - _binary_picker_js_start;
58     }
59 }
60 
NAPI_file_picker_GetABCCode(const char ** buf,int * bufLen)61 extern "C" __attribute__((visibility("default"))) void NAPI_file_picker_GetABCCode(const char** buf,
62     int* bufLen)
63 {
64     if (buf != nullptr) {
65         *buf = _binary_picker_abc_start;
66     }
67 
68     if (bufLen != nullptr) {
69         *bufLen = _binary_picker_abc_end - _binary_picker_abc_start;
70     }
71 }
72 
73 /*
74  * module define
75  */
76 static napi_module g_module = {
77     .nm_version = 1,
78     .nm_flags = 0,
79     .nm_filename = nullptr,
80     .nm_register_func = Export,
81     .nm_modname = "file.picker",
82     .nm_priv = reinterpret_cast<void *>(0),
83     .reserved = {0}
84 };
85 
86 /*
87  * module register
88  */
RegisterModule(void)89 extern "C" __attribute__((constructor)) void RegisterModule(void)
90 {
91     napi_module_register(&g_module);
92 }
93 } // namespace Media
94 } // namespace OHOS
95