1/*
2 * Copyright (c) 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 <memory>
17
18#include "napi/native_node_api.h"
19#include "api_policy_adapter.h"
20
21using namespace std;
22
23extern const char _binary_atomicserviceweb_abc_start[];
24extern const char _binary_atomicserviceweb_abc_end[];
25
26namespace HMS::AtomicServiceWeb {
27    static napi_value CheckUrl(napi_env env, napi_callback_info info)
28    {
29        const int indexTwo = 2;
30        size_t requireArgc = 3;
31        size_t argc = 3;
32        napi_value args[3] = { nullptr };
33        NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
34
35        NAPI_ASSERT(env, argc >= requireArgc, "Wrong number of arguments");
36
37        napi_valuetype bundleNameType;
38        NAPI_CALL(env, napi_typeof(env, args[0], &bundleNameType));
39
40        napi_valuetype domainTypeType;
41        NAPI_CALL(env, napi_typeof(env, args[1], &domainTypeType));
42
43        napi_valuetype urlType;
44        NAPI_CALL(env, napi_typeof(env, args[indexTwo], &urlType));
45
46        NAPI_ASSERT(env, bundleNameType == napi_string && domainTypeType == napi_string && urlType == napi_string,
47            "Wrong argument type. String expected.");
48
49        size_t maxValueLen = 1024;
50        char bundleNameValue[maxValueLen];
51        size_t bundleNameLength = 0;
52        napi_get_value_string_utf8(env, args[0], bundleNameValue, maxValueLen, &bundleNameLength);
53        std::string bundleName = bundleNameValue;
54
55        char domainTypeValue[maxValueLen];
56        size_t domainTypeLength = 0;
57        napi_get_value_string_utf8(env, args[1], domainTypeValue, maxValueLen, &domainTypeLength);
58        std::string domainType = domainTypeValue;
59
60        char urlValue[maxValueLen];
61        size_t urlLength = 0;
62        napi_get_value_string_utf8(env, args[indexTwo], urlValue, maxValueLen, &urlLength);
63        std::string url = urlValue;
64
65        std::shared_ptr<ApiPolicyAdapter> apiPolicyAdapter = std::make_shared<ApiPolicyAdapter>();
66        int32_t res = apiPolicyAdapter->CheckUrl(bundleName, domainType, url);
67
68        napi_value result;
69        NAPI_CALL(env, napi_create_double(env, res, &result));
70        return result;
71    }
72
73    static napi_value Init(napi_env env, napi_value exports)
74    {
75        napi_property_descriptor desc[] = {
76            DECLARE_NAPI_FUNCTION("checkUrl", CheckUrl),
77        };
78        NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
79        return exports;
80    }
81
82    // Napi get abc code function
83    extern "C" __attribute__((visibility("default")))
84    void NAPI_atomicservice_AtomicServiceWeb_GetABCCode(const char **buf, int *buflen)
85    {
86        if (buf != nullptr) {
87            *buf = _binary_atomicserviceweb_abc_start;
88        }
89        if (buflen != nullptr) {
90            *buflen = _binary_atomicserviceweb_abc_end - _binary_atomicserviceweb_abc_start;
91        }
92    }
93
94    /*
95     * Module define
96     */
97    static napi_module AtomicServiceWebModule = {
98        .nm_version = 1,
99        .nm_flags = 0,
100        .nm_filename = nullptr,
101        .nm_register_func = Init,
102        .nm_modname = "atomicservice.AtomicServiceWeb",
103        .nm_priv = ((void*)0),
104        .reserved = { 0 },
105    };
106
107    /*
108     * Module registerfunction
109     */
110    extern "C" __attribute__((constructor)) void AtomicServiceWebRegisterModule(void)
111    {
112        napi_module_register(&AtomicServiceWebModule);
113    }
114}
115