1 /*
2 * Copyright (c) 2025 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 "netextattribute_context.h"
17
18 #include "netmanager_base_permission.h"
19 #include "net_manager_constants.h"
20 #include "napi_constant.h"
21 #include "constant.h"
22 #include "napi_utils.h"
23 #include "netmanager_base_log.h"
24
25 namespace OHOS {
26 namespace NetManagerStandard {
27
28 static constexpr const char *SET_NET_EXT_ATTRIBUTE = "ohos.permission.SET_NET_EXT_ATTRIBUTE";
29 static constexpr const char *GET_NETWORK_INFO = "ohos.permission.GET_NETWORK_INFO";
30
SetNetExtAttributeContext(napi_env env,std::shared_ptr<EventManager> & manager)31 SetNetExtAttributeContext::SetNetExtAttributeContext(napi_env env, std::shared_ptr<EventManager>& manager)
32 : BaseContext(env, manager) {}
33
CheckParamsType(napi_env env,napi_value * params,size_t paramsCount)34 bool SetNetExtAttributeContext::CheckParamsType(napi_env env, napi_value *params, size_t paramsCount)
35 {
36 if (paramsCount == PARAM_DOUBLE_OPTIONS) {
37 if (NapiUtils::GetValueType(env, params[ARG_INDEX_0]) != napi_object) {
38 return false;
39 }
40 auto value = NapiUtils::GetNamedProperty(env, params[ARG_INDEX_0], KEY_NET_ID);
41 return NapiUtils::GetValueType(env, value) == napi_number &&
42 NapiUtils::GetValueType(env, params[ARG_INDEX_1]) == napi_string;
43 }
44
45 return false;
46 }
47
ParseParams(napi_value * params,size_t paramsCount)48 void SetNetExtAttributeContext::ParseParams(napi_value *params, size_t paramsCount)
49 {
50 if (!NetManagerPermission::CheckPermission(SET_NET_EXT_ATTRIBUTE)) {
51 SetErrorCode(NETMANAGER_ERR_PERMISSION_DENIED);
52 return;
53 }
54 if (!CheckParamsType(GetEnv(), params, paramsCount)) {
55 NETMANAGER_BASE_LOGE("check params type failed");
56 SetErrorCode(NETMANAGER_ERR_INVALID_PARAMETER);
57 return;
58 }
59
60 int32_t netId_ = NapiUtils::GetInt32Property(GetEnv(), params[ARG_INDEX_0], KEY_NET_ID);
61 netHandle_.SetNetId(netId_);
62 netExtAttribute_ = NapiUtils::GetStringFromValueUtf8(GetEnv(), params[ARG_INDEX_1]);
63 SetParseOK(true);
64 }
65
GetNetExtAttributeContext(napi_env env,std::shared_ptr<EventManager> & manager)66 GetNetExtAttributeContext::GetNetExtAttributeContext(napi_env env, std::shared_ptr<EventManager>& manager)
67 : BaseContext(env, manager) {}
68
ParseParams(napi_value * params,size_t paramsCount)69 void GetNetExtAttributeContext::ParseParams(napi_value *params, size_t paramsCount)
70 {
71 if (!NetManagerPermission::CheckPermission(GET_NETWORK_INFO)) {
72 SetErrorCode(NETMANAGER_ERR_PERMISSION_DENIED);
73 return;
74 }
75 if (paramsCount != PARAM_JUST_OPTIONS || NapiUtils::GetValueType(GetEnv(), params[ARG_INDEX_0]) != napi_object) {
76 NETMANAGER_BASE_LOGE("check params type failed");
77 SetErrorCode(NETMANAGER_ERR_INVALID_PARAMETER);
78 return;
79 }
80 auto value = NapiUtils::GetNamedProperty(GetEnv(), params[ARG_INDEX_0], KEY_NET_ID);
81 if (NapiUtils::GetValueType(GetEnv(), value) != napi_number) {
82 NETMANAGER_BASE_LOGE("check params type failed");
83 SetErrorCode(NETMANAGER_ERR_INVALID_PARAMETER);
84 return;
85 }
86 int32_t netId_ = NapiUtils::GetInt32Property(GetEnv(), params[ARG_INDEX_0], KEY_NET_ID);
87 netHandle_.SetNetId(netId_);
88 SetParseOK(true);
89 }
90 } // namespace NetManagerStandard
91 } // namespace OHOS
92