123b3eb3cSopenharmony_ci/*
223b3eb3cSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
323b3eb3cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
423b3eb3cSopenharmony_ci * you may not use this file except in compliance with the License.
523b3eb3cSopenharmony_ci * You may obtain a copy of the License at
623b3eb3cSopenharmony_ci *
723b3eb3cSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
823b3eb3cSopenharmony_ci *
923b3eb3cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1023b3eb3cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1123b3eb3cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1223b3eb3cSopenharmony_ci * See the License for the specific language governing permissions and
1323b3eb3cSopenharmony_ci * limitations under the License.
1423b3eb3cSopenharmony_ci */
1523b3eb3cSopenharmony_ci
1623b3eb3cSopenharmony_ci#include <memory>
1723b3eb3cSopenharmony_ci
1823b3eb3cSopenharmony_ci#include "napi/native_node_api.h"
1923b3eb3cSopenharmony_ci#include "api_policy_adapter.h"
2023b3eb3cSopenharmony_ci
2123b3eb3cSopenharmony_ciusing namespace std;
2223b3eb3cSopenharmony_ci
2323b3eb3cSopenharmony_ciextern const char _binary_atomicserviceweb_abc_start[];
2423b3eb3cSopenharmony_ciextern const char _binary_atomicserviceweb_abc_end[];
2523b3eb3cSopenharmony_ci
2623b3eb3cSopenharmony_cinamespace HMS::AtomicServiceWeb {
2723b3eb3cSopenharmony_ci    static napi_value CheckUrl(napi_env env, napi_callback_info info)
2823b3eb3cSopenharmony_ci    {
2923b3eb3cSopenharmony_ci        const int indexTwo = 2;
3023b3eb3cSopenharmony_ci        size_t requireArgc = 3;
3123b3eb3cSopenharmony_ci        size_t argc = 3;
3223b3eb3cSopenharmony_ci        napi_value args[3] = { nullptr };
3323b3eb3cSopenharmony_ci        NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
3423b3eb3cSopenharmony_ci
3523b3eb3cSopenharmony_ci        NAPI_ASSERT(env, argc >= requireArgc, "Wrong number of arguments");
3623b3eb3cSopenharmony_ci
3723b3eb3cSopenharmony_ci        napi_valuetype bundleNameType;
3823b3eb3cSopenharmony_ci        NAPI_CALL(env, napi_typeof(env, args[0], &bundleNameType));
3923b3eb3cSopenharmony_ci
4023b3eb3cSopenharmony_ci        napi_valuetype domainTypeType;
4123b3eb3cSopenharmony_ci        NAPI_CALL(env, napi_typeof(env, args[1], &domainTypeType));
4223b3eb3cSopenharmony_ci
4323b3eb3cSopenharmony_ci        napi_valuetype urlType;
4423b3eb3cSopenharmony_ci        NAPI_CALL(env, napi_typeof(env, args[indexTwo], &urlType));
4523b3eb3cSopenharmony_ci
4623b3eb3cSopenharmony_ci        NAPI_ASSERT(env, bundleNameType == napi_string && domainTypeType == napi_string && urlType == napi_string,
4723b3eb3cSopenharmony_ci            "Wrong argument type. String expected.");
4823b3eb3cSopenharmony_ci
4923b3eb3cSopenharmony_ci        size_t maxValueLen = 1024;
5023b3eb3cSopenharmony_ci        char bundleNameValue[maxValueLen];
5123b3eb3cSopenharmony_ci        size_t bundleNameLength = 0;
5223b3eb3cSopenharmony_ci        napi_get_value_string_utf8(env, args[0], bundleNameValue, maxValueLen, &bundleNameLength);
5323b3eb3cSopenharmony_ci        std::string bundleName = bundleNameValue;
5423b3eb3cSopenharmony_ci
5523b3eb3cSopenharmony_ci        char domainTypeValue[maxValueLen];
5623b3eb3cSopenharmony_ci        size_t domainTypeLength = 0;
5723b3eb3cSopenharmony_ci        napi_get_value_string_utf8(env, args[1], domainTypeValue, maxValueLen, &domainTypeLength);
5823b3eb3cSopenharmony_ci        std::string domainType = domainTypeValue;
5923b3eb3cSopenharmony_ci
6023b3eb3cSopenharmony_ci        char urlValue[maxValueLen];
6123b3eb3cSopenharmony_ci        size_t urlLength = 0;
6223b3eb3cSopenharmony_ci        napi_get_value_string_utf8(env, args[indexTwo], urlValue, maxValueLen, &urlLength);
6323b3eb3cSopenharmony_ci        std::string url = urlValue;
6423b3eb3cSopenharmony_ci
6523b3eb3cSopenharmony_ci        std::shared_ptr<ApiPolicyAdapter> apiPolicyAdapter = std::make_shared<ApiPolicyAdapter>();
6623b3eb3cSopenharmony_ci        int32_t res = apiPolicyAdapter->CheckUrl(bundleName, domainType, url);
6723b3eb3cSopenharmony_ci
6823b3eb3cSopenharmony_ci        napi_value result;
6923b3eb3cSopenharmony_ci        NAPI_CALL(env, napi_create_double(env, res, &result));
7023b3eb3cSopenharmony_ci        return result;
7123b3eb3cSopenharmony_ci    }
7223b3eb3cSopenharmony_ci
7323b3eb3cSopenharmony_ci    static napi_value Init(napi_env env, napi_value exports)
7423b3eb3cSopenharmony_ci    {
7523b3eb3cSopenharmony_ci        napi_property_descriptor desc[] = {
7623b3eb3cSopenharmony_ci            DECLARE_NAPI_FUNCTION("checkUrl", CheckUrl),
7723b3eb3cSopenharmony_ci        };
7823b3eb3cSopenharmony_ci        NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
7923b3eb3cSopenharmony_ci        return exports;
8023b3eb3cSopenharmony_ci    }
8123b3eb3cSopenharmony_ci
8223b3eb3cSopenharmony_ci    // Napi get abc code function
8323b3eb3cSopenharmony_ci    extern "C" __attribute__((visibility("default")))
8423b3eb3cSopenharmony_ci    void NAPI_atomicservice_AtomicServiceWeb_GetABCCode(const char **buf, int *buflen)
8523b3eb3cSopenharmony_ci    {
8623b3eb3cSopenharmony_ci        if (buf != nullptr) {
8723b3eb3cSopenharmony_ci            *buf = _binary_atomicserviceweb_abc_start;
8823b3eb3cSopenharmony_ci        }
8923b3eb3cSopenharmony_ci        if (buflen != nullptr) {
9023b3eb3cSopenharmony_ci            *buflen = _binary_atomicserviceweb_abc_end - _binary_atomicserviceweb_abc_start;
9123b3eb3cSopenharmony_ci        }
9223b3eb3cSopenharmony_ci    }
9323b3eb3cSopenharmony_ci
9423b3eb3cSopenharmony_ci    /*
9523b3eb3cSopenharmony_ci     * Module define
9623b3eb3cSopenharmony_ci     */
9723b3eb3cSopenharmony_ci    static napi_module AtomicServiceWebModule = {
9823b3eb3cSopenharmony_ci        .nm_version = 1,
9923b3eb3cSopenharmony_ci        .nm_flags = 0,
10023b3eb3cSopenharmony_ci        .nm_filename = nullptr,
10123b3eb3cSopenharmony_ci        .nm_register_func = Init,
10223b3eb3cSopenharmony_ci        .nm_modname = "atomicservice.AtomicServiceWeb",
10323b3eb3cSopenharmony_ci        .nm_priv = ((void*)0),
10423b3eb3cSopenharmony_ci        .reserved = { 0 },
10523b3eb3cSopenharmony_ci    };
10623b3eb3cSopenharmony_ci
10723b3eb3cSopenharmony_ci    /*
10823b3eb3cSopenharmony_ci     * Module registerfunction
10923b3eb3cSopenharmony_ci     */
11023b3eb3cSopenharmony_ci    extern "C" __attribute__((constructor)) void AtomicServiceWebRegisterModule(void)
11123b3eb3cSopenharmony_ci    {
11223b3eb3cSopenharmony_ci        napi_module_register(&AtomicServiceWebModule);
11323b3eb3cSopenharmony_ci    }
11423b3eb3cSopenharmony_ci}
115