1 /*
2  * Copyright (c) 2022-2023 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 "network_observer.h"
17 #include "net_conn_client.h"
18 #include "network_constant.h"
19 
20 #include "netmanager_base_log.h"
21 #include "securec.h"
22 
23 static constexpr const char *NETWORK_NONE = "none";
24 
25 static constexpr const char *NETWORK_WIFI = "WiFi";
26 
27 static std::mutex OBSERVER_MUTEX;
28 
29 namespace OHOS::NetManagerStandard {
30 std::map<EventManager *, sptr<NetworkObserver>> g_observerMap;
31 
MakeNetworkResponse(napi_env env,NetworkType * data)32 static napi_value MakeNetworkResponse(napi_env env, NetworkType *data)
33 {
34     auto deleter = [](NetworkType *t) { delete t; };
35     std::unique_ptr<NetworkType, decltype(deleter)> netType(data, deleter);
36 
37     napi_value obj = NapiUtils::CreateObject(env);
38     if (netType->bearerTypes.find(BEARER_WIFI) != netType->bearerTypes.end()) {
39         NapiUtils::SetStringPropertyUtf8(env, obj, KEY_TYPE, NETWORK_WIFI);
40         NapiUtils::SetBooleanProperty(env, obj, KEY_METERED, false);
41         return obj;
42     }
43 
44     if (netType->bearerTypes.find(BEARER_CELLULAR) != netType->bearerTypes.end()) {
45         std::string type = "";
46         int32_t ret = NetConnClient::GetInstance().GetSlotType(type);
47         if (ret != NETMANAGER_SUCCESS || type.empty()) {
48             type = "none";
49         }
50         NapiUtils::SetStringPropertyUtf8(env, obj, KEY_TYPE, type);
51         NapiUtils::SetBooleanProperty(env, obj, KEY_METERED, true);
52         return obj;
53     }
54 
55     NapiUtils::SetStringPropertyUtf8(env, obj, KEY_TYPE, NETWORK_NONE);
56     NapiUtils::SetBooleanProperty(env, obj, KEY_METERED, false);
57     return obj;
58 }
59 
NetAvailable(sptr<NetHandle> & netHandle)60 int32_t NetworkObserver::NetAvailable(sptr<NetHandle> &netHandle)
61 {
62     return 0;
63 }
64 
NetCapabilitiesChange(sptr<NetHandle> & netHandle,const sptr<NetAllCapabilities> & netAllCap)65 int32_t NetworkObserver::NetCapabilitiesChange(sptr<NetHandle> &netHandle, const sptr<NetAllCapabilities> &netAllCap)
66 {
67     NETMANAGER_BASE_LOGI("NetworkObserver::NetCapabilitiesChange");
68 
69     std::lock_guard<std::mutex> lock(OBSERVER_MUTEX);
70     if (!manager_) {
71         NETMANAGER_BASE_LOGI("no event manager");
72         return 0;
73     }
74 
75     if (manager_->HasEventListener(EVENT_SUBSCRIBE)) {
76         auto netType = new NetworkType;
77         netType->bearerTypes = netAllCap->bearerTypes_;
78         manager_->EmitByUv(EVENT_SUBSCRIBE, netType, CallbackTemplate<MakeNetworkResponse>);
79     } else {
80         NETMANAGER_BASE_LOGI("NO EVENT_SUBSCRIBE");
81     }
82     return 0;
83 }
84 
NetConnectionPropertiesChange(sptr<NetHandle> & netHandle,const sptr<NetLinkInfo> & info)85 int32_t NetworkObserver::NetConnectionPropertiesChange(sptr<NetHandle> &netHandle, const sptr<NetLinkInfo> &info)
86 {
87     return 0;
88 }
89 
NetLost(sptr<NetHandle> & netHandle)90 int32_t NetworkObserver::NetLost(sptr<NetHandle> &netHandle)
91 {
92     return 0;
93 }
94 
NetUnavailable()95 int32_t NetworkObserver::NetUnavailable()
96 {
97     NETMANAGER_BASE_LOGI("NetworkObserver::NetUnavailable");
98 
99     std::lock_guard<std::mutex> lock(OBSERVER_MUTEX);
100     if (!manager_) {
101         NETMANAGER_BASE_LOGI("no event manager");
102         return 0;
103     }
104     if (manager_->HasEventListener(EVENT_SUBSCRIBE)) {
105         auto netType = new NetworkType;
106         manager_->EmitByUv(EVENT_SUBSCRIBE, netType, CallbackTemplate<MakeNetworkResponse>);
107     } else {
108         NETMANAGER_BASE_LOGI("NO EVENT_SUBSCRIBE");
109     }
110     return 0;
111 }
112 
NetBlockStatusChange(sptr<NetHandle> & netHandle,bool blocked)113 int32_t NetworkObserver::NetBlockStatusChange(sptr<NetHandle> &netHandle, bool blocked)
114 {
115     return 0;
116 }
117 
SetManager(EventManager * manager)118 void NetworkObserver::SetManager(EventManager *manager)
119 {
120     manager_ = manager;
121 }
122 } // namespace OHOS::NetManagerStandard
123